Commit a3b1d2e1 authored by jehan's avatar jehan

big bunch of code for marshalling

parent 43ab5a03
......@@ -114,6 +114,8 @@ void belle_sip_object_delete(void *obj);
void *belle_sip_object_cast(belle_sip_object_t *obj, belle_sip_type_id_t id, const char *castname, const char *file, int fileno);
char* belle_sip_object_to_string(belle_sip_object_t* obj);
unsigned int belle_sip_object_is_instance_of(belle_sip_object_t * obj,belle_sip_type_id_t id);
BELLE_SIP_END_DECLS
......
......@@ -64,7 +64,7 @@ void belle_sip_header_address_set_displayname(belle_sip_header_address_t* addres
typedef struct _belle_sip_header belle_sip_header_t;
const char* belle_sip_header_get_name (const belle_sip_header_t* obj);
void belle_sip_header_set_name (belle_sip_header_t* obj,const char* value);
int belle_sip_header_marshal(belle_sip_header_t* header, char* buff, unsigned int offset,unsigned int buff_size);
#define BELLE_SIP_HEADER(t) BELLE_SIP_CAST(t,belle_sip_header_t)
/***********************
......
......@@ -39,8 +39,12 @@ void belle_sip_parameters_set_parameter(belle_sip_parameters_t* obj,const char*
const belle_sip_list_t * belle_sip_parameters_get_parameter_names(belle_sip_parameters_t* obj);
const belle_sip_list_t * belle_sip_parameters_get_parameters(belle_sip_parameters_t* obj);
void belle_sip_parameters_remove_parameter(belle_sip_parameters_t* obj,const char* name);
int belle_sip_parameters_marshal(belle_sip_parameters_t* obj, char* buff,unsigned int offset,unsigned int buff_size);
#define BELLE_SIP_PARAMETERS(obj) BELLE_SIP_CAST(obj,belle_sip_parameters_t)
#endif /*PARAMETERS_H_*/
......@@ -174,6 +174,8 @@ typedef struct _belle_sip_uri belle_sip_uri_t;
*/
char* belle_sip_uri_to_string(belle_sip_uri_t* uri) ;
int belle_sip_uri_marshal(belle_sip_uri_t* uri, char* buff,unsigned int offset,unsigned int buff_size);
#define BELLE_SIP_URI(obj) BELLE_SIP_CAST(obj,belle_sip_uri_t)
#endif /*BELLE_SIP_URI_H_*/
......
This diff is collapsed.
......@@ -31,6 +31,7 @@
typedef void (*belle_sip_object_destroy_t)(belle_sip_object_t*);
typedef void (*belle_sip_object_clone_t)(belle_sip_object_t* obj, const belle_sip_object_t *orig);
typedef int (*belle_sip_object_marshal_t)(belle_sip_object_t* obj, char* buff,unsigned int offset,size_t buff_size);
struct _belle_sip_object_vptr{
belle_sip_type_id_t id;
......@@ -38,6 +39,8 @@ struct _belle_sip_object_vptr{
void *interfaces; /*unused for the moment*/
belle_sip_object_destroy_t destroy;
belle_sip_object_clone_t clone;
belle_sip_object_marshal_t marshal;
};
typedef struct _belle_sip_object_vptr belle_sip_object_vptr_t;
......@@ -59,13 +62,14 @@ extern belle_sip_object_vptr_t belle_sip_object_t_vptr;
#define BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_END };
#define BELLE_SIP_INSTANCIATE_VPTR(object_type,parent_type,destroy,clone) \
#define BELLE_SIP_INSTANCIATE_VPTR(object_type,parent_type,destroy,clone,marshal) \
belle_sip_object_vptr_t object_type##_vptr={ \
BELLE_SIP_TYPE_ID(object_type), \
(belle_sip_object_vptr_t*)&BELLE_SIP_OBJECT_VPTR_NAME(parent_type), \
NULL, \
(belle_sip_object_destroy_t)destroy, \
(belle_sip_object_clone_t)clone \
(belle_sip_object_clone_t)clone, \
(belle_sip_object_marshal_t)marshal\
}
......@@ -362,12 +366,17 @@ belle_sip_##object_type##_t* belle_sip_##object_type##_parse (const char* value)
}
#define BELLE_SIP_NEW(object_type,super_type) BELLE_SIP_NEW_HEADER(object_type,super_type,NULL)
#define BELLE_SIP_NEW_HEADER(object_type,super_type,name) \
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_##object_type##_t,belle_sip_##super_type##_t , belle_sip_##object_type##_destroy, belle_sip_##object_type##_clone); \
#define BELLE_SIP_NEW_HEADER(object_type,super_type,name) BELLE_SIP_NEW_HEADER_INIT(object_type,super_type,name,header)
#define BELLE_SIP_NEW_HEADER_INIT(object_type,super_type,name,init_type) \
BELLE_SIP_INSTANCIATE_VPTR( belle_sip_##object_type##_t\
, belle_sip_##super_type##_t\
, belle_sip_##object_type##_destroy\
, belle_sip_##object_type##_clone\
, belle_sip_##object_type##_marshal); \
belle_sip_##object_type##_t* belle_sip_##object_type##_new () { \
belle_sip_##object_type##_t* l_object = belle_sip_object_new(belle_sip_##object_type##_t);\
belle_sip_##super_type##_init((belle_sip_##super_type##_t*)l_object); \
belle_sip_##init_type##_init((belle_sip_##init_type##_t*) l_object); \
if (name) belle_sip_header_set_name(BELLE_SIP_HEADER(l_object),name);\
return l_object;\
}
......
......@@ -42,7 +42,7 @@ void belle_sip_fd_source_init(belle_sip_source_t *s, belle_sip_source_func_t fun
s->notify=func;
}
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_source_t,belle_sip_object_t,belle_sip_source_destroy,NULL);
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_source_t,belle_sip_object_t,belle_sip_source_destroy,NULL,NULL);
belle_sip_source_t * belle_sip_fd_source_new(belle_sip_source_func_t func, void *data, int fd, unsigned int events, unsigned int timeout_value_ms){
belle_sip_source_t *s=belle_sip_object_new(belle_sip_source_t);
......@@ -89,7 +89,7 @@ static int main_loop_done(void *data, unsigned int events){
return TRUE;
}
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_main_loop_t,belle_sip_object_t,belle_sip_main_loop_destroy,NULL);
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_main_loop_t,belle_sip_object_t,belle_sip_main_loop_destroy,NULL,NULL);
belle_sip_main_loop_t *belle_sip_main_loop_new(void){
belle_sip_main_loop_t*m=belle_sip_object_new(belle_sip_main_loop_t);
......
......@@ -74,7 +74,8 @@ belle_sip_object_vptr_t belle_sip_object_t_vptr={
NULL, /*no parent, it's god*/
NULL,
_belle_sip_object_uninit,
_belle_sip_object_clone
_belle_sip_object_clone,
NULL
};
void belle_sip_object_delete(void *ptr){
......@@ -105,6 +106,7 @@ belle_sip_object_t *belle_sip_object_clone(const belle_sip_object_t *obj){
belle_sip_fatal("Object of type %i cannot be cloned, it does not provide a clone() implementation.",vptr->id);
return NULL;
}else vptr->clone(newobj,obj);
vptr=vptr->parent;
}
return newobj;
}
......@@ -130,3 +132,22 @@ void belle_sip_object_set_name(belle_sip_object_t* object,const char* name) {
const char* belle_sip_object_get_name(belle_sip_object_t* object) {
return object->name;
}
int belle_sip_object_marshal(belle_sip_object_t* obj, char* buff,unsigned int offset,size_t buff_size) {
belle_sip_object_vptr_t *vptr=obj->vptr;
while (vptr != NULL) {
if (vptr->marshal != NULL) {
return vptr->marshal(obj,buff,offset,buff_size);
} else {
vptr=vptr->parent;
}
}
return -1; /*no implementation found*/
}
char* belle_sip_object_to_string(belle_sip_object_t* obj) {
char buff[2048]; /*to be optimized*/
int size = belle_sip_object_marshal(obj,buff,0,sizeof(buff));
buff[size]='\0';
return strdup(buff);
}
......@@ -31,9 +31,23 @@ static void belle_sip_parameters_destroy(belle_sip_parameters_t* params) {
static void belle_sip_parameters_clone(belle_sip_parameters_t *params, const belle_sip_parameters_t *orig){
belle_sip_fatal("not implemented");
}
int belle_sip_parameters_marshal(belle_sip_parameters_t* params, char* buff,unsigned int offset,unsigned int buff_size) {
belle_sip_list_t* list=params->param_list;
unsigned int curent_offset=offset;
for(;list!=NULL;list=list->next){
belle_sip_param_pair_t* container = (belle_sip_param_pair_t* )(list->data);
if (container->value) {
curent_offset+=snprintf(buff+curent_offset,buff_size-curent_offset,";%s=%s",container->name,container->value);
} else {
curent_offset+=snprintf(buff+curent_offset,buff_size-curent_offset,";%s",container->name);
}
}
return curent_offset-offset;
}
BELLE_SIP_NEW(parameters,header)
const belle_sip_list_t * belle_sip_parameters_get_parameters(belle_sip_parameters_t* obj) {
return obj->param_list;
}
const char* belle_sip_parameters_get_parameter(belle_sip_parameters_t* params,const char* name) {
belle_sip_list_t * lResult = belle_sip_list_find_custom(params->param_list, (belle_sip_compare_func)belle_sip_param_pair_comp_func, name);
if (lResult) {
......
......@@ -56,10 +56,39 @@ void belle_sip_uri_destroy(belle_sip_uri_t* uri) {
belle_sip_object_unref(BELLE_SIP_OBJECT(uri->header_list));
}
int belle_sip_uri_marshal(belle_sip_uri_t* uri, char* buff,unsigned int offset,unsigned int buff_size) {
unsigned int current_offset=offset;
const belle_sip_list_t* list=belle_sip_parameters_get_parameters(uri->header_list);
current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%s:",uri->secure?"sips":"sip");
if (uri->user) {
current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%s@",uri->user);
}
if (uri->host) {
current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"%s",uri->host);
} else {
belle_sip_warning("no host found in this uri");
}
if (uri->port>0) {
current_offset+=snprintf(buff+current_offset,buff_size-current_offset,":%i",uri->port);
}
current_offset+=belle_sip_parameters_marshal(&uri->params,buff,current_offset,buff_size);
for(;list!=NULL;list=list->next){
belle_sip_param_pair_t* container = list->data;
if (list == belle_sip_parameters_get_parameters(uri->header_list)) {
//first case
current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"?%s=%s",container->name,container->value);
} else {
//subsequent headers
current_offset+=snprintf(buff+current_offset,buff_size-current_offset,"&%s=%s",container->name,container->value);
}
}
return current_offset-offset;
}
BELLE_SIP_PARSE(uri);
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_uri_t,belle_sip_parameters_t,belle_sip_uri_destroy,NULL);
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_uri_t,belle_sip_parameters_t,belle_sip_uri_destroy,NULL,belle_sip_uri_marshal);
belle_sip_uri_t* belle_sip_uri_new () {
......
......@@ -46,7 +46,7 @@ static void belle_sip_message_destroy(belle_sip_message_t *msg){
belle_sip_list_free(msg->header_list);
}
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_message_t,belle_sip_object_t,belle_sip_message_destroy,NULL);
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_message_t,belle_sip_object_t,belle_sip_message_destroy,NULL,NULL);
BELLE_SIP_PARSE(message)
......@@ -131,7 +131,9 @@ static void belle_sip_request_destroy(belle_sip_request_t* request) {
static void belle_sip_request_clone(belle_sip_request_t *request, const belle_sip_request_t *orig){
if (orig->method) request->method=belle_sip_strdup(orig->method);
}
int belle_sip_request_marshal(belle_sip_request_t* request, char* buff,unsigned int offset,unsigned int buff_size) {
}
BELLE_SIP_NEW(request,message)
BELLE_SIP_PARSE(request)
GET_SET_STRING(belle_sip_request,method);
......@@ -253,7 +255,9 @@ static void belle_sip_response_clone(belle_sip_response_t *resp, const belle_sip
if (orig->sip_version) resp->sip_version=belle_sip_strdup(orig->sip_version);
if (orig->reason_phrase) resp->reason_phrase=belle_sip_strdup(orig->reason_phrase);
}
int belle_sip_response_marshal(belle_sip_response_t *resp, char* buff,unsigned int offset,unsigned int buff_size) {
return 0;
}
BELLE_SIP_NEW(response,message);
BELLE_SIP_PARSE(response)
GET_SET_STRING(belle_sip_response,reason_phrase);
......
......@@ -33,7 +33,7 @@ static void belle_sip_provider_uninit(belle_sip_provider_t *p){
belle_sip_list_free(p->lps);
}
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_provider_t,belle_sip_object_t,belle_sip_provider_uninit,NULL);
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_provider_t,belle_sip_object_t,belle_sip_provider_uninit,NULL,NULL);
belle_sip_provider_t *belle_sip_provider_new(belle_sip_stack_t *s, belle_sip_listening_point_t *lp){
belle_sip_provider_t *p=belle_sip_object_new(belle_sip_provider_t);
......
......@@ -29,7 +29,7 @@ static void belle_sip_sender_task_uninit(belle_sip_sender_task_t *t){
if (t->resolver_id>0) belle_sip_main_loop_cancel_source (stack->ml,t->resolver_id);
}
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_sender_task_t,belle_sip_object_t,belle_sip_sender_task_uninit,NULL);
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_sender_task_t,belle_sip_object_t,belle_sip_sender_task_uninit,NULL,NULL);
belle_sip_sender_task_t * belle_sip_sender_task_new(belle_sip_provider_t *provider, belle_sip_sender_task_callback_t cb, void *data){
belle_sip_sender_task_t *t=belle_sip_object_new(belle_sip_sender_task_t);
......
......@@ -25,7 +25,7 @@ static void belle_sip_stack_destroy(belle_sip_stack_t *stack){
belle_sip_list_free(stack->lp);
}
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_stack_t,belle_sip_object_t,belle_sip_stack_destroy,NULL);
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_stack_t,belle_sip_object_t,belle_sip_stack_destroy,NULL,NULL);
belle_sip_stack_t * belle_sip_stack_new(const char *properties){
belle_sip_stack_t *stack=belle_sip_object_new(belle_sip_stack_t);
......
......@@ -48,7 +48,7 @@ static void transaction_destroy(belle_sip_transaction_t *t){
if (t->stask) belle_sip_object_unref(t->stask);
}
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_transaction_t,belle_sip_object_t,transaction_destroy,NULL);
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_transaction_t,belle_sip_object_t,transaction_destroy,NULL,NULL);
void *belle_sip_transaction_get_application_data(const belle_sip_transaction_t *t){
return t->appdata;
......@@ -110,7 +110,7 @@ void belle_sip_server_transaction_send_response(belle_sip_server_transaction_t *
static void server_transaction_destroy(belle_sip_server_transaction_t *t){
}
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_server_transaction_t,belle_sip_transaction_t,server_transaction_destroy,NULL);
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_server_transaction_t,belle_sip_transaction_t,server_transaction_destroy,NULL,NULL);
belle_sip_server_transaction_t * belle_sip_server_transaction_new(belle_sip_provider_t *prov,belle_sip_request_t *req){
belle_sip_server_transaction_t *t=belle_sip_object_new(belle_sip_server_transaction_t);
......@@ -313,7 +313,7 @@ void belle_sip_client_transaction_add_response(belle_sip_client_transaction_t *t
static void client_transaction_destroy(belle_sip_client_transaction_t *t ){
}
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_client_transaction_t, belle_sip_transaction_t,client_transaction_destroy,NULL);
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_client_transaction_t, belle_sip_transaction_t,client_transaction_destroy,NULL,NULL);
belle_sip_client_transaction_t * belle_sip_client_transaction_new(belle_sip_provider_t *prov, belle_sip_request_t *req){
belle_sip_client_transaction_t *t=belle_sip_object_new(belle_sip_client_transaction_t);
......
This diff is collapsed.
......@@ -31,6 +31,10 @@ static int clean_suite_uri(void) {
static void testSIMPLEURI(void) {
belle_sip_uri_t* L_uri = belle_sip_uri_parse("sip:titi.com");
char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
L_uri = belle_sip_uri_parse(l_raw_uri);
belle_sip_free(l_raw_uri);
CU_ASSERT_PTR_NULL(belle_sip_uri_get_user(L_uri));
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "titi.com");
CU_ASSERT_PTR_NULL(belle_sip_uri_get_transport_param(L_uri));
......@@ -39,6 +43,10 @@ static void testSIMPLEURI(void) {
static void testCOMPLEXURI(void) {
belle_sip_uri_t * L_uri = belle_sip_uri_parse("sip:toto@titi.com:5060;transport=tcp");
char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
L_uri = belle_sip_uri_parse(l_raw_uri);
belle_sip_free(l_raw_uri);
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_user(L_uri), "toto");
CU_ASSERT_EQUAL(belle_sip_uri_get_port(L_uri), 5060);
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "titi.com");
......@@ -47,6 +55,10 @@ static void testCOMPLEXURI(void) {
}
static void testSIPSURI(void) {
belle_sip_uri_t * L_uri = belle_sip_uri_parse("sips:linphone.org");
char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
L_uri = belle_sip_uri_parse(l_raw_uri);
belle_sip_free(l_raw_uri);
CU_ASSERT_EQUAL(belle_sip_uri_is_secure(L_uri), 1);
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
L_uri = belle_sip_uri_parse("sip:linphone.org");
......@@ -55,11 +67,19 @@ static void testSIPSURI(void) {
}
static void test_ip_host(void) {
belle_sip_uri_t * L_uri = belle_sip_uri_parse("sip:192.168.0.1");
char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
L_uri = belle_sip_uri_parse(l_raw_uri);
belle_sip_free(l_raw_uri);
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "192.168.0.1");
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
}
static void test_lr(void) {
belle_sip_uri_t * L_uri = belle_sip_uri_parse("sip:192.168.0.1;lr");
char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
L_uri = belle_sip_uri_parse(l_raw_uri);
belle_sip_free(l_raw_uri);
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "192.168.0.1");
CU_ASSERT_EQUAL(belle_sip_uri_has_lr_param(L_uri), 1);
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
......@@ -67,6 +87,10 @@ static void test_lr(void) {
}
static void test_maddr(void) {
belle_sip_uri_t * L_uri = belle_sip_uri_parse("sip:192.168.0.1;lr;maddr=linphone.org");
char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
L_uri = belle_sip_uri_parse(l_raw_uri);
belle_sip_free(l_raw_uri);
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_maddr_param(L_uri), "linphone.org");
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
......@@ -76,6 +100,10 @@ static void test_uri_parameters () {
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
L_uri = belle_sip_uri_parse("sip:maddr=@192.168.0.1;lr;maddr=192.168.0.1;user=ip;ttl=140;transport=sctp;method=INVITE;rport=5060");
char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
L_uri = belle_sip_uri_parse(l_raw_uri);
belle_sip_free(l_raw_uri);
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_maddr_param(L_uri), "192.168.0.1");
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_user_param(L_uri), "ip");
CU_ASSERT_EQUAL(belle_sip_uri_get_ttl_param(L_uri),140);
......@@ -86,12 +114,20 @@ static void test_uri_parameters () {
}
static void test_headers(void) {
belle_sip_uri_t * L_uri = belle_sip_uri_parse("sip:192.168.0.1?toto=titi");
char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
L_uri = belle_sip_uri_parse(l_raw_uri);
belle_sip_free(l_raw_uri);
CU_ASSERT_PTR_NOT_NULL_FATAL(belle_sip_uri_get_header(L_uri,"toto"));
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_header(L_uri,"toto"), "titi");
CU_ASSERT_PTR_NULL(belle_sip_uri_get_header(L_uri,"bla"));
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
L_uri = belle_sip_uri_parse("sip:192.168.0.1?toto=titi&header2=popo");
l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
L_uri = belle_sip_uri_parse(l_raw_uri);
belle_sip_free(l_raw_uri);
CU_ASSERT_PTR_NOT_NULL_FATAL(belle_sip_uri_get_header(L_uri,"toto"));
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_header(L_uri,"header2"), "popo");
......
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