47 std::map<uint64, RequestReply*>::iterator i, e;
48 mutex.enter(200, __FUNCTION__);
81 if (!
mutex.enter(200, __FUNCTION__))
85 msgConnect->
setString(
"URI",
"ExecutorConnect");
89 channel =
manager->addTCPConnection(con.
addr.c_str(), con.
port, con.
encryption,
PROTOCOL_MESSAGE,
true, 0,
this, con.
conID, con.
location, 1000, (
const char*)msgConnect->
data, msgConnect->
getSize());
145 std::list<RequestGatewayConnection>::iterator i, e =
connections.end();
146 if (!
mutex.enter(200, __FUNCTION__))
150 if ((*i).conID == conid) {
155 (*i).lastFailTime = now;
161 (*i).lastFailTime = 0;
162 (*i).lastConTime = now;
206 if (!msg)
return false;
215 if (!
mutex.enter(1000, __FUNCTION__)) {
221 std::list<RequestGatewayConnection>::iterator i, e =
connections.end();
224 if ((*i).conID == conid) {
273 if (msg =
longExecQ.waitForAndTakeFirst(timeoutMS)) {
283 if (msg =
shortExecQ.waitForAndTakeFirst(timeoutMS)) {
292 if (!msg)
return false;
295 if (!
mutex.enter(200, __FUNCTION__))
298 std::map<uint64,RequestReply*>::iterator i =
requestMap.find(execRef);
306 std::list<RequestGatewayConnection>::iterator iCon, eCon =
connections.end();
308 while (iCon != eCon) {
309 if ((*iCon).conID == i->second->origin) {
328 if (!
mutex.enter(200, __FUNCTION__))
331 std::map<uint64,RequestReply*>::iterator i =
requestMap.find(execRef);
332 std::map<uint64,RequestGatewayConnection>::iterator con;
335 LogPrint(0,
LOG_SYSTEM, 0,
"Executor couldn't find request to reply to (%llu)", execRef);
342 std::list<RequestGatewayConnection>::iterator iCon, eCon =
connections.end();
344 while (iCon != eCon) {
345 if ((*iCon).conID == reply->
origin) {
390 while (msg =
replyQ.waitForAndTakeFirst(50)) {
391 if (!
mutex.enter(200, __FUNCTION__)) {
474 if (!
conMutex.enter(1000, __FUNCTION__)) {
489 uint32 success = 0, failed = 0, notConnected = 0;
491 std::list<RequestGatewayConnection>::iterator iCon, eCon =
connections.end();
493 while (iCon != eCon) {
546 std::list<DataMessage*> storedMessages;
547 std::list<DataMessage*>::iterator i, e = storedMessages.end();
559 storedMessages.push_back(msg);
563 i = storedMessages.begin();
566 i = storedMessages.erase(i);
#define NETWORKEVENT_CONNECT
Connection established.
#define NETWORKEVENT_DISCONNECT_RETRYING
Connection lost; reconnection attempts in progress.
#define NETWORKEVENT_DISCONNECT
Connection closed for good.
#define NETWORKEVENT_RECONNECT
Connection re-established after a failure (autoreconnect).
#define PROTOCOL_MESSAGE
CMSDK binary DataMessage protocol (size-prefixed frames).
Request-system worker side: RequestExecutor pulls requests from a RequestGateway, processes them and ...
#define thread_ret_val(ret)
#define THREAD_FUNCTION_CALL
The central Psyclone data container: a self-contained binary message with typed, named user entries.
bool setString(const char *key, const char *value)
setString(const char* key, const char* value)
bool setInt(const char *key, int64 value)
setInt(const char* key, int64 value)
DataMessageHeader * data
Pointer to the message's flat memory block (header + user entries).
uint32 getSize()
getSize() Get message size Many types of data of any size can be put into a message as user entries; ...
uint64 getReference()
getReference() Get and return message reference id
bool setRecvTime(uint64 time)
setRecvTime(uint64 time)
bool setReference(uint64 ref)
setReference(uint64 ref) Set message reference
const char * getString(const char *key)
getString(const char* key)
uint64 getRecvTime()
getRecvTime()
bool setSendTime(uint64 time)
setSendTime(uint64 time)
One logical network interface: a group of listeners/connections with shared dispatch.
Central owner of all channels, listeners and connections in a process.
uint64 lastHeartbeat
Time of last heartbeat (ms epoch).
uint64 longReceivedCount
Total long requests received.
uint32 sendStatusNow()
Send a status heartbeat (queue sizes) to all gateways.
bool replyToQuery(DataMessage *msg)
Send a reply for a previously pulled request back to its gateway.
bool shutdownNetwork()
Disconnect from all gateways and stop the network stack.
utils::WaitQueuePointer< DataMessage * > replyQ
Outgoing replies awaiting dispatch.
DataMessage * waitForShortRequest(uint32 timeoutMS)
Pull the next short request (worker thread API).
utils::Mutex mutex
Guards request bookkeeping.
NetworkManager * manager
Owned network stack.
bool replyToGateway(DataMessage *msg)
Route a finished reply to the gateway that sent the request.
std::string executorName
Display name for logging.
bool run()
Worker loop: gateway upkeep, heartbeats, reply dispatch.
uint32 threadID
Worker thread id.
uint64 shortReceivedCount
Total short requests received.
utils::WaitQueuePointer< DataMessage * > shortExecQ
Pending short requests.
int32 heartbeatIntervalMS
Interval between status heartbeats (ms).
std::map< uint64, RequestReply * > requestMap
In-flight request bookkeeping by ref id.
bool receiveMessage(DataMessage *msg, NetworkChannel *channel, uint64 conid)
NetworkReceiver hook: incoming request messages, routed to the short/long queue.
uint64 sentCount
Total messages sent to gateways.
bool addGateway(uint32 id, std::string addr, uint16 port, uint8 encryption=NOENC)
Register a gateway to connect to (repeatable for redundancy).
DataMessage * waitForLongRequest(uint32 timeoutMS)
Pull the next long request (worker thread API).
bool setLongRequestLimit(uint32 limit)
Cap the number of long requests processed concurrently.
utils::Mutex conMutex
Guards connections.
std::vector< std::string > longReqNames
Request names classified as long.
bool receiveNetworkEvent(NetworkEvent *evt, NetworkChannel *channel, uint64 conid)
NetworkReceiver hook: gateway connect/disconnect events (drives reconnection).
bool sendMessageToGateway(DataMessage *msg, RequestGatewayConnection &con)
Send a message on one gateway link.
NetworkChannel * channel
Channel carrying the gateway links.
uint32 executorID
Numeric id reported to gateways.
utils::WaitQueuePointer< DataMessage * > longExecQ
Pending long requests.
std::list< RequestGatewayConnection > connections
Configured gateways and their state.
uint32 longReqLimit
Max concurrent long requests (see setLongRequestLimit()).
virtual ~RequestExecutor()
uint64 lastRefID
Last issued executor reference id.
bool addLongRequestName(const char *name)
Register a request name to be treated as a long-running request.
friend THREAD_RET THREAD_FUNCTION_CALL RequestExecutorRun(THREAD_ARG arg)
Thread entry point for the RequestExecutor worker loop.
RequestExecutor(uint32 id=0, const char *name=NULL)
uint64 repliedCount
Total replies sent.
Future/handle for one in-flight request: holds the request message, the eventual reply,...
uint64 gatewayRef
Gateway-side reference id.
bool setStatus(RequestStatus status)
Set the current lifecycle status (wakes waiters when terminal).
uint64 execRef
Executor-side reference id.
uint64 origin
Originating endpoint/connection (packed uint64), for reply routing.
bool giveRequestMessage(DataMessage *msg)
Attach the outgoing request message, transferring ownership.
bool isRunning
Set by the worker while its loop is active.
virtual bool stop(uint32 timeout=200)
Ask the worker loop to finish and wait for it to do so.
bool shouldContinue
Loop-continuation flag; cleared by stop().
friend THREAD_RET THREAD_FUNCTION_CALL ShortExecutorRun(THREAD_ARG arg)
TestRequestExecutor(uint32 id, uint32 processTimeMS)
friend THREAD_RET THREAD_FUNCTION_CALL LongExecutorRun(THREAD_ARG arg)
static bool CreateThread(THREAD_FUNCTION func, void *args, uint32 &newID, uint32 reqID=0)
Create a new native thread and start it immediately.
uint64 GetTimeNow()
Return the current absolute time (µs since year 0) according to the TMC.
int32 GetTimeAgeMS(uint64 t)
Age of a timestamp relative to now, in milliseconds.
THREAD_RET THREAD_FUNCTION_CALL RequestExecutorRun(THREAD_ARG arg)
Thread entry point for the RequestExecutor worker loop.
std::string TextVectorConcat(std::vector< std::string >, const char *sep, bool allowEmpty=true)
Concatenate vector entries with sep.
const char * stristr(const char *str, const char *substr, uint32 len=0)
Case-insensitive strstr.
std::string StringFormat(const char *format,...)
printf into a std::string.
THREAD_RET THREAD_FUNCTION_CALL ShortExecutorRun(THREAD_ARG arg)
THREAD_RET THREAD_FUNCTION_CALL LongExecutorRun(THREAD_ARG arg)
Notification of a connection lifecycle change (connect, disconnect, buffer state.....
uint8 type
NETWORKEVENT_* event type.
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).
void clear()
Reset to the unconfigured/disconnected state.