LRAUV  revA
ESPComm.h
Go to the documentation of this file.
1 
9 #ifndef ESPCOMM_H_
10 #define ESPCOMM_H_
11 
12 #include "logger/Logger.h"
13 #include "utils/Str.h"
14 #include "io/LineReader.h"
15 #include "io/SocketServer.h"
16 #include "io/SocketClient.h"
17 
18 #include <fstream> // std::ofstream
19 
20 
26 class ESPComm
27 {
28 public:
29 
31  typedef enum { OK = 0,
38  } Error;
39 
49  ESPComm( Str name,
50  const int serverSocketPort,
51  Logger& logger,
52  size_t readBufferSize = 1024 );
53 
64  ESPComm( Str name,
65  const char* espServerAddr,
66  const int espServerPort,
67  Logger& logger,
68  size_t readBufferSize = 1024 );
69 
71  ~ESPComm();
72 
74  ESPComm& open() ;
75 
77  ESPComm& acceptClient( int timeoutMillis = 0 ) ;
78 
85 
90 
103  ESPComm& connectAsClient( int timeoutMillis );
104 
117  ESPComm& setEspServerAddressAndPort( Str espServerAddr, const int espServerPort );
118 
123  {
124  return espServerAddr_.length() > 0 && espServerPort_ > 0;
125  }
126 
131  {
132  Str str = espServerAddr_;
133  str += ":";
134  str += espServerPort_;
135  return str;
136  }
137 
142  ESPComm& submitAbort();
143 
145  inline bool isClientConnected()
146  {
147  return socketClient_ != 0;
148  }
149 
151  ESPComm& close();
152 
153  virtual bool isReadable()
154  {
155  return socketClient_ != 0;
156  }
157 
158  virtual bool eof()
159  {
160  return isReadable();
161  }
162 
163  virtual bool isWritable()
164  {
165  return socketClient_ != 0;
166  }
167 
170  {
171  UNKNOWN = 0,
172  RESULT = 0201, // the ruby eval return value stream
173  OUTPUT = 0202, // the ruby eval output stream (puts, etc.)
174  EXCEPTION = 0203, // synchronous exceptions resulting from ruby cmd eval
175  LOG = 0204, // asynchronous logging stream (messages not directly related to cmds)
176  SET_PROMPT = 0205, // set prompt string (only last line is used)
177  STATE_VEC = 0206, // the state vector update stream (not yet implemented)
178  STATUS = 0207, // status (state) updates
179  PROMPT = 0200, // used like a stream identifier, but not really one. It merely causes the client to prompt for input when received
180  };
181 
182  const char* streamName( EspStream stream )
183  {
184  switch( stream )
185  {
186  case UNKNOWN:
187  return "unknown";
188  case RESULT:
189  return "result";
190  case OUTPUT:
191  return "output";
192  case EXCEPTION:
193  return "exception";
194  case LOG:
195  return "log";
196  case SET_PROMPT:
197  return "set_prompt";
198  case STATUS:
199  return "status";
200  case PROMPT:
201  return "prompt";
202  default:
203  return "unrecognized-esp-stream";
204  }
205  }
206 
207  // Operating ESP states:
208  enum EspState
209  {
221  };
222 
223  const char* espStateName( EspState espState )
224  {
225  switch( espState )
226  {
227  case ES_UNKNOWN:
228  return "ES_UNKNOWN";
229  case ES_IDLE:
230  return "ES_IDLE";
231  case ES_LOADING:
232  return "ES_LOADING";
233  case ES_READY:
234  return "ES_READY";
235  case ES_FILTERING:
236  return "ES_FILTERING";
237  case ES_PAUSED:
238  return "ES_PAUSED";
239  case ES_FILTERING_ERR:
240  return "ES_FILTERING_ERR";
241  case ES_PROCESSING:
242  return "ES_PROCESSING";
243  case ES_PROCESS_FAILED:
244  return "ES_PROCESS_FAILED";
245  case ES_PROCESSED:
246  return "ES_PROCESSED";
247  case ES_UNLOADING:
248  return "ES_UNLOADING";
249  default:
250  return "(invalid EspState)";
251  }
252  };
253 
255  {
256  if( strcmp( "IDLE", str ) == 0 ) return ES_IDLE;
257  if( strcmp( "LOADING", str ) == 0 ) return ES_LOADING;
258  if( strcmp( "READY", str ) == 0 ) return ES_READY;
259  if( strcmp( "FILTERING", str ) == 0 ) return ES_FILTERING;
260  if( strcmp( "PAUSED", str ) == 0 ) return ES_PAUSED;
261  if( strcmp( "FILTERING_ERR", str ) == 0 ) return ES_FILTERING_ERR;
262  if( strcmp( "PROCESSING", str ) == 0 ) return ES_PROCESSING;
263  if( strcmp( "PROCESS_FAILED", str ) == 0 ) return ES_PROCESS_FAILED;
264  if( strcmp( "PROCESSED", str ) == 0 ) return ES_PROCESSED;
265  if( strcmp( "UNLOADING", str ) == 0 ) return ES_UNLOADING;
266 
267  logger_.syslog( Str( "Unrecognized string for ESP state: '" ) + str + "'",
268  Syslog::ERROR );
269  return ES_UNKNOWN;
270  };
271 
279  void setEspLogFile( const char* path, bool onlyLog = true );
280 
289  int readLine( char* buffer, size_t bufferSize, EspStream *stream = 0 ) ;
290 
292  {
293  return espState_;
294  }
295 
297  {
298  return currentStream_;
299  }
300 
308  Str* getLastResult() ;
309 
317  Str* getLastException() ;
318 
330  int sendLine( const char* buffer, size_t bufferSize = 0xFFFFFFFF );
331 
339  Str escape( const char* buffer, size_t bufferSize = 0xFFFFFFFF );
340 
343  {
344  return lastError_;
345  }
346 
347  inline bool hasError()
348  {
349  return lastError_ != OK;
350  }
351 
352  const char* errorString();
353 
354  void setDebug( bool debugOn )
355  {
356  debug_ = debugOn;
357  }
358 
359 private:
360 
361  // disallow copy constructor
362  ESPComm( const ESPComm& old );
363 
365 
366  int serverSocketPort_; // for LRAUV acting as server in socket communication
367  SocketServer* socketServer_; // the corresponding LRAUV server socket
368 
369  Str espServerAddr_; // for ESP acting as server in socket communication
371 
372  Socket* socketClient_; // the ESP client connection socket
373 
376 
378 
379  Str lastResult_; // ongoing last result
380  bool lastResultComplete_; // is the last result now complete?
381 
382  Str lastException_; // ongoing last exception if any
383  bool lastExceptionComplete_; // is the last exception now complete?
384 
385  EspState espState_; // current ESP operating state
386 
388 
390 
391  std::ofstream* espLogFile_;
392  bool onlyLog_;
393 
394  bool debug_;
395 
396  int sendLineAux( Socket* socket, const char* buffer, size_t bufferSize = 0xFFFFFFFF );
397 
398  // convenience methods to facilitate testing outside of the regular LRAUV framework
399  void logDebug( Str msg );
400  void logError( Str msg );
401 
402 };
403 
404 #endif /*ESPCOMM_H_*/
405 
Client-side interface for injecting log data into the log queue.
Definition: Logger.h:30
EspStream currentStream_
Definition: ESPComm.h:377
Definition: ESPComm.h:177
A wrapper around sys/socket.h for tcp communications between Socket instances – use daughter classes...
Definition: Socket.h:33
bool hasEspServerAddressAndPort()
Do we already know the address and port where the ESP is/will be running as server?
Definition: ESPComm.h:122
Socket * socketClient_
Definition: ESPComm.h:372
virtual bool isWritable()
Definition: ESPComm.h:163
Definition: ESPComm.h:36
virtual bool eof()
Definition: ESPComm.h:158
ESPComm::Error lastError_
Definition: ESPComm.h:387
LineReader class declaration.
Definition: ESPComm.h:178
Str lastResult_
Definition: ESPComm.h:379
ESPComm & open()
Starts the server on the given port.
Definition: ESPComm.cpp:90
Definition: ESPComm.h:220
virtual bool isReadable()
Definition: ESPComm.h:153
Definition: ESPComm.h:214
void logError(Str msg)
Definition: ESPComm.cpp:599
Definition: ESPComm.h:210
EspState
Definition: ESPComm.h:208
bool onlyLog_
Definition: ESPComm.h:392
void setDebug(bool debugOn)
Definition: ESPComm.h:354
Str * getLastResult()
Gets the last result received from the ESP.
Definition: ESPComm.cpp:491
int readLine(char *buffer, size_t bufferSize, EspStream *stream=0)
Reads a line (non-blocking) from the ESP.
Definition: ESPComm.cpp:362
Str showEspServerAddressAndPort()
log helper: "address:port" string with the espServerAddr_ and espServerPort fields.
Definition: ESPComm.h:130
ESPComm & submitAbort()
Issues command to request the ESP to interrupt the thread associated with this client.
Definition: ESPComm.cpp:291
Utility for the concrete concern of reading lines from a given file descriptor in a non-blocking fash...
Definition: LineReader.h:26
Str * getLastException()
Gets the last exception received from the ESP.
Definition: ESPComm.cpp:503
Base socket-level interface to the ESP.
Definition: ESPComm.h:26
Definition: ESPComm.h:37
Str espServerAddr_
Definition: ESPComm.h:369
Contains the SocketServer class declaration.
size_t length() const
Definition: Str.h:196
SocketServer * socketServer_
Definition: ESPComm.h:367
Definition: ESPComm.h:217
void logDebug(Str msg)
Definition: ESPComm.cpp:593
Definition: ESPComm.h:31
int espServerPort_
Definition: ESPComm.h:370
const char * streamName(EspStream stream)
Definition: ESPComm.h:182
EspState espStateFromReportedString(const char *str)
Definition: ESPComm.h:254
Str lastException_
Definition: ESPComm.h:382
const char * errorString()
Definition: ESPComm.cpp:605
Definition: Syslog.h:31
bool debug_
Definition: ESPComm.h:394
void setEspLogFile(const char *path, bool onlyLog=true)
Received lines from the ESP are reported in the given file.
Definition: ESPComm.cpp:342
Definition: ESPComm.h:216
Definition: ESPComm.h:212
ESPComm(Str name, const int serverSocketPort, Logger &logger, size_t readBufferSize=1024)
Constructor for when the LRAUV is to act as the server in the socket communication.
Definition: ESPComm.cpp:14
Str escape(const char *buffer, size_t bufferSize=0xFFFFFFFF)
Debugging utility: returns a readable version of contents received from the ESP.
Definition: ESPComm.cpp:515
Error
Errors.
Definition: ESPComm.h:31
int serverSocketPort_
Definition: ESPComm.h:366
ESPComm & acceptClient(int timeoutMillis=0)
Client connection accept with timeout (0 by default)
Definition: ESPComm.cpp:124
Logger logger_
Definition: ESPComm.h:389
Definition: ESPComm.h:175
ESPComm::Error getError()
get last error
Definition: ESPComm.h:342
Replacement for standard template class string.
Definition: Str.h:12
Contains the SocketClient class declaration.
void syslog(const char *msg, Syslog::Severity sev=Syslog::DEBUG)
Create a syslog entry, push into queue, C char* version.
Definition: Logger.cpp:67
Definition: ESPComm.h:173
EspState getEspState()
Definition: ESPComm.h:291
~ESPComm()
Destructor.
Definition: ESPComm.cpp:79
Definition: ESPComm.h:211
EspStream
the ESP virtual streams
Definition: ESPComm.h:169
Definition: ESPComm.h:219
Implements a listening Socket server.
Definition: SocketServer.h:19
Definition: ESPComm.h:179
std::ofstream * espLogFile_
Definition: ESPComm.h:391
Contains the Logger class definition.
Definition: ESPComm.h:171
Definition: ESPComm.h:213
ESPComm & setEspServerAddressAndPort(Str espServerAddr, const int espServerPort)
Sets the address and port of the ESP server.
Definition: ESPComm.cpp:283
size_t readBufferSize_
Definition: ESPComm.h:375
int sendLine(const char *buffer, size_t bufferSize=0xFFFFFFFF)
Sends a single line to the ESP.
Definition: ESPComm.cpp:559
Str getPeerAddress()
Gets the address of peer connected to the client socket.
Definition: ESPComm.cpp:183
int sendLineAux(Socket *socket, const char *buffer, size_t bufferSize=0xFFFFFFFF)
Definition: ESPComm.cpp:564
EspState espState_
Definition: ESPComm.h:385
bool lastExceptionComplete_
Definition: ESPComm.h:383
bool isClientConnected()
is a client connected?
Definition: ESPComm.h:145
Definition: ESPComm.h:215
Definition: ESPComm.h:34
LineReader * lineReader_
Definition: ESPComm.h:374
Definition: ESPComm.h:33
Definition: ESPComm.h:174
Str name_
Definition: ESPComm.h:364
Definition: ESPComm.h:218
ESPComm & close()
Closes underlying sockets.
Definition: ESPComm.cpp:316
bool lastResultComplete_
Definition: ESPComm.h:380
Definition: ESPComm.h:32
Definition: ESPComm.h:35
Definition: ESPComm.h:172
bool hasError()
Definition: ESPComm.h:347
ESPComm & connectAsClient()
Can be called when hasEspServerAddressAndPort is true.
Definition: ESPComm.cpp:214
const char * espStateName(EspState espState)
Definition: ESPComm.h:223
EspStream getCurrentStream()
Definition: ESPComm.h:296
Definition: ESPComm.h:176