CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
RequestClient.h
Go to the documentation of this file.
1
95#if !defined(_REQUESTCLIENT_H_)
96#define _REQUESTCLIENT_H_
97
98#pragma once
99
100#include "NetworkManager.h"
101#include "MovingAverage.h"
102#include "jsmn.h"
103
104namespace cmlabs{
105
107// Request Reply
109
110class RequestReply;
114typedef void (* RequestCallbackFunction)(RequestReply& reply);
115
119enum 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};
120
135protected:
136 // stats?
143
144public:
146 virtual ~RequestReply() { delete(requestMsg); delete(replyMsg); callback = NULL; status = IDLE; }
147
148 uint64 startTime;
149 uint64 finishTime;
150 uint64 origin;
151 uint64 processor;
152 uint64 clientRef;
153 uint64 execRef;
154 uint64 gatewayRef;
155 uint64 customRef;
156 uint32 systemID;
158 bool isInUse;
159
161 bool isComplete();
168 std::string getStatusText();
175
181
193 bool giveReplyMessage(DataMessage* msg);
203
207 RequestStatus waitForResult(uint32 timeoutMS);
219 DataMessage* waitForMessage(uint32 timeoutMS, bool takeMessage = false);
220
222 uint64 getRequestDuration();
224 uint32 getRequestDurationMS();
225};
226
227
229// Request Queue (implemented in RequestGateway.cpp)
231
242public:
244 static bool UnitTest();
245
246 RequestQueue();
247 virtual ~RequestQueue();
248
250 bool init(uint32 id);
251
253 uint32 getCount() { return (uint32)requestQ.size(); }
254
259 bool addRequest(RequestReply* req, double priority = 0);
265 // RequestReply* getRequest(uint64 ref);
268 bool completeRequest(RequestReply* req);
269 //uint32 getActiveRequestCount();
270
273 uint32 removeStaleRequests(uint32 ttlMS);
276 std::list<RequestReply*> takeQueue();
277
279 std::string toXML();
281 std::string toJSON();
282
283protected:
284 std::list<RequestReply*> requestQ;
286 uint32 id;
287};
288
289
291// Request Connection
293
322
330 uint32 id;
331 uint64 lastConTime;
333 std::string addr;
334 uint16 port;
335 uint64 conID;
336 uint64 location;
340};
341
357class RequestClient : public Runnable, public NetworkReceiver {
358public:
360
366 static bool TestServerLogin(const char* address, uint16 port, const char* username, const char* password, const char* reqAfterLogin);
367
371 static std::string SendRequestAndWaitForJSON(RequestClient* client, DataMessage* msg, int timeoutMS);
375 static DataMessage* SendRequestAndWaitForReply(RequestClient* client, DataMessage* msg, int timeoutMS);
380 static RequestReply* SendRequestAndWaitForReplyObject(RequestClient* client, DataMessage* msg, int timeoutMS);
383 static std::string GetJSONReplyParameter(const char* json, const char* name);
390 static DataMessage* CreateRequestMessage(uint8 operation, const char* req,
391 const char* key1 = NULL, const char* val1 = NULL,
392 const char* key2 = NULL, const char* val2 = NULL,
393 const char* key3 = NULL, const char* val3 = NULL,
394 const char* key4 = NULL, const char* val4 = NULL,
395 const char* key5 = NULL, const char* val5 = NULL,
396 const char* key6 = NULL, const char* val6 = NULL,
397 const char* key7 = NULL, const char* val7 = NULL,
398 const char* key8 = NULL, const char* val8 = NULL,
399 const char* key9 = NULL, const char* val9 = NULL,
400 const char* key10 = NULL, const char* val10 = NULL,
401 const char* key11 = NULL, const char* val11 = NULL,
402 const char* key12 = NULL, const char* val12 = NULL,
403 const char* key13 = NULL, const char* val13 = NULL,
404 const char* key14 = NULL, const char* val14 = NULL,
405 const char* key15 = NULL, const char* val15 = NULL,
406 const char* key16 = NULL, const char* val16 = NULL,
407 const char* key17 = NULL, const char* val17 = NULL,
408 const char* key18 = NULL, const char* val18 = NULL,
409 const char* key19 = NULL, const char* val19 = NULL,
410 const char* key20 = NULL, const char* val20 = NULL
411 );
412
414 virtual ~RequestClient();
415
417 bool isConnected();
420 bool waitForConnection(uint32 timeoutMS);
422 bool reconnect();
426 bool addGateway(uint32 id, std::string addr, uint16 port, uint8 encryption = NOENC);
427
460 bool postRequest(DataMessage *msg, RequestCallbackFunction callback, uint32 timeoutMS);
464 bool finishRequest(RequestReply* reply);
465
467 bool receiveNetworkEvent(NetworkEvent* evt, NetworkChannel* channel, uint64 conid);
469 bool receiveMessage(DataMessage* msg, NetworkChannel* channel, uint64 conid);
471 bool receiveHTTPReply(HTTPReply* reply, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
472
473protected:
476 std::list<RequestGatewayConnection> connections;
480 std::map<uint64,RequestReply*> requestMap;
481 uint32 threadID;
482 uint64 lastRefID;
483 uint64 sentCount;
486
488 bool sendRequest(RequestReply* reply);
490 bool run();
494 uint32 sendStatusNow();
495};
496
499
504public:
506 TestRequestClient(uint32 id);
507 bool init(uint16 port, uint32 gatewayCount, double sendfreq, int32 reconnectMS, uint32 payload, const char* longTestName);
508 bool finishUp();
509 bool isStillRunning() { return isRunning; }
510protected:
511 bool run();
512
513 uint32 id;
514 uint16 port;
516 double sendfreq;
518 uint32 payload;
519 std::string longTestName;
523};
524
526
530public:
532 TestWebRequestClient(uint32 id);
533 bool init(uint16 port, uint32 gatewayCount, double sendfreq, int32 reconnectMS, uint32 payload, const char* longTestName);
534 bool finishUp();
535 bool isStillRunning() { return isRunning; }
536protected:
537 bool run();
538
539 uint32 id;
540 uint16 port;
542 double sendfreq;
544 uint32 payload;
545 std::string longTestName;
549};
550
552
553
554
558public:
561 bool init(uint16 port, uint32 gatewayCount, double sendfreq, int32 reconnectMS, uint32 payload, const char* longTestName);
562 bool finishUp();
563 bool isStillRunning() { return isRunning; }
564protected:
565 bool run();
566
567 uint32 id;
568 uint16 port;
570 double sendfreq;
572 uint32 payload;
573 std::string longTestName;
577};
578
580
581
582}
583
584#endif // _REQUESTCLIENT_H_
Time-binned moving average / throughput tracker.
#define NOENC
Plain, unencrypted transport.
Connection/channel management layer: multi-protocol listeners, typed dispatch, HTTP client — and the ...
#define THREAD_RET
Definition Utils.h:127
#define THREAD_FUNCTION_CALL
Definition Utils.h:129
#define THREAD_ARG
Definition Utils.h:130
The central Psyclone data container: a self-contained binary message with typed, named user entries.
A parsed or generated HTTP response.
A parsed or generated HTTP request (also used for WebSocket upgrade handshakes).
Circular-buffer moving statistics: query average, sum and per-second throughput over recent time wind...
One logical network interface: a group of listeners/connections with shared dispatch.
Central owner of all channels, listeners and connections in a process.
bool receiveHTTPReply(HTTPReply *reply, HTTPRequest *req, NetworkChannel *channel, uint64 conid)
NetworkReceiver hook: HTTP replies (when talking to a gateway over HTTP).
uint64 lastRefID
Last issued client reference id.
static DataMessage * SendRequestAndWaitForReply(RequestClient *client, DataMessage *msg, int timeoutMS)
Convenience: post msg, wait, and return the reply message.
utils::Mutex conMutex
Guards connections.
std::map< uint64, RequestReply * > requestMap
In-flight requests by client reference id.
bool receiveNetworkEvent(NetworkEvent *evt, NetworkChannel *channel, uint64 conid)
NetworkReceiver hook: gateway connect/disconnect events (drives reconnection).
NetworkManager * manager
Owned network stack for gateway links.
bool run()
Worker loop: connection upkeep, outgoing queue, timeouts.
bool receiveMessage(DataMessage *msg, NetworkChannel *channel, uint64 conid)
NetworkReceiver hook: reply messages from gateways, matched to pending requests.
bool finishRequest(RequestReply *reply)
Release a RequestReply obtained from postRequest() back to the client's pool.
RequestReply * postRequest(DataMessage *msg)
Post a request asynchronously.
bool sendRequest(RequestReply *reply)
Serialise and send one request to the best available gateway.
bool reconnect()
Force reconnection of gateway links.
static bool TestServerLogin(const char *address, uint16 port, const char *username, const char *password, const char *reqAfterLogin)
Manual test: log in to a request server and issue a request.
static std::string SendRequestAndWaitForJSON(RequestClient *client, DataMessage *msg, int timeoutMS)
Convenience: post msg on client, wait, and return the reply's JSON text.
uint64 receivedCount
Total replies received.
uint32 sendStatusNow()
Send a status/heartbeat message to all gateways.
utils::WaitQueuePointer< RequestReply * > outQ
Outgoing requests awaiting dispatch.
static std::string GetJSONReplyParameter(const char *json, const char *name)
Extract a named top-level string value from a JSON reply.
bool addGateway(uint32 id, std::string addr, uint16 port, uint8 encryption=NOENC)
Register a gateway to connect to (may be called several times for redundancy).
static DataMessage * CreateRequestMessage(uint8 operation, const char *req, const char *key1=NULL, const char *val1=NULL, const char *key2=NULL, const char *val2=NULL, const char *key3=NULL, const char *val3=NULL, const char *key4=NULL, const char *val4=NULL, const char *key5=NULL, const char *val5=NULL, const char *key6=NULL, const char *val6=NULL, const char *key7=NULL, const char *val7=NULL, const char *key8=NULL, const char *val8=NULL, const char *key9=NULL, const char *val9=NULL, const char *key10=NULL, const char *val10=NULL, const char *key11=NULL, const char *val11=NULL, const char *key12=NULL, const char *val12=NULL, const char *key13=NULL, const char *val13=NULL, const char *key14=NULL, const char *val14=NULL, const char *key15=NULL, const char *val15=NULL, const char *key16=NULL, const char *val16=NULL, const char *key17=NULL, const char *val17=NULL, const char *key18=NULL, const char *val18=NULL, const char *key19=NULL, const char *val19=NULL, const char *key20=NULL, const char *val20=NULL)
Build a request DataMessage from an operation, request name and up to 20 key/value string pairs (NULL...
bool waitForConnection(uint32 timeoutMS)
Block until a gateway connection is established.
friend THREAD_RET THREAD_FUNCTION_CALL RequestClientRun(THREAD_ARG arg)
Thread entry point for the RequestClient worker loop.
bool sendMessageToGateway(DataMessage *msg, RequestGatewayConnection &con)
Send a message on a specific gateway link.
uint32 threadID
Worker thread id.
std::list< RequestGatewayConnection > connections
Configured gateways and their state.
NetworkChannel * channel
Channel carrying the gateway connections.
static RequestReply * SendRequestAndWaitForReplyObject(RequestClient *client, DataMessage *msg, int timeoutMS)
Convenience: post msg, wait, and return the whole RequestReply object.
uint64 sentCount
Total requests sent.
utils::Mutex mutex
Guards request bookkeeping.
MovingAverage avgStats
Rolling round-trip statistics.
Priority-ordered queue of pending RequestReply objects (per executor connection).
uint32 id
Owner id for logging.
std::list< RequestReply * > takeQueue()
Atomically take all queued requests (used to redistribute a dead executor's backlog).
bool init(uint32 id)
Initialise the queue.
RequestReply * getNextRequest()
Pop the next request to dispatch.
std::list< RequestReply * > requestQ
The ordered pending requests.
bool completeRequest(RequestReply *req)
Mark a request as done and remove it from tracking.
bool addRequest(RequestReply *req, double priority=0)
Insert a request, ordered by priority (higher = closer to the front).
uint32 removeStaleRequests(uint32 ttlMS)
Fail and remove requests older than ttlMS milliseconds.
static bool UnitTest()
Self-test of queue ordering/priority behaviour.
utils::Mutex mutex
Guards requestQ.
RequestReply * getNextTimedoutRequest()
Find and remove the next request whose deadline has passed.
Future/handle for one in-flight request: holds the request message, the eventual reply,...
uint64 gatewayRef
Gateway-side reference id.
DataMessage * peekRequestMessage()
RequestCallbackFunction callback
uint64 customRef
Free-form reference for application use.
bool setRequestMessageCopy(DataMessage *msg)
Attach a copy of the outgoing request message (caller keeps msg).
DataMessage * replyMsg
bool isLongReq
True when classified as a long-running request (separate queue/limits).
bool setCallback(RequestCallbackFunction callback)
Register a completion callback (alternative to blocking waits).
bool setStatus(RequestStatus status)
Set the current lifecycle status (wakes waiters when terminal).
DataMessage * getRequestMessageCopy()
bool replyToRequest(DataMessage *msg, RequestStatus status)
Complete this request with a reply (called by the network layer).
RequestStatus getStatus()
bool giveReplyMessage(DataMessage *msg)
Attach the reply message, transferring ownership.
uint64 execRef
Executor-side reference id.
uint64 startTime
When the request was created (ms epoch).
uint32 systemID
Id of the system/gateway this request belongs to.
std::string getStatusText()
uint64 origin
Originating endpoint/connection (packed uint64), for reply routing.
RequestStatus waitForResult(uint32 timeoutMS)
Block until the request reaches a terminal status or the timeout expires.
RequestCallbackFunction getCallback()
uint64 finishTime
When a terminal status was set (ms epoch; 0 while pending).
uint64 processor
Executor id that processed (or is processing) the request.
DataMessage * getReplyMessageCopy()
bool isInUse
Pooling flag: false once finishRequest() releases the object for reuse.
DataMessage * requestMsg
utils::Semaphore semaphore
uint64 clientRef
Client-side reference id correlating request and reply.
bool setReplyMessageCopy(DataMessage *msg)
Attach a copy of the reply message (caller keeps msg).
bool giveRequestMessage(DataMessage *msg)
Attach the outgoing request message, transferring ownership.
DataMessage * waitForMessage(uint32 timeoutMS, bool takeMessage=false)
Block for the reply message itself.
DataMessage * peekReplyMessage()
Runnable()
Initialise flags: not running, allowed to continue.
friend THREAD_RET THREAD_FUNCTION_CALL TestRequestClientRun(THREAD_ARG arg)
bool init(uint16 port, uint32 gatewayCount, double sendfreq, int32 reconnectMS, uint32 payload, const char *longTestName)
bool init(uint16 port, uint32 gatewayCount, double sendfreq, int32 reconnectMS, uint32 payload, const char *longTestName)
friend THREAD_RET THREAD_FUNCTION_CALL TestWebRequestClientRun(THREAD_ARG arg)
bool init(uint16 port, uint32 gatewayCount, double sendfreq, int32 reconnectMS, uint32 payload, const char *longTestName)
friend THREAD_RET THREAD_FUNCTION_CALL TestWebSocketRequestClientRun(THREAD_ARG arg)
Recursive mutual-exclusion lock, optionally named for cross-process use.
Definition Utils.h:463
Counting semaphore, optionally named for cross-process use.
Definition Utils.h:502
WaitQueue specialization for owned heap pointers: clear()/destructor delete remaining entries.
Definition Utils.h:823
uint64 GetTimeNow()
Return the current absolute time (µs since year 0) according to the TMC.
Definition PsyTime.cpp:69
RequestStatus
Lifecycle states of a request as it moves through client, gateway and executor.
void(* RequestCallbackFunction)(RequestReply &reply)
Signature for asynchronous completion callbacks registered on a RequestReply.
THREAD_RET THREAD_FUNCTION_CALL RequestClientRun(THREAD_ARG arg)
Thread entry point for the RequestClient worker loop.
Third-party (vendored): jsmn minimalistic JSON tokenizer by Serge Zaitsev (MIT licence),...
THREAD_RET THREAD_FUNCTION_CALL TestRequestClientRun(THREAD_ARG arg)
THREAD_RET THREAD_FUNCTION_CALL TestWebSocketRequestClientRun(THREAD_ARG arg)
THREAD_RET THREAD_FUNCTION_CALL TestWebRequestClientRun(THREAD_ARG arg)
Notification of a connection lifecycle change (connect, disconnect, buffer state.....
Gateway-side record of one connected client or executor.
uint64 location
Remote endpoint packed as uint64.
uint32 longReqQProcessingSize
Peer-reported long requests currently processing.
uint64 lastConTime
Last successful connect/heartbeat time (ms epoch).
RequestQueue longReqQueue
Gateway-side queue of long requests for this peer.
RequestQueue shortReqQueue
Gateway-side queue of short requests for this peer.
uint32 removeStaleRequests(uint32 ttlMS)
Fail and drop requests older than ttlMS from both queues.
uint32 reportedShortReqQSize
Peer-reported short-request queue size.
uint32 count
Total requests routed via this connection.
uint64 lastFailTime
Last connection failure time (ms epoch).
uint64 lastStatusTime
When the peer last reported its status (ms epoch).
void clear()
Reset all counters/ids to the disconnected state.
MovingAverage longAvgStats
Rolling latency stats for long requests.
MovingAverage shortAvgStats
Rolling latency stats for short requests.
uint32 reportedLongReqQSize
Peer-reported long-request queue size.
uint64 conID
NetworkManager connection id.
Client/executor-side record of one gateway it connects to.
uint8 encryption
NOENC or SSLENC for this link.
uint32 id
Gateway id (configuration key).
uint64 location
Resolved endpoint packed as uint64.
uint64 conID
NetworkManager connection id (0 when disconnected).
std::string addr
Gateway host name or IP.
uint64 lastFailTime
Last failed connect time (ms epoch; drives retry backoff).
uint64 lastConTime
Last successful connect time (ms epoch).
void clear()
Reset to the unconfigured/disconnected state.