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_connection.c 2170 2014-09-09 05:19:48Z achaloyan@gmail.com $
17 */
18
19 #include "mrcp_connection.h"
20 #include "apt_pool.h"
21
mrcp_connection_create(void)22 mrcp_connection_t* mrcp_connection_create(void)
23 {
24 mrcp_connection_t *connection;
25 apr_pool_t *pool = apt_pool_create();
26 if(!pool) {
27 return NULL;
28 }
29
30 connection = apr_palloc(pool,sizeof(mrcp_connection_t));
31 connection->pool = pool;
32 apt_string_reset(&connection->remote_ip);
33 connection->l_sockaddr = NULL;
34 connection->r_sockaddr = NULL;
35 connection->sock = NULL;
36 connection->id = NULL;
37 connection->verbose = TRUE;
38 connection->access_count = 0;
39 APR_RING_ELEM_INIT(connection,link);
40 connection->channel_table = apr_hash_make(pool);
41 connection->parser = NULL;
42 connection->generator = NULL;
43 connection->rx_buffer = NULL;
44 connection->rx_buffer_size = 0;
45 connection->tx_buffer = NULL;
46 connection->tx_buffer_size = 0;
47
48 return connection;
49 }
50
mrcp_connection_destroy(mrcp_connection_t * connection)51 void mrcp_connection_destroy(mrcp_connection_t *connection)
52 {
53 if(connection && connection->pool) {
54 apr_pool_destroy(connection->pool);
55 }
56 }
57
mrcp_connection_channel_add(mrcp_connection_t * connection,mrcp_control_channel_t * channel)58 apt_bool_t mrcp_connection_channel_add(mrcp_connection_t *connection, mrcp_control_channel_t *channel)
59 {
60 if(!connection || !channel) {
61 return FALSE;
62 }
63 apr_hash_set(connection->channel_table,channel->identifier.buf,channel->identifier.length,channel);
64 channel->connection = connection;
65 connection->access_count++;
66 return TRUE;
67 }
68
mrcp_connection_channel_find(const mrcp_connection_t * connection,const apt_str_t * identifier)69 mrcp_control_channel_t* mrcp_connection_channel_find(const mrcp_connection_t *connection, const apt_str_t *identifier)
70 {
71 if(!connection || !identifier) {
72 return NULL;
73 }
74 return apr_hash_get(connection->channel_table,identifier->buf,identifier->length);
75 }
76
mrcp_connection_channel_remove(mrcp_connection_t * connection,mrcp_control_channel_t * channel)77 apt_bool_t mrcp_connection_channel_remove(mrcp_connection_t *connection, mrcp_control_channel_t *channel)
78 {
79 if(!connection || !channel) {
80 return FALSE;
81 }
82 apr_hash_set(connection->channel_table,channel->identifier.buf,channel->identifier.length,NULL);
83 channel->connection = NULL;
84 connection->access_count--;
85 return TRUE;
86 }
87
mrcp_connection_disconnect_raise(mrcp_connection_t * connection,const mrcp_connection_event_vtable_t * vtable)88 apt_bool_t mrcp_connection_disconnect_raise(mrcp_connection_t *connection, const mrcp_connection_event_vtable_t *vtable)
89 {
90 if(vtable && vtable->on_disconnect) {
91 mrcp_control_channel_t *channel;
92 void *val;
93 apr_hash_index_t *it = apr_hash_first(connection->pool,connection->channel_table);
94 /* walk through the list of channels and raise disconnect event for them */
95 for(; it; it = apr_hash_next(it)) {
96 apr_hash_this(it,NULL,NULL,&val);
97 channel = val;
98 if(channel) {
99 vtable->on_disconnect(channel);
100 }
101 }
102 }
103 return TRUE;
104 }
105