1 /*
2  * Copyright 2008-2014 Arsen Chaloyan
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Id: mrcp_sig_agent.c 2136 2014-07-04 06:33:36Z achaloyan@gmail.com $
17  */
18 
19 #include "mrcp_sig_agent.h"
20 #include "mrcp_session.h"
21 #include "apt_pool.h"
22 
23 /** Factory of MRCP signaling agents */
24 struct mrcp_sa_factory_t {
25 	/** Array of pointers to signaling agents */
26 	apr_array_header_t   *agents_arr;
27 	/** Index of the current agent */
28 	int                   index;
29 };
30 
31 /** Create signaling agent */
mrcp_signaling_agent_create(const char * id,void * obj,apr_pool_t * pool)32 MRCP_DECLARE(mrcp_sig_agent_t*) mrcp_signaling_agent_create(const char *id, void *obj, apr_pool_t *pool)
33 {
34 	mrcp_sig_agent_t *sig_agent = apr_palloc(pool,sizeof(mrcp_sig_agent_t));
35 	sig_agent->id = id;
36 	sig_agent->pool = pool;
37 	sig_agent->obj = obj;
38 	sig_agent->resource_factory = NULL;
39 	sig_agent->parent = NULL;
40 	sig_agent->task = NULL;
41 	sig_agent->msg_pool = NULL;
42 	sig_agent->create_server_session = NULL;
43 	sig_agent->create_client_session = NULL;
44 	return sig_agent;
45 }
46 
47 /** Create factory of signaling agents */
mrcp_sa_factory_create(apr_pool_t * pool)48 MRCP_DECLARE(mrcp_sa_factory_t*) mrcp_sa_factory_create(apr_pool_t *pool)
49 {
50 	mrcp_sa_factory_t *sa_factory = apr_palloc(pool,sizeof(mrcp_sa_factory_t));
51 	sa_factory->agents_arr = apr_array_make(pool,1,sizeof(mrcp_sig_agent_t*));
52 	sa_factory->index = 0;
53 	return sa_factory;
54 }
55 
56 /** Add signaling agent to pool */
mrcp_sa_factory_agent_add(mrcp_sa_factory_t * sa_factory,mrcp_sig_agent_t * sig_agent)57 MRCP_DECLARE(apt_bool_t) mrcp_sa_factory_agent_add(mrcp_sa_factory_t *sa_factory, mrcp_sig_agent_t *sig_agent)
58 {
59 	mrcp_sig_agent_t **slot;
60 	if(!sig_agent)
61 		return FALSE;
62 
63 	slot = apr_array_push(sa_factory->agents_arr);
64 	*slot = sig_agent;
65 	return TRUE;
66 }
67 
68 /** Determine whether factory is empty. */
mrcp_sa_factory_is_empty(const mrcp_sa_factory_t * sa_factory)69 MRCP_DECLARE(apt_bool_t) mrcp_sa_factory_is_empty(const mrcp_sa_factory_t *sa_factory)
70 {
71 	return apr_is_empty_array(sa_factory->agents_arr);
72 }
73 
74 /** Select next available signaling agent */
mrcp_sa_factory_agent_select(mrcp_sa_factory_t * sa_factory)75 MRCP_DECLARE(mrcp_sig_agent_t*) mrcp_sa_factory_agent_select(mrcp_sa_factory_t *sa_factory)
76 {
77 	mrcp_sig_agent_t *sig_agent = APR_ARRAY_IDX(sa_factory->agents_arr, sa_factory->index, mrcp_sig_agent_t*);
78 	if(++sa_factory->index == sa_factory->agents_arr->nelts) {
79 		sa_factory->index = 0;
80 	}
81 	return sig_agent;
82 }
83 
84 /** Allocate MRCP signaling settings */
mrcp_signaling_settings_alloc(apr_pool_t * pool)85 MRCP_DECLARE(mrcp_sig_settings_t*) mrcp_signaling_settings_alloc(apr_pool_t *pool)
86 {
87 	mrcp_sig_settings_t *settings = apr_palloc(pool,sizeof(mrcp_sig_settings_t));
88 	settings->server_ip = NULL;
89 	settings->server_port = 0;
90 	settings->user_name = NULL;
91 	settings->resource_location = NULL;
92 	settings->resource_map = apr_table_make(pool,2);
93 	settings->force_destination = FALSE;
94 	settings->feature_tags = NULL;
95 	return settings;
96 }
97 
98 
mrcp_session_create(apr_size_t padding)99 MRCP_DECLARE(mrcp_session_t*) mrcp_session_create(apr_size_t padding)
100 {
101 	mrcp_session_t *session;
102 	apr_pool_t *pool = apt_pool_create();
103 	if(!pool) {
104 		return NULL;
105 	}
106 	session = apr_palloc(pool,sizeof(mrcp_session_t)+padding);
107 	session->pool = pool;
108 	session->obj = NULL;
109 	session->log_obj = NULL;
110 	session->name = NULL;
111 	session->signaling_agent = NULL;
112 	session->connection_agent = NULL;
113 	session->media_engine = NULL;
114 	session->rtp_factory = NULL;
115 	session->request_vtable = NULL;
116 	session->response_vtable = NULL;
117 	session->event_vtable = NULL;
118 	apt_string_reset(&session->id);
119 	session->last_request_id = 0;
120 	return session;
121 }
122 
mrcp_session_destroy(mrcp_session_t * session)123 MRCP_DECLARE(void) mrcp_session_destroy(mrcp_session_t *session)
124 {
125 	if(session->pool) {
126 		apr_pool_destroy(session->pool);
127 	}
128 }
129