AURobotServers
4
|
#include <uclienthandler.h>
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 | |
UClientFuncBase * | baseHandler |
UTime | dataTime |
UEvents * | dataTrap |
UClientFuncBase * | func [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 } |
Services a client port and transfers data to the appropriate data functions
UClientHandler::UClientHandler | ( | ) |
Constructor
References addFunction(), baseHandler, binData, binTagName, dataTrap, funcCnt, MAX_BIN_NAME_LENGTH, namespaceUse, rxTimeoutMs, serverNamespaceLevel, serverToLocalTime, serverToLocalTimeValid, setNamespace(), showInfo, showWarnings, threadRunning, threadStop, tryHoldConnection, and tryReConnect.
|
virtual |
Destructor
References baseHandler, stop(), and threadRunning.
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 | ( | ) |
Send a close tag and close connection
References UClientPort::closeConnection(), sendNamespaceCloseTag(), and serverNamespaceLevel.
Referenced by clientCmdLine(), ULaserSim::closePort(), UResIf::interfaceTick(), ULaserSim::openPort(), and testSmlClient().
|
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().
|
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().
|
inline |
Get setting of namespaceUse flag
References namespaceUse.
|
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().
|
inline |
Get the time offset that needs to be added to servertime to get local time.
References serverToLocalTime.
|
private |
New data is received - use the data (if possible) Data is in data and dataCnt is count of valid data. on return the used parts of the data is removed.
References binData, UEventTrap::callTrap(), UClientFuncBase::changedNamespace(), UClientFuncBase::commandList(), dataTrap, UClientFuncBase::decodeLock, func, funcCnt, USmlTagIn::getAttValue(), UEvents::getEvent(), USmlTagIn::getTagName(), UClientFuncBase::handleNewData(), handlePingReply(), USmlSource::ifName, interfaceAlive(), interfaceAliveTag(), inThisStringList(), USmlTagIn::isAFullTag(), USmlTagIn::isAnEndTag(), USmlTagIn::isAPhonyTag(), USmlTagIn::isAStartTag(), UClientPort::isConnected(), USmlTagIn::isTagA(), USmlTagIn::isTagAnEnd(), ULock::lock(), MAX_SML_NAME_LENGTH, moduleLock, USmlTagIn::print(), sendNamespaceCloseTag(), sendNamespaceOpenTag(), serverNamespaceLevel, serverNamespaceName, showInfo, showWarnings, and ULock::unlock().
Referenced by interfaceAlive(), and threadRunLoop().
|
private |
Calculate transmission delay and time offset from server
References absd(), bool2str(), USmlTagIn::getNextAttribute(), MAX_SML_NAME_LENGTH, UTime::Now(), USmlTagIn::print(), serverToLocalTime, serverToLocalTimeValid, UTime::setTime(), and UClientPort::verboseMessages.
Referenced by gotNewData(), and interfaceAlive().
|
inlineprotectedvirtual |
Alive from other tags
Reimplemented in UResIf.
References doTimeTick(), gotNewData(), handlePingReply(), and inThisStringList().
Referenced by gotNewData().
|
inlineprotectedvirtual |
|
inlineprotectedvirtual |
|
private |
Find if this string is one of the strings in the strlist
Referenced by gotNewData(), and interfaceAlive().
|
inline |
Is time difference between server and client valid?
References connectionChange(), sendPing(), serverToLocalTimeValid, setNamespace(), and setNamespaceLost().
|
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 | ) |
Print status for client
References bool2str(), func, funcCnt, UClientPort::getHost(), UClientPort::getHostIP(), UClientPort::getPort(), UClientPort::isConnected(), ULock::lock(), moduleLock, UClientPort::readCnt, serverToLocalTime, serverToLocalTimeValid, and ULock::unlock().
Referenced by clientCmdLine(), and testSmlClient().
|
protected |
Sent the namespace close tag
References MAX_SML_NAME_LENGTH, namespaceName, namespaceUse, and UClientPort::sendMsg().
Referenced by closeConnection(), and gotNewData().
|
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().
|
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 | ||
) |
Print current status to buffer string with this initial prestring.
References bool2str(), func, funcCnt, UClientPort::getHost(), UClientPort::getHostIP(), UClientPort::getPort(), UClientPort::isConnected(), ULock::lock(), moduleLock, UClientPort::readCnt, serverToLocalTime, serverToLocalTimeValid, and ULock::unlock().
Referenced by UResIf::print(), UResCamIf::print(), and UResLaserIf::print().
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 | ( | ) |
The loop called by the read thread. It will return when the threadStop is set true.
References binData, binTagName, UClientPort::connectTime, USmlSource::data, USmlSource::dataCnt, dataTime, doTimeTick(), UClientPortSml::getMoreData(), USmlSource::getNextTag(), USmlSource::getRxDataBuffer(), USmlSource::getRxDataBufferCnt(), USmlTagIn::getTagCnt(), UTime::getTimePassed(), USmlSource::gotFirstTag, gotNewData(), UClientPort::isConnected(), notConnectedLoop, UTime::Now(), UClientPort::readCnt, UClientPort::rxByteCnt, rxTimeoutMs, sendPing(), serverToLocalTimeValid, USmlTagIn::setBinaryTag(), threadRunning, threadStop, UClientPort::tryConnect(), tryHoldConnection, tryReConnect, UClientPort::verboseMessages, and Wait().
Referenced by clientThreadRunCall().
|
private |
pointer to the base data handler, so that it can be released properly
Referenced by UClientHandler(), and ~UClientHandler().
|
protected |
is all data binary
Referenced by UResIf::binNameValid(), gotNewData(), UResIf::interfaceTick(), threadRunLoop(), and UClientHandler().
|
protected |
Referenced by UResIf::binName(), UResIf::interfaceTick(), threadRunLoop(), and UClientHandler().
|
private |
Time of last received data (by client handler)
Referenced by threadRunLoop().
|
private |
OnEvent stack
Referenced by addDataTrap(), delDataTrap(), gotNewData(), and UClientHandler().
|
private |
Functions to handle the commands
Referenced by addFunction(), doTimeTick(), gotNewData(), print(), setVerbose(), and snprint().
|
private |
Number of functions loaded
Referenced by addFunction(), doTimeTick(), gotNewData(), print(), setVerbose(), snprint(), and UClientHandler().
|
staticprotected |
binary tag name
Referenced by UResIf::interfaceTick(), and UClientHandler().
|
static |
Number of data trapc created - when used. Created by first 'addDataTrap'
Referenced by addDataTrap().
|
private |
Lock for receiving data and changing data handling modyules
Referenced by addFunction(), doTimeTick(), gotNewData(), print(), setVerbose(), and snprint().
|
protected |
The attribute string send after the namespace name
Referenced by sendNamespaceOpenTag(), and setNamespace().
|
protected |
Namespace to be used by this client
Referenced by sendNamespaceCloseTag(), sendNamespaceOpenTag(), and setNamespace().
|
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().
|
protected |
Number of client read thread loops that the client been not-connected
Referenced by threadRunLoop().
|
protected |
rx timeout value in ms
Referenced by UResIf::interfaceTick(), UResIf::rxTimeout(), threadRunLoop(), and UClientHandler().
|
protected |
Tag level 0 = no namespace, 1 = root level, 2 = normal instde namespace
Referenced by closeConnection(), gotNewData(), UResIf::quitRemoteEnd(), setNamespaceLost(), and UClientHandler().
|
protected |
Namespace name used by the connected server
Referenced by getServerNamespaceName(), gotNewData(), and setNamespaceLost().
|
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().
|
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().
|
private |
|
private |
Thread running flag
Referenced by isThreadRunning(), openConnection(), start(), stop(), threadRunLoop(), UClientHandler(), and ~UClientHandler().
|
private |
Flag to stop thread - terminates the thread
Referenced by doTimeTick(), start(), stop(), threadRunLoop(), and UClientHandler().
bool UClientHandler::tryHoldConnection |
Try reconnect if connection is lost
Referenced by clientCmdLine(), ULaserSim::closePort(), connectionChange(), UResIf::interfaceTick(), ULaserSim::openPort(), threadRunLoop(), UClientHandler(), UResCamIf::UResCamIf(), UResIf::UResIf(), and UResLaserIf::UResLaserIf().
|
protected |
Try reconnect, that is disconnect and reconnect - may be change in port or host
Referenced by openConnection(), setHost(), setPort(), threadRunLoop(), and UClientHandler().