Teonet library  0.4.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
teoackm.c

Get multi records ACK event

This Example shows how to send multi records with TR-UDP and get ACK event from remote peer

Created on August 14, 2015, 1:53 PM

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ev_mgr.h"
#include "tr-udp_stat.h"
#define TACKM_VERSION "0.0.2"
// This application commands
#define CMD_U_STAT "stat"
#define CMD_U_RESET "reset"
#define CMD_U_DATA_OR_STAT "data_or_stat"
#define SERVER_NAME "none"
const char* PRESS_A = "(Press A to return to main application menu)";
enum {
size_t data_len, void *user_data) {
char *peer_to = ke->teo_cfg.app_argv[1];
switch(event) {
// Calls immediately after event manager starts
if(strcmp(peer_to, SERVER_NAME))
printf("Connecting to peer: %s ...\n", peer_to);
else
printf("Server mode\n");
break;
// Send when peer connected
{
printf("Peer %s connected at %s:%d \n",
rd->from, rd->addr, rd->port);
}
break;
// Send when peer disconnected
{
if(rd->from != NULL) {
printf("Peer %s was disconnected \n", rd->from);
}
else printf("Peer was disconnected\n");
}
break;
// Check hotkey
{
int i, idx, num_packets, command = *(int*)data;
// Check hotkey
switch(command) {
// Exit
case '0':
ev_break (ke->ev_loop, EVBREAK_ONE);
break;
// Send packets
case '1':
printf("How much package to send (0 - to exit): ");
if(scanf("%d", &num_packets)); // \todo check scanf error
printf("Send %d messages to %s\n",num_packets, peer_to);
idx = 0;
for(i = 0; i < num_packets; i++) {
char buffer[KSN_BUFFER_DB_SIZE];
sprintf(buffer, "#%d: Teoack Hello!", ++idx);
printf("Send message \"%s\"\n", buffer);
ksnCoreSendCmdto(ke->kc, (char*)peer_to,
CMD_USER, buffer, strlen(buffer)+1);
}
printf("\nACK ID: ");
break;
// Show local statistic
case '2':
printf("%s\n", PRESS_A);
break;
// Send request to show remote peer statistic
case '3':
(char*)peer_to,
CMD_USER + 1,
break;
// Send reset
case '4':
{
ksnet_arp_data *arp = (ksnet_arp_data *)ksnetArpGet(ke->kc->ka, peer_to);
if(arp != NULL) {
// Make address from string
struct sockaddr_in remaddr; // remote address
socklen_t addrlen = sizeof(remaddr);
if(!make_addr(arp->addr, arp->port, (__SOCKADDR_ARG) &remaddr, &addrlen)) {
trudpChannelData *tcd = trudpGetChannel(ke->kc->ku, (__CONST_SOCKADDR_ARG)&remaddr, addrlen, 0);
if((tcd != (void*)-1) && (tcd != NULL)) {
trudp_ChannelSendReset(tcd);
}
}
}
// Show menu
ke->event_cb(ke, EV_K_USER , NULL, 0, NULL);
}
break;
// Show data or statistic at server
case '5':
{
ksnet_arp_data *arp_data = (ksnet_arp_data *)ksnetArpGet(ke->kc->ka, peer_to);
if(arp_data != NULL) {
// Make address from string
struct sockaddr_in remaddr; // remote address
socklen_t addrlen = sizeof(remaddr); // length of addresses
if(!make_addr(arp_data->addr, arp_data->port, (__SOCKADDR_ARG) &remaddr, &addrlen)) {
//Make command string
char *command = ksnet_formatMessage("%s %d",
);
// Send command to server
(char*)peer_to,
CMD_USER + 1,
command, strlen(command) + 1
);
// Free command string memory
free(command);
}
}
// Show menu
ke->event_cb(ke, EV_K_USER , NULL, 0, NULL);
}
break;
default:
break;
}
}
}
break;
// Send by timer
case EV_K_USER:
case EV_K_TIMER:
{
if(strcmp(peer_to, ksnetEvMgrGetHostName(ke))) {
// If peer_to is connected
if (ksnetArpGet(ke->kc->ka, peer_to) != NULL) {
ksnetEvMgrSetCustomTimer(ke, 0.00); // Stop timer
printf("\n"
"Multi send test menu:\n"
"\n"
" 1 - send packets\n"
" 2 - show TR-UDP statistics\n"
" 3 - get and show remote peer TR-UDP statistics\n"
" 4 - send TR-UDP reset\n"
" 5 - show data or TR-UDP statistic screen at server (now: %s)\n"
" 0 - exit\n"
"\n"
"teoackm $ "
, show_data_or_statistic_at_server ? "data" : "statistic"
);
fflush(stdout);
}
}
}
break;
// Send when DATA received
{
// DATA event
switch(rd->cmd) {
// Server got DATA from client
case CMD_USER:
printf("Got DATA: %s\n", (char*)rd->data);
}
break;
// Server got CONTROL from client
case CMD_USER + 1:
{
// Send statistic back
if(!strcmp((char*)rd->data, CMD_U_STAT)) {
char *stat = ksnTRUDPstatShowStr(ke->kc->ku, 0);
CMD_USER + 2, stat, strlen(stat)+1);
free(stat);
}
// Show data or statistic
else if(!strncmp((char*)rd->data, CMD_U_DATA_OR_STAT,
strlen(CMD_U_DATA_OR_STAT))) {
sscanf((char*)rd->data, "%*s %d",
printf("show_data_or_statistic_at_server %d\n",
// Show DATA - stop TR-UDP statistic
}
//Show Statistic - start TR-UDP statistic
else {
}
}
}
break;
// Client got statistic from remote peer (server)
case CMD_USER + 2:
printf("%s%s\n", (char*)rd->data, PRESS_A);
break;
}
}
break;
// Teo ACK received use ksnetAllowAckEvent to allow this event
{
// ACK event
if(strcmp(peer_to, SERVER_NAME) && strcmp((char*)rd->data, CMD_U_STAT)) {
printf("%d ", *(uint32_t*)user_data);
fflush(stdout);
}
//ksnetEvMgrSetCustomTimer(ke, 1.00); // Set custom timer interval
}
break;
// Undefined event (an error)
default:
break;
}
}
int main(int argc, char** argv) {
printf("Teoack multi example ver " TACKM_VERSION ", based on teonet ver. "
VERSION "\n");
// Application parameters
const char *app_argv[] = { "", "peer_to"};
ksnetEvMgrAppParam app_param;
app_param.app_argc = 2;
app_param.app_argv = app_argv;
app_param.app_descr = NULL;
// Initialize teonet event manager and Read configuration
// Set custom timer interval
// Allow ACK event
// Start teonet
return (EXIT_SUCCESS);
}