AURobotServers
4
|
#include <userverclient.h>
Public Member Functions | |
bool | blockSend (const char *buffer, int length, int msTimeout) |
char * | getClientName () |
int | getCnn () |
const char * | getLogFilename () |
bool | getLogReply () |
bool | getLogTimestamp () |
const char * | getNamespace () |
const char * | getNamespaceAttributes () |
USmlTagIn * | getNamespaceTag () |
int | getSerial () |
const UTime * | getTimeOfLastMessage () |
bool | initConnection (int clientNumber, int clnt, struct sockaddr_in from, UServerInQueue *queue, UTime *aliveTime) |
bool | isActive () |
bool | isLogOpen () |
virtual bool | justConnected () |
bool | lock () |
void | logClose () |
bool | logOpen () |
void | print (char *preStr) |
bool | receiveData () |
void | resourceUpdated () |
void | sendAliveReply () |
void | setLogReply (bool value) |
void | setLogTimestamp (bool value) |
void | setResLink (UResLink *resourceLinks) |
void | stopConnection (bool sendHUP, const char *namespaceName) |
bool | tryLock () |
void | unlock () |
UServerClient (int index) | |
virtual | ~UServerClient () |
Public Attributes | |
int | clientSerial |
UTime | connectTime |
UTime | punkTime |
bool | queueRawData |
Protected Member Functions | |
void | connectionLost () |
int | findEndTag (const char *fromPos, int size) |
void | gotNewMessage () |
bool | logWrite (const char *data, const int dataCnt, bool isReply=false) |
void | sendStreamRequest (const char *reqTyp) |
Protected Attributes | |
char | clientNamespaceCloseTag [MAX_SML_NAME_LENGTH+3] |
int | clientNamespaceCloseTagCnt |
int | clientNamespaceLevel |
char | clientNamespaceMsg [MAX_MESSAGE_LENGTH_TO_CAM] |
USmlTagIn | clientNamespaceTag |
ULogFile | logCmd |
ULock | logLock |
bool | logReply |
bool | logTimestamp |
Private Member Functions | |
void | sendPingReply (USmlTagIn *tag, int length) |
bool | trapMessage (int client, const char *msg, int size) |
Private Attributes | |
int | clientIndex |
struct sockaddr_in | clientInfo |
int | conn |
bool | connected |
int | msgBufCnt |
char | msgBuff [MAX_MESSAGE_LENGTH_TO_CAM *2+1] |
UTime | msgBufTime |
unsigned int | msgReceived |
unsigned int | msgSend |
unsigned int | msgSkippedBytes |
UResLink * | resLink |
UServerInQueue * | rxQueue |
UTime * | serverAlive |
unsigned int | tcpipReceived |
pthread_mutex_t | txlock |
Class of functions to handle one accepted connection for a socket server.
UServerClient::UServerClient | ( | int | index | ) |
Constructor
index | is the client number of this client connection. |
parent | port is a pointer to the port object where this client belong. |
References clientIndex, clientNamespaceLevel, clientNamespaceMsg, clientNamespaceTag, clientSerial, conn, connected, logReply, logTimestamp, msgBufCnt, msgReceived, msgSend, msgSkippedBytes, queueRawData, resLink, serverAlive, USmlTagIn::setTag(), tcpipReceived, and txlock.
|
virtual |
Destructor
bool UServerClient::blockSend | ( | const char * | buffer, |
int | length, | ||
int | msTimeout | ||
) |
Send data to client. if connection to client is open the data is send. returns false if client do not exist or data could not be send within timeout period.
References conn, connected, connectionLost(), logReply, logWrite(), mini(), msgSend, and Wait().
Referenced by getCnn(), UServerPort::gotNewClient(), URawServerPort::runServer(), sendAliveReply(), UCmdExe::sendMsg(), UCmdExe::sendMsgAll(), sendPingReply(), sendStreamRequest(), and stopConnection().
|
protected |
Connection is lost - if ant cleanup is needed
References clientIndex, UResLink::connectionLost(), and resLink.
Referenced by blockSend(), getTimeOfLastMessage(), and stopConnection().
|
protected |
Search from 'fromPos' and at maximum 'size' characters for an matshing end tag (or a '
') return 0 if not found.
Referenced by getTimeOfLastMessage(), and gotNewMessage().
char * UServerClient::getClientName | ( | ) |
Get IP number of client as string.
References clientInfo, and connected.
Referenced by getCnn(), URawServerPort::gotNewClient(), UServerPort::gotNewClient(), UCmdExe::handleServerCommand(), logOpen(), print(), and UServerPort::serviceClients().
|
inline |
Get connection stream handle - for use in poll, etc.
References blockSend(), conn, getClientName(), initConnection(), print(), receiveData(), and stopConnection().
Referenced by UServerPort::serviceClients().
|
inline |
Get logfile name
References ULogFile::getLogFileName(), and logCmd.
Referenced by URawServerPort::gotNewClient(), UCmdExe::handleServerCommand(), logClose(), logOpen(), and setLogReply().
|
inline |
|
inline |
is a timestamp to be added to logfile
References logTimestamp.
Referenced by UCmdExe::handleServerCommand().
|
inline |
Get namespace (last used namespace for client) .
References clientNamespaceTag, and USmlTagIn::getTagName().
Referenced by resourceUpdated().
|
inline |
Get the the attribute part of the current namespace message from this client.
References clientNamespaceMsg.
|
inline |
Get namespace tag (last used namespace for client) .
References clientNamespaceTag, resourceUpdated(), and setResLink().
Referenced by UCmdExe::handleServerCommand().
|
inline |
Get serial number for the connection
References clientSerial.
|
inline |
Get pointer to time of last received message for this client
References connectionLost(), data, findEndTag(), gotNewMessage(), logWrite(), msgBufTime, sendAliveReply(), sendPingReply(), sendStreamRequest(), and trapMessage().
Referenced by UServerPort::serviceClients().
|
protected |
Called when data or additional data is received from socket port. message is in msgBuff with a length of msgBuffCnt.
References UServerInQueue::addMessage(), clientIndex, clientNamespaceCloseTag, clientNamespaceCloseTagCnt, clientNamespaceLevel, clientNamespaceMsg, clientNamespaceTag, findEndTag(), USmlTagIn::getTagName(), USmlTagIn::isAStartTag(), isLogOpen(), logWrite(), MAX_MESSAGE_LENGTH_TO_CAM, MAX_SML_NAME_LENGTH, mini(), msgBufCnt, msgBuff, msgReceived, msgSkippedBytes, queueRawData, resourceUpdated(), rxQueue, USmlTagIn::setTag(), and trapMessage().
Referenced by getTimeOfLastMessage(), and receiveData().
bool UServerClient::initConnection | ( | int | clientNumber, |
int | clnt, | ||
struct sockaddr_in | from, | ||
UServerInQueue * | queue, | ||
UTime * | aliveTime | ||
) |
Set connection handle (clnt) and the struct with the client info - name etc.
References clientInfo, conn, connected, connectTime, justConnected(), msgBufCnt, msgBufTime, msgReceived, msgSend, UTime::Now(), UTime::now(), rxQueue, serverAlive, and tcpipReceived.
Referenced by getCnn(), and UServerPort::runServerThread().
|
inline |
returns true if connection is open.
References connected.
Referenced by UCmdExe::executeFunction(), UServerPort::getFreeClientHandle(), UCmdExe::handleServerCommand(), UCmdExe::isClientAlive(), print(), UCmdExe::sendMsg(), UCmdExe::sendMsgAll(), and UServerPort::serviceClients().
|
inline |
Is logfile open?
References ULogFile::isOpen(), and logCmd.
Referenced by gotNewMessage(), and UCmdExe::handleServerCommand().
|
virtual |
This is also the place to send any welcome messages. The virtual function is called before receive thread is activated. Must returns true if connection is acceptable.
References appName, clientIndex, logCmd, logName, MAX_FILENAME_LENGTH, and ULogFile::setLogName().
Referenced by initConnection().
|
inline |
Lock communication to client. Returns false, if an error occured. Returns true when the client can be and is locked.
References txlock.
Referenced by UCmdExe::executeFunction(), UCmdExe::sendMsgAll(), and sendStreamRequest().
void UServerClient::logClose | ( | ) |
Close logfile. Resets any flags used (e.g. log also reply)
References clientIndex, ULogFile::closeLog(), getLogFilename(), ULock::lock(), logCmd, logLock, logReply, logTimestamp, and ULock::unlock().
Referenced by UCmdExe::handleServerCommand(), and setLogTimestamp().
bool UServerClient::logOpen | ( | ) |
Open a logfile dataPath with the application name with client-number added, e.g. /mnt/ram/userver_client_1.log.
References clientIndex, ULogFile::closeLog(), getClientName(), getLogFilename(), ULogFile::isOpen(), logCmd, logWrite(), and ULogFile::openLog().
Referenced by URawServerPort::gotNewClient(), UCmdExe::handleServerCommand(), and setLogTimestamp().
|
protected |
Log this string to logfile. Returns false if no logfile is open. File must be opened by a logOpen(.) call, and closed with a logClose(). Only the first 'dataCnt' characters are saved, or until a '\0' is found..
References data, ULogFile::getF(), UTime::getMicrosec(), UTime::getSec(), UTime::getTimeAsString(), ULogFile::isOpen(), ULock::lock(), logCmd, logLock, logTimestamp, UTime::now(), and ULock::unlock().
Referenced by blockSend(), getTimeOfLastMessage(), gotNewMessage(), and logOpen().
void UServerClient::print | ( | char * | preStr | ) |
Print client status
References clientNamespaceMsg, getClientName(), UTime::getTimeAsString(), isActive(), msgBufTime, msgReceived, msgSend, and msgSkippedBytes.
Referenced by getCnn(), and UServerPort::print().
bool UServerClient::receiveData | ( | ) |
Data is available to receive, so receive the data and process as needed (i.e. put in queue)
References conn, gotNewMessage(), MAX_MESSAGE_LENGTH_TO_CAM, msgBufCnt, msgBuff, msgBufTime, msgSkippedBytes, UTime::Now(), stopConnection(), tcpipReceived, and Wait().
Referenced by getCnn(), and UServerPort::serviceClients().
void UServerClient::resourceUpdated | ( | ) |
Ressource is updated - action may be needed
References getNamespace(), UResLink::isStreamingPoseHist(), UResLink::lockPoseHist(), resLink, sendStreamRequest(), and UResLink::unlockPoseHist().
Referenced by getNamespaceTag(), and gotNewMessage().
void UServerClient::sendAliveReply | ( | ) |
Send message to client with alive time for for the main thread in this server
References blockSend(), UTime::getTimePassed(), serverAlive, tryLock(), and unlock().
Referenced by getTimeOfLastMessage(), UServerPort::serviceClients(), and trapMessage().
|
private |
Send a ping reply directly from client handler
References blockSend(), UTime::getMicrosec(), USmlTagIn::getNextAttribute(), UTime::getSec(), USmlTagIn::getTagName(), MAX_SML_NAME_LENGTH, UTime::Now(), and UTime::setTime().
Referenced by getTimeOfLastMessage(), and trapMessage().
|
protected |
Send request for streaming data. Typically used for request of robot pose.
References blockSend(), lock(), and unlock().
Referenced by getTimeOfLastMessage(), and resourceUpdated().
|
inline |
Set logging of reply messages.
References bool2str(), clientIndex, getLogFilename(), and logReply.
Referenced by UCmdExe::handleServerCommand().
|
inline |
Set timestamping of logged items.
References logClose(), logOpen(), and logTimestamp.
Referenced by URawServerPort::gotNewClient(), and UCmdExe::handleServerCommand().
void UServerClient::setResLink | ( | UResLink * | resourceLinks | ) |
Set ressource for fast responce messages
References resLink.
Referenced by UServerPort::getFreeClientHandle(), and getNamespaceTag().
void UServerClient::stopConnection | ( | bool | sendHUP, |
const char * | namespaceName | ||
) |
Stop connection and send a HUP (hangup) if 'sendHUP' is true.
References blockSend(), clientNamespaceLevel, conn, connected, connectionLost(), MAX_SML_NAME_LENGTH, and Wait().
Referenced by UCmdExe::closeClient(), getCnn(), receiveData(), and UServerPort::serviceClients().
|
private |
Trap high speed messages that needs immidiate action and should not be queued
References UResPoseHist::addIfNeeded(), clientIndex, USmlTag::getPoseTVQ(), USmlTagIn::isTagA(), UResLink::lockPoseHist(), resLink, sendAliveReply(), sendPingReply(), UResLink::setStreamingPoseHist(), USmlTagIn::setTag(), and UResLink::unlockPoseHist().
Referenced by getTimeOfLastMessage(), and gotNewMessage().
|
inline |
Lock communication to client and return emmidiately. Returns false, if an error occured or the client is locked already. Returns true if the client is locked by the calling thread.
References txlock.
Referenced by sendAliveReply().
|
inline |
Unlock communication to client and return emmidiately.
References txlock.
Referenced by UCmdExe::executeFunction(), sendAliveReply(), UCmdExe::sendMsgAll(), and sendStreamRequest().
|
private |
Index number of client - set at creation
Referenced by connectionLost(), gotNewMessage(), justConnected(), logClose(), logOpen(), setLogReply(), trapMessage(), and UServerClient().
|
private |
Structure with adress information etc.
Referenced by getClientName(), and initConnection().
|
protected |
Namespace close tag to look for
Referenced by gotNewMessage().
|
protected |
Length of the namespace close tag
Referenced by gotNewMessage().
|
protected |
debug log Tag level 0 = no namespace, 1 = root level, 2 = normal instde namespace
Referenced by gotNewMessage(), stopConnection(), and UServerClient().
|
protected |
The attribute string send after the namespace name
Referenced by getNamespaceAttributes(), gotNewMessage(), print(), and UServerClient().
|
protected |
Tag with namespace
Referenced by getNamespace(), getNamespaceTag(), gotNewMessage(), and UServerClient().
int UServerClient::clientSerial |
The client connection serial number - set at creation, and is unique for the session
Referenced by UServerPort::getFreeClientHandle(), getSerial(), and UServerClient().
|
private |
Connection handle
Referenced by blockSend(), getCnn(), initConnection(), receiveData(), stopConnection(), and UServerClient().
|
private |
Is a client connected
Referenced by blockSend(), getClientName(), initConnection(), isActive(), stopConnection(), and UServerClient().
UTime UServerClient::connectTime |
connect time - set when connection was established
Referenced by initConnection(), and UCmdExe::isClientAlive().
|
protected |
Logfile handle for logging of commands (and reply)
Referenced by getLogFilename(), isLogOpen(), justConnected(), logClose(), logOpen(), and logWrite().
|
protected |
Lock on logfile. As tx and reply is executed in different threads a lock is needed
Referenced by logClose(), and logWrite().
|
protected |
Should reply be logged too?
Referenced by blockSend(), getLogReply(), logClose(), setLogReply(), and UServerClient().
|
protected |
Should logging be timestamped.
Referenced by getLogTimestamp(), logClose(), logWrite(), setLogTimestamp(), and UServerClient().
|
private |
Already received part of message buffer
Referenced by gotNewMessage(), initConnection(), receiveData(), and UServerClient().
|
private |
Message buffer for partially received messages
Referenced by gotNewMessage(), and receiveData().
|
private |
Time of last received data, used to empty part of buffer if out of byte sync.
Referenced by getTimeOfLastMessage(), initConnection(), print(), and receiveData().
|
private |
statistics - Number of received messages
Referenced by gotNewMessage(), initConnection(), print(), and UServerClient().
|
private |
statistics - messages send to this client
Referenced by blockSend(), initConnection(), print(), and UServerClient().
|
private |
Statistics - skipped bytes in seach of byte-sync
Referenced by gotNewMessage(), print(), receiveData(), and UServerClient().
UTime UServerClient::punkTime |
Time of last punk beeing send (to test if client connection is alive)
Referenced by UServerPort::serviceClients().
bool UServerClient::queueRawData |
queue the raw data - one entry per read
Referenced by URawServerPort::gotNewClient(), gotNewMessage(), and UServerClient().
|
private |
A pose history resource pointer - if module is loaded
Referenced by connectionLost(), resourceUpdated(), setResLink(), trapMessage(), and UServerClient().
|
private |
Queue, where to put received data
Referenced by gotNewMessage(), and initConnection().
|
private |
Server alive time. The last time the server thread was reported alive.
Referenced by initConnection(), sendAliveReply(), and UServerClient().
|
private |
statistics - Number of received TCP/IP packeges
Referenced by initConnection(), receiveData(), and UServerClient().
|
private |
lock, when reading or writing. This lock is locked, when a function is initiated and unlocked after the function returns. So this lock should not be tested or used inside a function.
Referenced by lock(), tryLock(), unlock(), and UServerClient().