Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
slib_server.cc
1 /*
2  * COPYRIGHT (c) 1988-1994 BY *
3  * PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS. *
4  * See the source file SLIB.C for more information. *
5 
6  * Reorganization of files (Mar 1999) by Alan W Black <awb@cstr.ed.ac.uk>
7 
8  * Client/server functions
9 
10 */
11 #include <cstdio>
12 #include "EST_io_aux.h"
13 #include "siod.h"
14 #include "siodp.h"
15 
16 int siod_server_socket = -1;
17 
18 LISP siod_send_lisp_to_client(LISP x)
19 {
20  // Send x to the client
21  if (siod_server_socket == -1)
22  {
23  err("siod: not in server mode",x);
24  }
25 
26  EST_String tmpfile = make_tmp_filename();
27  FILE *fd;
28  EST_String m = siod_sprint(x);
29 
30  if ((fd=fopen(tmpfile,"wb")) == NULL)
31  {
32  cerr << "siod: can't open temporary file \"" <<
33  tmpfile << "\" for client lisp return" << endl;
34  }
35  else
36  {
37  fwrite((const char *)m,sizeof(char),m.length(),fd);
38  fwrite("\n",1,1,fd);
39  fclose(fd);
40 #ifdef WIN32
41  send(siod_server_socket,"LP\n",3,0);
42 #else
43  write(siod_server_socket,"LP\n",3);
44 #endif
45  socket_send_file(siod_server_socket,tmpfile);
46  unlink(tmpfile);
47  }
48 
49  return x;
50 }
51 
52 void sock_acknowledge_error()
53 {
54  // Called to let client know if server gets an error
55  // Thanks to mcb for pointing out this omission
56 
57  if (siod_server_socket != -1)
58 #ifdef WIN32
59  send(siod_server_socket,"ER\n",3,0);
60 #else
61  write(siod_server_socket,"ER\n",3);
62 #endif
63 
64 }
65 
66 static void acknowledge_sock_print(LISP x)
67 { // simple return "OK" -- used in server socket mode
68 
69  siod_send_lisp_to_client(x);
70 #ifdef WIN32
71  send(siod_server_socket,"OK\n",3,0);
72 #else
73  write(siod_server_socket,"OK\n",3);
74 #endif
75 }
76 
77 static void ignore_puts(char *x)
78 {
79  (void)x;
80 }
81 
82 long repl_from_socket(int fd)
83 {
84  /* Read from given fd as stdin */
85  struct repl_hooks hd;
86 
87 #ifdef WIN32
88  if (!SetStdHandle(STD_INPUT_HANDLE,(HANDLE)fd))
89  {
90  GetLastError();
91  cerr << "repl_from_socket: couldn't set stdin to socket\n";
92  }
93 #else
94  dup2(fd,0); // make socket into stdin
95  // dup2(fd,1); // make socket into stdout
96 #endif
97  hd.repl_puts = ignore_puts;
98  hd.repl_print = acknowledge_sock_print;
99  hd.repl_eval = NULL;
100 #ifdef WIN32
101  hd.repl_read = lreadwinsock;
102 #else
103  hd.repl_read = NULL;
104 #endif
105  siod_interactive = FALSE;
106  siod_server_socket = fd;
107 
108  return repl_driver(1,0,&hd);
109 }
110 
111 void init_subrs_srv(void)
112 {
113  init_subr_1("send_client",siod_send_lisp_to_client,
114  "(send_client EXPR)\n\
115  Send EXPR to client. In server mode this will send a printed form of\n\
116  ESPR to the client. It is the client's job to expect it.");
117 }