Edinburgh Speech Tools  2.1-release
EST_Server.h
Go to the documentation of this file.
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 /** \class EST_Server
42  * \brief Client-server interface.
43  *
44  * An EST_Server object represents a server, it can be at either
45  * end of a network connection. That is, a server process has an
46  * EST_Server object representing it's wait-dispatch-answer loop,
47  * while a client process has an EST_Server object which represents
48  * the server process.
49  *
50  * @author Richard Caley <rjc@cstr.ed.ac.uk>
51  * @version $Id: EST_Server.h,v 1.4 2004/05/04 00:00:16 awb Exp $
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;
75 
77  {
78  public:
83  Args args;
84  Result res;
85 
87  virtual ~RequestHandler();
88  virtual EST_String process(void)=0;
89  };
90 
92  {
93  public:
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.
199  /// Are we connected at the moment?
200  bool connected(void);
201  /// Disconnect.
203  //@}
204 
205  virtual bool parse_command(const EST_String command,
208  Args &arguments);
209 
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 
float end(const EST_Item &item)
Definition: EST_item_aux.cc:96
Answer one client at a time.
Definition: EST_Server.h:64
EST_Server(EST_String name, EST_String type)
Create a server connection by name, defaulting to "fringe", the default server name.
For off a process for each client.
Definition: EST_Server.h:68
void return_value(BufferedSocket &socket, Result &res, bool last)
const EST_String type(void) const
Type of server.
void run(RequestHandler &handler)
void handle_client(BufferedSocket &socket, RequestHandler &handler)
A service record.
const EST_String address(void) const
Dotted numeric IP address.
Bizarre state.
Definition: EST_Server.h:60
bool execute(const EST_String package, const EST_String operation, const Args &arguments, ResultHandler &handler)
virtual EST_String build_result(const Result &res)
void run_sequential(RequestHandler &handler)
int port(void) const
Port number.
virtual EST_String process(void)=0
const EST_String name(void) const
Name of server.
void err(const char *message, LISP x) EST_NORETURN
Definition: slib.cc:608
const EST_String servername(void) const
Domain name or IP number.
virtual EST_String build_command(const EST_String package, const EST_String operation, const Args &arguments)
Mode
What type of server is this.
Definition: EST_Server.h:58
EST_Features Args
Definition: EST_Server.h:73
Answer requests from several clients, as requests arrive.
Definition: EST_Server.h:66
EST_connect_status connect(void)
Connect to the server.
virtual bool parse_command(const EST_String command, EST_String &package, EST_String &operation, Args &arguments)
virtual ~EST_Server()
Destroy the connection.
Client-server interface.
Definition: EST_Server.h:53
bool check_cookie(BufferedSocket &socket)
void write(BufferedSocket &s, const EST_String string, const EST_String term="")
EST_Features Result
Definition: EST_Server.h:74
bool connected(void)
Are we connected at the moment?
Multi-threaded (not implemented)
Definition: EST_Server.h:70
EST_String read_data(BufferedSocket &s, const EST_String end, int &eof)
EST_connect_status
Client end of the connection.
Definition: EST_Server.h:62
EST_connect_status disconnect(void)
Disconnect.
virtual bool parse_result(const EST_String resultString, Result &res)
void return_error(BufferedSocket &socket, EST_String err)
const EST_String hostname(void) const
Domain name of the server.
bool process_command(BufferedSocket &socket, EST_String command, RequestHandler &handler)