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

Using the Teonet Subscribe module

Module: subscribe.c This is example application to subscribe one teonet application to other teonet application event. This application has code for two type of applications: server and client. Client subscribed to server and get EV_K_SUBSCRIBE event when event happen at the server application.

Start applications:

In client application use:

In server application use:

Only the EV_K_CONNECTED, EV_K_DISCONNECTED, EV_K_L0_CONNECTED and EV_K_L0_DISCONNECTED events sent automatically by the teonet library. All other events should be described and send by applications provided this events from its event callback function.

Created on January 5, 2016, 3:38 PM

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ev_mgr.h"
#define TSSCR_VERSION "0.0.1"
static int app_type = 0;
size_t data_len, void *user_data) {
switch(event) {
// Connected event received
{
// Client send subscribe command to server
char *peer = ((ksnCorePacketData*)data)->from;
if(!strcmp(peer, ke->teo_cfg.app_argv[1])) {
printf("The peer: '%s' was connected\n", peer);
// Subscribe to EV_K_RECEIVED event in remote peer
}
}
break;
// Calls by timer started in main() function by calling the
// ksnetEvMgrSetCustomTimer() function
case EV_K_TIMER:
{
// Client send CMD_USER command to server
"Hello!", 7);
}
break;
// Subscribe event received
{
teoSScrData *ssrc_data = rd->data;
printf("EV_K_SUBSCRIBE received from: %s, event: %d, command: %d, "
"from: %s, data: %s\n",
rd->from, ssrc_data->ev, ssrc_data->cmd, ssrc_data->data,
ssrc_data->data + strlen(ssrc_data->data) + 1 );
}
break;
// Some other events
default:
break;
}
}
size_t data_len, void *user_data) {
switch(event) {
// Calls when client peer disconnected
{
if(rd->from != NULL) {
printf("Peer %s was disconnected \n", rd->from);
// Server UnSubscribe peer
// UnSubscribe peer from EV_K_RECEIVED event
}
else printf("Peer was disconnected\n");
}
break;
// Calls when data received
{
// Server send EV_K_SUBSCRIBE command to client when CMD_USER
// command received
if(rd->cmd == CMD_USER) {
printf("EV_K_RECEIVED command: %d, from %s, data: %s\n",
rd->cmd, rd->from, (char*)rd->data);
size_t rec_data_len = rd->from_len + rd->data_len;
char *rec_data = malloc(rec_data_len);
memcpy(rec_data, rd->from, rd->from_len);
memcpy(rec_data + rd->from_len, rd->data, rd->data_len);
// Send EV_K_RECEIVED event to all subscribers
teoSScrSend(ke->kc->kco->ksscr, EV_K_RECEIVED, rec_data,
rec_data_len, rd->cmd);
free(rec_data);
}
}
break;
// Some other events
default:
break;
}
}
void event_cb(ksnetEvMgrClass *ke, ksnetEvMgrEvents event, void *data,
size_t data_len, void *user_data) {
switch(event) {
// Calls immediately after event manager starts
{
// Type of application (client or server)
printf("Type of this application: ");
// Client
if(strcmp(ke->teo_cfg.app_argv[1], "null")) {
printf("Client peer subscribed to '%s' peer\n",
ke->teo_cfg.app_argv[1]);
app_type = 0;
}
// Server
else {
printf("Server peer to subscribe to\n");
app_type = 1;
}
}
break;
// All other events (switch to clients or servers event_cb)
default:
if(!app_type) event_cb_client(ke, event, data, data_len, user_data);
else event_cb_server(ke, event, data, data_len, user_data);
break;
}
}
int main(int argc, char** argv) {
// Show application info
printf("Teosscr example ver " TSSCR_VERSION ", "
"based on teonet ver. " VERSION "\n");
// Application parameters
const char *app_argv[] = { "", "remote_peer"};
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. See "case EV_K_TIMER" to continue this example
// Start teonet
return (EXIT_SUCCESS);
}