Commit a67d23ea authored by jehan's avatar jehan

implement keep alives

parent 2a172e58
......@@ -24,8 +24,24 @@ BELLE_SIP_BEGIN_DECLS
const char *belle_sip_listening_point_get_ip_address(const belle_sip_listening_point_t *lp);
int belle_sip_listening_point_get_port(const belle_sip_listening_point_t *lp);
const char *belle_sip_listening_point_get_transport(const belle_sip_listening_point_t *ip);
const char *belle_sip_listening_point_get_ip_address(const belle_sip_listening_point_t *ip);
const char *belle_sip_listening_point_get_transport(const belle_sip_listening_point_t *lp);
const char *belle_sip_listening_point_get_ip_address(const belle_sip_listening_point_t *lp);
/*
* set keep alive frequency in ms
* @param lp object
* @param ms keep alive period in ms. Values <=0 disable keep alive
* */
void belle_sip_listening_point_set_keep_alive(belle_sip_listening_point_t *lp,int ms);
/*
* get keep alive frequency in ms
* @param lp object
* @return keep alive period in ms. Values <=0 disable keep alive
* */
int belle_sip_listening_point_get_keep_alive(const belle_sip_listening_point_t *lp);
/**
* get the listening information as an URI
* @return IP/port/transport as an URI
......
......@@ -64,6 +64,22 @@ belle_sip_main_loop_t *belle_sip_main_loop_new(void);
**/
unsigned long belle_sip_main_loop_add_timeout(belle_sip_main_loop_t *ml, belle_sip_source_func_t func, void *data, unsigned int timeout_value_ms);
/**
* Adds a timeout into the main loop
* @param ml
* @param func a callback function to be called to notify timeout expiration
* @param data a pointer to be passed to the callback
* @param timeout_value_ms duration of the timeout.
* @param timer_name name of the timer, can be null
* @returns timeout belle_sip_source_t with ref count = 1
**/
belle_sip_source_t* belle_sip_main_loop_create_timeout(belle_sip_main_loop_t *ml
, belle_sip_source_func_t func
, void *data
, unsigned int timeout_value_ms
,const char* timer_name);
/**
* Creates a timeout source, similarly to belle_sip_main_loop_add_timeout().
......
......@@ -273,11 +273,18 @@ void belle_sip_main_loop_add_source(belle_sip_main_loop_t *ml, belle_sip_source_
ml->nsources++;
}
unsigned long belle_sip_main_loop_add_timeout(belle_sip_main_loop_t *ml, belle_sip_source_func_t func, void *data, unsigned int timeout_value_ms){
belle_sip_source_t* belle_sip_main_loop_create_timeout(belle_sip_main_loop_t *ml
, belle_sip_source_func_t func
, void *data
, unsigned int timeout_value_ms
,const char* timer_name) {
belle_sip_source_t * s=belle_sip_timeout_source_new(func,data,timeout_value_ms);
belle_sip_object_set_name((belle_sip_object_t*)s,"timeout");
belle_sip_object_set_name((belle_sip_object_t*)s,timer_name);
belle_sip_main_loop_add_source(ml,s);
return s;
}
unsigned long belle_sip_main_loop_add_timeout(belle_sip_main_loop_t *ml, belle_sip_source_func_t func, void *data, unsigned int timeout_value_ms){
belle_sip_source_t * s=belle_sip_main_loop_create_timeout(ml,func,data,timeout_value_ms,"Timer");
belle_sip_object_unref(s);
return s->id;
}
......
......@@ -38,6 +38,7 @@ static void belle_sip_listening_point_uninit(belle_sip_listening_point_t *lp){
belle_sip_object_unref(lp->listening_uri);
lp->channel_listener=NULL; /*does not unref provider*/
belle_sip_uninit_sockets();
belle_sip_listening_point_set_keep_alive(lp,-1);
}
......@@ -145,4 +146,69 @@ void belle_sip_listener_set_channel_listener(belle_sip_listening_point_t *lp,bel
lp->channel_listener=channel_listener;
}
static int send_keep_alive(belle_sip_channel_t* obj) {
/*keep alive*/
const char* crlfcrlf = "\r\n\r\n";
int size=strlen(crlfcrlf);
if (belle_sip_channel_send(obj,crlfcrlf,size)<0){
belle_sip_error("channel [%p]: could not send [%i] bytes of keep alive from [%s://%s:%i] to [%s:%i]" ,obj
,size
,belle_sip_channel_get_transport_name(obj)
,obj->local_ip
,obj->local_port
,obj->peer_name
,obj->peer_port);
return -1;
}else{
belle_sip_message("channel [%p]: keep alive sent to [%s://%s:%i]"
,obj
,belle_sip_channel_get_transport_name(obj)
,obj->peer_name
,obj->peer_port);
return 0;
}
}
static int keep_alive_timer_func(void *user_data, unsigned int events) {
belle_sip_listening_point_t* lp=(belle_sip_listening_point_t*)user_data;
belle_sip_list_t* iterator;
belle_sip_channel_t* channel;
for (iterator=lp->channels;iterator!=NULL;iterator=iterator->next) {
channel=(belle_sip_channel_t*)iterator->data;
if (send_keep_alive(channel)) {
channel_set_state(channel,BELLE_SIP_CHANNEL_ERROR);
belle_sip_channel_close(channel);
}
}
return BELLE_SIP_CONTINUE;
}
void belle_sip_listening_point_set_keep_alive(belle_sip_listening_point_t *lp,int ms) {
if (ms <=0) {
if(lp->keep_alive_timer) {
belle_sip_main_loop_remove_source(lp->stack->ml,lp->keep_alive_timer);
belle_sip_object_unref(lp->keep_alive_timer);
lp->keep_alive_timer=NULL;
}
return;
}
if (!lp->keep_alive_timer) {
lp->keep_alive_timer = belle_sip_main_loop_create_timeout(lp->stack->ml
, keep_alive_timer_func
, lp
, ms
,"keep alive") ;
} else {
belle_sip_source_set_timeout(lp->keep_alive_timer,ms);
}
return;
}
int belle_sip_listening_point_get_keep_alive(const belle_sip_listening_point_t *lp) {
return lp->keep_alive_timer?belle_sip_source_get_timeout(lp->keep_alive_timer):-1;
}
......@@ -40,8 +40,9 @@ struct belle_sip_listening_point{
belle_sip_object_t base;
belle_sip_stack_t *stack;
belle_sip_list_t *channels;
belle_sip_channel_listener_t* channel_listener; /*inital chennel listener used for channel creation*/
belle_sip_channel_listener_t* channel_listener; /*initial chennel listener used for channel creation*/
belle_sip_uri_t* listening_uri;
belle_sip_source_t* keep_alive_timer;
};
void belle_sip_listening_point_init(belle_sip_listening_point_t *lp, belle_sip_stack_t *s, const char *address, int port);
......
......@@ -113,7 +113,7 @@ static test_listener_t *listener;
int register_init(void) {
stack=belle_sip_stack_new(NULL);
belle_sip_listening_point_t *lp=belle_sip_stack_create_listening_point(stack,"0.0.0.0",7060,"UDP");
belle_sip_listening_point_t* lp=belle_sip_stack_create_listening_point(stack,"0.0.0.0",7060,"UDP");
prov=belle_sip_stack_create_provider(stack,lp);
lp=belle_sip_stack_create_listening_point(stack,"0.0.0.0",7060,"TCP");
......@@ -256,7 +256,12 @@ static void stateless_register_tcp(void){
static void stateful_register_udp(void){
register_test(NULL,1);
}
static void stateful_register_udp_with_keep_alive() {
belle_sip_listening_point_set_keep_alive(belle_sip_provider_get_listening_point(prov,"udp"),200);
register_test(NULL,1);
belle_sip_main_loop_sleep(belle_sip_stack_get_main_loop(stack),500);
belle_sip_listening_point_set_keep_alive(belle_sip_provider_get_listening_point(prov,"udp"),-1);
}
static void stateful_register_udp_with_outband_proxy(void){
register_with_outband("udp",1,test_domain);
}
......@@ -340,9 +345,13 @@ static void test_register_authenticate() {
int belle_sip_register_test_suite(){
CU_pSuite pSuite = CU_add_suite("Register", register_init, register_uninit);
if (NULL == CU_add_test(pSuite, "stateful-udp-register", stateful_register_udp)) {
if (NULL == CU_add_test(pSuite, "stateful_register_udp", stateful_register_udp)) {
return CU_get_error();
}
if (NULL == CU_add_test(pSuite, "stateful_register_udp_with_keep_alive", stateful_register_udp_with_keep_alive)) {
return CU_get_error();
}
if (NULL == CU_add_test(pSuite, "stateful-udp-register-with-network-delay", stateful_register_udp_delayed)) {
return CU_get_error();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment