AURobotServers  4
Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions | Private Attributes | List of all members
UClientHandler Class Reference

#include <uclienthandler.h>

Inheritance diagram for UClientHandler:
Inheritance graph

Public Member Functions

unsigned long addDataTrap (const char *key, EVENT_CALL onEvent, void *object)
 
bool addFunction (UClientFuncBase *functionHandler, bool remove=false)
 
void closeConnection ()
 
virtual void connectionChange (bool nowConnected)
 
void delDataTrap (unsigned long serial)
 
bool getNamespaceUse ()
 
const char * getServerNamespaceName ()
 
double getServerToLocalTime ()
 
bool isServerToLocalTimeValid ()
 
bool isThreadRunning ()
 
bool openConnection ()
 
void print (const char *preString)
 
void sendPing ()
 
void setHost (const char *host)
 
void setNamespace (const char *toName, const char *attributeString)
 
void setNamespaceLost ()
 
void setNamespaceUse (bool value)
 
void setPort (const int port)
 
void setVerbose (bool value)
 
const char * snprint (const char *preString, char *buff, const int buffCnt)
 
bool start ()
 
void stop (bool andWait)
 
void threadRunLoop ()
 
 UClientHandler ()
 
virtual ~UClientHandler ()
 
- Public Member Functions inherited from UClientPortSml
virtual const char * getSourceName ()
 
 UClientPortSml ()
 
- Public Member Functions inherited from UClientPort
bool blockSend (const char *buffer, int length)
 
void closeConnection ()
 
int getDataFromLine (char *buffer, int bufferSize, int pollTimeoutMs)
 
const char * getHost ()
 
const char * getHostIP ()
 
int getPort ()
 
bool isConnected ()
 
bool isVerbose ()
 
bool sendMsg (const char *message)
 
bool sendWithLock (const char *message)
 
void setHost (const char *toHost)
 
void setPort (int toPort)
 
void setVerbose (bool value)
 
bool tryConnect ()
 
 UClientPort ()
 
virtual ~UClientPort ()
 
- Public Member Functions inherited from ULock
bool lock ()
 
void lockInit ()
 
void post ()
 
bool tryLock ()
 
bool tryWait ()
 
 ULock ()
 
void unlock ()
 
bool wait ()
 
 ~ULock ()
 
- Public Member Functions inherited from USmlSource
void allowTagAtStartOfLineOnly ()
 
const char * getErrorBuffer ()
 
virtual int getLineNumber ()
 
virtual int getNBytes (char *buffer, int n, int msTimeout=100)
 
virtual bool getNextTag (USmlTag *tag, int msTimeout=100, USmlTagIn *failEndTag=NULL, char *beforeTagBuffer=NULL, int *beforeTagCnt=NULL)
 
char * getRxDataBuffer ()
 
int getRxDataBufferCnt ()
 
bool getToEndTag (USmlTagIn *tag, char *buffer, const int bufferSize, int msTimeout=100, USmlTagIn *lastTag=NULL)
 
bool isErrorText ()
 
bool isVerbose ()
 
virtual bool logSourceData (const char *, int)
 
void setErrorBuffer (char *buf, const int bufCnt)
 
virtual void setLineNumber (int)
 
int setRxDataBufferCnt (int newMaxLength)
 
virtual bool skipNBytes (int n, int msTimeout=100)
 
virtual bool skipToEndTag (USmlTagIn *tag, int msTimeout=100)
 
virtual void syntaxError (const char *message)
 
 USmlSource ()
 
virtual ~USmlSource ()
 

Public Attributes

bool showInfo
 
bool showWarnings
 
bool tryHoldConnection
 
- Public Attributes inherited from UClientPort
int rxByteCnt
 
ULock tx
 
- Public Attributes inherited from USmlSource
const char * ifName
 

Static Public Attributes

static const int MAX_DATA_TRAPS = 100
 

Protected Member Functions

virtual void interfaceAlive ()
 
virtual void interfaceAliveTag (USmlTag *)
 
virtual void interfaceTick ()
 
void sendNamespaceCloseTag ()
 
virtual void sendNamespaceOpenTag ()
 
- Protected Member Functions inherited from UClientPortSml
virtual bool doVerboseMessages ()
 
virtual int getMoreData (char *buffer, int bufferSize, int pollTimeoutMs)
 
virtual bool isSourceAvailable ()
 
virtual bool outputData (const char *message)
 
- Protected Member Functions inherited from USmlSource
void clearRxBuffer ()
 

Protected Attributes

bool binData
 is all data binary More...
 
char binTagName [MAX_BIN_NAME_LENGTH+1]
 
char namespaceAttributes [MAX_NORMAL_MESSAGE_LENGTH]
 
char namespaceName [MAX_SML_NAME_LENGTH]
 
bool namespaceUse
 
int notConnectedLoop
 
int rxTimeoutMs
 rx timeout value in ms More...
 
int serverNamespaceLevel
 
char serverNamespaceName [MAX_SML_NAME_LENGTH]
 
bool tryReConnect
 
- Protected Attributes inherited from UClientPort
enum UClientPort:: { ... }  connectionLostReason
 
UTime connectTime
 
char host [MAX_HOST_LENGTH]
 
char hostIP [MAX_HOST_LENGTH]
 
int port
 
unsigned int readCnt
 
bool verboseMessages
 
- Protected Attributes inherited from USmlSource
int dataCnt
 
char * errorBuffer
 
int errorBufferCnt
 
bool gotFirstTag
 
bool tagStartAtStartOfLineOnly
 

Static Protected Attributes

static const int MAX_BIN_NAME_LENGTH = 30
 binary tag name More...
 

Private Member Functions

void doTimeTick ()
 
void gotNewData (USmlTag *tag)
 
void handlePingReply (USmlTagIn *tag)
 
bool inThisStringList (const char *str, const char *strList)
 

Private Attributes

UClientFuncBasebaseHandler
 
UTime dataTime
 
UEventsdataTrap
 
UClientFuncBasefunc [MAX_FUNCTION_COUNT_CLIENT]
 
int funcCnt
 
ULock moduleLock
 
double serverToLocalTime
 
bool serverToLocalTimeValid
 
pthread_t threadHandle
 
bool threadRunning
 
bool threadStop
 

Additional Inherited Members

- Protected Types inherited from UClientPort
enum  { notLost, forReopen, rxError, txError }
 

Detailed Description

Services a client port and transfers data to the appropriate data functions

Author
Christian Andersen

Constructor & Destructor Documentation

UClientHandler::UClientHandler ( )
UClientHandler::~UClientHandler ( )
virtual

Destructor

References baseHandler, stop(), and threadRunning.

Member Function Documentation

unsigned long UClientHandler::addDataTrap ( const char *  key,
EVENT_CALL  onEvent,
void *  object 
)

Add a data trap - to get data from the stream for other purposes. The trap will be effective until deleted.

References UEvents::add(), dataTrap, and MAX_DATA_TRAPS.

Referenced by getServerNamespaceName(), and UFunctionIf::handleIf().

bool UClientHandler::addFunction ( UClientFuncBase functionHandler,
bool  remove = false 
)

Add function to handle some of the incomming data. If a function handles exactly the same tags, then it will be replaced with this new handler. If 'remove' is true, then handler will be removed from the list. Returns true if action is performed. Prints on console if not.

References UClientFuncBase::commandList(), func, funcCnt, ULock::lock(), MAX_FUNCTION_COUNT_CLIENT, moduleLock, and ULock::unlock().

Referenced by clientCmdLine(), UResIf::setResource(), testSmlClient(), UClientHandler(), and ULaserSim::ULaserSim().

void UClientHandler::closeConnection ( )
void UClientHandler::connectionChange ( bool  nowConnected)
virtual

Is called when connection status is changed. NB! if overwritten, then remember to call same function from ancestor class.

Reimplemented from UClientPort.

Reimplemented in UResIf, UResLaserIf, and UResCamIf.

References UClientPort::connectionChange(), USmlSource::gotFirstTag, UClientPort::host, UClientPort::port, serverToLocalTime, serverToLocalTimeValid, setNamespaceLost(), and tryHoldConnection.

Referenced by UResIf::connectionChange(), and isServerToLocalTimeValid().

void UClientHandler::delDataTrap ( unsigned long  serial)

Delete a data trap

References dataTrap, and UEvents::del().

Referenced by getServerNamespaceName(), and UFunctionIf::handleIf().

void UClientHandler::doTimeTick ( )
private

Tell each client function that time has passed and connection is idle. Can be used to update the GUI

References func, funcCnt, interfaceTick(), ULock::lock(), moduleLock, threadStop, and ULock::unlock().

Referenced by interfaceAlive(), and threadRunLoop().

bool UClientHandler::getNamespaceUse ( )
inline

Get setting of namespaceUse flag

References namespaceUse.

const char* UClientHandler::getServerNamespaceName ( )
inline

Get the last communicated server namespace name. Also available after the connection is closed.

References addDataTrap(), delDataTrap(), UClientPort::host, openConnection(), UClientPort::port, serverNamespaceName, setHost(), and setPort().

double UClientHandler::getServerToLocalTime ( )
inline

Get the time offset that needs to be added to servertime to get local time.

References serverToLocalTime.

void UClientHandler::gotNewData ( USmlTag tag)
private
void UClientHandler::handlePingReply ( USmlTagIn tag)
private
virtual void UClientHandler::interfaceAlive ( )
inlineprotectedvirtual

Alive from other tags

Reimplemented in UResIf.

References doTimeTick(), gotNewData(), handlePingReply(), and inThisStringList().

Referenced by gotNewData().

virtual void UClientHandler::interfaceAliveTag ( USmlTag )
inlineprotectedvirtual

Alive tick from receives data loop

Reimplemented in UResIf.

Referenced by gotNewData().

virtual void UClientHandler::interfaceTick ( )
inlineprotectedvirtual

Time tick from receive data loop

Reimplemented in UResIf.

Referenced by doTimeTick().

bool UClientHandler::inThisStringList ( const char *  str,
const char *  strList 
)
private

Find if this string is one of the strings in the strlist

Referenced by gotNewData(), and interfaceAlive().

bool UClientHandler::isServerToLocalTimeValid ( )
inline

Is time difference between server and client valid?

References connectionChange(), sendPing(), serverToLocalTimeValid, setNamespace(), and setNamespaceLost().

bool UClientHandler::isThreadRunning ( )
inline

Is the read thread running?. Returns true if running

References threadRunning.

Referenced by ULaserSim::openPort().

bool UClientHandler::openConnection ( )

Get number of loaded functions Get pointer to function with this index Set hold connection - this should result in a connection attempt within 0.05 secs. and an attempt to maintain connection with an attempt every abot 1-1.5 second.

References UClientPort::isConnected(), start(), threadRunning, tryReConnect, and Wait().

Referenced by clientCmdLine(), getServerNamespaceName(), ULaserSim::openPort(), and testSmlClient().

void UClientHandler::print ( const char *  preString)
void UClientHandler::sendNamespaceCloseTag ( )
protected

Sent the namespace close tag

References MAX_SML_NAME_LENGTH, namespaceName, namespaceUse, and UClientPort::sendMsg().

Referenced by closeConnection(), and gotNewData().

void UClientHandler::sendNamespaceOpenTag ( )
protectedvirtual

Send namespace open tag with loaded attribute string

Reimplemented from UClientPort.

References MAX_NORMAL_MESSAGE_LENGTH, MAX_SML_NAME_LENGTH, namespaceAttributes, namespaceName, namespaceUse, and UClientPort::sendMsg().

Referenced by gotNewData().

void UClientHandler::sendPing ( )

Send a ping command to server to get time difference between servers.

References UTime::getMicrosec(), UTime::getSec(), UTime::Now(), and UClientPort::sendMsg().

Referenced by clientCmdLine(), isServerToLocalTimeValid(), and threadRunLoop().

void UClientHandler::setHost ( const char *  host)

Set host, and if the host is new, then reopen to the new host

References UClientPort::host, UClientPort::setHost(), and tryReConnect.

Referenced by clientCmdLine(), getServerNamespaceName(), UResIf::openConnection(), ULaserSim::openPort(), and testSmlClient().

void UClientHandler::setNamespace ( const char *  toName,
const char *  attributeString 
)

Set namespace name, and a string with XML coded attributes. Name may be line "clientTalk". Name may not be empty (default is "client"). Attributes may be e.g. "name=\"myclient" version="1.0" ", remember the quotes for the value part of attribute names. Attribute string may be an empty string.

References MAX_NORMAL_MESSAGE_LENGTH, MAX_SML_NAME_LENGTH, namespaceAttributes, and namespaceName.

Referenced by clientCmdLine(), UFunctionIf::createResources(), isServerToLocalTimeValid(), testSmlClient(), and UClientHandler().

void UClientHandler::setNamespaceLost ( )

Set server namespace lost - i.e. waiting for new namespace from server

References serverNamespaceLevel, and serverNamespaceName.

Referenced by connectionChange(), and isServerToLocalTimeValid().

void UClientHandler::setNamespaceUse ( bool  value)
inline

Should namespace opening and closing tags be used?

References namespaceUse.

Referenced by clientCmdLine().

void UClientHandler::setPort ( const int  port)

Set port, and if the port is new, then reopen to the new host

References UClientPort::port, UClientPort::setPort(), and tryReConnect.

Referenced by clientCmdLine(), getServerNamespaceName(), UResIf::openConnection(), ULaserSim::openPort(), and testSmlClient().

void UClientHandler::setVerbose ( bool  value)

Set verbose flag for handler and functions

References func, funcCnt, ULock::lock(), moduleLock, ULock::unlock(), and UClientPort::verboseMessages.

Referenced by clientCmdLine(), UFunctionIf::createResources(), and testSmlClient().

const char * UClientHandler::snprint ( const char *  preString,
char *  buff,
const int  buffCnt 
)
bool UClientHandler::start ( )

Start handling incomming messages in seperate thread. Returns true if thread started.

References clientThreadRunCall(), threadHandle, threadRunning, and threadStop.

Referenced by clientCmdLine(), UFunctionIf::createResources(), openConnection(), ULaserSim::openPort(), and testSmlClient().

void UClientHandler::stop ( bool  andWait)

Stop receive thread

References threadHandle, threadRunning, and threadStop.

Referenced by clientCmdLine(), testSmlClient(), and ~UClientHandler().

void UClientHandler::threadRunLoop ( )

Member Data Documentation

UClientFuncBase* UClientHandler::baseHandler
private

pointer to the base data handler, so that it can be released properly

Referenced by UClientHandler(), and ~UClientHandler().

bool UClientHandler::binData
protected
char UClientHandler::binTagName[MAX_BIN_NAME_LENGTH+1]
protected
UTime UClientHandler::dataTime
private

Time of last received data (by client handler)

Referenced by threadRunLoop().

UEvents* UClientHandler::dataTrap
private

OnEvent stack

Referenced by addDataTrap(), delDataTrap(), gotNewData(), and UClientHandler().

UClientFuncBase* UClientHandler::func[MAX_FUNCTION_COUNT_CLIENT]
private

Functions to handle the commands

Referenced by addFunction(), doTimeTick(), gotNewData(), print(), setVerbose(), and snprint().

int UClientHandler::funcCnt
private

Number of functions loaded

Referenced by addFunction(), doTimeTick(), gotNewData(), print(), setVerbose(), snprint(), and UClientHandler().

const int UClientHandler::MAX_BIN_NAME_LENGTH = 30
staticprotected

binary tag name

Referenced by UResIf::interfaceTick(), and UClientHandler().

const int UClientHandler::MAX_DATA_TRAPS = 100
static

Number of data trapc created - when used. Created by first 'addDataTrap'

Referenced by addDataTrap().

ULock UClientHandler::moduleLock
private

Lock for receiving data and changing data handling modyules

Referenced by addFunction(), doTimeTick(), gotNewData(), print(), setVerbose(), and snprint().

char UClientHandler::namespaceAttributes[MAX_NORMAL_MESSAGE_LENGTH]
protected

The attribute string send after the namespace name

Referenced by sendNamespaceOpenTag(), and setNamespace().

char UClientHandler::namespaceName[MAX_SML_NAME_LENGTH]
protected

Namespace to be used by this client

Referenced by sendNamespaceCloseTag(), sendNamespaceOpenTag(), and setNamespace().

bool UClientHandler::namespaceUse
protected

Some connextions do not like namespace (like MRC), this flag determines if it should be used

Referenced by getNamespaceUse(), UResIf::interfaceTick(), sendNamespaceCloseTag(), sendNamespaceOpenTag(), setNamespaceUse(), UClientHandler(), and ULaserSim::ULaserSim().

int UClientHandler::notConnectedLoop
protected

Number of client read thread loops that the client been not-connected

Referenced by threadRunLoop().

int UClientHandler::rxTimeoutMs
protected

rx timeout value in ms

Referenced by UResIf::interfaceTick(), UResIf::rxTimeout(), threadRunLoop(), and UClientHandler().

int UClientHandler::serverNamespaceLevel
protected

Tag level 0 = no namespace, 1 = root level, 2 = normal instde namespace

Referenced by closeConnection(), gotNewData(), UResIf::quitRemoteEnd(), setNamespaceLost(), and UClientHandler().

char UClientHandler::serverNamespaceName[MAX_SML_NAME_LENGTH]
protected

Namespace name used by the connected server

Referenced by getServerNamespaceName(), gotNewData(), and setNamespaceLost().

double UClientHandler::serverToLocalTime
private

Time to add to get from server time to local time. This time is set by a ping command.

Referenced by connectionChange(), getServerToLocalTime(), handlePingReply(), print(), snprint(), and UClientHandler().

bool UClientHandler::serverToLocalTimeValid
private

Is server time offset valid. Is set to valid after a ping with an out-home time difference of less than 0.2 ms

Referenced by connectionChange(), handlePingReply(), isServerToLocalTimeValid(), print(), snprint(), threadRunLoop(), and UClientHandler().

bool UClientHandler::showInfo

Show info messages on console

Referenced by gotNewData(), UFunctionIf::handleIf(), and UClientHandler().

bool UClientHandler::showWarnings

Show warnings (and errors) on console

Referenced by gotNewData(), UFunctionIf::handleIf(), and UClientHandler().

pthread_t UClientHandler::threadHandle
private

Thread handle for command read thrad.

Referenced by start(), and stop().

bool UClientHandler::threadRunning
private
bool UClientHandler::threadStop
private

Flag to stop thread - terminates the thread

Referenced by doTimeTick(), start(), stop(), threadRunLoop(), and UClientHandler().

bool UClientHandler::tryHoldConnection
bool UClientHandler::tryReConnect
protected

Try reconnect, that is disconnect and reconnect - may be change in port or host

Referenced by openConnection(), setHost(), setPort(), threadRunLoop(), and UClientHandler().


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