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

#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 ()
 
USmlTagIngetNamespaceTag ()
 
int getSerial ()
 
const UTimegetTimeOfLastMessage ()
 
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
 
UResLinkresLink
 
UServerInQueuerxQueue
 
UTimeserverAlive
 
unsigned int tcpipReceived
 
pthread_mutex_t txlock
 

Detailed Description

Class of functions to handle one accepted connection for a socket server.

Author
Christian Andersen

Constructor & Destructor Documentation

UServerClient::UServerClient ( int  index)

Constructor

Parameters
indexis the client number of this client connection.
parentport 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.

UServerClient::~UServerClient ( )
virtual

Destructor

Member Function Documentation

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().

void UServerClient::connectionLost ( )
protected

Connection is lost - if ant cleanup is needed

References clientIndex, UResLink::connectionLost(), and resLink.

Referenced by blockSend(), getTimeOfLastMessage(), and stopConnection().

int UServerClient::findEndTag ( const char *  fromPos,
int  size 
)
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 ( )
int UServerClient::getCnn ( )
inline

Get connection stream handle - for use in poll, etc.

References blockSend(), conn, getClientName(), initConnection(), print(), receiveData(), and stopConnection().

Referenced by UServerPort::serviceClients().

const char* UServerClient::getLogFilename ( )
inline
bool UServerClient::getLogReply ( )
inline

is logfile open for reply?

References logReply.

Referenced by UCmdExe::handleServerCommand().

bool UServerClient::getLogTimestamp ( )
inline

is a timestamp to be added to logfile

References logTimestamp.

Referenced by UCmdExe::handleServerCommand().

const char* UServerClient::getNamespace ( )
inline

Get namespace (last used namespace for client) .

References clientNamespaceTag, and USmlTagIn::getTagName().

Referenced by resourceUpdated().

const char* UServerClient::getNamespaceAttributes ( )
inline

Get the the attribute part of the current namespace message from this client.

References clientNamespaceMsg.

USmlTagIn* UServerClient::getNamespaceTag ( )
inline

Get namespace tag (last used namespace for client) .

References clientNamespaceTag, resourceUpdated(), and setResLink().

Referenced by UCmdExe::handleServerCommand().

int UServerClient::getSerial ( )
inline

Get serial number for the connection

References clientSerial.

const UTime* UServerClient::getTimeOfLastMessage ( )
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().

void UServerClient::gotNewMessage ( )
protected
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().

bool UServerClient::isActive ( )
inline
bool UServerClient::isLogOpen ( )
inline

Is logfile open?

References ULogFile::isOpen(), and logCmd.

Referenced by gotNewMessage(), and UCmdExe::handleServerCommand().

bool UServerClient::justConnected ( )
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().

bool UServerClient::lock ( )
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().

bool UServerClient::logWrite ( const char *  data,
const int  dataCnt,
bool  isReply = false 
)
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)
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 ( )
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().

void UServerClient::sendPingReply ( USmlTagIn tag,
int  length 
)
private
void UServerClient::sendStreamRequest ( const char *  reqTyp)
protected

Send request for streaming data. Typically used for request of robot pose.

References blockSend(), lock(), and unlock().

Referenced by getTimeOfLastMessage(), and resourceUpdated().

void UServerClient::setLogReply ( bool  value)
inline

Set logging of reply messages.

References bool2str(), clientIndex, getLogFilename(), and logReply.

Referenced by UCmdExe::handleServerCommand().

void UServerClient::setLogTimestamp ( bool  value)
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().

bool UServerClient::trapMessage ( int  client,
const char *  msg,
int  size 
)
private
bool UServerClient::tryLock ( )
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().

void UServerClient::unlock ( )
inline

Unlock communication to client and return emmidiately.

References txlock.

Referenced by UCmdExe::executeFunction(), sendAliveReply(), UCmdExe::sendMsgAll(), and sendStreamRequest().

Member Data Documentation

int UServerClient::clientIndex
private

Index number of client - set at creation

Referenced by connectionLost(), gotNewMessage(), justConnected(), logClose(), logOpen(), setLogReply(), trapMessage(), and UServerClient().

struct sockaddr_in UServerClient::clientInfo
private

Structure with adress information etc.

Referenced by getClientName(), and initConnection().

char UServerClient::clientNamespaceCloseTag[MAX_SML_NAME_LENGTH+3]
protected

Namespace close tag to look for

Referenced by gotNewMessage().

int UServerClient::clientNamespaceCloseTagCnt
protected

Length of the namespace close tag

Referenced by gotNewMessage().

int UServerClient::clientNamespaceLevel
protected

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

Referenced by gotNewMessage(), stopConnection(), and UServerClient().

char UServerClient::clientNamespaceMsg[MAX_MESSAGE_LENGTH_TO_CAM]
protected

The attribute string send after the namespace name

Referenced by getNamespaceAttributes(), gotNewMessage(), print(), and UServerClient().

USmlTagIn UServerClient::clientNamespaceTag
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().

int UServerClient::conn
private
bool UServerClient::connected
private
UTime UServerClient::connectTime

connect time - set when connection was established

Referenced by initConnection(), and UCmdExe::isClientAlive().

ULogFile UServerClient::logCmd
protected

Logfile handle for logging of commands (and reply)

Referenced by getLogFilename(), isLogOpen(), justConnected(), logClose(), logOpen(), and logWrite().

ULock UServerClient::logLock
protected

Lock on logfile. As tx and reply is executed in different threads a lock is needed

Referenced by logClose(), and logWrite().

bool UServerClient::logReply
protected

Should reply be logged too?

Referenced by blockSend(), getLogReply(), logClose(), setLogReply(), and UServerClient().

bool UServerClient::logTimestamp
protected

Should logging be timestamped.

Referenced by getLogTimestamp(), logClose(), logWrite(), setLogTimestamp(), and UServerClient().

int UServerClient::msgBufCnt
private

Already received part of message buffer

Referenced by gotNewMessage(), initConnection(), receiveData(), and UServerClient().

char UServerClient::msgBuff[MAX_MESSAGE_LENGTH_TO_CAM *2+1]
private

Message buffer for partially received messages

Referenced by gotNewMessage(), and receiveData().

UTime UServerClient::msgBufTime
private

Time of last received data, used to empty part of buffer if out of byte sync.

Referenced by getTimeOfLastMessage(), initConnection(), print(), and receiveData().

unsigned int UServerClient::msgReceived
private

statistics - Number of received messages

Referenced by gotNewMessage(), initConnection(), print(), and UServerClient().

unsigned int UServerClient::msgSend
private

statistics - messages send to this client

Referenced by blockSend(), initConnection(), print(), and UServerClient().

unsigned int UServerClient::msgSkippedBytes
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().

UResLink* UServerClient::resLink
private

A pose history resource pointer - if module is loaded

Referenced by connectionLost(), resourceUpdated(), setResLink(), trapMessage(), and UServerClient().

UServerInQueue* UServerClient::rxQueue
private

Queue, where to put received data

Referenced by gotNewMessage(), and initConnection().

UTime* UServerClient::serverAlive
private

Server alive time. The last time the server thread was reported alive.

Referenced by initConnection(), sendAliveReply(), and UServerClient().

unsigned int UServerClient::tcpipReceived
private

statistics - Number of received TCP/IP packeges

Referenced by initConnection(), receiveData(), and UServerClient().

pthread_mutex_t UServerClient::txlock
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().


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