CARMA C++
Communicator.h
Go to the documentation of this file.
1 #ifndef SZA_UTIL_COMMUNICATOR_H
2 #define SZA_UTIL_COMMUNICATOR_H
3 
11 #include <list>
12 #include <string>
13 #include <sstream>
14 
15 #include "carma/szautil/AntNum.h"
16 #include "carma/szautil/String.h"
18 #include "carma/szautil/TimeVal.h"
19 
20 #define COMM_PARSER_FN(fn) void (fn)(sza::util::String& str, void* arg)
21 #define COMM_END_PARSER_FN(fn) void (fn)(void* arg)
22 
23 #define LOG_OUTPUT
24 
25 #ifdef LOG_OUTPUT
26 #include <fstream>
27 #endif
28 
29 namespace sza {
30 namespace util {
31 
32  class Communicator {
33  public:
34 
35 #ifdef LOG_OUTPUT
36  std::ofstream* fout_;
37 #endif
38 
39  // A generalized object for managing strings received from the
40  // power strips. If a parser is registered, it will be called
41  // to process the string
42 
43  struct RcvdStr {
44  std::string start_;
45  std::string stop_;
46  COMM_PARSER_FN(*parser_);
47  void* arg_;
48  COMM_END_PARSER_FN(*endParser_);
49  void* endArg_;
50  bool matchAny_;
51  std::ostringstream os;
52 
53  RcvdStr();
54  RcvdStr(const RcvdStr& str);
55  RcvdStr(std::string str, COMM_PARSER_FN(*parser)=0, void* arg=0);
56  RcvdStr(std::string start, std::string stop,
57  COMM_PARSER_FN(*parser), void* arg,
58  COMM_END_PARSER_FN(*endParser), void* endArg);
59 
60  void initialize();
61  };
62 
66  Communicator();
67 
71  virtual ~Communicator();
72 
73  // The number of seconds we will wait for a response
74 
75  static const unsigned COMMAND_TIMEOUT_SEC = 10;
76 
81  bool timedOut();
82 
86  virtual int getFd();
87 
88  // Write a string to the server
89 
90  virtual void writeString(std::string);
91 
92  // Concatenate a string received from the server
93 
94  virtual void concatenateString(std::ostringstream& os);
95 
96  void run();
97 
101  virtual void processClientMessage();
102 
106  void registerTimeOut();
107 
108  protected:
109 
110  // An object for managing the TCP/IP connection to the power strip
111 
112  sza::util::TcpClient* client_;
113 
114  // A timer we will use for keeping track of timeouts
115 
116  sza::util::TimeVal timer_;
117 
122  std::list<std::string> sentStrings_;
123  std::list<RcvdStr> rcvdStrings_;
124 
128  std::list<std::string>::iterator sentStringIter_;
129  std::list<RcvdStr>::iterator rcvdStringIter_;
130 
131  std::ostringstream os_;
132 
136  virtual void terminateCommSequence(bool error);
137 
141  void enableTimeOut(bool enable);
142 
143  void checkLine();
144 
145  static COMM_PARSER_FN(sendNextString);
146  void sendNextString();
147 
148  virtual void execSendNextString();
149 
150  void doSomething();
151 
155  void advanceIterator(bool bufferReset);
156 
157  void checkIterators();
158 
159  }; // End class Communicator
160 
161 } // End namespace util
162 } // End namespace sza
163 
164 
165 
166 #endif // End #ifndef SZA_UTIL_COMMUNICATOR_H
A class for managing timeval/timespec structs.
Definition: TimeVal.h:53
Tagged: Fri Nov 14 12:39:38 UTC 2003.
Tagged: Fri Nov 14 12:39:31 UTC 2003.
Started: Sat Mar 6 14:44:17 UTC 2004.
Tagged: Wed May 12 09:30:13 PDT 2004.