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

File: teofossa.c Author: Kirill Scherba kiril.nosp@m.l@sc.nosp@m.herba.nosp@m..ru

Check integration teonet with fossa

The Teonet runs in main thread and the fossa runs in separate thread.

How to execute this test:

1) Start this test application in terminal:

 tests/fossa/teofossa teofossa -a 127.0.0.1

2) Start telnet in other terminal:

 telnet 0 1234

3) Type in the terminal some text:

 Hello world!

4) Press Ctrl+C in the teonet terminal window to stop this test.

The text "Hello world!" will be:

Created on July 20, 2015, 5:09 PM

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "fossa.h"
#include "ev_mgr.h"
#define TFOSSA_VERSION VERSION
pthread_t tid; // Fossa thread id
size_t data_len, void *user_data) {
switch (ev) {
case EV_K_ASYNC:
{
char *d = strndup(data, data_len); // Create temporary data buffer to add 0 at the end of string
d[strcspn(d, "\r\n")] = 0; // Remove trailing CRLF
printf("Async event was received %d bytes: %s\n", (int)data_len, (char*) d);
// This event handler implements simple TCP echo server
ns_send(user_data, data, data_len); // Echo received data back
free(d); // Free temporary data buffer
}
break;
default:
break;
}
}
static void ev_handler(struct ns_connection *nc, int ev, void *ev_data) {
struct mbuf *io = &nc->recv_mbuf;
switch (ev) {
case NS_RECV:
// Send event to teonet
ksnetEvMgrAsync(nc->mgr->user_data, io->buf, io->len, nc);
// Discard data from recv buffer
mbuf_remove(io, io->len); // Discard data from recv buffer
break;
default:
break;
}
}
void* fossa(void *ke) {
struct ns_mgr mgr;
ns_mgr_init(&mgr, ke);
// Note that many connections can be added to a single event manager
// Connections can be created at any point, e.g. in event handler function
ns_bind(&mgr, "1234", ev_handler);
for (;;) {
ns_mgr_poll(&mgr, 100);
}
ns_mgr_free(&mgr);
return NULL;
}
int main(int argc, char** argv) {
// Hello message
printf("Teofossa " TFOSSA_VERSION "\n");
// Initialize teonet event manager and Read configuration
// Start fossa thread
int err = pthread_create(&tid, NULL, &fossa, ke);
if (err != 0) printf("\nCan't create thread :[%s]", strerror(err));
else printf("\nThread created successfully\n");
// Set custom timer interval
//ksnetEvMgrSetCustomTimer(ke, 1.00);
// Hello message
ksnet_printf(&ke->teo_cfg, MESSAGE, "Started...\n\n");
// Start teonet
return (EXIT_SUCCESS);
}