transaction.c 5.13 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 23 24 25 26 27 28
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);
	t->request=req;
	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 36 37 38 39 40

BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_transaction_t)={
		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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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){
59 60
	t->state=BELLE_SIP_TRANSACTION_TERMINATED;
	belle_sip_provider_set_transaction_terminated(t->provider,t);
61 62 63 64 65 66
}

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

67
/*
Simon Morlat's avatar
Simon Morlat committed
68 69
 * Server transaction
 */
70

71 72 73
static void server_transaction_destroy(belle_sip_server_transaction_t *t){
}

Simon Morlat's avatar
Simon Morlat committed
74

75
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_server_transaction_t);
Simon Morlat's avatar
Simon Morlat committed
76 77 78 79 80 81 82 83
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_server_transaction_t)={
		{
			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
84

85
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
86
	belle_sip_server_transaction_t *t=belle_sip_object_new(belle_sip_server_transaction_t);
87 88 89 90
	belle_sip_transaction_init((belle_sip_transaction_t*)t,prov,req);
	return t;
}

Simon Morlat's avatar
Simon Morlat committed
91 92 93
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);
}
94 95


96 97 98
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
99

Simon Morlat's avatar
Simon Morlat committed
100 101 102
/*
 * client transaction
 */
103 104

void belle_sip_client_transaction_send_request(belle_sip_client_transaction_t *t){
Simon Morlat's avatar
Simon Morlat committed
105 106
	belle_sip_hop_t hop={0};
	belle_sip_channel_t *chan;
Simon Morlat's avatar
Simon Morlat committed
107
	belle_sip_provider_t *prov=t->base.provider;
Simon Morlat's avatar
Simon Morlat committed
108
	
Simon Morlat's avatar
Simon Morlat committed
109 110 111
	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
112 113
	}
	
Simon Morlat's avatar
Simon Morlat committed
114 115 116 117 118 119 120
	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
121 122 123 124
		}
	}
}

Simon Morlat's avatar
Simon Morlat committed
125
static void client_transaction_destroy(belle_sip_client_transaction_t *t ){
Simon Morlat's avatar
Simon Morlat committed
126 127
}

Simon Morlat's avatar
Simon Morlat committed
128 129 130 131 132 133 134 135 136 137
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
138 139
}

Simon Morlat's avatar
Simon Morlat committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
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)={
	{
		BELLE_SIP_VPTR_INIT(belle_sip_client_transaction_t,belle_sip_transaction_t,FALSE),
		(belle_sip_object_destroy_t)client_transaction_destroy,
		NULL,
		NULL
	},
	NULL,
	NULL
};
Simon Morlat's avatar
Simon Morlat committed
157

Simon Morlat's avatar
Simon Morlat committed
158 159
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);
160 161 162
}