Teonet library  0.4.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ev_mgr.c File Reference
#include <stdio.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <signal.h>
#include <pthread.h>
#include <stdbool.h>
#include "ev_mgr.h"
#include "net_multi.h"
#include "utils/utils.h"
#include "utils/rlutil.h"
#include "modules/metric.h"
#include "commands_creator.h"

Macros

#define MODULE   "event_manager"
 File: ev_mgr.c Author: Kirill Scherba kiril.nosp@m.l@sc.nosp@m.herba.nosp@m..ru More...
 
#define RUN_NAME   "-teonet.run"
 
#define kev   ((ksnetEvMgrClass*)(ka->ke))
 
#define kev   ((ksnetEvMgrClass *)w->data)
 
#define kev   ((ksnetEvMgrClass*)(w->data))
 
#define SEND_EVENT(data, data_len, user_data)   kev->event_cb(kev, EV_K_ASYNC , data, data_len, user_data)
 
#define kev   ((ksnetEvMgrClass *)w->data)
 

Typedefs

typedef void(* set_sigaction_h )(int, siginfo_t *, void *)
 Set sigaction function typedef. More...
 

Functions

ksnetEvMgrClass__ke_from_command_class (ksnCommandClass *X)
 
ksnetEvMgrClass__ke_from_L0_class (ksnLNullClass *X)
 
ksnetArpClass__arp_from_command_class (ksnCommandClass *X)
 
void idle_cb (EV_P_ ev_idle *w, int revents)
 Idle callback (runs every 11.5 second of Idle time) More...
 
void idle_activity_cb (EV_P_ ev_idle *w, int revents)
 Check activity Idle callback. More...
 
void idle_async_cb (EV_P_ ev_idle *w, int revents)
 Async Idle callback. More...
 
void timer_cb (EV_P_ ev_timer *w, int revents)
 Timer callback (runs every KSNET_EVENT_MGR_TIMER = 0.25 sec) More...
 
void host_cb (EV_P_ ev_io *w, int revents)
 Teonet host fd read event callback. More...
 
void sig_async_cb (EV_P_ ev_async *w, int revents)
 Async Event callback (Signal) More...
 
void sigint_cb (struct ev_loop *loop, ev_signal *w, int revents)
 Signal to stop event loop callback. More...
 
void sigusr2_cb (struct ev_loop *loop, ev_signal *w, int revents)
 SIGUSR2 signal handler. More...
 
int modules_init (ksnetEvMgrClass *ke)
 Initialize connected to Event Manager Modules. More...
 
void modules_destroy (ksnetEvMgrClass *ke)
 Destroy modules. More...
 
int send_cmd_disconnect_cb (ksnetArpClass *ka, char *name, ksnet_arp_data *arp_data, void *data)
 
void metric_teonet_count (teoMetricClass *tm)
 Send default teonet metrics. More...
 
ksnetEvMgrClassksnetEvMgrInit (int argc, char **argv, ksn_event_cb_type event_cb, int options)
 Initialize KSNet Event Manager and network. More...
 
ksnetEvMgrClassksnetEvMgrInitPort (int argc, char **argv, void(*event_cb)(ksnetEvMgrClass *ke, ksnetEvMgrEvents event, void *data, size_t data_len, void *user_data), int options, int port, void *user_data)
 Initialize KSNet Event Manager and network and set new default port. More...
 
void teoSetAppType (ksnetEvMgrClass *ke, char *type)
 Set Teonet application type. More...
 
void teoSetAppVersion (ksnetEvMgrClass *ke, char *version)
 Set Teonet application version. More...
 
const char * teoGetAppType (ksnetEvMgrClass *ke)
 Get current application type. More...
 
char * teoGetFullAppTypeFromHostInfo (host_info_data *hid)
 
const char * teoGetAppVersion (ksnetEvMgrClass *ke)
 Get current application version. More...
 
const char * teoGetLibteonetVersion ()
 Get teonet library version. More...
 
void ksnetEvMgrStop (ksnetEvMgrClass *ke)
 Stop event manager. More...
 
int ksnetEvMgrStatus (ksnetEvMgrClass *ke)
 
int ksnetEvMgrRun (ksnetEvMgrClass *ke)
 Start KSNet Event Manager and network communication. More...
 
int ksnetEvMgrFree (ksnetEvMgrClass *ke, int free_async)
 Free ksnetEvMgrClass after run. More...
 
void ksnetEvMgrSetCustomTimer (ksnetEvMgrClass *ke, double time_interval)
 Set custom timer interval. More...
 
char * ksnetEvMgrGetHostName (ksnetEvMgrClass *ke)
 Return host name. More...
 
host_info_data * teoGetHostInfo (ksnetEvMgrClass *ke, size_t *hd_len)
 Get host info data. More...
 
void ksnetEvMgrAsync (ksnetEvMgrClass *ke, void *data, size_t data_len, void *user_data)
 Async call to Event manager. More...
 
double ksnetEvMgrGetTime (ksnetEvMgrClass *ke)
 Get KSNet event manager time. More...
 
void connect_r_host_cb (ksnetEvMgrClass *ke)
 Connect to remote host. More...
 
void open_local_port (ksnetEvMgrClass *ke)
 Send packet to local address to open private firewall. More...
 
int remove_peer_addr (ksnetEvMgrClass *ke, __CONST_SOCKADDR_ARG addr)
 Remove peer by address. More...
 
int check_connected_cb (ksnetArpClass *ka, char *peer_name, ksnet_arp_data_ext *arp, void *data)
 Check connected peers, send trip time request and disconnect peer at timeout. More...
 
int ksnetEvMgrRestart (int argc, char **argv)
 Restart application. More...
 

Variables

int teo_argc
 
char ** teo_argv
 
const char * null_str = ""
 File: ev_mgr.h Author: Kirill Scherba kiril.nosp@m.l@sc.nosp@m.herba.nosp@m..ru More...
 
char run_file [KSN_BUFFER_SIZE]
 

Macro Definition Documentation

#define kev   ((ksnetEvMgrClass*)(ka->ke))
#define kev   ((ksnetEvMgrClass *)w->data)
#define kev   ((ksnetEvMgrClass*)(w->data))
#define kev   ((ksnetEvMgrClass *)w->data)
#define MODULE   "event_manager"
#define RUN_NAME   "-teonet.run"

Referenced by ksnetEvMgrRun().

#define SEND_EVENT (   data,
  data_len,
  user_data 
)    kev->event_cb(kev, EV_K_ASYNC , data, data_len, user_data)

Referenced by sig_async_cb().

Typedef Documentation

typedef void(* set_sigaction_h)(int, siginfo_t *, void *)

Set sigaction function typedef.

Function Documentation

ksnetArpClass* __arp_from_command_class ( ksnCommandClass X)

References ksnCommandClass::kc.

ksnetEvMgrClass* __ke_from_command_class ( ksnCommandClass X)

References ksnCommandClass::kc.

ksnetEvMgrClass* __ke_from_L0_class ( ksnLNullClass X)

References ksnLNullClass::ke.

int check_connected_cb ( ksnetArpClass ka,
char *  peer_name,
ksnet_arp_data_ext *  arp,
void *  data 
)

Check connected peers, send trip time request and disconnect peer at timeout.

Parameters
ka
peer_name
arp_data
data

References CHECK_EVENTS_AFTER, kev, ksnCommandSendCmdEcho(), ksnetEvMgrGetTime(), send_cmd_disconnect_cb(), TRIPTIME, and TRIPTIME_LEN.

Referenced by idle_activity_cb().

void host_cb ( EV_P_ ev_io *  w,
int  revents 
)

Teonet host fd read event callback.

param loop

Parameters
w
revents
void idle_activity_cb ( EV_P_ ev_idle *  w,
int  revents 
)

Check activity Idle callback.

Start(restart) connection to R-Host and check connections to all peers.

param loop

Parameters
w
revents

References check_connected_cb(), connect_r_host_cb(), DEBUG_VV, kev, ksn_printf, ksnetArpGetAll(), MODULE, and UINT32_MAX.

Referenced by ksnetEvMgrRun().

void idle_async_cb ( EV_P_ ev_idle *  w,
int  revents 
)

Async Idle callback.

Process async queue messages

param loop

Parameters
w
revents

References kev.

Referenced by ksnetEvMgrRun().

void idle_cb ( EV_P_ ev_idle *  w,
int  revents 
)

Idle callback (runs every 11.5 second of Idle time)

This callback started by event manager timer after every CHECK_EVENTS_AFTER = 11.5 second. Send two user event: first EV_K_STARTED sent at startup (when application started) and second EV_K_IDLE sent every tick.

param loop

Parameters
wPointer to watcher
revents
Todo:
: open_local_port(kev);
Todo:
: host_cb(EV_A_ (ev_io*)w, revents);

References connect_r_host_cb(), DEBUG_VV, EV_K_IDLE, EV_K_STARTED, ksnetEvMgrClass::ev_loop, ksnetEvMgrClass::event_cb, ksnCoreClass::host_w, ksnetEvMgrClass::idle_count, ksnetEvMgrClass::kc, ksn_printf, ksnCoreSetEventTime, MODULE, teonet_cfg::r_tcp_f, teoAsyncClass::t_id, ksnetEvMgrClass::ta, ksnetEvMgrClass::teo_cfg, and UINT32_MAX.

Referenced by ksnetEvMgrRun().

void ksnetEvMgrAsync ( ksnetEvMgrClass ke,
void *  data,
size_t  data_len,
void *  user_data 
)

Async call to Event manager.

Parameters
kePointer to ksnetEvMgrClass
dataPointer to data
data_lenData length
user_dataPointer to user data
Examples:
teofossa.c.

References ksnetEvMgrClass::async_mutex, ksnetEvMgrClass::async_queue, ksnetEvMgrClass::ev_loop, malloc(), and ksnetEvMgrClass::sig_async_w.

Referenced by teoLoggingClientSend().

char* ksnetEvMgrGetHostName ( ksnetEvMgrClass ke)
inline
ksnetEvMgrClass* ksnetEvMgrInit ( int  argc,
char **  argv,
ksn_event_cb_type  event_cb,
int  options 
)

Initialize KSNet Event Manager and network.

Parameters
argcNumber of applications arguments (from main)
argvApplications arguments array (from main)
event_cbEvents callback function called when an event happens
optionsOptions set:
READ_OPTIONS - read options from command line parameters;
READ_CONFIGURATION - read options from configuration file
Returns
Pointer to created ksnetEvMgrClass
Examples:
teocque.c, teofossa.c, teolarge.c, teosend.c, teoterm.c, and teotun.c.

References ksnetEvMgrInitPort().

Referenced by main(), and test2().

ksnetEvMgrClass* ksnetEvMgrInitPort ( int  argc,
char **  argv,
void(*)(ksnetEvMgrClass *ke, ksnetEvMgrEvents event, void *data, size_t data_len, void *user_data)  event_cb,
int  options,
int  port,
void *  user_data 
)

Initialize KSNet Event Manager and network and set new default port.

Parameters
argcNumber of applications arguments (from main)
argvApplications arguments array (from main)
event_cbEvents callback function called when an event happens
optionsOptions set:
READ_OPTIONS - read options from command line parameters;
READ_CONFIGURATION - read options from configuration file
portSet default port number if non 0
user_dataPointer to user data or NULL if absent
Returns
Pointer to created ksnetEvMgrClass
Examples:
teoack.c, teoackm.c, teodb_ex.c, teol0cli.c, teomulti_t.c, teosscr.c, teostream.c, and teotcp.c.

References teonet_cfg::app_argc, teonet_cfg::app_argv, APP_PARAM, ksnetEvMgrClass::argc, ksnetEvMgrClass::argv, ksnetEvMgrClass::async_mutex, BLOCK_CLI_INPUT, teonet_cfg::block_cli_input_f, ksnetEvMgrClass::custom_timer_interval, ksnetEvMgrClass::ev_loop, ksnetEvMgrClass::event_cb, event_cb(), ke, ksnetEvMgrClass::kf, ksnetEvMgrClass::kl, ksnetEvMgrClass::km, ksnetEvMgrClass::ks, ksnet_configInit(), ksnet_optRead(), ksnet_optSetApp(), ksnetEvMgrClass::kt, ksnetEvMgrClass::last_custom_timer, malloc(), ksnetEvMgrClass::n_next, ksnetEvMgrClass::n_prev, ksnetEvMgrClass::net_count, ksnetEvMgrClass::net_idx, null_str, teonet_cfg::port, ksnetEvMgrClass::printf_mutex, read_config(), READ_CONFIGURATION, READ_OPTIONS, ksnetEvMgrClass::runEventMgr, teo_argc, teo_argv, ksnetEvMgrClass::teo_cfg, ksnetEvMgrClass::teo_class, ksnetEvMgrClass::tp, and ksnetEvMgrClass::user_data.

Referenced by ksnetEvMgrInit(), ksnMultiInit(), main(), teoMultiAddNet(), teo::Teonet::Teonet(), and teonet_t().

int ksnetEvMgrRestart ( int  argc,
char **  argv 
)

Restart application.

Parameters
argc
argv
Returns

References free(), ksnet_formatMessage(), ksnet_stringArrSplit(), and usleep().

Referenced by ksnetEvMgrFree(), ksnMultiDestroy(), and sigusr2_cb().

void ksnetEvMgrSetCustomTimer ( ksnetEvMgrClass ke,
double  time_interval 
)

Set custom timer interval.

Parameters
kePointer to ksnetEvMgrClass
time_interval
Examples:
teoack.c, teoackm.c, teocque.c, teolarge.c, teosend.c, and teosscr.c.

References ksnetEvMgrClass::custom_timer_interval, ksnetEvMgrGetTime(), and ksnetEvMgrClass::last_custom_timer.

Referenced by event_cb(), main(), and teo::Teonet::setCustomTimer().

int ksnetEvMgrStatus ( ksnetEvMgrClass ke)
void ksnetEvMgrStop ( ksnetEvMgrClass ke)
inline
void open_local_port ( ksnetEvMgrClass ke)
int remove_peer_addr ( ksnetEvMgrClass ke,
__CONST_SOCKADDR_ARG  addr 
)

Remove peer by address.

Parameters
kePointer to ksnetEvMgrClass
addr
Returns
Return values
0peer not found
1peer removed
-1peer not removed, this is this host

References ksnCoreClass::ka, ksnetEvMgrClass::kc, and ksnetArpFindByAddr().

Referenced by trudp_event_cb().

int send_cmd_disconnect_cb ( ksnetArpClass ka,
char *  name,
ksnet_arp_data *  arp_data,
void *  data 
)

Referenced by check_connected_cb().

void sig_async_cb ( EV_P_ ev_async *  w,
int  revents 
)

Async Event callback (Signal)

param loop

Parameters
w
revents

References data, DEBUG_VV, free(), kev, ksn_puts, MODULE, and SEND_EVENT.

Referenced by ksnetEvMgrRun().

void sigint_cb ( struct ev_loop *  loop,
ev_signal *  w,
int  revents 
)

Signal to stop event loop callback.

Parameters
loop
w
revents

References DEBUG, DEBUG_KSNET, ksn_puts, and MODULE.

Referenced by ksnetEvMgrRun().

void sigusr2_cb ( struct ev_loop *  loop,
ev_signal *  w,
int  revents 
)
const char* teoGetAppType ( ksnetEvMgrClass ke)
inline

Get current application type.

Parameters
kePointer to ksnetEvMgrClass
Returns
Application type string

References ksnetEvMgrClass::app_type.

Referenced by teoGetHostInfo().

const char* teoGetAppVersion ( ksnetEvMgrClass ke)
inline

Get current application version.

Parameters
kePointer to ksnetEvMgrClass
Returns
Application version string

References ksnetEvMgrClass::app_version.

Referenced by teoGetHostInfo().

char* teoGetFullAppTypeFromHostInfo ( host_info_data *  hid)
host_info_data* teoGetHostInfo ( ksnetEvMgrClass ke,
size_t *  hd_len 
)

Get host info data.

Parameters
kePointer to ksnetEvMgrClass
hd_lenPointer to host_info_data length holder
Returns
Pointer to host_info_data, should be free after use

References ksnetEvMgrGetHostName(), teonet_cfg::l0_allow_f, malloc(), null_str, ksnetEvMgrClass::teo_cfg, teoGetAppType(), teoGetAppVersion(), VERSION, and teonet_cfg::vpn_connect_f.

Referenced by createCmdConnectPacket(), createCmdConnectRPacketUdp(), and ksnetArpAddHost().

const char* teoGetLibteonetVersion ( )
inline

Get teonet library version.

Returns

References VERSION.

Referenced by teo::Teonet::getTeonetVersion(), and main().

void teoSetAppType ( ksnetEvMgrClass ke,
char *  type 
)
inline

Set Teonet application type.

Parameters
kePointer to ksnetEvMgrClass
typeApplication type string
Examples:
teodb_ex.c.

References ksnetEvMgrClass::app_type.

Referenced by event_cb(), main(), and teo::Teonet::setAppType().

void teoSetAppVersion ( ksnetEvMgrClass ke,
char *  version 
)
inline

Set Teonet application version.

Parameters
kePointer to ksnetEvMgrClass
versionApplication version string

References ksnetEvMgrClass::app_version.

Referenced by event_cb(), main(), and teo::Teonet::setAppVersion().

Variable Documentation

const char* null_str = ""
char run_file[KSN_BUFFER_SIZE]

Referenced by ksnetEvMgrFree(), and ksnetEvMgrRun().

int teo_argc

Referenced by ksnetEvMgrInitPort().

char ** teo_argv

Referenced by ksnetEvMgrInitPort().