Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
EST_Server.h
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 
35 #ifndef __EST_SERVER_H__
36 #define __EST_SERVER_H__
37 
38 #include "EST_Features.h"
39 #include "EST_ServiceTable.h"
40 
41 /** Client-server interface.
42  *
43  * An EST_Server object represents a server, it can be at either
44  * end of a network connection. That is, a server process has an
45  * EST_Server object representing it's wait-dispatch-answer loop,
46  * while a client process has an EST_Server object which represents
47  * the server process.
48  *
49  * @author Richard Caley <rjc@cstr.ed.ac.uk>
50  * @version $Id: EST_Server.h,v 1.4 2004/05/04 00:00:16 awb Exp $
51  */
52 
53 class EST_Server {
54 
55 public:
56 
57  /// What type of server is this.
58  enum Mode {
59  /// Bizarre state
61  /// Client end of the connection.
62  sm_client = 1,
63  /// Answer one client at a time.
65  /// Answer requests from several clients, as requests arrive.
67  /// For off a process for each client.
68  sm_fork = 4,
69  /// Multi-threaded (not implemented)
71  };
72 
73  typedef EST_Features Args;
74  typedef EST_Features Result;
75 
77  {
78  public:
79  EST_Server *server;
80  EST_String requestString;
81  EST_String package;
82  EST_String operation;
83  Args args;
84  Result res;
85 
87  virtual ~RequestHandler();
88  virtual EST_String process(void)=0;
89  };
90 
92  {
93  public:
94  EST_Server *server;
95  EST_String resString;
96  Result res;
97 
98  ResultHandler();
99  virtual ~ResultHandler();
100  virtual void process(void)=0;
101  };
102 
104  {
105  public:
106  int s;
107  int bpos;
108  int blen;
109  char *buffer;
110 
111  BufferedSocket(int socket);
112  ~BufferedSocket();
113  void ensure(int n);
114  int read_data(void);
115  };
116 
117 private:
118  /// Then server we are connected to.
119  EST_ServiceTable::Entry p_entry;
120  void *p_serv_addr;
121  int p_socket;
122  BufferedSocket *p_buffered_socket;
123  ostream *p_trace;
124  Mode p_mode;
125 
126  void zero(void);
127  void init(ostream *trace);
128 
129  void initClient(const EST_ServiceTable::Entry &e, ostream *trace);
130  void initClient(EST_String name, EST_String type, ostream *trace);
131  void initClient(EST_String hostname, int port, ostream *trace);
132 
133  void initServer(Mode mode, EST_String name, EST_String type, ostream *trace);
134 
135 protected:
136  void write(BufferedSocket &s, const EST_String string, const EST_String term = "");
137 
138  EST_String read_data(BufferedSocket &s, const EST_String end, int &eof);
139 
140  bool check_cookie(BufferedSocket &socket);
141 
142  bool process_command(BufferedSocket &socket, EST_String command, RequestHandler &handler);
143 
144  void handle_client(BufferedSocket &socket, RequestHandler &handler);
145 
146  void return_error(BufferedSocket &socket, EST_String err);
147 
148  void return_value(BufferedSocket &socket, Result &res, bool last);
149 
150  void run_sequential(RequestHandler &handler);
151 
152 public:
153 
154  /**@name Client end constructors.
155  */
156  //@{
157  /// Create a server connection by name, defaulting to "fringe", the default server name.
159 
160  EST_Server(EST_String name, EST_String type, ostream *trace);
161 
162  /// Create a server connection by explicitly saying where to connect to.
164  EST_Server(EST_String hostname, int port, ostream *trace);
165  //@}
166 
167  /**@name Server end constructors
168  */
169  //@{
171  EST_Server(Mode mode, EST_String name, EST_String type, ostream *trace);
172  //@}
173 
174  /// Destroy the connection.
175  virtual ~EST_Server();
176 
177  /**@name information about the server.
178  */
179  //@{
180  /// Name of server.
181  const EST_String name(void) const;
182  /// Type of server.
183  const EST_String type(void) const;
184  /// Domain name of the server.
185  const EST_String hostname(void) const;
186  /// Dotted numeric IP address
187  const EST_String address(void) const;
188  /// Domain name or IP number
189  const EST_String servername(void) const;
190  /// Port number
191  int port(void) const;
192  //@}
193 
194  /**@name connection management
195  */
196  //@{
197  /// Connect to the server.
198  EST_connect_status connect(void);
199  /// Are we connected at the moment?
200  bool connected(void);
201  /// Disconnect.
202  EST_connect_status disconnect(void);
203  //@}
204 
205  virtual bool parse_command(const EST_String command,
206  EST_String &package,
207  EST_String &operation,
208  Args &arguments);
209 
210  virtual EST_String build_command(const EST_String package,
211  const EST_String operation,
212  const Args &arguments);
213 
214  virtual bool parse_result(const EST_String resultString,
215  Result &res);
216 
217  virtual EST_String build_result(const Result &res);
218 
219 
220  bool execute(const EST_String package,
221  const EST_String operation,
222  const Args &arguments,
223  ResultHandler &handler);
224 
225  bool execute(const EST_String command,
226  ResultHandler &handler);
227 
228  void run(RequestHandler &handler);
229 
230 };
231 
232 #endif
233