95#if !defined(_REQUESTCLIENT_H_)
96#define _REQUESTCLIENT_H_
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};
250 bool init(uint32
id);
366 static bool TestServerLogin(
const char* address, uint16 port,
const char* username,
const char* password,
const char* reqAfterLogin);
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
426 bool addGateway(uint32
id, std::string addr, uint16 port, uint8 encryption =
NOENC);
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_FUNCTION_CALL
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).
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).
uint64 getRequestDuration()
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.
uint32 getRequestDurationMS()
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.
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)
TestRequestClient(uint32 id)
TestWebRequestClient(uint32 id)
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)
TestWebSocketRequestClient(uint32 id)
friend THREAD_RET THREAD_FUNCTION_CALL TestWebSocketRequestClientRun(THREAD_ARG arg)
Recursive mutual-exclusion lock, optionally named for cross-process use.
Counting semaphore, optionally named for cross-process use.
WaitQueue specialization for owned heap pointers: clear()/destructor delete remaining entries.
uint64 GetTimeNow()
Return the current absolute time (µs since year 0) according to the TMC.
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.