Module: sip-router Branch: master Commit: 0abf67a47450f0350a7826ec0719e66f4fc61022 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=0abf67a4...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: Sun Mar 30 15:27:53 2014 +0200
evapi: execute cfg event routes
- event_route[evapi:connection-new] - new client connected - event_route[evapi:connection-closed] - client connection closed - event_route[evapi:message-received] - message received from client
---
modules/evapi/Makefile | 1 + modules/evapi/evapi_dispatch.c | 105 ++++++++++++++++++++++++++++++++++++---- modules/evapi/evapi_dispatch.h | 2 + modules/evapi/evapi_mod.c | 9 ++++ 4 files changed, 108 insertions(+), 9 deletions(-)
diff --git a/modules/evapi/Makefile b/modules/evapi/Makefile index cd6d856..097c552 100644 --- a/modules/evapi/Makefile +++ b/modules/evapi/Makefile @@ -32,5 +32,6 @@ DEFS+=-DKAMAILIO_MOD_INTERFACE
SERLIBPATH=../../lib SER_LIBS+=$(SERLIBPATH)/srutils/srutils +SER_LIBS+=$(SERLIBPATH)/kcore/kcore
include ../../Makefile.modules diff --git a/modules/evapi/evapi_dispatch.c b/modules/evapi/evapi_dispatch.c index 59297e2..5a13ae4 100644 --- a/modules/evapi/evapi_dispatch.c +++ b/modules/evapi/evapi_dispatch.c @@ -36,6 +36,7 @@ #include "../../sr_module.h" #include "../../dprint.h" #include "../../ut.h" +#include "../../lib/kcore/faked_msg.h"
static int _evapi_notify_sockets[2];
@@ -51,6 +52,77 @@ typedef struct _evapi_client { #define EVAPI_MAX_CLIENTS 8 static evapi_client_t _evapi_clients[EVAPI_MAX_CLIENTS];
+typedef struct _evapi_evroutes { + int con_new; + int con_closed; + int msg_received; +} evapi_evroutes_t; + +static evapi_evroutes_t _evapi_rts; + +static int _evapi_con_idx = -1; + +/** + * + */ +void evapi_init_event_routes(void) +{ + memset(&_evapi_rts, 0, sizeof(evapi_evroutes_t)); + + _evapi_rts.con_new = route_get(&event_rt, "evapi:connection-new"); + if (_evapi_rts.con_new < 0 || event_rt.rlist[_evapi_rts.con_new] == NULL) + _evapi_rts.con_new = -1; + _evapi_rts.con_closed = route_get(&event_rt, "evapi:connection-closed"); + if (_evapi_rts.con_closed < 0 || event_rt.rlist[_evapi_rts.con_closed] == NULL) + _evapi_rts.con_closed = -1; + _evapi_rts.msg_received = route_get(&event_rt, "evapi:message-received"); + if (_evapi_rts.msg_received < 0 || event_rt.rlist[_evapi_rts.msg_received] == NULL) + _evapi_rts.msg_received = -1; +} + +/** + * + */ +int evapi_run_cfg_route(int conidx, int rt) +{ + int backup_rt; + struct run_act_ctx ctx; + sip_msg_t *fmsg; + + if(conidx<0) + return -1; + + if(rt<0) + return 0; + + fmsg = faked_msg_next(); + backup_rt = get_route_type(); + _evapi_con_idx = conidx; + set_route_type(REQUEST_ROUTE); + init_run_actions_ctx(&ctx); + run_top_route(event_rt.rlist[rt], fmsg, 0); + _evapi_con_idx = -1; + set_route_type(backup_rt); + return 0; +} + +/** + * + */ +int evapi_cfg_close_connection(void) +{ + if(_evapi_con_idx<0 || _evapi_con_idx>=EVAPI_MAX_CLIENTS) + return -1; + if(_evapi_clients[_evapi_con_idx].connected==1 + && _evapi_clients[_evapi_con_idx].sock > 0) { + close(_evapi_clients[_evapi_con_idx].sock); + _evapi_clients[_evapi_con_idx].connected = 0; + _evapi_clients[_evapi_con_idx].sock = 0; + return 0; + } + return -2; +} + /** * */ @@ -129,22 +201,32 @@ void evapi_recv_client(struct ev_loop *loop, struct ev_io *watcher, int revents) return; }
+ for(i=0; i<EVAPI_MAX_CLIENTS; i++) { + if(_evapi_clients[i].connected==1 && _evapi_clients[i].sock==watcher->fd) { + break; + } + } + if(i==EVAPI_MAX_CLIENTS) { + LM_ERR("cannot lookup client socket %d\n", watcher->fd); + return; + } + if(rlen == 0) { /* client is gone */ - for(i=0; i<EVAPI_MAX_CLIENTS; i++) { - if(_evapi_clients[i].connected==1 && _evapi_clients[i].sock==watcher->fd) { - _evapi_clients[i].connected = 0; - _evapi_clients[i].sock = 0; - break; - } - } + evapi_run_cfg_route(i, _evapi_rts.con_closed); + _evapi_clients[i].connected = 0; + _evapi_clients[i].sock = 0; ev_io_stop(loop, watcher); free(watcher); - LM_INFO("client closing connection\n"); + LM_INFO("client closing connection - pos [%d] addr [%s:%d]\n", + i, _evapi_clients[i].src_addr, _evapi_clients[i].src_port); return; }
- LM_NOTICE("received [%.*s]\n", (int)rlen, rbuffer); + LM_NOTICE("{%d} [%s:%d] - received [%.*s]\n", + i, _evapi_clients[i].src_addr, _evapi_clients[i].src_port, + (int)rlen, rbuffer); + evapi_run_cfg_route(i, _evapi_rts.msg_received); }
/** @@ -212,6 +294,11 @@ void evapi_accept_client(struct ev_loop *loop, struct ev_io *watcher, int revent LM_DBG("new connection - pos[%d] from: [%s:%d]\n", i, _evapi_clients[i].src_addr, _evapi_clients[i].src_port);
+ evapi_run_cfg_route(i, _evapi_rts.con_new); + + if(_evapi_clients[i].connected == 0) + return; + /* start watcher to read messages from whatchers */ ev_io_init(evapi_client, evapi_recv_client, csock, EV_READ); ev_io_start(loop, evapi_client); diff --git a/modules/evapi/evapi_dispatch.h b/modules/evapi/evapi_dispatch.h index 33afdef..5db3bd7 100644 --- a/modules/evapi/evapi_dispatch.h +++ b/modules/evapi/evapi_dispatch.h @@ -35,4 +35,6 @@ int evapi_run_worker(int prank);
int evapi_relay(str *evdata);
+void evapi_init_event_routes(void); + #endif diff --git a/modules/evapi/evapi_mod.c b/modules/evapi/evapi_mod.c index 9a999d1..089be86 100644 --- a/modules/evapi/evapi_mod.c +++ b/modules/evapi/evapi_mod.c @@ -35,6 +35,7 @@ #include "../../mem/shm_mem.h" #include "../../mod_fix.h" #include "../../cfg/cfg_struct.h" +#include "../../lib/kcore/faked_msg.h"
#include "../../modules/tm/tm_load.h"
@@ -95,6 +96,12 @@ static int mod_init(void) { char *p;
+ /* init faked sip msg */ + if(faked_msg_init()<0) { + LM_ERR("failed to init faked sip msg\n"); + return -1; + } + if(load_tm_api( &tmb ) < 0) { LM_INFO("cannot load the TM-functions - async relay disabled\n"); memset(&tmb, 0, sizeof(tm_api_t)); @@ -119,6 +126,8 @@ static int mod_init(void) /* add child to update local config framework structures */ cfg_register_child(1 + _evapi_workers);
+ evapi_init_event_routes(); + return 0; }