botsense  3.2.0
RoadNarrows Client-Server Proxied Services Framework
bsProxyMod.c File Reference

Proxied interface module management operations. More...

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <dlfcn.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/new.h"
#include "rnr/hash.h"
#include "rnr/dliststr.h"
#include "rnr/path.h"
#include "botsense/BotSense.h"
#include "botsense/bsProxyModIF.h"
#include "botsense/bsProxyMsgs.h"
#include "bsProxy.h"

Go to the source code of this file.

Macros

#define DLL_EXT   ".so"
 standard dll library file name extension
 
#define BS_AUX_LIBDIR   "/prj/lib"
 additional bsProxy library directory
 
#define BS_PLUGIN_DIR   "."
 bsProxy module plugin subdirectory
 
#define BSPROXY_MOD_HASH_MIN   32
 minimum hash table size
 
#define BSPROXY_MOD_HASH_MAX   256
 maximum hash table size
 
#define BSPROXY_MOD_LOG_ERROR(moduri, ecode, efmt, ...)
 Log Proxy Server Module Error. More...
 

Functions

static void ModLock ()
 Lock module's global mutual exclusion.
 
static void ModUnlock ()
 Unlock module's global mutual exclusion.
 
static bool_t ModTryLock ()
 Try to lock module's global mutual exclusion. More...
 
static void ModDllInit (DListStr_T *pDListLibPath)
 
static char * ModDllNewCanonicalName (const char *sModName)
 Make DLL canonical file name. More...
 
static void * ModDllOpen (const char *sModUri)
 Dynamically load bsProxy server compatible interface module library. More...
 
static void ModDllClose (const char *sModUri, void *dllHandle)
 Unload the dynamic library from the application. More...
 
static void * ModDllSym (BsProxyModIF_T *pModIF, const char *sSym)
 Get the address of where the module symbol is loaded into memory. More...
 
static void ModDllDeleteIF (BsProxyModIF_T *pModIF)
 Delete module interface block. More...
 
static BsProxyModIF_TModDllNewIF (const char *sModUri, void *dllHandle)
 Allocate an attached exported module interface block. More...
 
static void ModHashDeleteData (void *sModUri, void *pModIF)
 Delete hash node data callback. More...
 
static int ModHashAdd (const char *sModUri, BsProxyModIF_T *pModIF)
 Add the module data to the i/f module hash table. More...
 
static void ModHashDelete (BsProxyModIF_T *pModIF)
 Remove and delete the module data from the i/f module hash table. More...
 
static BsProxyModIF_TModHashGetIF (const char *sModUri)
 Get the interface module's i/f from the module hash table. More...
 
void ModCbSendRsp (BsVConnHnd_T hndVConn, BsTid_T uTid, BsMsgId_T uMsgId, byte_t bufRsp[], size_t uRspLen)
 Send module-specific repsonse callback function. More...
 
void ModCbSendOkRsp (BsVConnHnd_T hndVConn, BsTid_T uTid)
 Send generic ok repsonse callback function. More...
 
void ModCbSendErrorRsp (BsVConnHnd_T hndVConn, BsTid_T uTid, int nECode, const char *sErrFmt,...)
 Send generic error repsonse callback function. More...
 
BsModRsrcTbl_TModCbRsrcTblNew (int nMaxResources)
 Allocatae a new module resource table of fixed size. More...
 
void ModCbRsrcTblDelete (BsModRsrcTbl_T *pRsrcTbl)
 Delete an allocated resource table. More...
 
int ModCbRsrcAdd (BsModRsrcTbl_T *pRsrcTbl, BsVConnHnd_T hndVConn, void *pRsrc)
 Add a new resource to the resource table. More...
 
void * ModCbRsrcRemove (BsModRsrcTbl_T *pRsrcTbl, BsVConnHnd_T hndVConn)
 Remove a resource from the resource table. More...
 
const char * ModCbGetDevUri (BsVConnHnd_T hndVConn, char dest[], size_t n)
 Copy the device URI associated with the virtual connection. More...
 
void ModCbIterDelete (BsModIter_T *pIter)
 Delete the module iterator. More...
 
BsModIter_TModCbIterNext (BsModIter_T *pIter)
 Get the next set of information given the iterator state. More...
 
BsModIter_TModCbIterFirst (BsModIterOver_T eOver, const char *sPattern)
 Start an iterator over the virtual connections matching the given pattern. More...
 
void ModOneTimeInit (DListStr_T *pDListLibPath)
 The bsProxy interface module handling one-time initialization. More...
 
char * ModNewModUri (const char *sModName)
 Convert the module name to a quasi Uniform Resource Id. More...
 
BsProxyModIF_TModGetLoadedIF (const char *sModUri)
 Get the defined interface from a previously loaded i/f module. More...
 
BsProxyModIF_TModLoad (const char *sModUri)
 Load the interface module. More...
 
void ModUnload (BsProxyModIF_T *pModIF)
 Unload the interface module. More...
 

Variables

static pthread_mutex_t BsModMutex
 bsProxy module operations mutex
 
static hash_t * BsModHashTbl
 i/f module hash table
 
static DListStr_T * BsModDllPaths
 module search paths
 
static BsModProxyCb_T BsModProxyCallbacks
 Interface module callbacks to bsProxy server services. More...
 

Detailed Description

Proxied interface module management operations.

LastChangedDate
2010-08-20 11:36:38 -0600 (Fri, 20 Aug 2010)
Rev
568
See also
http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Definition in file bsProxyMod.c.

Macro Definition Documentation

#define BSPROXY_MOD_LOG_ERROR (   moduri,
  ecode,
  efmt,
  ... 
)
Value:
LOGERROR("%s: \"%s\": %s(ecode=%d): " efmt, \
ServerHasName(), moduri, \
bsStrError(ecode), (ecode>=0? ecode: -ecode), \
##__VA_ARGS__)
INLINE_IN_H const char * ServerHasName()
Get the <b><i>BotSense</i></b> server&#39;s official name.
Definition: bsProxy.h:485

Log Proxy Server Module Error.

Parameters
moduriModule URI string.
ecode

Definition at line 107 of file bsProxyMod.c.

Referenced by ModDllClose(), ModDllOpen(), ModDllSym(), and ModLoad().

Function Documentation

const char* ModCbGetDevUri ( BsVConnHnd_T  hndVConn,
char  dest[],
size_t  n 
)

Copy the device URI associated with the virtual connection.

Parameters
hndVConnVirtual connection handle.
[out]destDestination buffer.
nSize of buffer.
Returns
Pointer to destination buffer dest.

Definition at line 832 of file bsProxyMod.c.

References BsProxyVConn_T::m_pThCtl, _bsproxy_th_ctl::m_sDevUri, VConnAcquire(), and VConnRelease().

833 {
834  BsProxyVConn_T *pVConn;
835 
836  if( (pVConn = VConnAcquire(hndVConn)) != NULL )
837  {
838  strncpy(dest, pVConn->m_pThCtl->m_sDevUri, n);
839  dest[n-1] = 0;
840  VConnRelease(hndVConn);
841  }
842  else
843  {
844  dest[0] = 0;
845  }
846 
847  return dest;
848 }
void VConnRelease(BsVConnHnd_T hndVConn)
Release the locked virtual client.
Definition: bsProxyVConn.c:585
BsProxyThCtl_T * m_pThCtl
service thread
Definition: bsProxy.h:225
BsProxyVConn_T * VConnAcquire(BsVConnHnd_T hndVConn)
Acquire virtual connection, locking it from other threads.
Definition: bsProxyVConn.c:552
const char * m_sDevUri
proxied device URI or server
Definition: bsProxy.h:150
void ModCbIterDelete ( BsModIter_T pIter)

Delete the module iterator.

Parameters
pIterModule iterator.

Definition at line 855 of file bsProxyMod.c.

References BsModIter_T::m_sDevUri, and BsModIter_T::m_sModUri.

Referenced by ModCbIterFirst(), and ModCbIterNext().

856 {
857  if( pIter != NULL )
858  {
859  delete(pIter->m_sDevUri);
860  delete(pIter->m_sModUri);
861  delete(pIter);
862  }
863 }
char * m_sDevUri
device URI
Definition: bsProxyModIF.h:227
char * m_sModUri
module URI
Definition: bsProxyModIF.h:228
BsModIter_T* ModCbIterFirst ( BsModIterOver_T  eOver,
const char *  sPattern 
)

Start an iterator over the virtual connections matching the given pattern.

If a virtual connection match is found, an iterator is allocated and its data is filled with the first set of information.

The caller must delete the iterator by calling ModCbIterNext() until there are no more matches (auto-delete) or by calling ModCbIterDelete().

Parameters
eOverWhat pattern to iterator over (see BsModIterOver_T).
sPatternDevice or module URI pattern string.
Returns
On success, returns the pointer to the allocacted module iterator.
On failure, NULL is returned.

Definition at line 959 of file bsProxyMod.c.

References BsModIterOverDevUri, BsModIterOverModUri, BsModIterOverVConn, BsModIter_T::m_eOver, BsModIter_T::m_hndVConn, BsModIter_T::m_rd, BsModIter_T::m_sDevUri, BsModIter_T::m_sModUri, ModCbIterDelete(), and ModCbIterNext().

960 {
961  BsModIter_T *pIter = NEW(BsModIter_T);
962 
963  pIter->m_eOver = eOver;
964  pIter->m_hndVConn = -1;
965  pIter->m_sDevUri = NULL;
966  pIter->m_sModUri = NULL;
967  pIter->m_rd = -1;
968 
969  switch( eOver )
970  {
971  case BsModIterOverDevUri:
972  pIter->m_sDevUri = new_strdup(sPattern);
973  return ModCbIterNext(pIter);
974  case BsModIterOverModUri:
975  pIter->m_sModUri = new_strdup(sPattern);
976  return ModCbIterNext(pIter);
977  case BsModIterOverVConn:
978  return ModCbIterNext(pIter);
979  default:
980  ModCbIterDelete(pIter);
981  return NULL;
982  }
983 }
iterator over virtual connections (no pattern)
Definition: bsProxyModIF.h:217
BsModIter_T * ModCbIterNext(BsModIter_T *pIter)
Get the next set of information given the iterator state.
Definition: bsProxyMod.c:877
char * m_sDevUri
device URI
Definition: bsProxyModIF.h:227
iterator over device URI
Definition: bsProxyModIF.h:215
int m_rd
module&#39;s resource descriptor
Definition: bsProxyModIF.h:229
BsVConnHnd_T m_hndVConn
virtual connection handle
Definition: bsProxyModIF.h:226
Module Iterator Type.
Definition: bsProxyModIF.h:223
char * m_sModUri
module URI
Definition: bsProxyModIF.h:228
void ModCbIterDelete(BsModIter_T *pIter)
Delete the module iterator.
Definition: bsProxyMod.c:855
iterator over module URI
Definition: bsProxyModIF.h:216
BsModIterOver_T m_eOver
iterator over enum
Definition: bsProxyModIF.h:225
BsModIter_T* ModCbIterNext ( BsModIter_T pIter)

Get the next set of information given the iterator state.

If a virtual connection match is found, an iterator data is updated.

If no match is found, the iterator is auto-deleted.

Parameters
pIterInitialized, allocated iterator.
Returns
On success, returns the pointer to the iterator.
On failure, NULL is returned.

Definition at line 877 of file bsProxyMod.c.

References BsModIterOverDevUri, BsModIterOverModUri, BsModIterOverVConn, BSPROXY_VCONN_NUMOF, BsModIter_T::m_eOver, BsModIter_T::m_hndVConn, BsProxyVConn_T::m_pModIF, BsProxyVConn_T::m_pThCtl, BsProxyVConn_T::m_rd, BsModIter_T::m_rd, _bsproxy_th_ctl::m_sDevUri, BsModIter_T::m_sDevUri, BsProxyModIF_T::m_sModUri, BsModIter_T::m_sModUri, ModCbIterDelete(), VConnAcquire(), and VConnRelease().

Referenced by ModCbIterFirst().

878 {
879  BsVConnHnd_T hndVConn;
880  BsProxyVConn_T *pVConn;
881  bool_t bHasMatch = false;
882 
883  if( pIter == NULL )
884  {
885  return NULL;
886  }
887 
888  for(hndVConn=pIter->m_hndVConn+1;
889  (pIter!=NULL) && (hndVConn<BSPROXY_VCONN_NUMOF);
890  ++hndVConn)
891  {
892  if( (pVConn = VConnAcquire(hndVConn)) != NULL )
893  {
894  switch( pIter->m_eOver )
895  {
896  case BsModIterOverDevUri:
897  if( !strcmp(pVConn->m_pThCtl->m_sDevUri, pIter->m_sDevUri) )
898  {
899  pIter->m_hndVConn = hndVConn;
900  delete(pIter->m_sModUri);
901  pIter->m_sModUri = new_strdup(pVConn->m_pModIF->m_sModUri);
902  pIter->m_rd = pVConn->m_rd;
903  bHasMatch = true;
904  }
905  break;
906  case BsModIterOverModUri:
907  if( !strcmp(pVConn->m_pModIF->m_sModUri, pIter->m_sModUri) )
908  {
909  pIter->m_hndVConn = hndVConn;
910  delete(pIter->m_sDevUri);
911  pIter->m_sDevUri = new_strdup(pVConn->m_pThCtl->m_sDevUri);
912  pIter->m_rd = pVConn->m_rd;
913  bHasMatch = true;
914  }
915  break;
916  case BsModIterOverVConn:
917  pIter->m_hndVConn = hndVConn;
918  delete(pIter->m_sDevUri);
919  pIter->m_sDevUri = new_strdup(pVConn->m_pThCtl->m_sDevUri);
920  delete(pIter->m_sModUri);
921  pIter->m_sModUri = new_strdup(pVConn->m_pModIF->m_sModUri);
922  pIter->m_rd = pVConn->m_rd;
923  bHasMatch = true;
924  break;
925  default:
926  ModCbIterDelete(pIter);
927  pIter = NULL;
928  break;
929  }
930  VConnRelease(hndVConn);
931  }
932  }
933 
934  if( !bHasMatch )
935  {
936  ModCbIterDelete(pIter);
937  pIter = NULL;
938  }
939 
940  return pIter;
941 }
iterator over virtual connections (no pattern)
Definition: bsProxyModIF.h:217
int m_rd
module resource descriptor
Definition: bsProxy.h:227
void VConnRelease(BsVConnHnd_T hndVConn)
Release the locked virtual client.
Definition: bsProxyVConn.c:585
#define BSPROXY_VCONN_NUMOF
total number of module handles
Definition: BotSense.h:144
BsProxyThCtl_T * m_pThCtl
service thread
Definition: bsProxy.h:225
char * m_sDevUri
device URI
Definition: bsProxyModIF.h:227
BsProxyVConn_T * VConnAcquire(BsVConnHnd_T hndVConn)
Acquire virtual connection, locking it from other threads.
Definition: bsProxyVConn.c:552
iterator over device URI
Definition: bsProxyModIF.h:215
int m_rd
module&#39;s resource descriptor
Definition: bsProxyModIF.h:229
const char * m_sModUri
module Uniform Resource Id
Definition: bsProxy.h:122
BsVConnHnd_T m_hndVConn
virtual connection handle
Definition: bsProxyModIF.h:226
char * m_sModUri
module URI
Definition: bsProxyModIF.h:228
void ModCbIterDelete(BsModIter_T *pIter)
Delete the module iterator.
Definition: bsProxyMod.c:855
iterator over module URI
Definition: bsProxyModIF.h:216
const char * m_sDevUri
proxied device URI or server
Definition: bsProxy.h:150
BsModIterOver_T m_eOver
iterator over enum
Definition: bsProxyModIF.h:225
BsProxyModIF_T * m_pModIF
interface module I/F
Definition: bsProxy.h:226
int BsVConnHnd_T
virtual connection handle type
Definition: BotSense.h:151
int ModCbRsrcAdd ( BsModRsrcTbl_T pRsrcTbl,
BsVConnHnd_T  hndVConn,
void *  pRsrc 
)

Add a new resource to the resource table.

Parameters
pRsrcTblPointer to resource table.
hndVConnVirtual connection handle.
pRsrcPointer to allocated resource associated with handle.
Returns
On success, BS_OK is returned.
On error, the appropriate < 0 negated BotSense Error Code is returned.

Definition at line 744 of file bsProxyMod.c.

References BS_ECODE_BAD_VCONN_HND, BS_ECODE_INTERNAL, BS_ECODE_NO_RSRC, BSMOD_IS_VCONN_HANDLE, BsModRsrcTbl_T::m_uInUseCount, BsModRsrcTbl_T::m_uMaxResources, BsModRsrcTbl_T::m_vecIndex, and BsModRsrcTbl_T::m_vecRsrc.

745 {
746  int index;
747 
748  // bad handle
749  if( !BSMOD_IS_VCONN_HANDLE(hndVConn) )
750  {
751  return -BS_ECODE_BAD_VCONN_HND;
752  }
753 
754  // no more resources available
755  else if( pRsrcTbl->m_uInUseCount >= pRsrcTbl->m_uMaxResources )
756  {
757  return -BS_ECODE_NO_RSRC;
758  }
759 
760  // find empty slot in resource table
761  for(index=0; index<pRsrcTbl->m_uMaxResources; ++index)
762  {
763  if( pRsrcTbl->m_vecRsrc[index] == NULL )
764  {
765  break;
766  }
767  }
768 
769  // internal error - log these
770  if( (uint_t)index >= pRsrcTbl->m_uMaxResources )
771  {
772  LOGERROR("Internal module data corruption.");
773  return -BS_ECODE_INTERNAL;
774  }
775 
776  // add resource to resource table
777  pRsrcTbl->m_vecRsrc[index] = pRsrc;
778  pRsrcTbl->m_vecIndex[hndVConn] = (byte_t)index;
779  pRsrcTbl->m_uInUseCount++;
780 
781  return index;
782 }
uint_t m_uInUseCount
resources in-use count
Definition: bsProxyModIF.h:141
byte_t m_vecIndex[BSPROXY_VCONN_MOD_NUMOF]
vecIndex[handle] -> index
Definition: bsProxyModIF.h:138
#define BSMOD_IS_VCONN_HANDLE(hndVConn)
Test if the handle is in the valid module virtual connection range.
Definition: bsProxyModIF.h:149
#define BS_ECODE_BAD_VCONN_HND
bad virtual connection handle
Definition: BotSense.h:79
uint_t m_uMaxResources
maximum resource count
Definition: bsProxyModIF.h:140
#define BS_ECODE_NO_RSRC
no resource available
Definition: BotSense.h:85
#define BS_ECODE_INTERNAL
internal error (bug)
Definition: BotSense.h:93
void ** m_vecRsrc
vecRsrc[index] -> rsrc
Definition: bsProxyModIF.h:139
void* ModCbRsrcRemove ( BsModRsrcTbl_T pRsrcTbl,
BsVConnHnd_T  hndVConn 
)

Remove a resource from the resource table.

The resource is not deleted.

Parameters
pRsrcTblPointer to resource table.
hndVConnVirtual connection handle.
Returns
On success, returns pointer to removed resource. On failure, NULL is returned.

Definition at line 795 of file bsProxyMod.c.

References BSMOD_IS_VCONN_HANDLE, BSMOD_RSRC_INDEX, BSPROXY_VCONN_UNDEF, BsModRsrcTbl_T::m_uInUseCount, BsModRsrcTbl_T::m_vecIndex, and BsModRsrcTbl_T::m_vecRsrc.

796 {
797  int index;
798  void *pRsrc;
799 
800  // bad handle
801  if( !BSMOD_IS_VCONN_HANDLE(hndVConn) )
802  {
803  return NULL;
804  }
805 
806  // get resource index
807  else if( (index = BSMOD_RSRC_INDEX(pRsrcTbl, hndVConn)) < 0 )
808  {
809  return NULL;
810  }
811 
812  // removed resource
813  pRsrc = pRsrcTbl->m_vecRsrc[index];
814 
815  // remove resource to resource table
816  pRsrcTbl->m_vecRsrc[index] = NULL;
817  pRsrcTbl->m_vecIndex[hndVConn] = (byte_t)BSPROXY_VCONN_UNDEF;
818  pRsrcTbl->m_uInUseCount--;
819 
820  return pRsrc;
821 }
uint_t m_uInUseCount
resources in-use count
Definition: bsProxyModIF.h:141
byte_t m_vecIndex[BSPROXY_VCONN_MOD_NUMOF]
vecIndex[handle] -> index
Definition: bsProxyModIF.h:138
#define BSMOD_IS_VCONN_HANDLE(hndVConn)
Test if the handle is in the valid module virtual connection range.
Definition: bsProxyModIF.h:149
#define BSPROXY_VCONN_UNDEF
undefined virtual connection handle
Definition: BotSense.h:139
#define BSMOD_RSRC_INDEX(pRsrcTbl, hndVConn)
Get the resource index given the handle.
Definition: bsProxyModIF.h:181
void ** m_vecRsrc
vecRsrc[index] -> rsrc
Definition: bsProxyModIF.h:139
void ModCbRsrcTblDelete ( BsModRsrcTbl_T pRsrcTbl)

Delete an allocated resource table.

Warning
Module-specific resources should be freed up prior to calling this function.
Parameters
pRsrcTblPointer to resource table.

Definition at line 726 of file bsProxyMod.c.

References BsModRsrcTbl_T::m_vecRsrc.

727 {
728  if( pRsrcTbl != NULL )
729  {
730  delete(pRsrcTbl->m_vecRsrc);
731  delete(pRsrcTbl);
732  }
733 }
void ** m_vecRsrc
vecRsrc[index] -> rsrc
Definition: bsProxyModIF.h:139
BsModRsrcTbl_T* ModCbRsrcTblNew ( int  nMaxResources)

Allocatae a new module resource table of fixed size.

The resource table is indexed by the virtual connection handle.

Parameters
nMaxResourcesMaximum number of simultaneous resources supported.
Returns
Pointer to allocated resource block.

Definition at line 703 of file bsProxyMod.c.

References BSPROXY_VCONN_UNDEF, BsModRsrcTbl_T::m_uInUseCount, BsModRsrcTbl_T::m_uMaxResources, BsModRsrcTbl_T::m_vecIndex, and BsModRsrcTbl_T::m_vecRsrc.

704 {
705  BsModRsrcTbl_T *pRsrcTbl = NEW(BsModRsrcTbl_T);
706 
707  pRsrcTbl->m_vecRsrc = new(sizeof(void *) * (size_t)nMaxResources);
708 
709  memset(pRsrcTbl->m_vecIndex, BSPROXY_VCONN_UNDEF,
710  sizeof(pRsrcTbl->m_vecIndex));
711 
712  pRsrcTbl->m_uMaxResources = (uint_t)nMaxResources;
713  pRsrcTbl->m_uInUseCount = 0;
714 
715  return pRsrcTbl;
716 }
Useful indirect indexing of handle to resource instance structure.
Definition: bsProxyModIF.h:136
uint_t m_uInUseCount
resources in-use count
Definition: bsProxyModIF.h:141
byte_t m_vecIndex[BSPROXY_VCONN_MOD_NUMOF]
vecIndex[handle] -> index
Definition: bsProxyModIF.h:138
#define BSPROXY_VCONN_UNDEF
undefined virtual connection handle
Definition: BotSense.h:139
uint_t m_uMaxResources
maximum resource count
Definition: bsProxyModIF.h:140
void ** m_vecRsrc
vecRsrc[index] -> rsrc
Definition: bsProxyModIF.h:139
void ModCbSendErrorRsp ( BsVConnHnd_T  hndVConn,
BsTid_T  uTid,
int  nECode,
const char *  sErrFmt,
  ... 
)

Send generic error repsonse callback function.

Note
Service thread request handler has already locked the virtual connection.
Parameters
hndVConnVirtual connection handle.
uTidRequest-Response transaction id.
nECodeOne of BotSense Error Codes.
sErrFmtError format string.
...Format string variable arguments.

Definition at line 671 of file bsProxyMod.c.

References BS_ECODE_NO_VCONN, ClientSendVErrorRsp(), BsProxyVConn_T::m_hndClient, ServerHasName(), and VConnGet().

676 {
677  BsProxyVConn_T *pVConn;
678  va_list ap;
679 
680  if( (pVConn = VConnGet(hndVConn)) != NULL )
681  {
682  va_start(ap, sErrFmt);
683  ClientSendVErrorRsp(pVConn->m_hndClient, uTid, nECode, sErrFmt, ap);
684  va_end(ap);
685  }
686  else
687  {
688  LOGERROR("%s: %s(ecode=%d): VConn=%d.",
690  hndVConn);
691  }
692 }
INLINE_IN_H const char * ServerHasName()
Get the <b><i>BotSense</i></b> server&#39;s official name.
Definition: bsProxy.h:485
BsProxyClientHnd_T m_hndClient
proxied client handle
Definition: bsProxy.h:224
BsProxyVConn_T * VConnGet(BsVConnHnd_T hndVConn)
Get the virtual connection entry associated with the handle.
Definition: bsProxyVConn.c:248
int ClientSendVErrorRsp(BsProxyClientHnd_T hndClient, BsTid_T uTid, int nECode, const char *sErrFmt, va_list ap)
Send va_list error response to the client.
#define BS_ECODE_NO_VCONN
virtual connection not found
Definition: BotSense.h:80
void ModCbSendOkRsp ( BsVConnHnd_T  hndVConn,
BsTid_T  uTid 
)

Send generic ok repsonse callback function.

Note
Service thread request handler has already locked the virtual connection.
Parameters
hndVConnVirtual connection handle.
uTidRequest-Response transaction id.

Definition at line 643 of file bsProxyMod.c.

References BS_ECODE_NO_VCONN, ClientSendOkRsp(), BsProxyVConn_T::m_hndClient, ServerHasName(), and VConnGet().

644 {
645  BsProxyVConn_T *pVConn;
646 
647  if( (pVConn = VConnGet(hndVConn)) != NULL )
648  {
649  ClientSendOkRsp(pVConn->m_hndClient, uTid);
650  }
651  else
652  {
653  LOGERROR("%s: %s(ecode=%d): VConn=%d.",
655  hndVConn);
656  }
657 }
INLINE_IN_H const char * ServerHasName()
Get the <b><i>BotSense</i></b> server&#39;s official name.
Definition: bsProxy.h:485
int ClientSendOkRsp(BsProxyClientHnd_T hndClient, BsTid_T uTid)
Send an ok response to the client.
BsProxyClientHnd_T m_hndClient
proxied client handle
Definition: bsProxy.h:224
BsProxyVConn_T * VConnGet(BsVConnHnd_T hndVConn)
Get the virtual connection entry associated with the handle.
Definition: bsProxyVConn.c:248
#define BS_ECODE_NO_VCONN
virtual connection not found
Definition: BotSense.h:80
void ModCbSendRsp ( BsVConnHnd_T  hndVConn,
BsTid_T  uTid,
BsMsgId_T  uMsgId,
byte_t  bufRsp[],
size_t  uRspLen 
)

Send module-specific repsonse callback function.

Note
Service thread request handler has already locked the virtual connection.
Parameters
hndVConnVirtual connection handle.
uTidRequest-Response transaction id.
uMsgIdResponse message id.
bufRspPacked repsonse message body.
uRspLenLength of response in buffer (number of bytes).

Definition at line 614 of file bsProxyMod.c.

References BS_ECODE_NO_VCONN, ClientSendRsp(), BsProxyVConn_T::m_hndClient, ServerHasName(), and VConnGet().

619 {
620  BsProxyVConn_T *pVConn;
621 
622  if( (pVConn = VConnGet(hndVConn)) != NULL )
623  {
624  ClientSendRsp(pVConn->m_hndClient, hndVConn, uTid, uMsgId, bufRsp, uRspLen);
625  }
626  else
627  {
628  LOGERROR("%s: %s(ecode=%d): VConn=%d.",
630  hndVConn);
631  }
632 }
INLINE_IN_H const char * ServerHasName()
Get the <b><i>BotSense</i></b> server&#39;s official name.
Definition: bsProxy.h:485
BsProxyClientHnd_T m_hndClient
proxied client handle
Definition: bsProxy.h:224
BsProxyVConn_T * VConnGet(BsVConnHnd_T hndVConn)
Get the virtual connection entry associated with the handle.
Definition: bsProxyVConn.c:248
#define BS_ECODE_NO_VCONN
virtual connection not found
Definition: BotSense.h:80
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.
static void ModDllClose ( const char *  sModUri,
void *  dllHandle 
)
static

Unload the dynamic library from the application.

Note
The dlopen() and dlclose() functions only load/unload when a library iff the internal reference count is zero. However, bsProxy also needs to track the reference count, which the dl interface does not provide access to.
Parameters
sModUriExpanded, canonical name of the interface module.
dllHandleDLL handle.

Definition at line 344 of file bsProxyMod.c.

References BS_ECODE_NO_EXEC, and BSPROXY_MOD_LOG_ERROR.

Referenced by ModUnload().

345 {
346  // clear any existing error
347  dlerror();
348 
349  if( dlclose(dllHandle) != 0 )
350  {
351  BSPROXY_MOD_LOG_ERROR(sModUri, BS_ECODE_NO_EXEC, "%s.", dlerror());
352  }
353 }
#define BS_ECODE_NO_EXEC
cannot execute
Definition: BotSense.h:88
#define BSPROXY_MOD_LOG_ERROR(moduri, ecode, efmt,...)
Log Proxy Server Module Error.
Definition: bsProxyMod.c:107
static void ModDllDeleteIF ( BsProxyModIF_T pModIF)
static

Delete module interface block.

Parameters
pModIFExported module interface.

Definition at line 396 of file bsProxyMod.c.

References BsProxyModIF_T::m_sModUri.

Referenced by ModDllNewIF(), ModHashDeleteData(), and ModLoad().

397 {
398  if( pModIF != NULL )
399  {
400  delete((char *)pModIF->m_sModUri);
401  delete(pModIF);
402  }
403 }
const char * m_sModUri
module Uniform Resource Id
Definition: bsProxy.h:122
static void ModDllInit ( DListStr_T *  pDListLibPath)
static

Initialize Dynamic Link Loader operating parameters.

Parameters
pDListLibPathList of optional library search paths.

For i/f module search path order:

  • command-line -L options in order listed
  • XML configuration paths
  • user's home lib directory if it exists
  • BotSense library install library path
  • default dlopen() locations (system dependent)

Definition at line 175 of file bsProxyMod.c.

References BS_AUX_LIBDIR, BS_PLUGIN_DIR, and BsModDllPaths.

Referenced by ModOneTimeInit().

176 {
177  DListStrIter_T iter;
178  char *sLibPath;
179  char *sPath;
180 
181  BsModDllPaths = DListStrNewDft();
182 
183  // Add command-line -L paths here
184  for(sLibPath=DListStrIterDataFirst(pDListLibPath, &iter);
185  sLibPath!=NULL;
186  sLibPath=DListStrIterDataNext(&iter))
187  {
188  // canonicalize path name
189  if( (sPath = NewSearchPathCanonicalized(sLibPath)) == NULL )
190  {
191  LOGDIAG2("Warning: \"%s\": Ignoring bad library path.", sLibPath);
192  }
193  else
194  {
195  DListStrAppend(BsModDllPaths, sPath);
196  }
197  }
198 
199  // RDK add xml config paths here
200 
201  // RDK add user's home/botsense/libs path here
202 
203  // Add installed prefix/lib/botsense/ path.
204  if( (sPath = NewJoinedPath(BS_AUX_LIBDIR, BS_PLUGIN_DIR)) != NULL )
205  {
206  DListStrAppend(BsModDllPaths, sPath);
207  }
208 }
#define BS_PLUGIN_DIR
bsProxy module plugin subdirectory
Definition: bsProxyMod.c:89
#define BS_AUX_LIBDIR
additional bsProxy library directory
Definition: bsProxyMod.c:85
static DListStr_T * BsModDllPaths
module search paths
Definition: bsProxyMod.c:97
static char* ModDllNewCanonicalName ( const char *  sModName)
static

Make DLL canonical file name.

The library path is expanded, canonicalized and any necessary file extension is appended.

Parameters
sModNameDLL library path name.
Returns
On success, an allocated, canonical library path name is returned.
On failure, NULL is returned.

Definition at line 222 of file bsProxyMod.c.

References DLL_EXT.

Referenced by ModNewModUri().

223 {
224  char *s = NULL;
225  char *t = NULL;
226  size_t m, n;
227 
228  // canonicalize module path name
229  if( (s = NewSearchPathCanonicalized(sModName)) == NULL )
230  {
231  LOGERROR("NewSearchPathCanonicalized(\"%s\") failed.", sModName);
232  return NULL;
233  }
234 
235  m = strlen(s);
236  n = strlen(DLL_EXT);
237 
238  // append library extension
239  if( (DLL_EXT != NULL) && ((m <= n) || strncasecmp(s+m-n, DLL_EXT, n)) )
240  {
241  t = NEWSTR(m+n);
242  sprintf(t, "%s%s", s, DLL_EXT);
243  delete(s);
244  }
245  else
246  {
247  t = s;
248  }
249 
250  return t;
251 }
#define DLL_EXT
standard dll library file name extension
Definition: bsProxyMod.c:79
static BsProxyModIF_T* ModDllNewIF ( const char *  sModUri,
void *  dllHandle 
)
static

Allocate an attached exported module interface block.

Note
dlsym() can return NULL with no errors if the symbol's value is NULL. Checking the dlerror() return is the safe way to determine if an error has occurred. However, a bsProxy compatible module's exported interface is required to be defined.
Parameters
sModUriExpanded, canonical name of the interface module.
dllHandleDLL handle.
Returns
On success, returns a pointer to the module's exported interface.
On failure, returns NULL.

Definition at line 420 of file bsProxyMod.c.

References BS_ECODE_BAD_MOD, BS_OK, BSMOD_SYM_CLOSE_S, BSMOD_SYM_EXIT_S, BSMOD_SYM_INFO_S, BSMOD_SYM_INIT_S, BSMOD_SYM_OPEN_S, BSMOD_SYM_REQUEST_S, BSMOD_SYM_TRACE_S, BsProxyModIF_T::m_dllHandle, BsProxyModIF_T::m_fnModClose, BsProxyModIF_T::m_fnModExit, BsProxyModIF_T::m_fnModInfo, BsProxyModIF_T::m_fnModInit, BsProxyModIF_T::m_fnModOpen, BsProxyModIF_T::m_fnModRequest, BsProxyModIF_T::m_fnModTrace, BsProxyModIF_T::m_sModUri, BsProxyModIF_T::m_uRefCnt, ModDllDeleteIF(), and ModDllSym().

Referenced by ModLoad().

421 {
422  BsProxyModIF_T *pModIF = NEW(BsProxyModIF_T);
423  int rc = BS_OK;
424 
425  pModIF->m_sModUri = new_strdup(sModUri); // self reference
426  pModIF->m_dllHandle = dllHandle; // opaque dll handle
427  pModIF->m_uRefCnt = 1; // module reference count
428 
429  //
430  // Init
431  //
433 
434  if( pModIF->m_fnModInit == NULL )
435  {
436  rc = -BS_ECODE_BAD_MOD;
437  }
438 
439  //
440  // Exit
441  //
443 
444  if( pModIF->m_fnModExit == NULL )
445  {
446  rc = -BS_ECODE_BAD_MOD;
447  }
448 
449  //
450  // Open
451  //
453 
454  if( pModIF->m_fnModOpen == NULL )
455  {
456  rc = -BS_ECODE_BAD_MOD;
457  }
458 
459  //
460  // Close
461  //
463 
464  if( pModIF->m_fnModClose == NULL )
465  {
466  rc = -BS_ECODE_BAD_MOD;
467  }
468 
469 
470  //
471  // Request
472  //
473  pModIF->m_fnModRequest = (BsModRequestFunc_P)ModDllSym(pModIF,
475 
476  if( pModIF->m_fnModRequest == NULL )
477  {
478  rc = -BS_ECODE_BAD_MOD;
479  }
480 
481  //
482  // Trace
483  //
485 
486  if( pModIF->m_fnModTrace == NULL )
487  {
488  rc = -BS_ECODE_BAD_MOD;
489  }
490 
491  //
492  // Info
493  //
495 
496  if( pModIF->m_fnModInfo == NULL )
497  {
498  rc = -BS_ECODE_BAD_MOD;
499  }
500 
501  // Error clean up.
502  if( rc < 0 )
503  {
504  ModDllDeleteIF(pModIF);
505  pModIF = NULL;
506  }
507 
508  return pModIF;
509 }
#define BSMOD_SYM_OPEN_S
open function string
Definition: bsProxyModIF.h:105
BsModTraceFunc_T * BsModTraceFunc_P
pointer to msg trace func.
Definition: bsProxyModIF.h:612
BsModExitFunc_P m_fnModExit
module exit
Definition: bsProxy.h:126
BsModRequestFunc_T * BsModRequestFunc_P
pointer to request func.
Definition: bsProxyModIF.h:601
BsModCloseFunc_T * BsModCloseFunc_P
pointer to close function
Definition: bsProxyModIF.h:581
BsModInfoFunc_T * BsModInfoFunc_P
pointer to mod info function
Definition: bsProxyModIF.h:621
static void * ModDllSym(BsProxyModIF_T *pModIF, const char *sSym)
Get the address of where the module symbol is loaded into memory.
Definition: bsProxyMod.c:365
BsModExitFunc_T * BsModExitFunc_P
pointer to exit function
Definition: bsProxyModIF.h:541
#define BSMOD_SYM_TRACE_S
tracing func string
Definition: bsProxyModIF.h:108
#define BS_OK
not an error, success
Definition: BotSense.h:66
BsModCloseFunc_P m_fnModClose
device close
Definition: bsProxy.h:128
BsModInfoFunc_P m_fnModInfo
static info
Definition: bsProxy.h:131
BsModOpenFunc_T * BsModOpenFunc_P
pointer to open function
Definition: bsProxyModIF.h:568
#define BS_ECODE_BAD_MOD
bad interface module
Definition: BotSense.h:84
BsModTraceFunc_P m_fnModTrace
tracing
Definition: bsProxy.h:130
#define BSMOD_SYM_EXIT_S
exit function string
Definition: bsProxyModIF.h:104
#define BSMOD_SYM_CLOSE_S
close function string
Definition: bsProxyModIF.h:106
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
BsModInitFunc_P m_fnModInit
module init
Definition: bsProxy.h:125
#define BSMOD_SYM_REQUEST_S
request func string
Definition: bsProxyModIF.h:107
#define BSMOD_SYM_INFO_S
info function string
Definition: bsProxyModIF.h:109
#define BSMOD_SYM_INIT_S
Required Module Symbols Strings.
Definition: bsProxyModIF.h:103
static void ModDllDeleteIF(BsProxyModIF_T *pModIF)
Delete module interface block.
Definition: bsProxyMod.c:396
BsModOpenFunc_P m_fnModOpen
device open
Definition: bsProxy.h:127
BsModInitFunc_T * BsModInitFunc_P
pointer to init function
Definition: bsProxyModIF.h:530
void * m_dllHandle
dynamic library loader handle
Definition: bsProxy.h:123
BsModRequestFunc_P m_fnModRequest
module request
Definition: bsProxy.h:129
static void* ModDllOpen ( const char *  sModUri)
static

Dynamically load bsProxy server compatible interface module library.

If the module path name is absolute, the only library at that location is attempted to be loaded. Otherwise, a series of path locations are searched for the library.

Note
The dlopen() and dlclose() functions only load/unload when a library iff the internal reference count is zero. However, bsProxy also needs to track the reference count, which the dl interface does not provide access to.
Parameters
sModUriExpanded, canonical name of the interface module.
Returns
On success, returns DLL handle.
On failure, returns NULL.

< paths dlist iterator

< a dll path

< fully qualified path name

< loaded dll handle

Definition at line 270 of file bsProxyMod.c.

References BS_ECODE_NO_MOD, BsModDllPaths, BSPROXY_MOD_LOG_ERROR, and ServerHasName().

Referenced by ModLoad().

271 {
272  DListStrIter_T iter; ///< paths dlist iterator
273  char *sPath; ///< a dll path
274  char *sFqPath; ///< fully qualified path name
275  void *dllHandle = NULL; ///< loaded dll handle
276 
277  if( sModUri == NULL )
278  {
279  return NULL;
280  }
281 
282  // clear any existing error
283  dlerror();
284 
285  //
286  // Module path name is an absolute path. No search is made, simply let
287  // dlopen() do the work.
288  //
289  if( PathIsAbsolute(sModUri) )
290  {
291  if( (sFqPath = NewRealPath(sModUri)) != NULL )
292  {
293  LOGDIAG3("%s: trying dlopen(\"%s\").", ServerHasName(), sFqPath);
294  dllHandle = dlopen(sFqPath, RTLD_LAZY | RTLD_GLOBAL);
295  delete(sFqPath);
296  }
297  }
298 
299  //
300  // Search list of paths for the module's location and load.
301  //
302  else
303  {
304  for(sPath=DListStrIterDataFirst(BsModDllPaths, &iter), dllHandle=NULL;
305  sPath!=NULL && dllHandle==NULL;
306  sPath=DListStrIterDataNext(&iter))
307  {
308  if( (sFqPath = NewJoinedPath(sPath, sModUri)) != NULL )
309  {
310  LOGDIAG3("%s: trying dlopen(\"%s\").", ServerHasName(), sFqPath);
311  dllHandle = dlopen(sFqPath, RTLD_LAZY | RTLD_GLOBAL);
312  delete(sFqPath);
313  }
314  }
315 
316  //
317  // Use dlopen() default search.
318  //
319  if( dllHandle == NULL )
320  {
321  LOGDIAG3("%s: trying dlopen(\"%s\").", ServerHasName(), sModUri);
322  dllHandle = dlopen(sModUri, RTLD_LAZY | RTLD_GLOBAL);
323  }
324  }
325 
326  if( dllHandle == NULL )
327  {
328  BSPROXY_MOD_LOG_ERROR(sModUri, BS_ECODE_NO_MOD, "%s.", dlerror());
329  }
330 
331  return dllHandle;
332 }
INLINE_IN_H const char * ServerHasName()
Get the <b><i>BotSense</i></b> server&#39;s official name.
Definition: bsProxy.h:485
static DListStr_T * BsModDllPaths
module search paths
Definition: bsProxyMod.c:97
#define BS_ECODE_NO_MOD
no interface module
Definition: BotSense.h:83
#define BSPROXY_MOD_LOG_ERROR(moduri, ecode, efmt,...)
Log Proxy Server Module Error.
Definition: bsProxyMod.c:107
static void* ModDllSym ( BsProxyModIF_T pModIF,
const char *  sSym 
)
static

Get the address of where the module symbol is loaded into memory.

Parameters
pModIFExported module interface.
sSymSymbol name.
Returns
On success, returns the symbol address.
On failure, NULL is returned.

Definition at line 365 of file bsProxyMod.c.

References BS_ECODE_BAD_MOD, BSPROXY_MOD_LOG_ERROR, BsProxyModIF_T::m_dllHandle, and BsProxyModIF_T::m_sModUri.

Referenced by ModDllNewIF().

366 {
367  void *pSym;
368  char *sEMsg;
369 
370  // clear any existing error
371  dlerror();
372 
373  // get symbol address
374  pSym = dlsym(pModIF->m_dllHandle, sSym);
375 
376  // error
377  if( pSym == NULL )
378  {
379  sEMsg = dlerror();
380  if( sEMsg == NULL )
381  {
382  sEMsg = "Not defined";
383  }
385  "%s: %s.", sSym, sEMsg);
386  }
387 
388  return pSym;
389 }
#define BS_ECODE_BAD_MOD
bad interface module
Definition: BotSense.h:84
const char * m_sModUri
module Uniform Resource Id
Definition: bsProxy.h:122
#define BSPROXY_MOD_LOG_ERROR(moduri, ecode, efmt,...)
Log Proxy Server Module Error.
Definition: bsProxyMod.c:107
void * m_dllHandle
dynamic library loader handle
Definition: bsProxy.h:123
BsProxyModIF_T* ModGetLoadedIF ( const char *  sModUri)

Get the defined interface from a previously loaded i/f module.

Multi-Thread safe.

Parameters
sModUriInterface module expanded, canonical name.
Returns
On success, returns a pointer to the module's exported interface.
Returns NULL if no interface is found.

Definition at line 1058 of file bsProxyMod.c.

References ModHashGetIF(), ModLock(), and ModUnlock().

Referenced by ClientSetTraceState().

1059 {
1060  BsProxyModIF_T *pModIF;
1061 
1062  ModLock();
1063 
1064  pModIF = ModHashGetIF(sModUri);
1065 
1066  ModUnlock();
1067 
1068  return pModIF;
1069 }
static void ModUnlock()
Unlock module&#39;s global mutual exclusion.
Definition: bsProxyMod.c:135
static BsProxyModIF_T * ModHashGetIF(const char *sModUri)
Get the interface module&#39;s i/f from the module hash table.
Definition: bsProxyMod.c:583
static void ModLock()
Lock module&#39;s global mutual exclusion.
Definition: bsProxyMod.c:121
static int ModHashAdd ( const char *  sModUri,
BsProxyModIF_T pModIF 
)
static

Add the module data to the i/f module hash table.

Parameters
sModUriAllocated interface module hash table key.
pModIFAllocated interface module hash table value.
Returns
On success, BS_OK is returned.
On error, the appropriate < 0 negated BotSense Error Code is returned.

Definition at line 538 of file bsProxyMod.c.

References BS_ECODE_NO_RSRC, BS_OK, BsModHashTbl, and ModHashDeleteData().

Referenced by ModLoad().

539 {
540  char *sKey = new_strdup(sModUri);
541 
542  // insert into hash table, automatically allocating hnode_t
543  if( !hash_insert(BsModHashTbl, sKey, pModIF) )
544  {
545  LOGERROR("hash_insert(%s,...) failed", sKey);
546  ModHashDeleteData(sKey, pModIF);
547  return -BS_ECODE_NO_RSRC;
548  }
549 
550  return BS_OK;
551 }
static hash_t * BsModHashTbl
i/f module hash table
Definition: bsProxyMod.c:96
#define BS_OK
not an error, success
Definition: BotSense.h:66
static void ModHashDeleteData(void *sModUri, void *pModIF)
Delete hash node data callback.
Definition: bsProxyMod.c:524
#define BS_ECODE_NO_RSRC
no resource available
Definition: BotSense.h:85
static void ModHashDelete ( BsProxyModIF_T pModIF)
static

Remove and delete the module data from the i/f module hash table.

Parameters
pModIFAllocated interface module hash table value.
Returns
On success, BS_OK is returned.
On error, the appropriate < 0 negated BotSense Error Code is returned.

Definition at line 560 of file bsProxyMod.c.

References BsModHashTbl, and BsProxyModIF_T::m_sModUri.

Referenced by ModUnload().

561 {
562  hnode_t *pNode;
563 
564  if( pModIF == NULL )
565  {
566  return;
567  }
568  else if( (pNode = hash_lookup(BsModHashTbl, pModIF->m_sModUri)) != NULL )
569  {
570  hash_node_delete(BsModHashTbl, pNode);
571  }
572 }
static hash_t * BsModHashTbl
i/f module hash table
Definition: bsProxyMod.c:96
const char * m_sModUri
module Uniform Resource Id
Definition: bsProxy.h:122
static void ModHashDeleteData ( void *  sModUri,
void *  pModIF 
)
static

Delete hash node data callback.

Both the key and value are dynamically allocated.

Parameters
sModUriInterface module hash table key.
pModIFInterface module hash table value.

Definition at line 524 of file bsProxyMod.c.

References ModDllDeleteIF().

Referenced by ModHashAdd(), and ModOneTimeInit().

525 {
526  delete(sModUri);
527  ModDllDeleteIF((BsProxyModIF_T *)pModIF);
528 }
static void ModDllDeleteIF(BsProxyModIF_T *pModIF)
Delete module interface block.
Definition: bsProxyMod.c:396
static BsProxyModIF_T* ModHashGetIF ( const char *  sModUri)
static

Get the interface module's i/f from the module hash table.

Parameters
sModUriInterface module hash table key.
Returns
On success, returns pointer to the module's exported interface.
On failure, returns NULL.

Definition at line 583 of file bsProxyMod.c.

References BsModHashTbl.

Referenced by ModGetLoadedIF(), ModLoad(), and ModUnload().

584 {
585  hnode_t *pNode;
586 
587  if( (pNode = hash_lookup(BsModHashTbl, sModUri)) == NULL )
588  {
589  return NULL;
590  }
591  else
592  {
593  return (BsProxyModIF_T *)hnode_get(pNode);
594  }
595 }
static hash_t * BsModHashTbl
i/f module hash table
Definition: bsProxyMod.c:96
BsProxyModIF_T* ModLoad ( const char *  sModUri)

Load the interface module.

The module's expanded, canonical name is determined, the dynamic library is loaded, the module's exported interface is attached, and the module's initialization routine is called.

The the same module is loaded again, the reference count is bumped and the already attached interface is returned.

Multi-Thread safe.

Parameters
sModUriInterface module expanded, canonical name.
Returns
On success, returns a pointer to the module's exported interface.
On failure, returns NULL.

Definition at line 1089 of file bsProxyMod.c.

References BS_ECODE_BAD_MOD, BS_ECODE_NO_MOD, BS_ECODE_NO_RSRC, BS_OK, BSPROXY_MOD_LOG_ERROR, BsProxyModIF_T::m_fnModInit, BsProxyModIF_T::m_sModUri, BsProxyModIF_T::m_uRefCnt, ModDllDeleteIF(), ModDllNewIF(), ModDllOpen(), ModHashAdd(), ModHashGetIF(), ModLock(), and ModUnlock().

Referenced by ClientSetTraceState(), and VConnOpenDev().

1090 {
1091  BsProxyModIF_T *pModIF = NULL;
1092  void *dllHandle = NULL;
1093  int rc;
1094 
1095  // lock global mutex
1096  ModLock();
1097 
1098  //
1099  // Check if module is already loaded.
1100  //
1101  if( (pModIF = ModHashGetIF(sModUri)) != NULL )
1102  {
1103  rc = BS_OK;
1104  pModIF->m_uRefCnt++;
1105  LOGDIAG1("Interface Module \"%s\" attached (refcnt=%u).",
1106  pModIF->m_sModUri, pModIF->m_uRefCnt);
1107  }
1108 
1109  //
1110  // Load the dynamic library.
1111  //
1112  else if( (dllHandle = ModDllOpen(sModUri)) == NULL )
1113  {
1114  rc = -BS_ECODE_NO_MOD;
1115  BSPROXY_MOD_LOG_ERROR(sModUri, rc, "Failed to open.");
1116  }
1117 
1118  //
1119  // Allocate new interface control blcok and attach module's exported
1120  // interface.
1121  //
1122  else if( (pModIF = ModDllNewIF(sModUri, dllHandle)) == NULL )
1123  {
1124  rc = -BS_ECODE_BAD_MOD;
1125  BSPROXY_MOD_LOG_ERROR(sModUri, rc, "Failed to get interface.");
1126  }
1127 
1128  //
1129  // Call the newly loaded module's exported initialization function to
1130  // initialize any module specific data and resources.
1131  //
1132  else if( (rc = pModIF->m_fnModInit(sModUri, &BsModProxyCallbacks)) < 0 )
1133  {
1134  rc = -BS_ECODE_BAD_MOD;
1135  BSPROXY_MOD_LOG_ERROR(sModUri, rc, "Failed to initialize.");
1136  }
1137 
1138  //
1139  // Add the module to the hash table (failure auto-deletes node data).
1140  //
1141  else if( (rc = ModHashAdd(sModUri, pModIF)) < 0 )
1142  {
1143  rc = -BS_ECODE_NO_RSRC;
1144  BSPROXY_MOD_LOG_ERROR(sModUri, rc, "Failed to add module to hash table.");
1145  }
1146 
1147  //
1148  // Successfully loaded new module.
1149  //
1150  else
1151  {
1152  rc = BS_OK;
1153  LOGDIAG1("Interface Module \"%s\" loaded (refcnt=%u).",
1154  pModIF->m_sModUri, pModIF->m_uRefCnt);
1155  }
1156 
1157  ModUnlock();
1158 
1159  //
1160  // Error clean up.
1161  //
1162  if( rc < 0 )
1163  {
1164  if( dllHandle != NULL )
1165  {
1166  dlclose(dllHandle);
1167  dllHandle = NULL;
1168  }
1169 
1170  if( pModIF != NULL )
1171  {
1172  ModDllDeleteIF(pModIF);
1173  pModIF = NULL;
1174  }
1175  }
1176 
1177  return pModIF;
1178 }
static void ModUnlock()
Unlock module&#39;s global mutual exclusion.
Definition: bsProxyMod.c:135
#define BS_OK
not an error, success
Definition: BotSense.h:66
static BsProxyModIF_T * ModDllNewIF(const char *sModUri, void *dllHandle)
Allocate an attached exported module interface block.
Definition: bsProxyMod.c:420
#define BS_ECODE_BAD_MOD
bad interface module
Definition: BotSense.h:84
static BsModProxyCb_T BsModProxyCallbacks
Interface module callbacks to bsProxy server services.
Definition: bsProxyMod.c:988
static int ModHashAdd(const char *sModUri, BsProxyModIF_T *pModIF)
Add the module data to the i/f module hash table.
Definition: bsProxyMod.c:538
#define BS_ECODE_NO_RSRC
no resource available
Definition: BotSense.h:85
uint_t m_uRefCnt
vconn reference count for this module
Definition: bsProxy.h:124
#define BS_ECODE_NO_MOD
no interface module
Definition: BotSense.h:83
const char * m_sModUri
module Uniform Resource Id
Definition: bsProxy.h:122
BsModInitFunc_P m_fnModInit
module init
Definition: bsProxy.h:125
static BsProxyModIF_T * ModHashGetIF(const char *sModUri)
Get the interface module&#39;s i/f from the module hash table.
Definition: bsProxyMod.c:583
static void * ModDllOpen(const char *sModUri)
Dynamically load bsProxy server compatible interface module library.
Definition: bsProxyMod.c:270
static void ModDllDeleteIF(BsProxyModIF_T *pModIF)
Delete module interface block.
Definition: bsProxyMod.c:396
#define BSPROXY_MOD_LOG_ERROR(moduri, ecode, efmt,...)
Log Proxy Server Module Error.
Definition: bsProxyMod.c:107
static void ModLock()
Lock module&#39;s global mutual exclusion.
Definition: bsProxyMod.c:121
char* ModNewModUri ( const char *  sModName)

Convert the module name to a quasi Uniform Resource Id.

Parameters
sModNameModule path name.
Returns
On success, an allocated, canonical library path name is returned.
On failure, NULL is returned.

Definition at line 1042 of file bsProxyMod.c.

References ModDllNewCanonicalName().

Referenced by ClientSetTraceState(), and VConnOpenDev().

1043 {
1044  return ModDllNewCanonicalName(sModName);
1045 }
static char * ModDllNewCanonicalName(const char *sModName)
Make DLL canonical file name.
Definition: bsProxyMod.c:222
void ModOneTimeInit ( DListStr_T *  pDListLibPath)

The bsProxy interface module handling one-time initialization.

Parameters
pDListLibPathList of optional library search paths.

Definition at line 1013 of file bsProxyMod.c.

References BsModHashTbl, BsModMutex, BSPROXY_MOD_HASH_MAX, BSPROXY_MOD_HASH_MIN, ModDllInit(), and ModHashDeleteData().

Referenced by ClientSetTraceState(), and main().

1014 {
1015  // create mutex
1016  pthread_mutex_init(&BsModMutex, NULL);
1017 
1018  BsModHashTbl = hash_table_create(
1019  true, // dynamic table sizing
1020  (hashcount_t)BSPROXY_MOD_HASH_MIN, // minimum size
1021  (hashcount_t)BSPROXY_MOD_HASH_MAX, // maximum size
1022  NULL, // use default comparator function
1023  NULL, // use default hashing function
1024  ModHashDeleteData); // hash node data deletion function
1025 
1026  // turn off most asserts()
1027  hash_set_self_verify(false);
1028 
1029  // initialize DLL environment
1030  ModDllInit(pDListLibPath);
1031 }
#define BSPROXY_MOD_HASH_MAX
maximum hash table size
Definition: bsProxyMod.c:93
#define BSPROXY_MOD_HASH_MIN
minimum hash table size
Definition: bsProxyMod.c:92
static hash_t * BsModHashTbl
i/f module hash table
Definition: bsProxyMod.c:96
static void ModDllInit(DListStr_T *pDListLibPath)
Definition: bsProxyMod.c:175
static void ModHashDeleteData(void *sModUri, void *pModIF)
Delete hash node data callback.
Definition: bsProxyMod.c:524
static pthread_mutex_t BsModMutex
bsProxy module operations mutex
Definition: bsProxyMod.c:95
static bool_t ModTryLock ( )
inlinestatic

Try to lock module's global mutual exclusion.

Returns
Returns true if lock is acquired. Otherwise returns false if mutex already locked.

Definition at line 153 of file bsProxyMod.c.

References BsModMutex.

154 {
155  return pthread_mutex_trylock(&BsModMutex) == 0? true: false;
156 }
static pthread_mutex_t BsModMutex
bsProxy module operations mutex
Definition: bsProxyMod.c:95
void ModUnload ( BsProxyModIF_T pModIF)

Unload the interface module.

The module's reference count is decremented. If the count is zero, then the module's exit routine is called and the module dynamic library is unloaded from bsProxy server application.

Multi-Thread safe.

Parameters
pModIFExported module interface.

Definition at line 1191 of file bsProxyMod.c.

References BsProxyModIF_T::m_dllHandle, BsProxyModIF_T::m_fnModExit, BsProxyModIF_T::m_sModUri, BsProxyModIF_T::m_uRefCnt, ModDllClose(), ModHashDelete(), ModHashGetIF(), ModLock(), and ModUnlock().

Referenced by ClientSetTraceState(), VConnClose(), and VConnOpenDev().

1192 {
1193 
1194  ModLock();
1195 
1196  if( (pModIF = ModHashGetIF(pModIF->m_sModUri)) != NULL )
1197  {
1198  if( pModIF->m_uRefCnt > 0 )
1199  {
1200  pModIF->m_uRefCnt--;
1201  }
1202 
1203  if( pModIF->m_uRefCnt == 0 )
1204  {
1205  // call module specific exported exit function
1206  pModIF->m_fnModExit();
1207 
1208  // unload the dll
1209  ModDllClose(pModIF->m_sModUri, pModIF->m_dllHandle);
1210 
1211  LOGDIAG1("Interface Module \"%s\" unloaded (refcnt=%u).",
1212  pModIF->m_sModUri, pModIF->m_uRefCnt);
1213 
1214  // delete data
1215  ModHashDelete(pModIF);
1216  }
1217  else
1218  {
1219  LOGDIAG1("Interface Module \"%s\" detached (refcnt=%u).",
1220  pModIF->m_sModUri, pModIF->m_uRefCnt);
1221  }
1222  }
1223 
1224  ModUnlock();
1225 }
BsModExitFunc_P m_fnModExit
module exit
Definition: bsProxy.h:126
static void ModUnlock()
Unlock module&#39;s global mutual exclusion.
Definition: bsProxyMod.c:135
static void ModDllClose(const char *sModUri, void *dllHandle)
Unload the dynamic library from the application.
Definition: bsProxyMod.c:344
static void ModHashDelete(BsProxyModIF_T *pModIF)
Remove and delete the module data from the i/f module hash table.
Definition: bsProxyMod.c:560
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
static BsProxyModIF_T * ModHashGetIF(const char *sModUri)
Get the interface module&#39;s i/f from the module hash table.
Definition: bsProxyMod.c:583
void * m_dllHandle
dynamic library loader handle
Definition: bsProxy.h:123
static void ModLock()
Lock module&#39;s global mutual exclusion.
Definition: bsProxyMod.c:121

Variable Documentation

BsModProxyCb_T BsModProxyCallbacks
static
Initial value:
=
{
}
void ModCbSendRsp(BsVConnHnd_T hndVConn, BsTid_T uTid, BsMsgId_T uMsgId, byte_t bufRsp[], size_t uRspLen)
Send module-specific repsonse callback function.
Definition: bsProxyMod.c:614
void ModCbSendErrorRsp(BsVConnHnd_T hndVConn, BsTid_T uTid, int nECode, const char *sErrFmt,...)
Send generic error repsonse callback function.
Definition: bsProxyMod.c:671
const char * ModCbGetDevUri(BsVConnHnd_T hndVConn, char dest[], size_t n)
Copy the device URI associated with the virtual connection.
Definition: bsProxyMod.c:832
BsModIter_T * ModCbIterNext(BsModIter_T *pIter)
Get the next set of information given the iterator state.
Definition: bsProxyMod.c:877
void ModCbRsrcTblDelete(BsModRsrcTbl_T *pRsrcTbl)
Delete an allocated resource table.
Definition: bsProxyMod.c:726
void * ModCbRsrcRemove(BsModRsrcTbl_T *pRsrcTbl, BsVConnHnd_T hndVConn)
Remove a resource from the resource table.
Definition: bsProxyMod.c:795
int ModCbRsrcAdd(BsModRsrcTbl_T *pRsrcTbl, BsVConnHnd_T hndVConn, void *pRsrc)
Add a new resource to the resource table.
Definition: bsProxyMod.c:744
BsModRsrcTbl_T * ModCbRsrcTblNew(int nMaxResources)
Allocatae a new module resource table of fixed size.
Definition: bsProxyMod.c:703
void ModCbSendOkRsp(BsVConnHnd_T hndVConn, BsTid_T uTid)
Send generic ok repsonse callback function.
Definition: bsProxyMod.c:643
BsModIter_T * ModCbIterFirst(BsModIterOver_T eOver, const char *sPattern)
Start an iterator over the virtual connections matching the given pattern.
Definition: bsProxyMod.c:959
void ModCbIterDelete(BsModIter_T *pIter)
Delete the module iterator.
Definition: bsProxyMod.c:855

Interface module callbacks to bsProxy server services.

Definition at line 988 of file bsProxyMod.c.