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_engine_factory.c 2136 2014-07-04 06:33:36Z achaloyan@gmail.com $
17  */
18 
19 #include <apr_hash.h>
20 #include "mrcp_engine_factory.h"
21 #include "mrcp_synth_state_machine.h"
22 #include "mrcp_recog_state_machine.h"
23 #include "mrcp_recorder_state_machine.h"
24 #include "mrcp_verifier_state_machine.h"
25 #include "apt_log.h"
26 
27 /** Engine factory declaration */
28 struct mrcp_engine_factory_t {
29 	apr_hash_t *engines;
30 	apr_pool_t *pool;
31 };
32 
33 
mrcp_engine_factory_create(apr_pool_t * pool)34 MRCP_DECLARE(mrcp_engine_factory_t*) mrcp_engine_factory_create(apr_pool_t *pool)
35 {
36 	mrcp_engine_factory_t *factory = apr_palloc(pool,sizeof(mrcp_engine_factory_t));
37 	factory->pool = pool;
38 	factory->engines = apr_hash_make(pool);
39 	return factory;
40 }
41 
42 /** Destroy registered engines and the factory */
mrcp_engine_factory_destroy(mrcp_engine_factory_t * factory)43 MRCP_DECLARE(apt_bool_t) mrcp_engine_factory_destroy(mrcp_engine_factory_t *factory)
44 {
45 	mrcp_engine_t *engine;
46 	apr_hash_index_t *it;
47 	void *val;
48 	apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Destroy MRCP Engines");
49 	it=apr_hash_first(factory->pool,factory->engines);
50 	for(; it; it = apr_hash_next(it)) {
51 		apr_hash_this(it,NULL,NULL,&val);
52 		engine = val;
53 		if(engine) {
54 			mrcp_engine_virtual_destroy(engine);
55 		}
56 	}
57 	apr_hash_clear(factory->engines);
58 	return TRUE;
59 }
60 
61 /** Open registered engines */
mrcp_engine_factory_open(mrcp_engine_factory_t * factory)62 MRCP_DECLARE(apt_bool_t) mrcp_engine_factory_open(mrcp_engine_factory_t *factory)
63 {
64 	mrcp_engine_t *engine;
65 	apr_hash_index_t *it;
66 	void *val;
67 	it = apr_hash_first(factory->pool,factory->engines);
68 	for(; it; it = apr_hash_next(it)) {
69 		apr_hash_this(it,NULL,NULL,&val);
70 		engine = val;
71 		if(engine) {
72 			mrcp_engine_virtual_open(engine);
73 		}
74 	}
75 	return TRUE;
76 }
77 
78 /** Close registered engines */
mrcp_engine_factory_close(mrcp_engine_factory_t * factory)79 MRCP_DECLARE(apt_bool_t) mrcp_engine_factory_close(mrcp_engine_factory_t *factory)
80 {
81 	mrcp_engine_t *engine;
82 	apr_hash_index_t *it;
83 	void *val;
84 	it=apr_hash_first(factory->pool,factory->engines);
85 	for(; it; it = apr_hash_next(it)) {
86 		apr_hash_this(it,NULL,NULL,&val);
87 		engine = val;
88 		if(engine) {
89 			mrcp_engine_virtual_close(engine);
90 		}
91 	}
92 	return TRUE;
93 }
94 
95 /** Register new engine */
mrcp_engine_factory_engine_register(mrcp_engine_factory_t * factory,mrcp_engine_t * engine)96 MRCP_DECLARE(apt_bool_t) mrcp_engine_factory_engine_register(mrcp_engine_factory_t *factory, mrcp_engine_t *engine)
97 {
98 	if(!engine || !engine->id) {
99 		return FALSE;
100 	}
101 
102 	switch(engine->resource_id) {
103 		case MRCP_SYNTHESIZER_RESOURCE:
104 			engine->create_state_machine = mrcp_synth_state_machine_create;
105 			break;
106 		case MRCP_RECOGNIZER_RESOURCE:
107 			engine->create_state_machine = mrcp_recog_state_machine_create;
108 			break;
109 		case MRCP_RECORDER_RESOURCE:
110 			engine->create_state_machine = mrcp_recorder_state_machine_create;
111 			break;
112 		case MRCP_VERIFIER_RESOURCE:
113 			engine->create_state_machine = mrcp_verifier_state_machine_create;
114 			break;
115 		default:
116 			break;
117 	}
118 
119 	if(!engine->create_state_machine) {
120 		return FALSE;
121 	}
122 
123 	apr_hash_set(factory->engines,engine->id,APR_HASH_KEY_STRING,engine);
124 	return TRUE;
125 }
126 
127 /** Get engine by name */
mrcp_engine_factory_engine_get(const mrcp_engine_factory_t * factory,const char * name)128 MRCP_DECLARE(mrcp_engine_t*) mrcp_engine_factory_engine_get(const mrcp_engine_factory_t *factory, const char *name)
129 {
130 	if(!name) {
131 		return NULL;
132 	}
133 	return apr_hash_get(factory->engines,name,APR_HASH_KEY_STRING);
134 }
135 
136 /** Find engine by resource identifier */
mrcp_engine_factory_engine_find(const mrcp_engine_factory_t * factory,mrcp_resource_id resource_id)137 MRCP_DECLARE(mrcp_engine_t*) mrcp_engine_factory_engine_find(const mrcp_engine_factory_t *factory, mrcp_resource_id resource_id)
138 {
139 	mrcp_engine_t *engine;
140 	void *val;
141 	apr_hash_index_t *it = apr_hash_first(factory->pool,factory->engines);
142 	/* walk through the engines */
143 	for(; it; it = apr_hash_next(it)) {
144 		apr_hash_this(it,NULL,NULL,&val);
145 		engine = val;
146 		if(engine && engine->resource_id == resource_id) {
147 			return engine;
148 		}
149 	}
150 	return NULL;
151 }
152 
153 /** Start iterating over the engines in a factory */
mrcp_engine_factory_engine_first(const mrcp_engine_factory_t * factory)154 MRCP_DECLARE(apr_hash_index_t*) mrcp_engine_factory_engine_first(const mrcp_engine_factory_t *factory)
155 {
156 	return apr_hash_first(factory->pool,factory->engines);
157 }
158