AURobotServers
4
|
#include <userverport.h>
Public Member Functions | |
void | connectionLost (int client) |
int | getActiveClientCnt () |
bool | getAllowConnections () |
UServerClient * | getClient (const int i) |
int | getClientCnt () |
char * | getHostName (char *nameBuffer, const int nameBufferCnt) |
int | getLastClient () |
int | getLastClientSerial () |
int | getPort () |
UServerInQueue * | getRxQueue () |
const char * | getServerNamespace () |
const char * | getServerNamespaceAttribute () |
virtual void | gotNewClient (UServerClient *cnn) |
bool | isOpen4Connections () |
bool | isServerNamespaceUse () |
virtual void | messageReceived () |
void | print (const char *preStr) |
bool | runServerThread () |
double | serverAliveLast () |
void | serverIsAlive () |
void | setAllowConnections (bool value) |
void | setPort (const int toPort) |
bool | setResource (UResBase *resource, bool remove) |
void | setServerNamespace (const char *name) |
void | setServerNamespaceAttribute (const char *attStr) |
void | setServerNamespaceUse (bool value) |
void | setVerbose (bool value) |
bool | start () |
bool | stop (bool andWait) |
UServerPort () | |
virtual | ~UServerPort () |
Public Attributes | |
UVariable * | varAlivePunkTime |
Protected Member Functions | |
int | getFreeClientHandle () |
bool | serviceClients (int msTimeout) |
void | updateVars () |
Protected Attributes | |
bool | allowConnections |
bool | open4Connections |
UResLink | resLink |
Private Attributes | |
UServerClient * | client [MAX_SOCKET_CLIENTS_SERVED] |
int | clientCnt |
int | clientCntActive |
UCmdExe * | eventServer |
int | lastClientNumber |
int | lastClientSerial |
int | recvLoops |
bool | running |
UServerInQueue | rxQueue |
UTime | serverAlive |
char | serverNamespace [MAX_SML_NAME_LENGTH] |
char | serverNamespaceAttribute [MAX_NAMESPACE_ATTRIBUTE_LENGTH] |
bool | serverNamespaceUse |
bool | terminate |
pthread_t | thServ |
UResVarPool * | varPool |
bool | verboseMessages |
Class used by a socket server to control and accept connections to one port number. The port number are determined during initialization.
UServerPort::UServerPort | ( | ) |
Constructor
References client, clientCnt, clientCntActive, eventServer, lastClientNumber, MAX_NAMESPACE_ATTRIBUTE_LENGTH, MAX_SML_NAME_LENGTH, MAX_SOCKET_CLIENTS_SERVED, UTime::now(), open4Connections, recvLoops, running, serverAlive, serverNamespace, serverNamespaceAttribute, serverNamespaceUse, terminate, varAlivePunkTime, varPool, and verboseMessages.
void UServerPort::connectionLost | ( | int | client | ) |
Should be called whenever a client is disconnected - to allow cleanup of obligations for this client
Referenced by getLastClient().
int UServerPort::getActiveClientCnt | ( | ) |
Get number of active clients
References client, and clientCnt.
Referenced by UCmdExe::handleServerCommand(), print(), runServerThread(), setVerbose(), and UCmdExe::updateLocalVar().
|
inline |
Get the allow connections flag if false, then server do not allow (new) connections
value | set to true, if to allow connections |
References allowConnections.
UServerClient * UServerPort::getClient | ( | const int | i | ) |
Get handle to connection - returns NULL if out of range
References client, and clientCnt.
Referenced by UCmdExe::closeClient(), UCmdExe::executeFunction(), getClientCnt(), UCmdExe::handleServerCommand(), UCmdExe::isClientAlive(), UCmdExe::sendMsg(), and UCmdExe::sendMsgAll().
|
inline |
Return number of initialized clients. The clients may be active or inactive (closed)
References clientCnt, getClient(), gotNewClient(), and messageReceived().
Referenced by UCmdExe::handleServerCommand(), and UCmdExe::sendMsgAll().
|
protected |
Get a free client handle
References client, clientCnt, UServerClient::clientSerial, UServerClient::isActive(), lastClientNumber, lastClientSerial, MAX_SOCKET_CLIENTS_SERVED, resLink, and UServerClient::setResLink().
Referenced by getLastClientSerial(), and runServerThread().
char * UServerPort::getHostName | ( | char * | nameBuffer, |
const int | nameBufferCnt | ||
) |
Get hostname - just a wrap of the normal 'gethostname' call. Returns a pointer to the provided string.
Referenced by setPort(), and UCmdExe::sysServerHelp().
|
inline |
Get client number of latest clinet connected - mostly for debug
References client, connectionLost(), and lastClientNumber.
Referenced by UCmdExe::updateLocalVar().
|
inline |
Get last used client serial number
References getFreeClientHandle(), lastClientSerial, serviceClients(), and updateVars().
Referenced by UCmdExe::updateLocalVar().
|
inline |
Get server port number
References serverPort.
Referenced by UCmdExe::handleServerCommand(), runLaserServerComponent(), runServer(), UCmdExe::sysServerHelp(), and UCmdExe::updateLocalVar().
|
inline |
Get receive message queue
References rxQueue, start(), and stop().
Referenced by UCmdExe::executeFunction(), UCmdExe::handleOneMessageFromQueue(), UCmdExe::handleOneServerPushMessage(), UCmdExe::handleServerCommand(), UCmdExe::isRxQueueEmpty(), UCmdExe::isRxQueueFull(), UCmdExe::postCommand(), UCmdExe::stop(), and testServerPort().
|
inline |
Get a pointer to the namespace element name.
References serverNamespace.
Referenced by UCmdExe::closeClient(), gotNewClient(), and UCmdExe::handleServerCommand().
|
inline |
Get a pointer to the namespace attribite string.
References serverNamespaceAttribute.
Referenced by gotNewClient().
|
virtual |
This function is called, when a new client is connected
Reimplemented in URawServerPort.
References UServerClient::blockSend(), UServerClient::getClientName(), getServerNamespace(), getServerNamespaceAttribute(), and serverNamespaceUse.
Referenced by getClientCnt(), and runServerThread().
|
inline |
Is server open for connections - i.e. port number is valid
References open4Connections.
Referenced by UCmdExe::handleServerCommand(), and UCmdExe::updateLocalVar().
|
inline |
Is the use flag for opening namespace message to new clients set.
References serverNamespaceUse.
|
virtual |
Function to trigger events, when new data is available. Should be overwritten by decendent classes.
References UServerPushImplement::event(), eventServer, UMsgQueue::getElements(), UMsgQueue::getUsedMsgCnt(), rxQueue, serverPort, and verboseMessages.
Referenced by getClientCnt(), and serviceClients().
void UServerPort::print | ( | const char * | preStr | ) |
Print server status to console, preseded by this string (preStr).
References bool2str(), client, clientCnt, getActiveClientCnt(), UServerClient::print(), UServerInQueue::print(), recvLoops, running, rxQueue, serverPort, and verboseMessages.
Referenced by UCmdExe::print(), setVerbose(), testServerGen(), and testServerPort().
bool UServerPort::runServerThread | ( | ) |
The thread that handles all port tasks. Runs until stop-flag is set, or if port can not be bound. Should not be called!, call start() to create the thread calling this function.
References allowConnections, client, getActiveClientCnt(), getFreeClientHandle(), gotNewClient(), UServerClient::initConnection(), open4Connections, running, rxQueue, serverAlive, serverPort, serviceClients(), terminate, updateVars(), verboseMessages, and Wait().
Referenced by runServerPort(), and setVerbose().
double UServerPort::serverAliveLast | ( | ) |
Get time passed since the server was last reported alive.
References UTime::getTimePassed(), and serverAlive.
Referenced by UCmdExe::handleAlive(), and serverIsAlive().
|
inline |
Alive call from server thread
References UTime::now(), serverAlive, and serverAliveLast().
Referenced by UCmdExe::run().
|
protected |
Service receive channel of all clients. Returns true is fata were processed. Returns false if nothing to process, either after poll-timeout or just no connections.
References client, clientCnt, clientCntActive, UServerClient::getClientName(), UServerClient::getCnn(), UVariable::getDouble(), UServerClient::getTimeOfLastMessage(), UTime::getTimePassed(), UServerClient::isActive(), MAX_SOCKET_CLIENTS_SERVED, messageReceived(), UTime::now(), UServerClient::punkTime, UServerClient::receiveData(), recvLoops, UServerClient::sendAliveReply(), serverNamespace, UServerClient::stopConnection(), updateVars(), varAlivePunkTime, and verboseMessages.
Referenced by getLastClientSerial(), and runServerThread().
|
inline |
Set the allow connections flag Existing connections are not closed
value | set to true, if to allow new connections |
References allowConnections.
Referenced by UCmdExe::run(), runCameraServerComponent(), and runLaserServerComponent().
|
inline |
Set server port number
References getHostName(), and serverPort.
Referenced by UCmdExe::handleServerCommand(), runCameraServerComponent(), runLaserServerComponent(), runServer(), testServerGen(), and testServerPort().
bool UServerPort::setResource | ( | UResBase * | resource, |
bool | remove | ||
) |
Set ressource for fast responce messages
References client, clientCnt, eventServer, UCmdExe::getResClassID(), UResBase::isAlsoA(), resLink, and UResLink::setResource().
Referenced by UCmdExe::deleteFunction(), UCmdExe::resourcesUpdated(), UCmdExe::setServer(), and setServerNamespaceAttribute().
|
inline |
Set name of namespace, that will be used as the opening element on start of communication with a new client.
References MAX_SML_NAME_LENGTH, and serverNamespace.
Referenced by runCameraServerComponent(), runLaserServerComponent(), runServer(), testServerGen(), and testServerPort().
|
inline |
Set attribute part of communication namestae element.
References MAX_NAMESPACE_ATTRIBUTE_LENGTH, serverNamespaceAttribute, and setResource().
Referenced by runCameraServerComponent(), runLaserServerComponent(), runServer(), and testServerPort().
|
inline |
Set use flag for opening namespace message to new clients.
References serverNamespaceUse.
|
inline |
Set verbose messages - mostly for debug purpose
References getActiveClientCnt(), print(), runServerThread(), and verboseMessages.
Referenced by UCmdExe::handleServerCommand(), runCameraServerComponent(), runLaserServerComponent(), runServer(), testServerGen(), and testServerPort().
bool UServerPort::start | ( | ) |
Start server port reveive loop. This loop will not start if port number is less than 1000. One loop handles all clients and adds messages to the command queue. An error is printed on console if port is not opened.
References running, runServerPort(), terminate, and thServ.
Referenced by getRxQueue(), runCameraServerComponent(), runLaserServerComponent(), runServer(), testServerGen(), and testServerPort().
bool UServerPort::stop | ( | bool | andWait | ) |
Stops the socket server and terminates all connection and terminates the thread. If andWait, then stop will wait until thread is terminated before return, otherwise function returns immidiately
References client, clientCnt, running, serverNamespace, terminate, and thServ.
Referenced by getRxQueue(), UCmdExe::stop(), and testServerPort().
|
protected |
Update var-pool variables
References clientCntActive, UVariable::d, UResVarPool::getVarPool(), open4Connections, serverPort, UVarPool::setLocalVar(), and varPool.
Referenced by getLastClientSerial(), runServerThread(), and serviceClients().
|
protected |
Should server allow connections, if not, then close server port - until changed.
Referenced by getAllowConnections(), runServerThread(), and setAllowConnections().
|
private |
Structure for the accepted connections
Referenced by getActiveClientCnt(), getClient(), getFreeClientHandle(), getLastClient(), print(), runServerThread(), serviceClients(), setResource(), stop(), UServerPort(), and ~UServerPort().
|
private |
Last used client connection
Referenced by getActiveClientCnt(), getClient(), getClientCnt(), getFreeClientHandle(), print(), serviceClients(), setResource(), stop(), UServerPort(), and ~UServerPort().
|
private |
Number of active clients
Referenced by serviceClients(), updateVars(), and UServerPort().
|
private |
Server that should be notified of events
Referenced by messageReceived(), setResource(), and UServerPort().
|
private |
client number to be used used for the last connection (-1 = no clients yet)
Referenced by getFreeClientHandle(), getLastClient(), and UServerPort().
|
private |
last client serial number used
Referenced by getFreeClientHandle(), and getLastClientSerial().
|
protected |
Is the selected port valid, i.e. open for connections
Referenced by isOpen4Connections(), runServerThread(), updateVars(), and UServerPort().
|
private |
Statistics - receive loops
Referenced by print(), serviceClients(), and UServerPort().
|
protected |
Pointer to ressources that nee immidiate data
Referenced by getFreeClientHandle(), and setResource().
|
private |
Running is true when accepting-calls thread is running
Referenced by print(), runServerThread(), start(), stop(), and UServerPort().
|
private |
Queue for received messages from all clients
Referenced by getRxQueue(), messageReceived(), print(), and runServerThread().
|
private |
The last time the server thread was reported alive
Referenced by runServerThread(), serverAliveLast(), serverIsAlive(), and UServerPort().
|
private |
Namespace to tell clients
Referenced by getServerNamespace(), serviceClients(), setServerNamespace(), stop(), and UServerPort().
|
private |
Server namespace attributes. This string must be coded to follow XML standard, and is send after the namespace and before the close '>' character
Referenced by getServerNamespaceAttribute(), setServerNamespaceAttribute(), and UServerPort().
|
private |
Sent namespace as opening message to the new client
Referenced by gotNewClient(), isServerNamespaceUse(), setServerNamespaceUse(), and UServerPort().
|
private |
Flag to stop active connections and stop accepting connections.
Referenced by runServerThread(), start(), stop(), and UServerPort().
UVariable* UServerPort::varAlivePunkTime |
Should clients be punk'ed with alive messages to see if they are alive
Referenced by serviceClients(), UCmdExe::setServer(), and UServerPort().
|
private |
Pointer to core var pool
Referenced by updateVars(), and UServerPort().
|
private |
Print more to info connection / console
Referenced by messageReceived(), print(), runServerThread(), serviceClients(), setVerbose(), and UServerPort().