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 | List of all members
BIAS::CScommClient Class Reference

class for sending/receiving data between clients and servers More...

#include <NetworkComm/CScommClient.hh>

+ Inheritance diagram for BIAS::CScommClient:
+ Collaboration diagram for BIAS::CScommClient:

Public Member Functions

void AddDebugLevel (const long int lv)
 
void AddDebugLevel (const std::string &name)
 
int ConnectServer (std::string serverName, unsigned int port=D_CS_DEFAULT_PORT, unsigned int timeOut=20000)
 for a client: tries to connect to server with hostname serverName. More...
 
 CScommClient (bool threaded=true)
 Set threaded to false for deactivating the receive-thread. 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...
 
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 SetName (std::string clientName)
 give the client a name, default ist $HOSTNAME 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...

 
 ~CScommClient ()
 

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
 

Detailed Description

class for sending/receiving data between clients and servers

Only registered msgs will be accepted at the server side, similar the client will accept only registered msgs from the server too. The client needs to know the hostname of the server. Usage:

  1. Konstruktor: CScommClient client;
  2. Register msgs: client.RegisterMsg(msgName, BIAS::CS_FLOAT, 3);
  3. connect server: client.ConnectServer("visu1");
  4. send and/or get Data: client.SendMsg(msgName,vecOfFloats);
    client.GetData(msgName, newVecOffloats);

See the examples also.

Author
Daniel Grest, Jan. 2004
Examples:
ExampleNetworkClient.cpp.

Definition at line 54 of file CScommClient.hh.

Constructor & Destructor Documentation

CScommClient::CScommClient ( 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 55 of file CScommClient.cpp.

References BIAS::CScommBase::isServer_, and BIAS::CScommBase::name_.

BIAS::CScommClient::~CScommClient ( )
inline

Definition at line 67 of file CScommClient.hh.

Member Function Documentation

int CScommBase::AcceptConn_ ( int  fd)
protectedinherited
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)
protectedinherited

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 BIAS::CScommBase::CheckRegisteredMsgs_(), BIAS::CScommBase::commPartners_, BIAS::CS_BINARY, BIAS::CS_FLOAT, BIAS::CS_INT, BIAS::CS_NONE, BIAS::CS_STRING, BIAS::CScommBase::keepAll_, BIAS::CScommData::lastProcessed, BIAS::CScommData::msgBuffer, BIAS::CScommData::name, BIAS::CScommBase::registeredMsgs_, and BIAS::CScommBase::threaded_.

Referenced by BIAS::CScommBase::receiveThread_(), and BIAS::CScommBase::syncReceive_().

int CScommBase::AnalyzeFirstMsg_ ( int  commNr)
protectedinherited
int CScommBase::CheckRegisteredMsgs_ ( std::string &  msgName,
const CScommData cPartner,
EdataType dataTypeToRead,
unsigned int &  dataSizeToRead 
)
protectedinherited
int CScommClient::ConnectServer ( std::string  serverName,
unsigned int  port = D_CS_DEFAULT_PORT,
unsigned int  timeOut = 20000 
)

for a client: tries to connect to server with hostname serverName.

Parameters
timeOutin ms
serverNamehostName of the server as ip or name
Returns
0 on success, -1 on timeOut, < -1 on other errors
Examples:
ExampleNetworkClient.cpp.

Definition at line 63 of file CScommClient.cpp.

References BIAS::CScommBase::AcceptConn_(), BIAS::CScommBase::commPartners_, BIAS::CScommBase::connectPort_, BIAS::CScommBase::log_, BIAS::CScommBase::logOut_, BIAS::CScommBase::name_, BIAS::CScommBase::portChange_, BIAS::CScommBase::verbose_, and BIAS::CScommBase::verboseOut_.

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

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)
inherited

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 BIAS::CScommBase::commPartners_.

Referenced by BIAS::CScommBase::SendMsg().

int BIAS::CScommBase::GetConnections ( )
inlineinherited

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 
)
inherited

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 BIAS::CScommBase::GetData_(), BIAS::CScommBase::registeredMsgs_, BIAS::CScommBase::syncReceive_(), and BIAS::CScommBase::threaded_.

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

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

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 BIAS::CScommBase::GetData_(), BIAS::CScommBase::registeredMsgs_, BIAS::CScommBase::syncReceive_(), and BIAS::CScommBase::threaded_.

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

Definition at line 143 of file CScommBase.cpp.

References BIAS::CScommBase::registeredMsgs_.

Referenced by BIAS::CScommBase::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 ( )
inlineinherited

Definition at line 200 of file CScommBase.hh.

void CScommBase::logOutPut_ ( const char *  data,
unsigned int  size 
)
protectedinherited
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
void BIAS::Debug::PrintDebugLevel ( std::ostream &  os = std::cout) const
inlineinherited

Definition at line 383 of file Debug.hh.

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

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 
)
inherited
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 
)
inherited

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 BIAS::CScommBase::SendMsg_().

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

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

Definition at line 371 of file CScommBase.cpp.

References BIAS::CScommBase::SendMsg_().

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

Definition at line 384 of file CScommBase.cpp.

References BIAS::CScommBase::SendMsg_().

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

maximum size for binary data is 15MB!

Definition at line 423 of file CScommBase.cpp.

References BIAS::CScommBase::commPartners_, BIAS::CScommBase::log_, and BIAS::CScommBase::logOut_.

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

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 BIAS::CScommBase::commPartners_, BIAS::CScommBase::GetCommPartnerName(), and BIAS::CScommBase::SendMsg().

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

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 BIAS::CScommBase::commPartners_, BIAS::CScommBase::log_, and BIAS::CScommBase::logOut_.

int CScommBase::SendMsg_ ( const std::string &  data)
protectedinherited
void BIAS::CScommBase::SetAutomaticPortChange ( bool  on)
inlineinherited

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)
inlineinherited

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)
inlineinherited

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)
inherited

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 BIAS::CScommBase::log_, and BIAS::CScommBase::verbose_.

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

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 BIAS::CScommBase::logOut_.

void BIAS::CScommClient::SetName ( std::string  clientName)
inline

give the client a name, default ist $HOSTNAME

Examples:
ExampleNetworkClient.cpp.

Definition at line 71 of file CScommClient.hh.

void CScommBase::SetVerbose ( bool  on)
inherited

gives some information about establising conn, and disconnecting etc.

Examples:
ExampleNetworkClient.cpp, and ExampleNetworkServer.cpp.

Definition at line 1018 of file CScommBase.cpp.

References BIAS::CScommBase::verbose_.

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

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

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 BIAS::CScommBase::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_ ( )
protectedinherited
int CScommBase::WaitForNewMessage ( std::string &  msgName)
inherited

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 BIAS::CScommBase::registeredMsgs_, BIAS::CScommBase::syncReceive_(), and BIAS::CScommBase::threaded_.

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_
protectedinherited
int BIAS::CScommBase::commPartnersConnected_
protectedinherited
int BIAS::CScommBase::connectPort_
protectedinherited
long int BIAS::Debug::GlobalDebugLevel = 0
staticprotectedinherited

Definition at line 513 of file Debug.hh.

bool BIAS::CScommBase::isServer_
protectedinherited
bool BIAS::CScommBase::keepAll_
protectedinherited

Definition at line 286 of file CScommBase.hh.

Referenced by BIAS::CScommBase::AnalyzeData_(), and BIAS::CScommBase::CScommBase().

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

Definition at line 299 of file CScommBase.hh.

std::string BIAS::CScommBase::name_
protectedinherited

Definition at line 293 of file CScommBase.hh.

Referenced by ConnectServer(), and CScommClient().

bool BIAS::CScommBase::onlyNewMsgs_
protectedinherited

Definition at line 280 of file CScommBase.hh.

Referenced by BIAS::CScommBase::CScommBase().

bool BIAS::CScommBase::portChange_
protectedinherited

Definition at line 284 of file CScommBase.hh.

Referenced by ConnectServer(), BIAS::CScommBase::CScommBase(), and BIAS::listenthread().

std::vector<CScommMsg> BIAS::CScommBase::registeredMsgs_
protectedinherited
pthread_mutex_t BIAS::CScommBase::registerLock_
protectedinherited
bool BIAS::CScommBase::terminateReceiver_
protectedinherited
bool BIAS::CScommBase::threaded_
protectedinherited
unsigned int BIAS::CScommBase::timeOut_
protectedinherited

Definition at line 294 of file CScommBase.hh.

Referenced by BIAS::CScommBase::CScommBase().

bool BIAS::CScommBase::timeOutFlag_
protectedinherited

Definition at line 295 of file CScommBase.hh.

Referenced by BIAS::CScommBase::CScommBase().

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

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