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

Teonet stream exampleStart stream server: examples/teostream teo-str NULL NULL -p 9000 -r 9099 -a 127.0.0.1

Start stream client: examples/teostream teostream teo-str str -r 9000 -a 127.0.0.1

Created on October 5, 2015, 5:06 PM

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "ev_mgr.h"
#define TSTR_VERSION "0.0.1"
size_t data_len, void *user_data) {
switch(event) {
// Calls immediately after event manager starts
break;
// This event send when new peer connected to this host (and to the mesh)
{
if(data != NULL) {
if(!strcmp(((ksnCorePacketData*)data)->from, ke->teo_cfg.app_argv[1])) {
printf("Peer \"%s\" was connected\n", ((ksnCorePacketData*)data)->from);
printf("Create stream name \"%s\" with peer \"%s\" ...\n",
ke->teo_cfg.app_argv[2], ke->teo_cfg.app_argv[1]);
// Send create stream request
}
}
} break;
// This event send when stream is connected to this host
// WRITE to stream
{
// Get stream parameters from event data (Stream Key)
ksnStreamMapData *smd = ksnStreamGetMapData(ke->ks, data, data_len);
printf("Stream name \"%s\" is connected to peer \"%s\" ...\n",
// Write data to output FD
if(write(sd.fd_out, "Hello stream!", 14) >= 0);
} break;
// Stream connection timeout
{
// Get stream parameters from event data (Stream Map Data)
// Do something ...
printf("Can't connect \"%s\" stream to peer \"%s\" during timeout\n",
} break;
// Got data from connected stream
// READ from stream
{
// Get stream parameters from event data (Stream Map Data)
// Read data to buffer
size_t rc;
char read_buf[KSN_BUFFER_SM_SIZE];
if((rc = read(sd.fd_in, read_buf, KSN_BUFFER_SM_SIZE)) >= 0) {
printf("Read %d bytes from stream name \"%s\" of peer \"%s\": "
"\"%s\"\n",
(int) rc, sd.stream_name, sd.peer_name, read_buf);
//if(write(sd.fd_out, read_buf, rc) >= 0);
}
} break;
{
// Get stream parameters from event data (Stream Key)
ksnStreamMapData *smd = ksnStreamGetMapData(ke->ks, data, data_len);
printf("Stream name \"%s\" was disconnected from peer \"%s\" ...\n",
} break;
// Undefined event (an error)
default:
break;
}
}
int main(int argc, char** argv) {
printf("Teostream example ver " TSTR_VERSION ", based on teonet ver. " VERSION "\n");
// Application parameters
const char *app_argv[] = { "", "peer_to", "stream_name"};
ksnetEvMgrAppParam app_param;
app_param.app_argc = 3;
app_param.app_argv = app_argv;
app_param.app_descr = NULL;
// Initialize teonet event manager and Read configuration
// Start teonet
return (EXIT_SUCCESS);
}