My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
Communicator.h
Go to the documentation of this file.
1 #ifndef GCP_UTIL_COMMUNICATOR_H
2 #define GCP_UTIL_COMMUNICATOR_H
3 
11 #include <list>
12 #include <string>
13 #include <sstream>
14 
15 #include "gcp/util/common/AntNum.h"
16 #include "gcp/util/common/String.h"
17 #include "gcp/util/common/TcpClient.h"
18 #include "gcp/util/common/TimeVal.h"
19 
20 #define COMM_PARSER_FN(fn) void (fn)(gcp::util::String& str, void* arg)
21 #define COMM_END_PARSER_FN(fn) void (fn)(void* arg)
22 
23 namespace gcp {
24 namespace util {
25 
26  class Communicator {
27  public:
28 
29  // A generalized object for managing strings received from the
30  // power strips. If a parser is registered, it will be called
31  // to process the string
32 
33  struct RcvdStr {
34  std::string start_;
35  std::string stop_;
36  COMM_PARSER_FN(*parser_);
37  void* arg_;
38  COMM_END_PARSER_FN(*endParser_);
39  void* endArg_;
40  bool matchAny_;
41  std::ostringstream os;
42 
43  RcvdStr();
44  RcvdStr(const RcvdStr& str);
45  RcvdStr(std::string str, COMM_PARSER_FN(*parser)=0, void* arg=0);
46  RcvdStr(std::string start, std::string stop,
47  COMM_PARSER_FN(*parser), void* arg,
48  COMM_END_PARSER_FN(*endParser), void* endArg);
49 
50  void initialize();
51  };
52 
56  Communicator();
57 
61  virtual ~Communicator();
62 
63  // The number of seconds we will wait for a response
64 
65  static const unsigned COMMAND_TIMEOUT_SEC = 10;
66 
71  bool timedOut();
72 
76  virtual int getFd();
77 
78  void run();
79 
83  virtual void processClientMessage();
84 
88  void registerTimeOut();
89 
90  protected:
91 
92  // An object for managing the TCP/IP connection to the power strip
93 
94  gcp::util::TcpClient* client_;
95 
96  // A timer we will use for keeping track of timeouts
97 
98  gcp::util::TimeVal timer_;
99 
104  std::list<std::string> sentStrings_;
105  std::list<RcvdStr> rcvdStrings_;
106 
110  std::list<std::string>::iterator sentStringIter_;
111  std::list<RcvdStr>::iterator rcvdStringIter_;
112 
113  std::ostringstream os_;
114 
118  virtual void terminateCommSequence(bool error);
119 
123  void enableTimeOut(bool enable);
124 
125  void checkLine();
126 
127  static COMM_PARSER_FN(sendNextString);
128 
129  virtual void sendNextString(void);
130 
134  void advanceIterator(bool bufferReset);
135 
136  void checkIterators();
137 
138  }; // End class Communicator
139 
140 } // End namespace util
141 } // End namespace gcp
142 
143 
144 
145 #endif // End #ifndef GCP_UTIL_COMMUNICATOR_H
void run()
Definition: Communicator.cc:263
virtual int getFd()
Definition: Communicator.cc:180
void advanceIterator(bool bufferReset)
Definition: Communicator.cc:130
Definition: Communicator.h:33
virtual void processClientMessage()
Definition: Communicator.cc:47
virtual ~Communicator()
Definition: Communicator.cc:22
std::list< std::string > sentStrings_
Definition: Communicator.h:104
Definition: Communicator.h:26
Definition: TcpClient.h:18
std::list< std::string >::iterator sentStringIter_
Definition: Communicator.h:110
virtual void terminateCommSequence(bool error)
Definition: Communicator.cc:35
void registerTimeOut()
Definition: Communicator.cc:151
Definition: TimeVal.h:55
Communicator()
Definition: Communicator.cc:14
bool timedOut()
Definition: Communicator.cc:161
void enableTimeOut(bool enable)