Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
siod_server.cc
1  /************************************************************************/
2  /* */
3  /* Centre for Speech Technology Research */
4  /* University of Edinburgh, UK */
5  /* Copyright (c) 1996,1997 */
6  /* All Rights Reserved. */
7  /* */
8  /* Permission is hereby granted, free of charge, to use and distribute */
9  /* this software and its documentation without restriction, including */
10  /* without limitation the rights to use, copy, modify, merge, publish, */
11  /* distribute, sublicense, and/or sell copies of this work, and to */
12  /* permit persons to whom this work is furnished to do so, subject to */
13  /* the following conditions: */
14  /* 1. The code must retain the above copyright notice, this list of */
15  /* conditions and the following disclaimer. */
16  /* 2. Any modifications must be clearly marked as such. */
17  /* 3. Original authors' names are not deleted. */
18  /* 4. The authors' names are not used to endorse or promote products */
19  /* derived from this software without specific prior written */
20  /* permission. */
21  /* */
22  /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23  /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24  /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25  /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26  /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27  /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28  /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29  /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30  /* THIS SOFTWARE. */
31  /* */
32  /*************************************************************************/
33  /* */
34  /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */
35  /* -------------------------------------------------------------------- */
36  /* Functions to talk to siod. */
37  /* */
38  /*************************************************************************/
39 
40 #include "siod.h"
41 #include "EST_SiodServer.h"
42 
43 static bool have_read_table=FALSE;
44 
45 static int tc_siod_server= -1;
46 
47 // SIOD support for siod server objects
48 
49 EST_SiodServer *get_c_siod_server(LISP x)
50 {
51  if (TYPEP(x,tc_siod_server))
52  return (EST_SiodServer *)USERVAL(x);
53  else
54  err("wrong type of argument to get_c_siod_server",x);
55 
56  return NULL; // err doesn't return but compilers don't know that
57 }
58 
59 int siod_siod_server_p(LISP x)
60 {
61  if (TYPEP(x,tc_siod_server))
62  return TRUE;
63  else
64  return FALSE;
65 }
66 
67 LISP siod_make_siod_server(EST_SiodServer *s)
68 {
69  if (s==0)
70  return NIL;
71  else
72  return siod_make_typed_cell(tc_siod_server,s);
73 }
74 
75 static void siod_server_free(LISP lserver)
76 {
77  EST_SiodServer *server = get_c_siod_server(lserver);
78  delete server;
79  USERVAL(lserver) = NULL;
80 }
81 
82 // Deal with the server table.
83 
84 LISP siod_read_server_table(LISP args)
85 {
86  if (NULLP(args))
88  else
89  EST_ServiceTable::read_table(get_c_string(CAR(args)));
90 
91  have_read_table=TRUE;
92 
93  return NIL;
94 }
95 
96 LISP siod_write_server_table(LISP args)
97 {
98  if (NULLP(args))
100  else
101  EST_ServiceTable::write_table(get_c_string(CAR(args)));
102 
103  return NIL;
104 }
105 
106 LISP siod_servers(void)
107 {
108  EST_StrList names;
109  EST_ServiceTable::names(names, "siod");
110 
111  if (!have_read_table)
112  siod_read_server_table(NIL);
113 
114  LISP lnames = NIL;
115 
117 
118  for(p.begin(names); p; ++p)
119  lnames = cons(strcons((*p).length(), *p), lnames);
120 
121  return lnames;
122 }
123 
124 // Creating and connecting to servers.
125 
126 static void siod_connect(EST_SiodServer *server)
127 {
128  switch (server->connect())
129  {
130  case connect_ok:
131  break;
132 
133  case connect_not_found_error:
134  EST_sys_error("Can't find host '%s:%d'", (const char *)server->servername(), server->port());
135  break;
136 
137  case connect_not_allowed_error:
138  EST_sys_error("Can't connect to '%s:%d'", (const char *)server->servername(), server->port());
139  break;
140 
141  default:
142  EST_sys_error("Error connecting to '%s:%d'", (const char *)server->servername(), server->port());
143  break;
144  }
145 }
146 
147 LISP siod_server(LISP lname)
148 {
149  if (CONSP(lname))
150  lname = CAR(lname);
151 
152  EST_String name=NULLP(lname)?"siod":get_c_string(lname);
153 
154  if (!have_read_table)
155  siod_read_server_table(NIL);
156 
157  LISP verbose = siod_get_lval("siod_verbose", NULL);
158 
159  EST_SiodServer *server =
160  new EST_SiodServer(name, !NULLP(verbose)?&cout:(ostream *)NULL);
161 
162  siod_connect(server);
163 
164  return siod_make_siod_server(server);
165 }
166 
167 LISP siod_service_loop(LISP lname)
168 {
169  if (CONSP(lname))
170  lname = CAR(lname);
171 
172  EST_String name=NULLP(lname)?"siod":get_c_string(lname);
173 
174  if (!have_read_table)
175  siod_read_server_table(NIL);
176 
177  LISP verbose = siod_get_lval("siod_verbose", NULL);
178 
180  name,
181  !NULLP(verbose)?&cout:(ostream *)NULL);
182 
183  return siod_make_siod_server(server);
184 }
185 
186 static EST_SiodServer *get_server(LISP lserver, bool &my_server)
187 {
188  if (siod_siod_server_p(lserver))
189  my_server=false;
190  else
191  {
192  my_server=true;
193  lserver = siod_server(lserver);
194  }
195 
196  return get_c_siod_server(lserver);
197 }
198 
199 LISP siod_connect(LISP lserver)
200 {
201  if (!siod_siod_server_p(lserver))
202  EST_error("not a siod server %s", get_c_string(lserver));
203 
204  EST_SiodServer *server = get_c_siod_server(lserver);
205 
206  if (server->connected())
207  siod_connect(server);
208 
209  return NIL;
210 }
211 
212 LISP siod_disconnect(LISP lserver)
213 {
214  if (!siod_siod_server_p(lserver))
215  EST_error("not a siod server %s", get_c_string(lserver));
216 
217  EST_SiodServer *server = get_c_siod_server(lserver);
218 
219  if (server->connected())
220  server->disconnect();
221  return NIL;
222 }
223 
224 LISP siod_server_run(LISP lserver)
225 {
226  if (!siod_siod_server_p(lserver))
227  EST_error("not a siod server %s", get_c_string(lserver));
228 
229  EST_SiodServer *server = get_c_siod_server(lserver);
230 
232 
233  siod_write_server_table(NIL);
234 
235  // Never returns
236  server->run(handler);
237 
238  return NIL;
239 }
240 
241 LISP siod_remote_command(LISP lserver,
242  LISP sexp)
243 {
244  bool my_server;
245 
246  EST_SiodServer *server = get_server(lserver, my_server);
247 
249 
250  args.set_val("sexp", est_val(sexp));
251 
253  EST_SiodServer::Result &res = handler.res;
254 
255  if (!server->connected())
256  siod_connect(server);
257 
258  if (!server->execute("scheme", "eval", args, handler))
259  {
260  EST_String err = res.S("ERROR");
261  if (my_server)
262  delete server;
263  return strcons(err.length(), err);
264  }
265 
266  if (my_server)
267  delete server;
268 
269  return scheme(handler.res.val("sexp"));
270 }
271 
272 void siod_server_init(void)
273 {
274  long kind;
275 
276  tc_siod_server = siod_register_user_type("SiodServer");
277 
278  set_gc_hooks(tc_siod_server, 0, NULL,NULL,NULL,siod_server_free,NULL,&kind);
279 
280 
281  init_lsubr("siod_read_server_table", siod_read_server_table,
282  "(siod_read_server_table &opt FILENAME)\n"
283  " Read the users table of siod servers, or the table\n"
284  " in FILENAME if given.");
285 
286  init_lsubr("siod_write_server_table", siod_write_server_table,
287  "(siod_write_server_table &opt FILENAME)\n"
288  " Write the users table of siod servers, or the table\n"
289  " in FILENAME if given.");
290 
291  init_subr_0("siod_servers", siod_servers,
292  "(siod_servers)\n"
293  " Returns a list of the know siod servers. This doesn't\n"
294  " guarantee that they are still running.");
295 
296  init_lsubr("siod_server", siod_server,
297  "(siod_server &opt NAME)\n"
298  " Return a connection to a siod server with the given name.\n"
299  " If name is omitted it defaults to \"siod\".");
300 
301  init_lsubr("siod_service_loop", siod_service_loop,
302  "(siod_server_loop &opt NAME)\n"
303  " Return an object representing a network siod server main loop.\n"
304  " This service can be started by calling \\[siod_server_run\\].\n"
305  " If name is omitted it defaults to \"siod\".");
306 
307  init_subr_1("siod_connect", siod_connect,
308  "(siod_connect SERVER)\n"
309  " Re-open the connection to the server.");
310 
311  init_subr_1("siod_disconnect", siod_disconnect,
312  "(siod_disconnect SERVER)\n"
313  " Close the connection to the server.");
314 
315  init_subr_1("siod_server_run", siod_server_run,
316  "(siod_server_run SERVER)\n"
317  " Start the main loop of a siod network server.");
318 
319  init_subr_2("siod_remote_command", siod_remote_command,
320  "(siod_remote_command SERVER COMMAND)\n"
321  " Evaluate COMMAND on the siod server SERVER.");
322 
323 }