CMSDK  2.0.1
RequestClient.h
1 #if !defined(_REQUESTCLIENT_H_)
2 #define _REQUESTCLIENT_H_
3 
4 #pragma once
5 
6 #include "NetworkManager.h"
7 #include "MovingAverage.h"
8 #include "jsmn.h"
9 
10 namespace cmlabs{
11 
13 // Request Reply
15 
16 class RequestReply;
17 typedef void (* RequestCallbackFunction)(RequestReply& reply);
18 
19 enum RequestStatus { NONE = 0, IDLE = 1, QUEUED = 2, PROCESSING = 3, SENT = 4, SUCCESS = 5, FAILED = 6, TIMEOUT = 7, TOOBUSY = 8, LOCALERROR = 9, NETWORKERROR = 10, SERVERERROR = 11};
20 
21 class RequestReply {
22 protected:
23  // stats?
24  RequestCallbackFunction callback;
25  RequestStatus status;
26  DataMessage* replyMsg;
27  DataMessage* requestMsg;
28  utils::Mutex mutex;
29  utils::Semaphore semaphore;
30 
31 public:
32  RequestReply() { requestMsg = replyMsg = NULL; callback = NULL; status = IDLE; systemID = 0; startTime = GetTimeNow(); origin = processor = clientRef = gatewayRef = execRef = finishTime = customRef = 0; isLongReq = false; isInUse = true; }
33  virtual ~RequestReply() { delete(requestMsg); delete(replyMsg); callback = NULL; status = IDLE; }
34 
35  uint64 startTime;
36  uint64 finishTime;
37  uint64 origin;
38  uint64 processor;
39  uint64 clientRef;
40  uint64 execRef;
41  uint64 gatewayRef;
42  uint64 customRef;
43  uint32 systemID;
44  bool isLongReq;
45  bool isInUse;
46 
47  bool isComplete();
48  bool setStatus(RequestStatus status);
49  RequestStatus getStatus();
50  std::string getStatusText();
51  bool setCallback(RequestCallbackFunction callback);
52  RequestCallbackFunction getCallback();
53 
54  bool replyToRequest(DataMessage* msg, RequestStatus status);
55 
56  bool giveRequestMessage(DataMessage* msg);
57  bool setRequestMessageCopy(DataMessage* msg);
58  DataMessage* getRequestMessageCopy();
59  DataMessage* peekRequestMessage();
60  bool giveReplyMessage(DataMessage* msg);
61  bool setReplyMessageCopy(DataMessage* msg);
62  DataMessage* getReplyMessageCopy();
63  DataMessage* peekReplyMessage();
64 
65  RequestStatus waitForResult(uint32 timeoutMS);
66  DataMessage* waitForMessage(uint32 timeoutMS, bool takeMessage = false);
67 
68  uint64 getRequestDuration();
69  uint32 getRequestDurationMS();
70 };
71 
72 
74 // Request Queue (implemented in RequestGateway.cpp)
76 
77 class RequestQueue {
78 public:
79  static bool UnitTest();
80 
81  RequestQueue();
82  virtual ~RequestQueue();
83 
84  bool init(uint32 id);
85 
86  uint32 getCount() { return (uint32)requestQ.size(); }
87 
88  bool addRequest(RequestReply* req, double priority = 0);
89  RequestReply* getNextRequest();
90  // RequestReply* getRequest(uint64 ref);
91  bool completeRequest(RequestReply* req);
92  //uint32 getActiveRequestCount();
93 
94  std::list<RequestReply*> takeQueue();
95 
96  std::string toXML();
97  std::string toJSON();
98 
99 protected:
100  std::list<RequestReply*> requestQ;
101  utils::Mutex mutex;
102  uint32 id;
103 };
104 
105 
107 // Request Connection
109 
111  uint64 lastConTime;
112  uint64 lastFailTime;
113  uint64 conID;
114  uint64 location;
115  uint64 lastStatusTime;
116  uint32 reportedShortReqQSize;
117  uint32 reportedLongReqQSize;
118  uint32 longReqQProcessingSize;
119  uint32 count;
120  RequestQueue shortReqQueue;
121  RequestQueue longReqQueue;
122  MovingAverage shortAvgStats;
123  MovingAverage longAvgStats;
124  void clear() { lastConTime=lastFailTime=conID=location=lastStatusTime=0; reportedShortReqQSize = reportedLongReqQSize = longReqQProcessingSize = count = 0; }
125 };
126 
128  uint32 id;
129  uint64 lastConTime;
130  uint64 lastFailTime;
131  std::string addr;
132  uint16 port;
133  uint64 conID;
134  uint64 location;
135  uint8 encryption;
136  void clear() { lastConTime=lastFailTime=conID=0; port=0; id=0; addr.clear(); conID=location=0; encryption = NOENC; }
137 };
138 
139 class RequestClient : public Runnable, public NetworkReceiver {
140 public:
141  friend THREAD_RET THREAD_FUNCTION_CALL RequestClientRun(THREAD_ARG arg);
142 
143  static bool TestServerLogin(const char* address, uint16 port, const char* username, const char* password, const char* reqAfterLogin);
144 
145  static std::string SendRequestAndWaitForJSON(RequestClient* client, DataMessage* msg, int timeoutMS);
146  static DataMessage* SendRequestAndWaitForReply(RequestClient* client, DataMessage* msg, int timeoutMS);
147  static std::string GetJSONReplyParameter(const char* json, const char* name);
148  static DataMessage* CreateRequestMessage(uint8 operation, const char* req,
149  const char* key1 = NULL, const char* val1 = NULL,
150  const char* key2 = NULL, const char* val2 = NULL,
151  const char* key3 = NULL, const char* val3 = NULL,
152  const char* key4 = NULL, const char* val4 = NULL,
153  const char* key5 = NULL, const char* val5 = NULL,
154  const char* key6 = NULL, const char* val6 = NULL,
155  const char* key7 = NULL, const char* val7 = NULL,
156  const char* key8 = NULL, const char* val8 = NULL,
157  const char* key9 = NULL, const char* val9 = NULL,
158  const char* key10 = NULL, const char* val10 = NULL,
159  const char* key11 = NULL, const char* val11 = NULL,
160  const char* key12 = NULL, const char* val12 = NULL,
161  const char* key13 = NULL, const char* val13 = NULL,
162  const char* key14 = NULL, const char* val14 = NULL,
163  const char* key15 = NULL, const char* val15 = NULL,
164  const char* key16 = NULL, const char* val16 = NULL,
165  const char* key17 = NULL, const char* val17 = NULL,
166  const char* key18 = NULL, const char* val18 = NULL,
167  const char* key19 = NULL, const char* val19 = NULL,
168  const char* key20 = NULL, const char* val20 = NULL
169  );
170 
171  RequestClient();
172  virtual ~RequestClient();
173 
174  bool isConnected();
175  bool reconnect();
176  bool addGateway(uint32 id, std::string addr, uint16 port, uint8 encryption = NOENC);
177 
178  RequestReply* postRequest(DataMessage *msg);
179  bool postRequest(DataMessage *msg, RequestCallbackFunction callback, uint32 timeoutMS);
180  bool finishRequest(RequestReply* reply);
181 
182  bool receiveNetworkEvent(NetworkEvent* evt, NetworkChannel* channel, uint64 conid);
183  bool receiveMessage(DataMessage* msg, NetworkChannel* channel, uint64 conid);
184  bool receiveHTTPReply(HTTPReply* reply, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
185 
186 protected:
187  NetworkManager* manager;
188  NetworkChannel* channel;
189  std::list<RequestGatewayConnection> connections;
190  utils::Mutex mutex;
191  utils::Mutex conMutex;
193  std::map<uint64,RequestReply*> requestMap;
194  uint32 threadID;
195  uint64 lastRefID;
196  uint64 sentCount;
197  uint64 receivedCount;
198  MovingAverage avgStats;
199 
200  bool sendRequest(RequestReply* reply);
201  bool run();
202  bool sendMessageToGateway(DataMessage* msg, RequestGatewayConnection& con);
203 };
204 
205 THREAD_RET THREAD_FUNCTION_CALL RequestClientRun(THREAD_ARG arg);
206 
208 public:
209  friend THREAD_RET THREAD_FUNCTION_CALL TestRequestClientRun(THREAD_ARG arg);
210  TestRequestClient(bool isLong);
211 protected:
212  bool testRun();
213 };
214 
215 THREAD_RET THREAD_FUNCTION_CALL TestRequestClientRun(THREAD_ARG arg);
216 
217 }
218 
219 #endif // _REQUESTCLIENT_H_
Definition: RequestClient.h:110
Definition: NetworkProtocols.h:194
Definition: NetworkManager.h:47
Definition: Bitmap.h:7
Definition: NetworkManager.h:136
Definition: RequestClient.h:77
Definition: ThreadManager.h:33
Definition: RequestClient.h:127
Definition: RequestClient.h:207
Definition: MovingAverage.h:14
Definition: Utils.h:299
Definition: NetworkManager.h:25
Definition: NetworkProtocols.h:81
Definition: Utils.h:529
Definition: RequestClient.h:139
Definition: NetworkManager.h:12
Definition: Utils.h:276
Definition: RequestClient.h:21
Definition: DataMessage.h:95