botsense  3.2.0
RoadNarrows Client-Server Proxied Services Framework
bsProxy.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: BotSense
4 //
5 // Program: bsProxy
6 //
7 // File: bsProxy.h
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2010-08-23 09:53:11 -0600 (Mon, 23 Aug 2010) $
12  * $Rev: 570 $
13  *
14  * \brief \h_botsense bsProxy IP server declarations.
15  *
16  * \todo Add support for botsense configuration XML files.
17  * \todo Add fork-exec'ing to support multiple interfaces.
18  *
19  * \author Robin Knight (robin.knight@roadnarrows.com)
20  *
21  * \copyright
22  * \h_copy 2007-2017. RoadNarrows LLC.\n
23  * http://www.roadnarrows.com\n
24  * All Rights Reserved
25  */
26 // Permission is hereby granted, without written agreement and without
27 // license or royalty fees, to use, copy, modify, and distribute this
28 // software and its documentation for any purpose, provided that
29 // (1) The above copyright notice and the following two paragraphs
30 // appear in all copies of the source code and (2) redistributions
31 // including binaries reproduces these notices in the supporting
32 // documentation. Substantial modifications to this software may be
33 // copyrighted by their authors and need not follow the licensing terms
34 // described here, provided that the new terms are clearly indicated in
35 // all files where they apply.
36 //
37 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
38 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
39 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
40 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
41 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
42 // THE POSSIBILITY OF SUCH DAMAGE.
43 //
44 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
45 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
46 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
47 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
48 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
49 //
50 ////////////////////////////////////////////////////////////////////////////////
51 
52 #ifndef _BSPROXY_H
53 #define _BSPROXY_H
54 
55 #include <sys/time.h>
56 #include <sys/select.h>
57 #include <stdarg.h>
58 #include <pthread.h>
59 #include <errno.h>
60 
61 #include "rnr/rnrconfig.h"
62 #include "rnr/log.h"
63 #include "rnr/dlistvoid.h"
64 #include "rnr/dliststr.h"
65 #include "rnr/sock.h"
66 #include "rnr/sockset.h"
67 #include "rnr/netmsgs.h"
68 
69 #include "botsense/BotSense.h"
70 #include "botsense/libBotSense.h"
71 #include "botsense/bsProxyModIF.h"
72 #include "botsense/bsProxyMsgs.h"
73 
74 
75 //-----------------------------------------------------------------------------
76 // Data Types and Defines
77 //-----------------------------------------------------------------------------
78 
79 /*!
80  * Compiled Default Tunables.
81  *
82  * \note These can be overriden in the configuration XML file.
83  */
84 /*!
85  * \ingroup bsserver
86  * \defgroup bssrv_tunes Default Tunables
87  *
88  * \h_botsense bsProxy Tunables
89  *
90  * \note These can be overriden in the configuration XML file.
91  *
92  * \{
93  */
94 #define BSPROXY_TUNE_T_RECV 1000000 ///< 1.0s receive time out
95 #define BSPROXY_TUNE_T_SEND 500000 ///< 0.5s send time out
96 #define BSPROXY_TUNE_T_SELECT 5000000 ///< 5.0s send select time out
97 /*! \} */
98 
99 //
100 // Server socket set configuration.
101 //
102 #ifndef FD_SETSIZE
103 #define BSPROXY_FD_SETSIZE 256 ///< server socket set size
104 #else
105 #define BSPROXY_FD_SETSIZE FD_SETSIZE ///< server socket set size
106 #endif
107 
108 //
109 // Forward declarations
110 //
111 typedef struct _bsproxy_th_ctl *BsProxyThCtl_P; ///< service thread control
112 typedef struct _bsproxy_th_req *BsProxyThReq_P; ///< service thread request
113 
114 typedef int BsProxyClientHnd_T; ///< bsProxy server client handle
115 
116 
117 /*!
118  * Interface Module Exported Interface Control Block Structure Type
119  */
120 typedef struct
121 {
122  const char *m_sModUri; ///< module Uniform Resource Id
123  void *m_dllHandle; ///< dynamic library loader handle
124  uint_t m_uRefCnt; ///< vconn reference count for this module
125  BsModInitFunc_P m_fnModInit; ///< module init
126  BsModExitFunc_P m_fnModExit; ///< module exit
127  BsModOpenFunc_P m_fnModOpen; ///< device open
128  BsModCloseFunc_P m_fnModClose; ///< device close
129  BsModRequestFunc_P m_fnModRequest; ///< module request
131  BsModInfoFunc_P m_fnModInfo; ///< static info
133 
134 /*!
135  * \brief Service thread states.
136  */
137 typedef enum
138 {
139  BsProxyThStateUninit, ///< thread is not fully initialized
140  BsProxyThStateInit, ///< thread is initialized
141  BsProxyThStateRunning, ///< thread is running (nominal state)
142  BsProxyThStateExit ///< thread is exiting
144 
145 /*!
146  * \h_botsense Service Thread Control Block Structure Type
147  */
148 typedef struct _bsproxy_th_ctl
149 {
150  const char *m_sDevUri; ///< proxied device URI or server
151  uint_t m_uRefCnt; ///< vconn reference count for this thread
152  BsProxyThState_T m_eState; ///< thread state
153  pthread_mutex_t m_mutexSync; ///< synchronization mutex used by condition
154  pthread_cond_t m_condSync; ///< synchronization condition
155  DListVoid_T *m_queue; ///< thread request queue
156  pthread_t m_thread; ///< the service thread
157 
158  /*!
159  * \brief Service thread request handler.
160  *
161  * \param hndClient \h_botsense client handle.
162  * \param hndVConn Virtual connection handle.
163  * \param uTid Request-response transaction id.
164  * \param uMsgId Request message id.
165  * \param [in] bufReq Packed request message body buffer.
166  * \param uReqLen Length of packed request (number of bytes).
167  *
168  * \copydoc doc_return_std
169  */
170  int (*m_fnRequest)(BsProxyClientHnd_T hndClient,
171  BsVConnHnd_T hndVConn,
172  BsTid_T uTid,
173  BsMsgId_T uMsgId,
174  byte_t bufReq[],
175  size_t uReqLen);
177 
178 /*!
179  * \h_botsense Proxy Client States
180  */
181 typedef enum
182 {
183  BsProxyClientStateInit, ///< initialization state
184  BsProxyClientStateNominal, ///< normal operation
185  BsProxyClientStateReSync, ///< resyncing server with client
186  BsProxyClientStateZombie, ///< disconnected or fatal, not deleted
188 
189 /*!
190  * \h_botsense Proxy Client Control Block Structure
191  */
192 typedef struct
193 {
194  bool_t m_bBusy; ///< client is [not] busy
195  const char *m_sClientName; ///< client's (remote) name
196  Socket_T *m_pClientSock; ///< client opened TCP socket
198  uint_t m_uRefCnt; ///< vconn ref count for this client
199  bool_t m_bServerTrace; ///< do [not] trace server messages
201 
202 /*!
203  * \h_botsense Proxy Server Control Block Structure
204  */
205 typedef struct
206 {
207  const char *m_sServerName; ///< server's name
208  int m_nServerPort; ///< server's listener port
209  SockSet_T *m_pServerSockSet; ///< server socket set
210  Socket_T *m_pServerSockListener; ///< server listener socket
211  uint_t m_uServerRegClientCount; ///< num of reg. clients
213  ///< registered clients
214  pthread_mutex_t m_mutex; ///< server mutex lock
216 
217 /*!
218  * \h_botsense Virtual Connection Structure
219  */
220 typedef struct
221 {
222  bool_t m_bBusy; ///< virtual connection is [not] busy
223  BsVConnHnd_T m_hndVConn; ///< self reference
224  BsProxyClientHnd_T m_hndClient; ///< proxied client handle
225  BsProxyThCtl_T *m_pThCtl; ///< service thread
226  BsProxyModIF_T *m_pModIF; ///< interface module I/F
227  int m_rd; ///< module resource descriptor
229 
230 /*!
231  * \h_botsense Service Thread Request Structure Type
232  */
233 typedef struct _bsproxy_th_req
234 {
235  BsProxyClientHnd_T m_hndClient; ///< proxied client handle
236  BsVConnHnd_T m_hndVConn; ///< virtual connection handle
237  BsTid_T m_uTid; ///< request-response transaction id
238  BsMsgId_T m_uMsgId; ///< message id
239  byte_t *m_bufReq; ///< packed request message buffer
240  size_t m_uReqLen; ///< request buffer length
242 
243 /*!
244  * \h_botsense Server Listen Address, Port Pair Type
245  */
246 typedef struct
247 {
248  char *m_sAddr; ///< listen local address or hostname
249  int m_nPort; ///< listen port number
251 
252 
253 
254 //-----------------------------------------------------------------------------
255 // Data
256 //-----------------------------------------------------------------------------
257 
258 // The server control block.
260 
261 
262 //-----------------------------------------------------------------------------
263 // Logging and Support Macros
264 //-----------------------------------------------------------------------------
265 
266 /*!
267  * \brief Log Proxy Server Warning.
268  *
269  * \param hndClient Client handle.
270  * \param ecode \h_botsense error code.
271  * \param wfmt Warning output format string literal.
272  * \param ... Warning variable arguments.
273  */
274 #define BSPROXY_LOG_WARN(hndClient, ecode, wfmt, ...) \
275  LOGDIAG2("Warning: %s: %s: %s(ecode=%d): " wfmt, \
276  ServerHasName(), ClientHasName(hndClient), \
277  bsStrError(ecode), ((ecode)>=0? (ecode): -(ecode)), \
278  ##__VA_ARGS__)
279 
280 /*!
281  * \brief Log Proxy Server Error.
282  *
283  * \param hndClient Client handle.
284  * \param ecode \h_botsense error code.
285  * \param efmt Error output format string literal.
286  * \param ... Error variable arguments.
287  */
288 #define BSPROXY_LOG_ERROR(hndClient, ecode, efmt, ...) \
289  LOGERROR("%s: %s: %s(ecode=%d): " efmt, \
290  ServerHasName(), ClientHasName(hndClient), \
291  bsStrError(ecode), (ecode>=0? ecode: -ecode), \
292  ##__VA_ARGS__)
293 
294 /*!
295  * \brief Log Proxy Server NetMsgs (Un)Packing Error.
296  *
297  * \param hndClient Client handle.
298  * \param nmecode NetMsgs error code.
299  * \param efmt Error output format string literal.
300  * \param ... Error variable arguments.
301  */
302 #define BSPROXY_LOG_NMERROR(hndClient, nmecode, efmt, ...) \
303  BSPROXY_LOG_ERROR(hndClient, BS_ECODE_BAD_MSG, "%s(nmecode=%d): " efmt, \
304  nmStrError(nmecode), (nmecode>=0? nmecode: -nmecode), \
305  ##__VA_ARGS__)
306 
307 /*!
308  * \brief Log Proxy Server System Error.
309  *
310  * \param hndClient Client handle.
311  * \param efmt Error output format string litteral.
312  * \param ... Error variable arguments.
313  */
314 #define BSPROXY_LOG_SYSERROR(hndClient, efmt, ...) \
315  LOGERROR("%s: %s: %s(ecode=%d): %s(errno=%d)" efmt, \
316  ServerHasName(), ClientHasName(hndClient), \
317  bsStrError(BS_ECODE_SYS), BS_ECODE_SYS, \
318  strerror(errno), errno, ##__VA_ARGS__)
319 
320 /*!
321  * \brief Log \h_botsense Error and Send Error Response.
322  *
323  * \param hndClient Client handle.
324  * \param hndVConn Requestor's virtual connection handle.
325  * \param uTid Request-Response transaction id.
326  * \param ecode \h_botsense error code.
327  * \param efmt Error output format string literal.
328  * \param ... Error variable arguments.
329  */
330 #define BSPROXY_SEND_ERROR_RSP(hndClient, hndVConn, uTid, ecode, efmt, ...) \
331  do \
332  { \
333  BSPROXY_LOG_ERROR(hndClient, ecode, "req_vconn=%u: " efmt, \
334  hndVConn, ##__VA_ARGS__); \
335  ClientSendErrorRsp(hndClient, uTid, ecode, efmt, ##__VA_ARGS__); \
336  } while(0)
337 
338 /*!
339  * \brief Log NetMsgs (Un)Packing Error and Send Error Response.
340  *
341  * \param hndClient Client handle.
342  * \param hndVConn Requestor's virtual connection handle.
343  * \param uTid Request-Response transaction id.
344  * \param nmecode NetMsgs error code.
345  * \param efmt Error output format string literal.
346  * \param ... Error variable arguments.
347  */
348 #define BSPROXY_SEND_NMERROR_RSP(hndClient, hndVConn, uTid, nmecode, efmt, ...) \
349  do \
350  { \
351  BSPROXY_LOG_NMERROR(hndClient, nmecode, "req_vconn=%u: " efmt, \
352  hndVConn, ##__VA_ARGS__); \
353  ClientSendErrorRsp(hndClient, uTid, BS_ECODE_BAD_MSG, efmt, \
354  ##__VA_ARGS__); \
355  } while(0)
356 
357 /*!
358  * \brief Log System Error and Send Error Response.
359  *
360  * \param hndClient Client handle.
361  * \param hndVConn Requestor's virtual connection handle.
362  * \param uTid Request-Response transaction id.
363  * \param efmt Error output format string literal.
364  * \param ... Error variable arguments.
365  */
366 #define BSPROXY_SEND_SYSERROR_RSP(hndClient, hndVConn, uTid, efmt, ...) \
367  do \
368  { \
369  BSPROXY_LOG_SYSERROR(hndClient, "req_vconn=%u: " efmt, \
370  hndVConn, ##__VA_ARGS__); \
371  ClientSendErrorRsp(hndClient, uTid, BS_ECODE_SYS, \
372  "%s(errno=%d): vconn=%u:" efmt, \
373  strerror(errno), errno, hndVConn, ##__VA_ARGS__); \
374  } while(0)
375 
376 
377 /*!
378  * \brief Log client request.
379  *
380  * \param hndClient Client handle.
381  * \param pMsgHdr Request message header.
382  */
383 #ifdef LOG
384 #define BSPROXY_LOG_REQ(hndClient, pMsgHdr) \
385  do \
386  { \
387  if( LOGABLE(LOG_LEVEL_DIAG2) ) \
388  { \
389  LOGDIAG2("%s: %s:", ServerHasName(), ClientHasName(hndClient)); \
390  BsProxyLogMsgHdr("Rx Req", pMsgHdr); \
391  } \
392  } while(0)
393 #else
394 #define BSPROXY_LOG_REQ(hndClient, pMsgHdr)
395 #endif // LOG
396 
397 /*!
398  * \brief Log client request.
399  *
400  * \param hndClient Client handle.
401  * \param pMsgHdr Request message header.
402  */
403 #ifdef LOG
404 #define BSPROXY_LOG_RSP(hndClient, pMsgHdr) \
405  do \
406  { \
407  if( LOGABLE(LOG_LEVEL_DIAG2) ) \
408  { \
409  LOGDIAG2("%s: %s:", ServerHasName(), ClientHasName(hndClient)); \
410  BsProxyLogMsgHdr("Tx Rsp", pMsgHdr); \
411  } \
412  } while(0)
413 #else
414 #define BSPROXY_LOG_RSP(hndClient, pMsgHdr)
415 #endif // LOG
416 
417 /*!
418  * Check if handle is in virtual connection range.
419  *
420  * The special server handle is included.
421  *
422  * \sa bsProxyMsg.h
423  *
424  * \param hndVConn Virtual connection handle.
425  *
426  * \return true or false
427  */
428 #define BSPROXY_CHK_VCONN_HND(hndVConn) \
429  (((int)(hndVConn) >= BSPROXY_VCONN_MIN) && \
430  ((int)(hndVConn) <= BSPROXY_VCONN_MAX))
431 
432 /*!
433  * Check if handle is in virtual connection device module range.
434  *
435  * \sa bsProxyMsg.h
436  *
437  * \param hndVConn Virtual connection handle.
438  *
439  * \return true or false
440  */
441 #define BSPROXY_CHK_MOD_VCONN_HND(hndVConn) \
442  (((int)(hndVConn) >= BSPROXY_VCONN_MOD_MIN) && \
443  ((int)(hndVConn) <= BSPROXY_VCONN_MOD_MAX))
444 
445 
446 //-----------------------------------------------------------------------------
447 // BotSense Utilities
448 //-----------------------------------------------------------------------------
449 
450 /*!
451  * \brief Mark the current time. Resolution is microseconds.
452  *
453  * \param pTvMark Pointer to timeval structure to be populated with
454  * the current system seconds and useconds.
455  */
456 static inline void timer_mark(struct timeval *pTvMark)
457 {
458  if( gettimeofday(pTvMark, NULL) != OK )
459  {
460  LOGSYSERROR("gettimeofday()");
461  timerclear(pTvMark);
462  }
463 }
464 
465 extern uint_t timer_elapsed(struct timeval *pTvMark);
466 
467 /*!
468  * \brief Get the \h_botsense server.
469  *
470  * \note In the future, if bsProxy is expanded to be multi-threaded multi-server
471  * then this call will return the correct server given the caller's context.
472  *
473  * \return Pointer to the server's control block.
474  */
476 {
477  return BsServerCtl;
478 }
479 
480 /*!
481  * \brief Get the \h_botsense server's official name.
482  *
483  * \return Null-terminated server name string.
484  */
485 INLINE_IN_H const char *ServerHasName()
486 {
487  return ServerGet()->m_sServerName;
488 }
489 
490 /*!
491  * \brief Get the \h_botsense server's listening address.
492  *
493  * \return Null-terminated server address string.
494  */
495 INLINE_IN_H const char *ServerHasAddr()
496 {
497  return SocketAttrGetLocalName(ServerGet()->m_pServerSockListener);
498 }
499 
500 /*!
501  * \brief Get the \h_botsense server's listening port.
502  *
503  * \return Port.
504  */
505 INLINE_IN_H int ServerHasPort()
506 {
507  return ServerGet()->m_nServerPort;
508 }
509 
510 /*!
511  * \brief Convert the \h_botsense server client handle to client socket
512  * descriptor.
513  *
514  * \note Currently, the handle and socket descriptor are one and the same. But
515  * this mapping could change in the future.
516  *
517  * \param hndClient \h_botsense client handle.
518  *
519  * \return Client socket descriptor.
520  */
521 INLINE_IN_H int ServerClientHnd2Sd(BsProxyClientHnd_T hndClient)
522 {
523  return (int)hndClient;
524 }
525 
526 /*!
527  * \brief Convert the \h_botsense server client socket descriptor to client
528  * handle.
529  *
530  * \note Currently, the handle and socket descriptor are one and the same. But
531  * this mapping could change in the future.
532  *
533  * \param sd \h_botsense client socket descriptor.
534  *
535  * \return Client handle.
536  */
538 {
539  return (BsProxyClientHnd_T)sd;
540 }
541 
542 /*!
543  * \brief Get the \h_botsense server client.
544  *
545  * \note The client is not locked.
546  *
547  * \param hndClient \h_botsense client handle.
548  *
549  * \return \h_botsense client control block.
550  */
552 {
553  int sd = ServerClientHnd2Sd(hndClient);
554 
555  if( (sd >= 0) && (sd < BSPROXY_FD_SETSIZE) )
556  {
557  return ServerGet()->m_pServerRegClient[sd];
558  }
559  else
560  {
561  return NULL;
562  }
563 }
564 
565 /*!
566  * \brief Get this \h_botsense client official name.
567  *
568  * \warning The client is not locked. Use in a safe context.
569  *
570  * \param pClient \h_botsense client.
571  *
572  * \return Null-terminated client name string.
573  */
574 INLINE_IN_H const char *ClientThisHasName(BsProxyClientCtl_T *pClient)
575 {
576  return pClient != NULL? pClient->m_sClientName: "(none)";
577 }
578 
579 /*!
580  * \brief Get the \h_botsense client official name.
581  *
582  * \warning The client is not locked. Use in a safe context.
583  *
584  * \param hndClient \h_botsense client handle.
585  *
586  * \return Null-terminated client name string.
587  */
588 INLINE_IN_H const char *ClientHasName(BsProxyClientHnd_T hndClient)
589 {
590  return ClientThisHasName(ServerGetClient(hndClient));
591 }
592 
593 /*!
594  * \brief Get the \h_botsense client's server-terminated message trace state.
595  *
596  * \warning The client is not locked. Use in a safe context.
597  *
598  * \param hndClient \h_botsense client handle.
599  *
600  * \return Trace state.
601  */
602 INLINE_IN_H bool_t ClientGetTraceState(BsProxyClientHnd_T hndClient)
603 {
604  BsProxyClientCtl_T *pClient = ServerGetClient(hndClient);
605 
606  return pClient != NULL? pClient->m_bServerTrace: false;
607 }
608 
609 /*!
610  * \brief Set the \h_botsense client's server-terminated message trace state.
611  *
612  * \warning The client is not locked. Use in a safe context.
613  *
614  * \param hndClient \h_botsense client handle.
615  * \param bTrace New trace state.
616  */
617 INLINE_IN_H void ClientSetTraceState(BsProxyClientHnd_T hndClient,
618  bool_t bTrace)
619 {
620  BsProxyClientCtl_T *pClient = ServerGetClient(hndClient);
621 
622  if( pClient != NULL )
623  {
624  pClient->m_bServerTrace = bTrace;
625  }
626 }
627 
628 #ifdef LOG
629 extern void BsProxyLogMsgHdr(const char *sPreface, BsProxyMsgHdr_T *pMsgHdr);
630 #endif // LOG
631 
632 
633 //-----------------------------------------------------------------------------
634 // BotSense Client Prototypes
635 //-----------------------------------------------------------------------------
636 
637 extern void ClientOneTimeInit();
638 
640 
641 extern void ClientRelease(BsProxyClientHnd_T hndClient);
642 
643 extern BsProxyClientCtl_T *ClientNew(Socket_T *pSockClient);
644 
645 extern void ClientDelete(BsProxyClientCtl_T *pClient);
646 
647 extern void ClientSetState(BsProxyClientCtl_T *pClient,
648  BsProxyClientState_T eNewState);
649 
650 extern int ClientRecvReq(BsProxyClientHnd_T hndClient,
651  BsProxyMsgHdr_T *pMsgHdr,
652  byte_t **addrBuf);
653 
654 extern int ClientSendOkRsp(BsProxyClientHnd_T hndClient, BsTid_T uTid);
655 
656 extern int ClientSendErrorRsp(BsProxyClientHnd_T hndClient,
657  BsTid_T uTid,
658  int nECode,
659  const char *sErrFmt,
660  ...);
661 
662 extern int ClientSendVErrorRsp(BsProxyClientHnd_T hndClient,
663  BsTid_T uTid,
664  int nECode,
665  const char *sErrFmt,
666  va_list ap);
667 
668 extern int ClientSendServerRsp(BsProxyClientHnd_T hndClient,
669  BsTid_T uTid,
670  BsProxyMsgId_T uMsgId,
671  void *pMsgRsp);
672 
673 extern int ClientSendRsp(BsProxyClientHnd_T hndClient,
674  BsVConnHnd_T hndVConn,
675  BsTid_T uTid,
676  BsMsgId_T uMsgId,
677  byte_t bufRsp[],
678  size_t uRspSize);
679 
680 
681 //-----------------------------------------------------------------------------
682 // BotSense Virtual Connection Prototypes
683 //-----------------------------------------------------------------------------
684 
685 extern void VConnOneTimeInit();
686 
687 extern BsProxyVConn_T *VConnNew();
688 
689 extern void VConnDelete(BsProxyVConn_T *pVConn);
690 
691 extern BsProxyVConn_T *VConnGet(BsVConnHnd_T hndVConn);
692 
693 extern int VConnOpenServer();
694 
695 extern int VConnOpenDev(BsProxyClientHnd_T hndClient,
696  const char *sDevName,
697  const char *sModName,
698  byte_t argbuf[],
699  size_t uArgLen,
700  bool_t bTrace);
701 
702 extern int VConnClose(BsProxyClientHnd_T hndClient, BsVConnHnd_T hndVConn);
703 
704 extern BsProxyVConn_T *VConnAcquire(BsVConnHnd_T hndVConn);
705 
706 extern void VConnRelease(BsVConnHnd_T hndVConn);
707 
708 
709 //-----------------------------------------------------------------------------
710 // BotSense Interface Module Prototypes
711 //-----------------------------------------------------------------------------
712 
713 extern void ModOneTimeInit(DListStr_T *pDListLibPath);
714 
715 extern char *ModNewModUri(const char *sModName);
716 
717 extern BsProxyModIF_T *ModGetLoadedIF(const char *sModUri);
718 
719 extern BsProxyModIF_T *ModLoad(const char *sModUri);
720 
721 extern void ModUnload(BsProxyModIF_T *pModIF);
722 
723 
724 //-----------------------------------------------------------------------------
725 // BotSense Service Thread Prototypes
726 //-----------------------------------------------------------------------------
727 
728 extern BsProxyThReq_T *ThReqNew(BsProxyClientHnd_T hndClient,
729  BsVConnHnd_T hndVConn,
730  BsTid_T uTid,
731  BsMsgId_T uMsgId,
732  byte_t bufReq[],
733  size_t uReqLen);
734 
735 extern void ThReqDelete(BsProxyThReq_T *pThReq);
736 
737 extern int ThQueue(BsProxyThCtl_T *pThCtl,
738  BsProxyClientHnd_T hndClient,
739  BsVConnHnd_T hndVConn,
740  BsTid_T uTid,
741  BsMsgId_T uMsgId,
742  byte_t bufReq[],
743  size_t uReqLen);
744 
745 extern BsProxyThReq_T *ThDequeue(BsProxyThCtl_T *pThCtl);
746 
747 extern void ThOneTimeInit();
748 
749 extern char *ThNewDevUri(const char *sDevName);
750 
751 extern BsProxyThCtl_T *ThCreateDevThread(const char *sDevUri);
752 
754 
755 extern void ThDestroyThread(BsProxyThCtl_T *pThCtl);
756 
757 
758 //-----------------------------------------------------------------------------
759 // BotSense Server Request Thread Prototypes
760 //-----------------------------------------------------------------------------
761 
762 extern int ServerRequest(BsProxyClientHnd_T hndClient,
763  BsVConnHnd_T hndVConn,
764  BsTid_T uTid,
765  BsMsgId_T uMsgId,
766  byte_t bufReq[],
767  size_t uReqLen);
768 
769 
770 #endif // _BSPROXY_H
int ClientRecvReq(BsProxyClientHnd_T hndClient, BsProxyMsgHdr_T *pMsgHdr, byte_t **addrBuf)
Receive a request message from client.
uint_t BsMsgId_T
client message id type [0-64k].
Definition: BotSense.h:188
struct _bsproxy_th_req * BsProxyThReq_P
service thread request
Definition: bsProxy.h:112
int ClientSendErrorRsp(BsProxyClientHnd_T hndClient, BsTid_T uTid, int nECode, const char *sErrFmt,...)
Send an error response to the client.
BsProxyClientState_T m_eClientState
client state
Definition: bsProxy.h:197
Socket_T * m_pServerSockListener
server listener socket
Definition: bsProxy.h:210
BsModTraceFunc_T * BsModTraceFunc_P
pointer to msg trace func.
Definition: bsProxyModIF.h:612
BsModExitFunc_P m_fnModExit
module exit
Definition: bsProxy.h:126
BsProxyModIF_T * ModGetLoadedIF(const char *sModUri)
Get the defined interface from a previously loaded i/f module.
Definition: bsProxyMod.c:1058
BsProxyClientCtl_T * ClientAcquire(BsProxyClientHnd_T hndClient)
Acquire client, locking it from other threads.
int VConnOpenDev(BsProxyClientHnd_T hndClient, const char *sDevName, const char *sModName, byte_t argbuf[], size_t uArgLen, bool_t bTrace)
Open a virtual device connection.
Definition: bsProxyVConn.c:325
int m_rd
module resource descriptor
Definition: bsProxy.h:227
BotSense client application - bsProxy server-terminated core messages.
uint_t m_uRefCnt
vconn reference count for this thread
Definition: bsProxy.h:151
BsProxyThCtl_T * ThCreateServerThread()
Create the special server service thread.
void VConnRelease(BsVConnHnd_T hndVConn)
Release the locked virtual client.
Definition: bsProxyVConn.c:585
BsModRequestFunc_T * BsModRequestFunc_P
pointer to request func.
Definition: bsProxyModIF.h:601
BsModCloseFunc_T * BsModCloseFunc_P
pointer to close function
Definition: bsProxyModIF.h:581
BsProxyClientCtl_T * m_pServerRegClient[256]
registered clients
Definition: bsProxy.h:212
char * ThNewDevUri(const char *sDevName)
Convert the device name to a quasi Uniform Resource Id.
byte_t * m_bufReq
packed request message buffer
Definition: bsProxy.h:239
uint_t m_uServerRegClientCount
num of reg. clients
Definition: bsProxy.h:211
uint_t BsTid_T
client transaction id type [0-255].
Definition: BotSense.h:172
thread is exiting
Definition: bsProxy.h:142
Socket_T * m_pClientSock
client opened TCP socket
Definition: bsProxy.h:196
BsProxyThCtl_T * ThCreateDevThread(const char *sDevUri)
Create a device service thread.
BsModInfoFunc_T * BsModInfoFunc_P
pointer to mod info function
Definition: bsProxyModIF.h:621
BsProxyThCtl_T * m_pThCtl
service thread
Definition: bsProxy.h:225
void VConnDelete(BsProxyVConn_T *pVConn)
Delete allocated virtual connection entry.
Definition: bsProxyVConn.c:220
uint_t timer_elapsed(struct timeval *pTvMark)
Calculate the elapsed time between the given time mark and this call.
Definition: bsProxyUtils.c:79
void ClientRelease(BsProxyClientHnd_T hndClient)
Release the locked client.
INLINE_IN_H int ServerHasPort()
Get the <b><i>BotSense</i></b> server&#39;s listening port.
Definition: bsProxy.h:505
bool_t m_bBusy
client is [not] busy
Definition: bsProxy.h:194
BsModExitFunc_T * BsModExitFunc_P
pointer to exit function
Definition: bsProxyModIF.h:541
#define BSPROXY_FD_SETSIZE
server socket set size
Definition: bsProxy.h:103
int VConnOpenServer()
Open special server virtual connection.
Definition: bsProxyVConn.c:265
INLINE_IN_H const char * ServerHasName()
Get the <b><i>BotSense</i></b> server&#39;s official name.
Definition: bsProxy.h:485
INLINE_IN_H BsProxyServerCtl_T * ServerGet()
Get the <b><i>BotSense</i></b> server.
Definition: bsProxy.h:475
pthread_t m_thread
the service thread
Definition: bsProxy.h:156
bool_t m_bServerTrace
do [not] trace server messages
Definition: bsProxy.h:199
BsModCloseFunc_P m_fnModClose
device close
Definition: bsProxy.h:128
struct _bsproxy_th_ctl BsProxyThCtl_T
int BsProxyClientHnd_T
bsProxy server client handle
Definition: bsProxy.h:114
size_t m_uReqLen
request buffer length
Definition: bsProxy.h:240
char * ModNewModUri(const char *sModName)
Convert the module name to a quasi Uniform Resource Id.
Definition: bsProxyMod.c:1042
int VConnClose(BsProxyClientHnd_T hndClient, BsVConnHnd_T hndVConn)
Close the virtual connection.
Definition: bsProxyVConn.c:478
INLINE_IN_H BsProxyClientHnd_T ServerClientSd2Hnd(int sd)
Convert the <b><i>BotSense</i></b> server client socket descriptor to client handle.
Definition: bsProxy.h:537
void VConnOneTimeInit()
The bsProxy virtual connections one-time initialization.
Definition: bsProxyVConn.c:165
INLINE_IN_H int ServerClientHnd2Sd(BsProxyClientHnd_T hndClient)
Convert the <b><i>BotSense</i></b> server client handle to client socket descriptor.
Definition: bsProxy.h:521
BsProxyClientHnd_T m_hndClient
proxied client handle
Definition: bsProxy.h:235
BsModInfoFunc_P m_fnModInfo
static info
Definition: bsProxy.h:131
int ClientSendOkRsp(BsProxyClientHnd_T hndClient, BsTid_T uTid)
Send an ok response to the client.
const char * m_sServerName
server&#39;s name
Definition: bsProxy.h:207
pthread_mutex_t m_mutexSync
synchronization mutex used by condition
Definition: bsProxy.h:153
void ThDestroyThread(BsProxyThCtl_T *pThCtl)
Destroy service thread.
void BsProxyLogMsgHdr(const char *sPreface, BsProxyMsgHdr_T *pMsgHdr)
Log message header.
Definition: bsProxyUtils.c:108
<b><i>BotSense</i></b> client library declarations.
pthread_mutex_t m_mutex
server mutex lock
Definition: bsProxy.h:214
pthread_cond_t m_condSync
synchronization condition
Definition: bsProxy.h:154
BsModOpenFunc_T * BsModOpenFunc_P
pointer to open function
Definition: bsProxyModIF.h:568
INLINE_IN_H bool_t ClientGetTraceState(BsProxyClientHnd_T hndClient)
Get the <b><i>BotSense</i></b> client&#39;s server-terminated message trace state.
Definition: bsProxy.h:602
BsProxyVConn_T * VConnAcquire(BsVConnHnd_T hndVConn)
Acquire virtual connection, locking it from other threads.
Definition: bsProxyVConn.c:552
disconnected or fatal, not deleted
Definition: bsProxy.h:186
SockSet_T * m_pServerSockSet
server socket set
Definition: bsProxy.h:209
DListVoid_T * m_queue
thread request queue
Definition: bsProxy.h:155
BsProxyClientHnd_T m_hndClient
proxied client handle
Definition: bsProxy.h:224
BsProxyModIF_T * ModLoad(const char *sModUri)
Load the interface module.
Definition: bsProxyMod.c:1089
BsModTraceFunc_P m_fnModTrace
tracing
Definition: bsProxy.h:130
INLINE_IN_H BsProxyClientCtl_T * ServerGetClient(BsProxyClientHnd_T hndClient)
Get the <b><i>BotSense</i></b> server client.
Definition: bsProxy.h:551
uint_t m_uRefCnt
vconn ref count for this client
Definition: bsProxy.h:198
BsProxyThState_T
Service thread states.
Definition: bsProxy.h:137
thread is not fully initialized
Definition: bsProxy.h:139
BsProxyVConn_T * VConnGet(BsVConnHnd_T hndVConn)
Get the virtual connection entry associated with the handle.
Definition: bsProxyVConn.c:248
void ClientSetState(BsProxyClientCtl_T *pClient, BsProxyClientState_T eNewState)
Set client&#39;s state.
initialization state
Definition: bsProxy.h:183
BsProxyThReq_T * ThDequeue(BsProxyThCtl_T *pThCtl)
Dequeue a request for the given service thread.
uint_t m_uRefCnt
vconn reference count for this module
Definition: bsProxy.h:124
const char * m_sModUri
module Uniform Resource Id
Definition: bsProxy.h:122
void ClientOneTimeInit()
The <b><i>BotSense</i></b> bsProxy server one-time client control initialization.
BsVConnHnd_T m_hndVConn
virtual connection handle
Definition: bsProxy.h:236
BsProxyMsgId_T
Definition: bsProxyMsgs.h:35
BotSense Proxy Message Header Structure.
Definition: BotSense.h:278
BsVConnHnd_T m_hndVConn
self reference
Definition: bsProxy.h:223
normal operation
Definition: bsProxy.h:184
bool_t m_bBusy
virtual connection is [not] busy
Definition: bsProxy.h:222
thread is running (nominal state)
Definition: bsProxy.h:141
<b><i>BotSense</i></b> bsProxy Dynamically Linked Library module interface.
struct _bsproxy_th_req BsProxyThReq_T
BsProxyClientState_T
Definition: bsProxy.h:181
thread is initialized
Definition: bsProxy.h:140
INLINE_IN_H const char * ServerHasAddr()
Get the <b><i>BotSense</i></b> server&#39;s listening address.
Definition: bsProxy.h:495
BsModInitFunc_P m_fnModInit
module init
Definition: bsProxy.h:125
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.
int(* m_fnRequest)(BsProxyClientHnd_T hndClient, BsVConnHnd_T hndVConn, BsTid_T uTid, BsMsgId_T uMsgId, byte_t bufReq[], size_t uReqLen)
Service thread request handler.
Definition: bsProxy.h:170
BsProxyClientCtl_T * ClientNew(Socket_T *pSockClient)
Create new client control structure.
int m_nPort
listen port number
Definition: bsProxy.h:249
void ThReqDelete(BsProxyThReq_T *pThReq)
Delete service thread request.
static void timer_mark(struct timeval *pTvMark)
Mark the current time. Resolution is microseconds.
Definition: bsProxy.h:456
int ClientSendVErrorRsp(BsProxyClientHnd_T hndClient, BsTid_T uTid, int nECode, const char *sErrFmt, va_list ap)
Send va_list error response to the client.
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.
BsProxyThState_T m_eState
thread state
Definition: bsProxy.h:152
void ThOneTimeInit()
The service thread one-time global initialization.
const char * m_sClientName
client&#39;s (remote) name
Definition: bsProxy.h:195
INLINE_IN_H void ClientSetTraceState(BsProxyClientHnd_T hndClient, bool_t bTrace)
Set the <b><i>BotSense</i></b> client&#39;s server-terminated message trace state.
Definition: bsProxy.h:617
BsMsgId_T m_uMsgId
message id
Definition: bsProxy.h:238
int ClientSendRsp(BsProxyClientHnd_T hndClient, BsVConnHnd_T hndVConn, BsTid_T uTid, BsMsgId_T uMsgId, byte_t bufRsp[], size_t uRspSize)
Send module-specific response to the client.
struct _bsproxy_th_ctl * BsProxyThCtl_P
service thread control
Definition: bsProxy.h:111
void ModOneTimeInit(DListStr_T *pDListLibPath)
The bsProxy interface module handling one-time initialization.
Definition: bsProxyMod.c:1013
resyncing server with client
Definition: bsProxy.h:185
BsModOpenFunc_P m_fnModOpen
device open
Definition: bsProxy.h:127
BsModInitFunc_T * BsModInitFunc_P
pointer to init function
Definition: bsProxyModIF.h:530
INLINE_IN_H const char * ClientHasName(BsProxyClientHnd_T hndClient)
Get the <b><i>BotSense</i></b> client official name.
Definition: bsProxy.h:588
const char * m_sDevUri
proxied device URI or server
Definition: bsProxy.h:150
void ClientDelete(BsProxyClientCtl_T *pClient)
Delete a client with the server.
BsProxyModIF_T * m_pModIF
interface module I/F
Definition: bsProxy.h:226
void * m_dllHandle
dynamic library loader handle
Definition: bsProxy.h:123
BsTid_T m_uTid
request-response transaction id
Definition: bsProxy.h:237
<b><i>BotSense</i></b> package top-level, unifying header declarations.
BsProxyVConn_T * VConnNew()
Create a new, allocated virtual connection entry.
Definition: bsProxyVConn.c:183
int BsVConnHnd_T
virtual connection handle type
Definition: BotSense.h:151
int ClientSendServerRsp(BsProxyClientHnd_T hndClient, BsTid_T uTid, BsProxyMsgId_T uMsgId, void *pMsgRsp)
Send a server-terminated response message to client.
void ModUnload(BsProxyModIF_T *pModIF)
Unload the interface module.
Definition: bsProxyMod.c:1191
BsProxyServerCtl_T * BsServerCtl
The server control block.
Definition: bsProxy.c:831
char * m_sAddr
listen local address or hostname
Definition: bsProxy.h:248
int ServerRequest(BsProxyClientHnd_T hndClient, BsVConnHnd_T hndVConn, BsTid_T uTid, BsMsgId_T uMsgId, byte_t bufReq[], size_t uReqLen)
Server service thread request handler.
int m_nServerPort
server&#39;s listener port
Definition: bsProxy.h:208
INLINE_IN_H const char * ClientThisHasName(BsProxyClientCtl_T *pClient)
Get this <b><i>BotSense</i></b> client official name.
Definition: bsProxy.h:574
BsModRequestFunc_P m_fnModRequest
module request
Definition: bsProxy.h:129