Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends | List of all members
BIAS::CScommBase Class Reference

this class CScomm (ClientServer communciation) handles data comunication via TCP/IP for one server and multiple clients. More...

#include <NetworkComm/CScommBase.hh>

+ Inheritance diagram for BIAS::CScommBase:
+ Collaboration diagram for BIAS::CScommBase:

Public Member Functions

void AddDebugLevel (const long int lv)
 
void AddDebugLevel (const std::string &name)
 
 CScommBase (bool threaded=true)
 Set threaded to false for deactivating the receive-thread. More...
 
 CScommBase (const CScommBase &arg)
 copy ctor required due to ostream member with explicit ctor JW More...
 
bool DebugLevelIsSet (const long int lv) const
 
bool DebugLevelIsSet (const std::string &name) const
 
std::string GetCommPartnerName (int commPartnerNr)
 
returns the name of the commPartner with nr commPartnerNr or

empty string in case of a number, that is to high. More...

 
int GetConnections ()
 returns the number of connected commPartners More...
 
int GetData (const std::string &msgName, std::vector< float > &floatData)
 
returns instantly:<br>

0 if new data has arrived and the data in xxxData
1 if no new data has arrived, the data in xxxData stays unchanged
-1 in case of errors More...

 
int GetData (const std::string &msgName, std::vector< int > &intData)
 
int GetData (const std::string &msgName, std::vector< std::string > &stringData)
 
int GetData (const std::string &msgName, std::vector< char > &binaryData)
 
returns the binarydata in the vector, its wise to reserve enough

bytes, if the size is known. More...

 
int GetDebugLevel () const
 
std::ostream & GetDebugStream () const
 
void GetDebugStream (std::ostream &os) const
 
bool GetThreadedReceive ()
 
long int Name2DebugLevel (const std::string &name) const
 looks up a debuglevel in the internal map, returns 0 if not found More...
 
long int NewDebugLevel (const std::string &name)
 creates a new debuglevel More...
 
CScommBaseoperator= (const CScommBase &arg)
 assignment op. required due to ostream member with explicit ctor JW More...
 
void PrintDebugLevel (std::ostream &os=std::cout) const
 
int RegisterMsg (std::string msgName, EdataType dataType, int amount=1)
 
only registered msgs are accepted by commPartners

Register a msg with this functions. More...

 
int RegisterMsg (std::string commPartnerName, std::string msgName, EdataType dataType, int amount=1)
 same as above, but the msg is only accepted from a specific commPartner More...
 
void RemoveDebugLevel (const long int lv)
 
void RemoveDebugLevel (const std::string &name)
 
int SendMsg (const std::string &msgName, std::vector< float > &floatData)
 sends msg msgName to all connected commPartners. More...
 
int SendMsg (const std::string &msgName, std::vector< int > &intData)
 
int SendMsg (const std::string &msgName, std::vector< std::string > &stringData)
 
int SendMsg (const std::string &msgName, char *binaryData, unsigned int size)
 maximum size for binary data is 15MB! More...
 
int SendMsg (const std::string commPartnerName, const std::string &msgName, char *binaryData, unsigned int size)
 
like above, but msg is only send to one specific commPartner given

as string, which must equal the name given by the client to itself
More...

 
int SendMsg (int commPartnerNr, const std::string &msgName, char *binaryData, unsigned int size)
 
like above, but msg is only send to one specific commPartner given

as the index number of all connected partners More...

 
void SetAutomaticPortChange (bool on)
 
if set on, the server will take automatically the next free TCP port

for connections. More...

 
void SetConnectionTimeOut (unsigned int time)
 set the time in ms until a commPartner is assumed to be disconnected. More...
 
void SetDebugLevel (const long int lv)
 
void SetDebugLevel (const std::string &name)
 
void SetDebugStream (const std::ostream &os)
 
void SetKeepAll (bool on)
 If not threaded you can set this true to keep all received data. More...
 
void SetLog (bool on)
 
gives a lot of information regarding the connections, like the exact

data received and send. More...

 
void SetLogOut (std::ostream &outputStream)
 set the output of log msgs to another stream, e.g. More...
 
void SetVerbose (bool on)
 gives some information about establising conn, and disconnecting etc. More...
 
void SetVerboseOut (std::ostream &outputStream)
 set the output of verbose msgs to another stream, e.g. More...
 
void ShowDebugLevel (std::ostream &os=std::cout) const
 prints all internally known debuglevels More...
 
int WaitForNewMessage (std::string &msgName)
 
only if not threaded! <br>

Blocks until any new msg arrives, or returns instantly if a new message is already in a buffer slot. More...

 
virtual ~CScommBase ()
 

Static Public Member Functions

static long int GetGlobalDebugLevel ()
 
static void SetGlobalDebugLevel (long int lev)
 

Protected Member Functions

int AcceptConn_ (int fd)
 
int AnalyzeData_ (int clientNr)
 
return values:

0 no error all fine More...

 
int AnalyzeFirstMsg_ (int commNr)
 
int CheckRegisteredMsgs_ (std::string &msgName, const CScommData &cPartner, EdataType &dataTypeToRead, unsigned int &dataSizeToRead)
 
long ConsumeNextFreeDebuglevel_ ()
 returns the next available debuglevel More...
 
int GetData_ (const std::string &msgName)
 
void logOutPut_ (const char *data, unsigned int size)
 
void receiveThread_ (int clientNr)
 
int SendMsg_ (const std::string &data)
 
void syncReceive_ ()
 

Protected Attributes

long int _liDebugLevel
 
long int _liNextDebugLevel
 new concept, debuglevel are managed here in the debug class More...
 
std::map< std::string, long int > _String2Debuglevel
 
std::vector< CScommDatacommPartners_
 
int commPartnersConnected_
 
int connectPort_
 
bool isServer_
 
bool keepAll_
 
bool log_
 
std::ostream logOut_
 
std::vector< CScommMsgmsgBuffer_
 
std::string name_
 
bool onlyNewMsgs_
 
bool portChange_
 
std::vector< CScommMsgregisteredMsgs_
 
pthread_mutex_t registerLock_
 
bool terminateReceiver_
 
bool threaded_
 
unsigned int timeOut_
 
bool timeOutFlag_
 
bool verbose_
 
std::ostream verboseOut_
 

Static Protected Attributes

static std::ostream _zDebugStream
 
static long int GlobalDebugLevel = 0
 

Friends

void * receivethread (void *data)
 these are wrapper to comply to the pthread_create() interface More...
 

Detailed Description

this class CScomm (ClientServer communciation) handles data comunication via TCP/IP for one server and multiple clients.

It spawns threads itself for all communication. Messages can be registered by functions or within a file(not yet). See CScommClient or CScommServer for more details.

Author
Daniel Grest, Jan 2004

Definition at line 73 of file CScommBase.hh.

Constructor & Destructor Documentation

CScommBase::~CScommBase ( )
virtual

Definition at line 55 of file CScommBase.cpp.

CScommBase::CScommBase ( bool  threaded = true)

Set threaded to false for deactivating the receive-thread.

Data will be read from TCP-buffer only when GetData methods are called. Threaded receive is activated by default! Beware: if there's no msg in tcp-buffer, GetData will BLOCK until there is any new msg (this need not be the one GetData was called for, for example binary instead of int) if receive-buffer is completly filled, send blocks until msg can be delivered!

Definition at line 86 of file CScommBase.cpp.

References commPartners_, commPartnersConnected_, connectPort_, keepAll_, log_, onlyNewMsgs_, portChange_, registeredMsgs_, registerLock_, terminateReceiver_, threaded_, timeOut_, timeOutFlag_, and verbose_.

CScommBase::CScommBase ( const CScommBase arg)

copy ctor required due to ostream member with explicit ctor JW


Definition at line 124 of file CScommBase.cpp.

Member Function Documentation

int CScommBase::AcceptConn_ ( int  fd)
protected
void BIAS::Debug::AddDebugLevel ( const long int  lv)
inlineinherited
void BIAS::Debug::AddDebugLevel ( const std::string &  name)
inlineinherited

Definition at line 362 of file Debug.hh.

int CScommBase::AnalyzeData_ ( int  clientNr)
protected

return values:

0 no error all fine

warnings: 1 not enough data to analyze (<3 bytes) 2 no data after msg name 3 more data read than registered for a msg 4 less data read than registered A warning is returned if one of the above cases was true for any one msg in the buffer.

errors: -1 only trash received

Definition at line 564 of file CScommBase.cpp.

References CheckRegisteredMsgs_(), commPartners_, BIAS::CS_BINARY, BIAS::CS_FLOAT, BIAS::CS_INT, BIAS::CS_NONE, BIAS::CS_STRING, keepAll_, BIAS::CScommData::lastProcessed, BIAS::CScommData::msgBuffer, BIAS::CScommData::name, registeredMsgs_, and threaded_.

Referenced by receiveThread_(), and syncReceive_().

int CScommBase::AnalyzeFirstMsg_ ( int  commNr)
protected

Definition at line 763 of file CScommBase.cpp.

References commPartners_.

Referenced by receiveThread_(), and syncReceive_().

int CScommBase::CheckRegisteredMsgs_ ( std::string &  msgName,
const CScommData cPartner,
EdataType dataTypeToRead,
unsigned int &  dataSizeToRead 
)
protected

Definition at line 511 of file CScommBase.cpp.

References isServer_, BIAS::CScommData::name, registeredMsgs_, registerLock_, and threaded_.

Referenced by AnalyzeData_().

long BIAS::Debug::ConsumeNextFreeDebuglevel_ ( )
inlineprotectedinherited

returns the next available debuglevel

Author
woelk 09/2006

Definition at line 521 of file Debug.hh.

bool BIAS::Debug::DebugLevelIsSet ( const long int  lv) const
inlineinherited
bool BIAS::Debug::DebugLevelIsSet ( const std::string &  name) const
inlineinherited

Definition at line 350 of file Debug.hh.

std::string CScommBase::GetCommPartnerName ( int  commPartnerNr)

returns the name of the commPartner with nr commPartnerNr or

empty string in case of a number, that is to high.

Definition at line 1027 of file CScommBase.cpp.

References commPartners_.

Referenced by SendMsg().

int BIAS::CScommBase::GetConnections ( )
inline

returns the number of connected commPartners

Definition at line 179 of file CScommBase.hh.

Referenced by BIAS::VideoTransmitter::Process().

int CScommBase::GetData ( const std::string &  msgName,
std::vector< float > &  floatData 
)

returns instantly:<br>

0 if new data has arrived and the data in xxxData
1 if no new data has arrived, the data in xxxData stays unchanged
-1 in case of errors

Examples:
ExampleNetworkClient.cpp, and ExampleNetworkServer.cpp.

Definition at line 170 of file CScommBase.cpp.

References GetData_(), registeredMsgs_, syncReceive_(), and threaded_.

Referenced by BIAS::VideoSource_Net::GrabSingle(), and BIAS::VideoSource_Kinect2Net::runner().

int CScommBase::GetData ( const std::string &  msgName,
std::vector< int > &  intData 
)

Definition at line 197 of file CScommBase.cpp.

References GetData_(), registeredMsgs_, syncReceive_(), and threaded_.

int CScommBase::GetData ( const std::string &  msgName,
std::vector< std::string > &  stringData 
)

Definition at line 225 of file CScommBase.cpp.

References GetData_(), registeredMsgs_, syncReceive_(), and threaded_.

int CScommBase::GetData ( const std::string &  msgName,
std::vector< char > &  binaryData 
)

returns the binarydata in the vector, its wise to reserve enough

bytes, if the size is known.

Definition at line 253 of file CScommBase.cpp.

References GetData_(), registeredMsgs_, syncReceive_(), and threaded_.

int CScommBase::GetData_ ( const std::string &  msgName)
protected

Definition at line 143 of file CScommBase.cpp.

References registeredMsgs_.

Referenced by GetData().

int BIAS::Debug::GetDebugLevel ( ) const
inlineinherited
std::ostream& BIAS::Debug::GetDebugStream ( ) const
inlineinherited

Definition at line 405 of file Debug.hh.

void BIAS::Debug::GetDebugStream ( std::ostream &  os) const
inlineinherited

Definition at line 414 of file Debug.hh.

static long int BIAS::Debug::GetGlobalDebugLevel ( )
inlinestaticinherited

Definition at line 431 of file Debug.hh.

bool BIAS::CScommBase::GetThreadedReceive ( )
inline

Definition at line 200 of file CScommBase.hh.

void CScommBase::logOutPut_ ( const char *  data,
unsigned int  size 
)
protected

Definition at line 805 of file CScommBase.cpp.

References log_, and logOut_.

Referenced by receiveThread_(), SendMsg_(), and syncReceive_().

long int BIAS::Debug::Name2DebugLevel ( const std::string &  name) const
inlineinherited

looks up a debuglevel in the internal map, returns 0 if not found

Author
woelk 09/2006

Definition at line 454 of file Debug.hh.

long int BIAS::Debug::NewDebugLevel ( const std::string &  name)
inlineinherited
CScommBase & CScommBase::operator= ( const CScommBase arg)

assignment op. required due to ostream member with explicit ctor JW

Definition at line 133 of file CScommBase.cpp.

void BIAS::Debug::PrintDebugLevel ( std::ostream &  os = std::cout) const
inlineinherited

Definition at line 383 of file Debug.hh.

void CScommBase::receiveThread_ ( int  clientNr)
protected
int CScommBase::RegisterMsg ( std::string  msgName,
EdataType  dataType,
int  amount = 1 
)

only registered msgs are accepted by commPartners

Register a msg with this functions.

Amount is the number of dataTypes for this msg, for binarydata this is not necessary
The msg will be bound to the first commPartner sending such a msg. And will be accepted only from him later.
All msgNames have to be unique!

return zero on success <0 on errors

Examples:
ExampleNetworkClient.cpp, and ExampleNetworkServer.cpp.

Definition at line 315 of file CScommBase.cpp.

Referenced by BIAS::VideoSource_Net::OpenDevice(), and BIAS::VideoSource_Kinect2Net::runner().

int CScommBase::RegisterMsg ( std::string  commPartnerName,
std::string  msgName,
EdataType  dataType,
int  amount = 1 
)

same as above, but the msg is only accepted from a specific commPartner

Definition at line 321 of file CScommBase.cpp.

References BIAS::CScommMsg::commName, BIAS::CS_BINARY, BIAS::CScommMsg::dataSize, BIAS::CScommMsg::dataType, isServer_, BIAS::CScommMsg::msgName, registeredMsgs_, registerLock_, BIAS::CScommMsg::server, and threaded_.

void BIAS::Debug::RemoveDebugLevel ( const long int  lv)
inlineinherited

Definition at line 369 of file Debug.hh.

void BIAS::Debug::RemoveDebugLevel ( const std::string &  name)
inlineinherited

Definition at line 376 of file Debug.hh.

int CScommBase::SendMsg ( const std::string &  msgName,
std::vector< float > &  floatData 
)

sends msg msgName to all connected commPartners.

Msg doesn't have to be registered on the sender side, but must be registered on the receiver side!

Examples:
ExampleNetworkClient.cpp, and ExampleNetworkServer.cpp.

Definition at line 358 of file CScommBase.cpp.

References SendMsg_().

Referenced by BIAS::VideoTransmitter::Process(), and SendMsg().

int CScommBase::SendMsg ( const std::string &  msgName,
std::vector< int > &  intData 
)

Definition at line 371 of file CScommBase.cpp.

References SendMsg_().

int CScommBase::SendMsg ( const std::string &  msgName,
std::vector< std::string > &  stringData 
)

Definition at line 384 of file CScommBase.cpp.

References SendMsg_().

int CScommBase::SendMsg ( const std::string &  msgName,
char *  binaryData,
unsigned int  size 
)

maximum size for binary data is 15MB!

Definition at line 423 of file CScommBase.cpp.

References commPartners_, log_, and logOut_.

int CScommBase::SendMsg ( const std::string  commPartnerName,
const std::string &  msgName,
char *  binaryData,
unsigned int  size 
)

like above, but msg is only send to one specific commPartner given

as string, which must equal the name given by the client to itself

Returns
-2 if no such commPartner, 0 in case of no error

Definition at line 460 of file CScommBase.cpp.

References commPartners_, GetCommPartnerName(), and SendMsg().

int CScommBase::SendMsg ( int  commPartnerNr,
const std::string &  msgName,
char *  binaryData,
unsigned int  size 
)

like above, but msg is only send to one specific commPartner given

as the index number of all connected partners

Returns
-2 if no such commPartner, 0 in case of no error

Definition at line 472 of file CScommBase.cpp.

References commPartners_, log_, and logOut_.

int CScommBase::SendMsg_ ( const std::string &  data)
protected

Definition at line 395 of file CScommBase.cpp.

References commPartners_, log_, logOut_, and logOutPut_().

Referenced by SendMsg().

void BIAS::CScommBase::SetAutomaticPortChange ( bool  on)
inline

if set on, the server will take automatically the next free TCP port

for connections.

The client tries 10 consecutive different ports to conncect the server. Default is off. Useful if you don't care about port numbers and just want a connection somehow.

Definition at line 197 of file CScommBase.hh.

void BIAS::CScommBase::SetConnectionTimeOut ( unsigned int  time)
inline

set the time in ms until a commPartner is assumed to be disconnected.


If time is zero there is no timeOut. default is no Time out

Definition at line 189 of file CScommBase.hh.

void BIAS::Debug::SetDebugLevel ( const long int  lv)
inlineinherited
void BIAS::Debug::SetDebugLevel ( const std::string &  name)
inlineinherited

Definition at line 325 of file Debug.hh.

void BIAS::Debug::SetDebugStream ( const std::ostream &  os)
inlineinherited

Definition at line 398 of file Debug.hh.

static void BIAS::Debug::SetGlobalDebugLevel ( long int  lev)
inlinestaticinherited

Definition at line 424 of file Debug.hh.

void BIAS::CScommBase::SetKeepAll ( bool  on)
inline

If not threaded you can set this true to keep all received data.

Else a call of GetData() will deliver the newest msg only. Default is off. If threaded this is useless... (always off)

Definition at line 101 of file CScommBase.hh.

Referenced by BIAS::VideoTransmitter::Init(), and BIAS::VideoSource_Net::VideoSource_Net().

void CScommBase::SetLog ( bool  on)

gives a lot of information regarding the connections, like the exact

data received and send.

Implies verbose.

Examples:
ExampleNetworkClient.cpp.

Definition at line 1008 of file CScommBase.cpp.

References log_, and verbose_.

void CScommBase::SetLogOut ( std::ostream &  outputStream)

set the output of log msgs to another stream, e.g.

to a file. Default ist cout.

Definition at line 1013 of file CScommBase.cpp.

References logOut_.

void CScommBase::SetVerbose ( bool  on)

gives some information about establising conn, and disconnecting etc.

Examples:
ExampleNetworkClient.cpp, and ExampleNetworkServer.cpp.

Definition at line 1018 of file CScommBase.cpp.

References verbose_.

Referenced by BIAS::VideoSource_Net::OpenDevice(), and BIAS::VideoSource_Kinect2Net::runner().

void CScommBase::SetVerboseOut ( std::ostream &  outputStream)

set the output of verbose msgs to another stream, e.g.

to a file. Default ist cout.

Definition at line 1022 of file CScommBase.cpp.

References verboseOut_.

void BIAS::Debug::ShowDebugLevel ( std::ostream &  os = std::cout) const
inlineinherited

prints all internally known debuglevels

Author
woelk 09/2006

Definition at line 496 of file Debug.hh.

void CScommBase::syncReceive_ ( )
protected
int CScommBase::WaitForNewMessage ( std::string &  msgName)

only if not threaded! <br>

Blocks until any new msg arrives, or returns instantly if a new message is already in a buffer slot.

The msg name is returned in msgName.

The return value indicates how many additional new msgs are in the buffer slots, e.g. if 0 the msgName is the only new message.

Returns
<0 if an error occured, >=0 the amount of new msgs in other slots.

Definition at line 284 of file CScommBase.cpp.

References registeredMsgs_, syncReceive_(), and threaded_.

Friends And Related Function Documentation

void* receivethread ( void *  data)
friend

these are wrapper to comply to the pthread_create() interface

Referenced by AcceptConn_().

Member Data Documentation

long int BIAS::Debug::_liDebugLevel
protectedinherited

Definition at line 510 of file Debug.hh.

Referenced by BIAS::Debug::operator=(), and BIAS::ImageBase::operator=().

long int BIAS::Debug::_liNextDebugLevel
protectedinherited

new concept, debuglevel are managed here in the debug class

Definition at line 516 of file Debug.hh.

Referenced by BIAS::Debug::operator=().

std::map<std::string, long int> BIAS::Debug::_String2Debuglevel
protectedinherited

Definition at line 517 of file Debug.hh.

Referenced by BIAS::Debug::operator=().

std::ostream BIAS::Debug::_zDebugStream
staticprotectedinherited

Definition at line 511 of file Debug.hh.

Referenced by BIAS::Debug::operator=().

std::vector<CScommData> BIAS::CScommBase::commPartners_
protected
int BIAS::CScommBase::commPartnersConnected_
protected

Definition at line 277 of file CScommBase.hh.

Referenced by AcceptConn_(), CScommBase(), receiveThread_(), and syncReceive_().

int BIAS::CScommBase::connectPort_
protected
long int BIAS::Debug::GlobalDebugLevel = 0
staticprotectedinherited

Definition at line 513 of file Debug.hh.

bool BIAS::CScommBase::isServer_
protected
bool BIAS::CScommBase::keepAll_
protected

Definition at line 286 of file CScommBase.hh.

Referenced by AnalyzeData_(), and CScommBase().

bool BIAS::CScommBase::log_
protected
std::ostream BIAS::CScommBase::logOut_
protected
std::vector<CScommMsg> BIAS::CScommBase::msgBuffer_
protected

Definition at line 299 of file CScommBase.hh.

std::string BIAS::CScommBase::name_
protected
bool BIAS::CScommBase::onlyNewMsgs_
protected

Definition at line 280 of file CScommBase.hh.

Referenced by CScommBase().

bool BIAS::CScommBase::portChange_
protected
std::vector<CScommMsg> BIAS::CScommBase::registeredMsgs_
protected
pthread_mutex_t BIAS::CScommBase::registerLock_
protected

Definition at line 297 of file CScommBase.hh.

Referenced by CheckRegisteredMsgs_(), CScommBase(), and RegisterMsg().

bool BIAS::CScommBase::terminateReceiver_
protected

Definition at line 296 of file CScommBase.hh.

Referenced by CScommBase(), and receiveThread_().

bool BIAS::CScommBase::threaded_
protected
unsigned int BIAS::CScommBase::timeOut_
protected

Definition at line 294 of file CScommBase.hh.

Referenced by CScommBase().

bool BIAS::CScommBase::timeOutFlag_
protected

Definition at line 295 of file CScommBase.hh.

Referenced by CScommBase().

bool BIAS::CScommBase::verbose_
protected
std::ostream BIAS::CScommBase::verboseOut_
protected

The documentation for this class was generated from the following files: