![]() |
botsense
3.2.0
RoadNarrows Client-Server Proxied Services Framework
|
BotSense bsProxy service threads. More...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/new.h"
#include "rnr/hash.h"
#include "rnr/dlistvoid.h"
#include "rnr/path.h"
#include "botsense/BotSense.h"
#include "botsense/libBotSense.h"
#include "botsense/bsProxyMsgs.h"
#include "bsProxy.h"
Go to the source code of this file.
Macros | |
#define | BSPROXY_TH_HASH_MIN 8 |
minimum hash table size | |
#define | BSPROXY_TH_HASH_MAX 256 |
maximum hash table size | |
#define | BSPROXY_TH_MAX_QUEUE_SIZE 8 |
request queue maximum depth | |
#define | BSPROXY_TH_SERVER_URI "#SERVER" |
server "URI" | |
#define | BSPROXY_TH_LOG_ERROR(devuri, ecode, efmt, ...) |
Log Proxy Server Thread Error. More... | |
Functions | |
static int | ThQReqCmp (const void *pData1, const void *pData2) |
Queue callback to compare two queue entries. More... | |
static void | ThQReqDelete (void *pData) |
Queue callback to delete queue entry. More... | |
static BsProxyThCtl_T * | ThCtlBlkNew (const char *sDevUri) |
Allocate new service thread control block. More... | |
static void | ThCtlBlkDelete (BsProxyThCtl_T *pThCtl) |
Delete service thread control block. More... | |
static void | ThGlobalLock () |
Lock global mutual exclusion. | |
static void | ThGlobalUnlock () |
Unlock global mutual exclusion. | |
static bool_t | ThGlobalTryLock () |
Try to lock global mutual exclusion. More... | |
static void | ThSyncLock (BsProxyThCtl_T *pThCtl) |
Lock service thread's mutual exclusion. More... | |
static void | ThSyncUnlock (BsProxyThCtl_T *pThCtl) |
Unlock service thread's mutual exclusion. More... | |
static void | ThSyncSignal (BsProxyThCtl_T *pThCtl) |
Signal service thread's condition. More... | |
static void | ThSyncWait (BsProxyThCtl_T *pThCtl) |
Wait on service thread's condition. More... | |
static void | ThHashDeleteData (void *sDevUri, void *pThCtl) |
Delete hash node data callback. More... | |
static int | ThHashAdd (const char *sDevUri, BsProxyThCtl_T *pThCtl) |
Add the module data to the i/f module hash table. More... | |
static void | ThHashDelete (BsProxyThCtl_T *pThCtl) |
Remove and delete the module data from the i/f module hash table. More... | |
static BsProxyThCtl_T * | ThHashGetCtl (const char *sDevUri) |
Get the interface module's i/f from the module hash table. More... | |
static void | ThSyncRun (BsProxyThCtl_T *pThCtl) |
Command service thread to run. More... | |
static void | ThSyncExit (BsProxyThCtl_T *pThCtl) |
Command service thread to exit. More... | |
static void | ThSyncWaitForRun (BsProxyThCtl_T *pThCtl) |
Wait indefinitely until state change. More... | |
static int | ThDevRequest (BsProxyClientHnd_T hndClient, BsVConnHnd_T hndVConn, BsTid_T uTid, BsMsgId_T uMsgId, byte_t bufReq[], size_t uReqLen) |
Device service thread request handler. More... | |
static void * | ThServiceThread (void *pThArg) |
The service thread. More... | |
BsProxyThReq_T * | ThReqNew (BsProxyClientHnd_T hndClient, BsVConnHnd_T hndVConn, BsTid_T uTid, BsMsgId_T uMsgId, byte_t bufReq[], size_t uReqLen) |
Allocate and initalized a new service thread request. More... | |
void | ThReqDelete (BsProxyThReq_T *pThReq) |
Delete service thread request. More... | |
int | ThQueue (BsProxyThCtl_T *pThCtl, BsProxyClientHnd_T hndClient, BsVConnHnd_T hndVConn, BsTid_T uTid, BsMsgId_T uMsgId, byte_t bufReq[], size_t uReqLen) |
Queue a request for the given service thread. More... | |
BsProxyThReq_T * | ThDequeue (BsProxyThCtl_T *pThCtl) |
Dequeue a request for the given service thread. More... | |
void | ThOneTimeInit () |
The service thread one-time global initialization. | |
char * | ThNewDevUri (const char *sDevName) |
Convert the device name to a quasi Uniform Resource Id. More... | |
BsProxyThCtl_T * | ThCreateDevThread (const char *sDevUri) |
Create a device service thread. More... | |
BsProxyThCtl_T * | ThCreateServerThread () |
Create the special server service thread. More... | |
void | ThDestroyThread (BsProxyThCtl_T *pThCtl) |
Destroy service thread. More... | |
Variables | |
static pthread_mutex_t | BsThGlobalMutex |
bsProxy module operations mutex | |
static hash_t * | BsThHashTbl |
proxied device hash table | |
BotSense bsProxy service threads.
A Sevice thread provides a separate execution context to service client request-response exchanges. There are two types of service threads:
Server Thread | 1 thread to service server-terminated requests. |
Device Thread | N device threads, one attached to each URI unique, opened device. |
Definition in file bsProxyThread.c.
#define BSPROXY_TH_LOG_ERROR | ( | devuri, | |
ecode, | |||
efmt, | |||
... | |||
) |
Log Proxy Server Thread Error.
devuri | Device URI string. |
ecode |
Definition at line 101 of file bsProxyThread.c.
Referenced by ThCreateServerThread(), ThHashAdd(), ThQueue(), and ThServiceThread().
BsProxyThCtl_T* ThCreateDevThread | ( | const char * | sDevUri | ) |
Create a device service thread.
The actual thread is only created if it does not already exists. The internal reference count is incremented to keep track of the users of this thread.
sDevUri | Device URI. |
Definition at line 756 of file bsProxyThread.c.
References BsProxyThStateInit, _bsproxy_th_ctl::m_eState, _bsproxy_th_ctl::m_fnRequest, _bsproxy_th_ctl::m_sDevUri, _bsproxy_th_ctl::m_thread, _bsproxy_th_ctl::m_uRefCnt, ThCtlBlkDelete(), ThCtlBlkNew(), ThDevRequest(), ThGlobalLock(), ThGlobalUnlock(), ThHashAdd(), ThHashDelete(), ThHashGetCtl(), ThServiceThread(), and ThSyncRun().
Referenced by ClientSetTraceState(), and VConnOpenDev().
BsProxyThCtl_T* ThCreateServerThread | ( | ) |
Create the special server service thread.
There is only one server service thread per server. The server thread processes all server terminated requests.
Definition at line 837 of file bsProxyThread.c.
References BS_ECODE_INTERNAL, BSPROXY_TH_LOG_ERROR, BSPROXY_TH_SERVER_URI, BsProxyThStateInit, _bsproxy_th_ctl::m_eState, _bsproxy_th_ctl::m_fnRequest, _bsproxy_th_ctl::m_thread, _bsproxy_th_ctl::m_uRefCnt, ServerRequest(), ThCtlBlkDelete(), ThCtlBlkNew(), ThGlobalLock(), ThGlobalUnlock(), ThHashAdd(), ThHashDelete(), ThHashGetCtl(), ThServiceThread(), and ThSyncRun().
Referenced by ClientSetTraceState(), and VConnOpenServer().
|
static |
Delete service thread control block.
pThCtl | Pointer to thread control block. |
Definition at line 173 of file bsProxyThread.c.
References _bsproxy_th_ctl::m_condSync, _bsproxy_th_ctl::m_mutexSync, _bsproxy_th_ctl::m_queue, and _bsproxy_th_ctl::m_sDevUri.
Referenced by ThCreateDevThread(), ThCreateServerThread(), and ThHashDeleteData().
|
static |
Allocate new service thread control block.
sDevUri | Device URI. |
Definition at line 150 of file bsProxyThread.c.
References BsProxyThStateUninit, _bsproxy_th_ctl::m_condSync, _bsproxy_th_ctl::m_eState, _bsproxy_th_ctl::m_mutexSync, _bsproxy_th_ctl::m_queue, _bsproxy_th_ctl::m_sDevUri, _bsproxy_th_ctl::m_uRefCnt, ThQReqCmp(), and ThQReqDelete().
Referenced by ThCreateDevThread(), and ThCreateServerThread().
BsProxyThReq_T* ThDequeue | ( | BsProxyThCtl_T * | pThCtl | ) |
Dequeue a request for the given service thread.
The service thread will block if no queued request is present.
Definition at line 675 of file bsProxyThread.c.
References BsProxyThStateRunning, _bsproxy_th_ctl::m_eState, _bsproxy_th_ctl::m_queue, ThSyncLock(), ThSyncUnlock(), and ThSyncWait().
Referenced by ClientSetTraceState(), and ThServiceThread().
void ThDestroyThread | ( | BsProxyThCtl_T * | pThCtl | ) |
Destroy service thread.
The thread's reference count is decremented. If the count is zero, then the thread is actually destroyed with all queued requests deleted. The calling thread is suspended until the target service thread terminates.
pThCtl | Service thread control block. |
Definition at line 921 of file bsProxyThread.c.
References _bsproxy_th_ctl::m_sDevUri, _bsproxy_th_ctl::m_thread, _bsproxy_th_ctl::m_uRefCnt, ThGlobalLock(), ThGlobalUnlock(), ThHashDelete(), and ThSyncExit().
Referenced by ClientSetTraceState(), VConnClose(), and VConnOpenDev().
|
static |
Device service thread request handler.
hndClient |
Definition at line 462 of file bsProxyThread.c.
References BS_ECODE_NO_VCONN, BSPROXY_LOG_ERROR, BSPROXY_SEND_ERROR_RSP, BsProxyModIF_T::m_fnModRequest, BsProxyVConn_T::m_pModIF, VConnAcquire(), and VConnRelease().
Referenced by ThCreateDevThread().
|
inlinestatic |
Try to lock global mutual exclusion.
Definition at line 234 of file bsProxyThread.c.
References BsThGlobalMutex.
|
static |
Add the module data to the i/f module hash table.
sDevUri | Allocated Proxied device hash table key. |
pThCtl | Allocated service thread control. |
Definition at line 334 of file bsProxyThread.c.
References BS_ECODE_NO_RSRC, BS_OK, BSPROXY_TH_LOG_ERROR, BsThHashTbl, and ThHashDeleteData().
Referenced by ThCreateDevThread(), and ThCreateServerThread().
|
static |
Remove and delete the module data from the i/f module hash table.
pThCtl | Allocated service thread control. |
Definition at line 358 of file bsProxyThread.c.
References BsThHashTbl, and _bsproxy_th_ctl::m_sDevUri.
Referenced by ThCreateDevThread(), ThCreateServerThread(), and ThDestroyThread().
|
static |
Delete hash node data callback.
Both the key and value are dynamically allocated.
sDevUri | Proxied device hash table key. |
pThCtl | Service thread control. |
Definition at line 320 of file bsProxyThread.c.
References ThCtlBlkDelete().
Referenced by ThHashAdd(), and ThOneTimeInit().
|
static |
Get the interface module's i/f from the module hash table.
sDevUri | Proxied device hash table key. |
Definition at line 378 of file bsProxyThread.c.
References BsThHashTbl.
Referenced by ThCreateDevThread(), and ThCreateServerThread().
char* ThNewDevUri | ( | const char * | sDevName | ) |
Convert the device name to a quasi Uniform Resource Id.
sDevName | Device path name. |
Definition at line 737 of file bsProxyThread.c.
Referenced by ClientSetTraceState(), and VConnOpenDev().
|
static |
Queue callback to compare two queue entries.
pData1 | Pointer to queue entry 1. |
pData2 | Pointer to queue entry 2. |
Definition at line 121 of file bsProxyThread.c.
References BSPROXY_MAKE_MSGUID, _bsproxy_th_req::m_hndVConn, and _bsproxy_th_req::m_uMsgId.
Referenced by ThCtlBlkNew().
|
static |
Queue callback to delete queue entry.
pData | Pointer to queue entry. |
Definition at line 138 of file bsProxyThread.c.
References ThReqDelete().
Referenced by ThCtlBlkNew().
int ThQueue | ( | BsProxyThCtl_T * | pThCtl, |
BsProxyClientHnd_T | hndClient, | ||
BsVConnHnd_T | hndVConn, | ||
BsTid_T | uTid, | ||
BsMsgId_T | uMsgId, | ||
byte_t | bufReq[], | ||
size_t | uReqLen | ||
) |
Queue a request for the given service thread.
pThCtl | Service thread control block. |
hndClient | Client handle. |
hndVConn | Virtual connection handle. |
uTid | Request-Response transaction id. |
uMsgId | Request message id. |
bufReq | Allocated packed request message body. |
uReqLen | Length of request in buffer (number of bytes). |
Definition at line 626 of file bsProxyThread.c.
References BS_ECODE_NO_RSRC, BS_OK, BSPROXY_TH_LOG_ERROR, BSPROXY_TH_MAX_QUEUE_SIZE, _bsproxy_th_ctl::m_queue, _bsproxy_th_ctl::m_sDevUri, ServerHasName(), ThReqNew(), ThSyncLock(), ThSyncSignal(), and ThSyncUnlock().
Referenced by BsProxyDispatch(), and ClientSetTraceState().
void ThReqDelete | ( | BsProxyThReq_T * | pThReq | ) |
Delete service thread request.
pThReq | Service thread request. |
Definition at line 601 of file bsProxyThread.c.
References _bsproxy_th_req::m_bufReq.
Referenced by ClientSetTraceState(), ThQReqDelete(), and ThServiceThread().
BsProxyThReq_T* ThReqNew | ( | BsProxyClientHnd_T | hndClient, |
BsVConnHnd_T | hndVConn, | ||
BsTid_T | uTid, | ||
BsMsgId_T | uMsgId, | ||
byte_t | bufReq[], | ||
size_t | uReqLen | ||
) |
Allocate and initalized a new service thread request.
hndClient | Client handle. |
hndVConn | Virtual connection handle. |
uTid | Request-Response transaction id. |
uMsgId | Request message id. |
bufReq | Allocated packed request message body. |
uReqLen | Length of request in buffer (number of bytes). |
Definition at line 577 of file bsProxyThread.c.
References _bsproxy_th_req::m_bufReq, _bsproxy_th_req::m_hndClient, _bsproxy_th_req::m_hndVConn, _bsproxy_th_req::m_uMsgId, _bsproxy_th_req::m_uReqLen, and _bsproxy_th_req::m_uTid.
Referenced by ClientSetTraceState(), and ThQueue().
|
static |
The service thread.
pThArg | Thread argument. |
Definition at line 512 of file bsProxyThread.c.
References BS_ECODE_NO_EXEC, BSPROXY_TH_LOG_ERROR, BsProxyThStateExit, _bsproxy_th_req::m_bufReq, _bsproxy_th_ctl::m_eState, _bsproxy_th_ctl::m_fnRequest, _bsproxy_th_req::m_hndClient, _bsproxy_th_req::m_hndVConn, _bsproxy_th_ctl::m_sDevUri, _bsproxy_th_req::m_uMsgId, _bsproxy_th_req::m_uReqLen, _bsproxy_th_req::m_uTid, ThDequeue(), ThReqDelete(), and ThSyncWaitForRun().
Referenced by ThCreateDevThread(), and ThCreateServerThread().
|
static |
Command service thread to exit.
pThCtl | Service thread control block. |
Definition at line 421 of file bsProxyThread.c.
References BsProxyThStateExit, _bsproxy_th_ctl::m_eState, ThSyncLock(), ThSyncSignal(), and ThSyncUnlock().
Referenced by ThDestroyThread().
|
inlinestatic |
Lock service thread's mutual exclusion.
pThCtl | Service thread control. |
Definition at line 244 of file bsProxyThread.c.
References _bsproxy_th_ctl::m_mutexSync, and _bsproxy_th_ctl::m_sDevUri.
Referenced by ThDequeue(), ThQueue(), ThSyncExit(), ThSyncRun(), and ThSyncWaitForRun().
|
static |
Command service thread to run.
pThCtl | Service thread control block. |
Definition at line 405 of file bsProxyThread.c.
References BsProxyThStateRunning, _bsproxy_th_ctl::m_eState, ThSyncLock(), ThSyncSignal(), and ThSyncUnlock().
Referenced by ThCreateDevThread(), and ThCreateServerThread().
|
inlinestatic |
Signal service thread's condition.
pThCtl | Service thread control. |
Definition at line 278 of file bsProxyThread.c.
References _bsproxy_th_ctl::m_condSync, and _bsproxy_th_ctl::m_sDevUri.
Referenced by ThQueue(), ThSyncExit(), and ThSyncRun().
|
inlinestatic |
Unlock service thread's mutual exclusion.
pThCtl | Service thread control. |
Definition at line 261 of file bsProxyThread.c.
References _bsproxy_th_ctl::m_mutexSync, and _bsproxy_th_ctl::m_sDevUri.
Referenced by ThDequeue(), ThQueue(), ThSyncExit(), ThSyncRun(), and ThSyncWaitForRun().
|
inlinestatic |
Wait on service thread's condition.
pThCtl | Service thread control. |
Definition at line 295 of file bsProxyThread.c.
References _bsproxy_th_ctl::m_condSync, _bsproxy_th_ctl::m_mutexSync, and _bsproxy_th_ctl::m_sDevUri.
Referenced by ThDequeue(), and ThSyncWaitForRun().
|
static |
Wait indefinitely until state change.
pThCtl | Service thread control block. |
Definition at line 437 of file bsProxyThread.c.
References BsProxyThStateInit, _bsproxy_th_ctl::m_eState, ThSyncLock(), ThSyncUnlock(), and ThSyncWait().
Referenced by ThServiceThread().