transaction.c 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "belle_sip_internal.h"

21 22
static void belle_sip_transaction_init(belle_sip_transaction_t *t, belle_sip_provider_t *prov, belle_sip_request_t *req){
	if (req) belle_sip_object_ref(req);
Simon Morlat's avatar
Simon Morlat committed
23
	t->request=(belle_sip_request_t*)belle_sip_object_ref(req);
24 25 26 27 28
	t->provider=prov;
}

static void transaction_destroy(belle_sip_transaction_t *t){
	if (t->request) belle_sip_object_unref(t->request);
Simon Morlat's avatar
Simon Morlat committed
29 30
	if (t->prov_response) belle_sip_object_unref(t->prov_response);
	if (t->final_response) belle_sip_object_unref(t->final_response);
31
}
32

33
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_transaction_t);
Simon Morlat's avatar
Simon Morlat committed
34 35

BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_transaction_t)={
Simon Morlat's avatar
Simon Morlat committed
36
	{
Simon Morlat's avatar
Simon Morlat committed
37 38 39 40
		BELLE_SIP_VPTR_INIT(belle_sip_transaction_t,belle_sip_object_t,FALSE),
		(belle_sip_object_destroy_t) transaction_destroy,
		NULL,/*no clone*/
		NULL,/*no marshall*/
Simon Morlat's avatar
Simon Morlat committed
41 42
	},
	NULL /*on_terminate*/
Simon Morlat's avatar
Simon Morlat committed
43
};
Simon Morlat's avatar
Simon Morlat committed
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
void *belle_sip_transaction_get_application_data(const belle_sip_transaction_t *t){
	return t->appdata;
}

void belle_sip_transaction_set_application_data(belle_sip_transaction_t *t, void *data){
	t->appdata=data;
}

const char *belle_sip_transaction_get_branch_id(const belle_sip_transaction_t *t){
	return t->branch_id;
}

belle_sip_transaction_state_t belle_sip_transaction_get_state(const belle_sip_transaction_t *t){
	return t->state;
}

void belle_sip_transaction_terminate(belle_sip_transaction_t *t){
Simon Morlat's avatar
Simon Morlat committed
62 63
	belle_sip_transaction_terminated_event_t ev;
	
64 65
	t->state=BELLE_SIP_TRANSACTION_TERMINATED;
	belle_sip_provider_set_transaction_terminated(t->provider,t);
Simon Morlat's avatar
Simon Morlat committed
66 67 68 69 70 71
	BELLE_SIP_OBJECT_VPTR(t,belle_sip_transaction_t)->on_terminate(t);
	
	ev.source=t->provider;
	ev.transaction=t;
	ev.is_server_transaction=BELLE_SIP_IS_INSTANCE_OF(t,belle_sip_server_transaction_t);
	BELLE_SIP_PROVIDER_INVOKE_LISTENERS(t->provider,process_transaction_terminated,&ev);
72 73 74 75 76 77
}

belle_sip_request_t *belle_sip_transaction_get_request(belle_sip_transaction_t *t){
	return t->request;
}

Simon Morlat's avatar
Simon Morlat committed
78 79 80 81 82 83 84 85
void belle_sip_transaction_notify_timeout(belle_sip_transaction_t *t){
	belle_sip_timeout_event_t ev;
	ev.source=t->provider;
	ev.transaction=t;
	ev.is_server_transaction=BELLE_SIP_OBJECT_IS_INSTANCE_OF(t,belle_sip_server_transaction_t);
	BELLE_SIP_PROVIDER_INVOKE_LISTENERS(t->provider,process_timeout,&ev);
}

86
/*
Simon Morlat's avatar
Simon Morlat committed
87 88
 * Server transaction
 */
89

90 91 92
static void server_transaction_destroy(belle_sip_server_transaction_t *t){
}

Simon Morlat's avatar
Simon Morlat committed
93

94
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_server_transaction_t);
Simon Morlat's avatar
Simon Morlat committed
95
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_server_transaction_t)={
Simon Morlat's avatar
Simon Morlat committed
96
	{
Simon Morlat's avatar
Simon Morlat committed
97 98 99 100 101
		{
			BELLE_SIP_VPTR_INIT(belle_sip_server_transaction_t,belle_sip_transaction_t,FALSE),
			(belle_sip_object_destroy_t) server_transaction_destroy,
			NULL,
			NULL
Simon Morlat's avatar
Simon Morlat committed
102 103 104
		},
		NULL
	}
Simon Morlat's avatar
Simon Morlat committed
105
};
Simon Morlat's avatar
Simon Morlat committed
106

107
belle_sip_server_transaction_t * belle_sip_server_transaction_new(belle_sip_provider_t *prov,belle_sip_request_t *req){
Simon Morlat's avatar
Simon Morlat committed
108
	belle_sip_server_transaction_t *t=belle_sip_object_new(belle_sip_server_transaction_t);
109 110 111 112
	belle_sip_transaction_init((belle_sip_transaction_t*)t,prov,req);
	return t;
}

Simon Morlat's avatar
Simon Morlat committed
113 114 115
void belle_sip_server_transaction_send_response(belle_sip_server_transaction_t *t, belle_sip_response_t *resp){
	//BELLE_SIP_OBJECT_VPTR(t,belle_sip_transaction_t)->on_response((belle_sip_transaction_t*)t,resp);
}
116 117


118 119 120
belle_sip_request_t * belle_sip_client_transaction_create_cancel(belle_sip_client_transaction_t *t){
	return NULL;
}
Simon Morlat's avatar
Simon Morlat committed
121

Simon Morlat's avatar
Simon Morlat committed
122 123 124
/*
 * client transaction
 */
125 126

void belle_sip_client_transaction_send_request(belle_sip_client_transaction_t *t){
Simon Morlat's avatar
Simon Morlat committed
127 128
	belle_sip_hop_t hop={0};
	belle_sip_channel_t *chan;
Simon Morlat's avatar
Simon Morlat committed
129
	belle_sip_provider_t *prov=t->base.provider;
Simon Morlat's avatar
Simon Morlat committed
130
	
Simon Morlat's avatar
Simon Morlat committed
131 132 133
	if (t->base.state!=BELLE_SIP_TRANSACTION_INIT){
		belle_sip_error("belle_sip_client_transaction_send_request: bad state.");
		return;
Simon Morlat's avatar
Simon Morlat committed
134 135
	}
	
Simon Morlat's avatar
Simon Morlat committed
136 137 138 139 140 141 142
	belle_sip_stack_get_next_hop(prov->stack,t->base.request,&hop);
	chan=belle_sip_provider_get_channel(prov,hop.host, hop.port, hop.transport);
	if (chan){
		belle_sip_channel_add_listener(chan,BELLE_SIP_CHANNEL_LISTENER(t));
		t->base.channel=chan;
		if (belle_sip_channel_get_state(chan)==BELLE_SIP_CHANNEL_READY){
			BELLE_SIP_OBJECT_VPTR(t,belle_sip_client_transaction_t)->send_request(t);
Simon Morlat's avatar
Simon Morlat committed
143 144 145 146
		}
	}
}

Simon Morlat's avatar
Simon Morlat committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
void belle_sip_client_transaction_add_response(belle_sip_client_transaction_t *t, belle_sip_response_t *resp){
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)t;
	int pass=BELLE_SIP_OBJECT_VPTR(t,belle_sip_client_transaction_t)->on_response(t,resp);
	if (pass){
		belle_sip_response_event_t ev;

		if (base->prov_response)
			belle_sip_object_unref(base->prov_response);
		base->prov_response=(belle_sip_response_t*)belle_sip_object_ref(resp);
		
		ev.source=base->provider;
		ev.response=resp;
		ev.client_transaction=t;
		ev.dialog=NULL;
		BELLE_SIP_PROVIDER_INVOKE_LISTENERS(base->provider,process_response_event,&ev);
	}
}

Simon Morlat's avatar
Simon Morlat committed
165
static void client_transaction_destroy(belle_sip_client_transaction_t *t ){
Simon Morlat's avatar
Simon Morlat committed
166 167
}

Simon Morlat's avatar
Simon Morlat committed
168 169 170 171 172 173 174 175 176 177
static void on_channel_state_changed(belle_sip_channel_listener_t *l, belle_sip_channel_t *chan, belle_sip_channel_state_t state){
	belle_sip_client_transaction_t *t=(belle_sip_client_transaction_t*)l;
	switch(state){
		case BELLE_SIP_CHANNEL_READY:
			BELLE_SIP_OBJECT_VPTR(t,belle_sip_client_transaction_t)->send_request(t);
		break;
		default:
			/*ignored*/
		break;
	}
Simon Morlat's avatar
Simon Morlat committed
178 179
}

Simon Morlat's avatar
Simon Morlat committed
180 181 182 183 184 185 186 187 188
BELLE_SIP_IMPLEMENT_INTERFACE_BEGIN(belle_sip_client_transaction_t,belle_sip_channel_listener_t)
on_channel_state_changed,
NULL,
NULL
BELLE_SIP_IMPLEMENT_INTERFACE_END

BELLE_SIP_DECLARE_IMPLEMENTED_INTERFACES_1(belle_sip_client_transaction_t, belle_sip_channel_listener_t);
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_client_transaction_t)={
	{
Simon Morlat's avatar
Simon Morlat committed
189 190 191 192 193 194
		{
			BELLE_SIP_VPTR_INIT(belle_sip_client_transaction_t,belle_sip_transaction_t,FALSE),
			(belle_sip_object_destroy_t)client_transaction_destroy,
			NULL,
			NULL
		},
Simon Morlat's avatar
Simon Morlat committed
195 196 197 198 199
		NULL
	},
	NULL,
	NULL
};
Simon Morlat's avatar
Simon Morlat committed
200

Simon Morlat's avatar
Simon Morlat committed
201 202
void belle_sip_client_transaction_init(belle_sip_client_transaction_t *obj, belle_sip_provider_t *prov, belle_sip_request_t *req){
	belle_sip_transaction_init((belle_sip_transaction_t*)obj, prov,req);
203 204
}

205 206 207
belle_sip_ist_t *belle_sip_ist_new(belle_sip_provider_t *prov, belle_sip_request_t *req){
	return NULL;
}
208

209 210 211 212
belle_sip_nist_t *belle_sip_nist_new(belle_sip_provider_t *prov, belle_sip_request_t *req){
	return NULL;
}