Visual Servoing Platform  version 3.4.0
vpServer Class Reference

#include <vpServer.h>

+ Inheritance diagram for vpServer:

Public Member Functions

 vpServer ()
 
 vpServer (const int &port)
 
 vpServer (const std::string &adress_serv, const int &port_serv)
 
virtual ~vpServer ()
 
bool checkForConnections ()
 
bool isStarted ()
 
unsigned int getMaxNumberOfClients ()
 
unsigned int getNumberOfClients ()
 
void print ()
 
bool start ()
 
void setMaxNumberOfClients (unsigned int &l)
 
void addDecodingRequest (vpRequest *)
 
int getReceptorIndex (const char *name)
 
std::string getRequestIdFromIndex (const int &ind)
 
unsigned int getMaxSizeReceivedMessage ()
 
void print (const char *id="")
 
template<typename T >
int receive (T *object, const unsigned int &sizeOfObject=sizeof(T))
 
template<typename T >
int receiveFrom (T *object, const unsigned int &receptorEmitting, const unsigned int &sizeOfObject=sizeof(T))
 
std::vector< int > receiveRequest ()
 
std::vector< int > receiveRequestFrom (const unsigned int &receptorEmitting)
 
int receiveRequestOnce ()
 
int receiveRequestOnceFrom (const unsigned int &receptorEmitting)
 
std::vector< int > receiveAndDecodeRequest ()
 
std::vector< int > receiveAndDecodeRequestFrom (const unsigned int &receptorEmitting)
 
int receiveAndDecodeRequestOnce ()
 
int receiveAndDecodeRequestOnceFrom (const unsigned int &receptorEmitting)
 
void removeDecodingRequest (const char *)
 
template<typename T >
int send (T *object, const int unsigned &sizeOfObject=sizeof(T))
 
template<typename T >
int send (T *object, const unsigned int &sizeOfObject)
 
template<typename T >
int sendTo (T *object, const unsigned int &dest, const unsigned int &sizeOfObject=sizeof(T))
 
int sendRequest (vpRequest &req)
 
int sendRequestTo (vpRequest &req, const unsigned int &dest)
 
int sendAndEncodeRequest (vpRequest &req)
 
int sendAndEncodeRequestTo (vpRequest &req, const unsigned int &dest)
 
void setMaxSizeReceivedMessage (const unsigned int &s)
 
void setTimeoutSec (const long &sec)
 
void setTimeoutUSec (const long &usec)
 
void setVerbose (const bool &mode)
 

Protected Attributes

vpEmitter emitter
 
std::vector< vpReceptor > receptor_list
 
fd_set readFileDescriptor
 
int socketMax
 
std::vector< vpRequest * > request_list
 
unsigned int max_size_message
 
std::string separator
 
std::string beginning
 
std::string end
 
std::string param_sep
 
std::string currentMessageReceived
 
struct timeval tv
 
long tv_sec
 
long tv_usec
 
bool verboseMode
 

Detailed Description

This class represents a Transmission Control Protocol (TCP) server.

TCP provides reliable, ordered delivery of a stream of bytes from a program on one computer to another program on another computer.

Exemple of server's code, receiving and sending basic message. It corresponds to the client used in the first exemple of vpClient class' documentation.

#include <iostream>
#include <visp3/core/vpServer.h>
int main(int argc,const char** argv)
{
int port = 35000;
vpServer serv(port); //Launch the server on localhost
serv.start();
bool run = true;
int val;
while(run){
serv.checkForConnections();
if(serv.getNumberOfClients() > 0)
{
// Receiving a value from the first client
if(serv.receive(&val) != sizeof(int))
std::cout << "Error while receiving" << std::endl;
else
std::cout << "Received : " << val << std::endl;
val = val+1;
// Sending the new value to the first client
if(serv.send(&val) != sizeof(int))
std::cout << "Error while sending" << std::endl;
else
std::cout << "Sending : " << val << std::endl;
}
}
return 0;
}

Exemple of server's code, receiving a vpImage on request form. It correspond to the client used in the second exemple of vpClient class' documentation.

#include <visp3/core/vpServer.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayX.h>
#include "vpRequestImage.h" //See vpRequest class documentation
int main(int argc,const char** argv)
{
int port = 35000;
std::cout << "Port: " << port << std::endl;
vpServer serv(port);
serv.start();
#if defined(VISP_HAVE_X11)
vpDisplayX display;
#elif defined(VISP_HAVE_GDI) //Win32
vpDisplayGDI display;
#endif
vpRequestImage reqImage(&I);
serv.addDecodingRequest(&reqImage);
bool run = true;
while(run){
serv.checkForConnections();
if(serv.getNumberOfClients() > 0)
{
int index = serv.receiveAndDecodeRequestOnce();
std::string id = serv.getRequestIdFromIndex(index);
if(id == reqImage.getId())
{
#if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
if (! display.isInitialised() )
display.init(I, -1, -1, "Remote display");
#endif
// A click in the viewer to exit
if ( vpDisplay::getClick(I, false) )
run = false;
}
}
}
return 0;
}
See also
vpClient
vpRequest
vpNetwork
Examples:
testServer.cpp.

Definition at line 166 of file vpServer.h.

Constructor & Destructor Documentation

vpServer::vpServer ( )

Construct a server on the machine launching it.

Definition at line 51 of file vpServer.cpp.

References vpNetwork::emitter, and vpERROR_TRACE.

vpServer::vpServer ( const int &  port_serv)
explicit

Construct a server on the machine launching it, with a specified port.

Parameters
port_serv: server's port.

Definition at line 76 of file vpServer.cpp.

References vpNetwork::emitter, and vpERROR_TRACE.

vpServer::vpServer ( const std::string &  adress_serv,
const int &  port_serv 
)

Construct a server on the machine at a given adress, with a specified port.

Parameters
adress_serv: server's adress.
port_serv: server's port.

Definition at line 102 of file vpServer.cpp.

References vpNetwork::emitter, and vpERROR_TRACE.

vpServer::~vpServer ( )
virtual

Shutdown the server.

Definition at line 127 of file vpServer.cpp.

References vpNetwork::emitter, and vpNetwork::receptor_list.

Member Function Documentation

void vpNetwork::addDecodingRequest ( vpRequest req)
inherited

Add a decoding request to the emitter. This request will be used to decode the received messages. Each request must have a different id.

Warning
vpRequest is a virtual pure class. It has to be implemented according to the way how you want to decode the message received.
See also
vpNetwork::removeDecodingRequest()
Parameters
req: Request to add.

Definition at line 83 of file vpNetwork.cpp.

References vpRequest::getId(), and vpNetwork::request_list.

bool vpServer::checkForConnections ( )

Check if a client has connected or deconnected the server

Returns
True if a client connected or deconnected, false otherwise OR server not started yet.
Examples:
testServer.cpp.

Definition at line 203 of file vpServer.cpp.

References vpNetwork::emitter, vpNetwork::readFileDescriptor, vpNetwork::receptor_list, vpNetwork::socketMax, start(), vpNetwork::tv, vpNetwork::tv_sec, vpNetwork::tv_usec, and vpERROR_TRACE.

unsigned int vpServer::getMaxNumberOfClients ( )
inline

Get the maximum number of clients that can be connected to the server.

See also
vpServer::setMaxNumberOfClients()
Returns
Maximum number of clients.

Definition at line 202 of file vpServer.h.

unsigned int vpNetwork::getMaxSizeReceivedMessage ( )
inlineinherited

Get the maximum size that the emitter can receive (in request mode).

See also
vpNetwork::setMaxSizeReceivedMessage()
Returns
Acutal max size value.

Definition at line 193 of file vpNetwork.h.

unsigned int vpServer::getNumberOfClients ( )
inline

Get the number of clients connected to the server.

Returns
Number of clients connected.
Examples:
testServer.cpp.

Definition at line 209 of file vpServer.h.

References vpNetwork::print().

int vpNetwork::getReceptorIndex ( const char *  name)
inherited

Get the receptor index from its name. The name can be either the IP, or its name on the network.

Parameters
name: Name of the receptor.
Returns
Index of the receptor, or -1 if an error occurs.

Definition at line 138 of file vpNetwork.cpp.

References vpNetwork::receptor_list, and vpERROR_TRACE.

std::string vpNetwork::getRequestIdFromIndex ( const int &  ind)
inlineinherited

Get the Id of the request at the index ind.

Parameters
ind: Index of the request.
Returns
Id of the request.

Definition at line 179 of file vpNetwork.h.

bool vpServer::isStarted ( )
inline

Check if the server is started.

See also
vpServer::start()
Returns
True if the server is started, false otherwise.

Definition at line 193 of file vpServer.h.

void vpNetwork::print ( const char *  id = "")
inherited

Print the receptors.

Parameters
id: Message to display before the receptor's index.

Definition at line 123 of file vpNetwork.cpp.

References vpNetwork::receptor_list.

Referenced by getNumberOfClients(), vpClient::getNumberOfServers(), vpClient::print(), and print().

void vpServer::print ( )

Print the connected clients.

Definition at line 289 of file vpServer.cpp.

References vpNetwork::print().

template<typename T >
int vpNetwork::receive ( T *  object,
const unsigned int &  sizeOfObject = sizeof(T) 
)
inherited

Receives a object. The size of the received object is suppose to be the size of the type of the object. Note that a received message can correspond to a deconnection signal.

Warning
Using this function means that you know what kind of object you are suppose to receive, and when you are suppose to receive. If the emitter has several receptors. It might be a problem, and in that case you better use the "request" option.
See also
vpNetwork::receiveRequest()
vpNetwork::receiveRequestOnce()
vpNetwork::receiveAndDecodeRequest()
vpNetwork::receiveAndDecodeRequestOnce()
Parameters
object: Received object.
sizeOfObject: Size of the received object.
Returns
the number of bytes received, or -1 if an error occured.
Examples:
testClient.cpp, and testServer.cpp.

Definition at line 283 of file vpNetwork.h.

References vpERROR_TRACE, and vpTRACE.

std::vector< int > vpNetwork::receiveAndDecodeRequest ( )
inherited
std::vector< int > vpNetwork::receiveAndDecodeRequestFrom ( const unsigned int &  receptorEmitting)
inherited

Receives and decode requests, from a specific emitter, untils there is request to receive.

Warning
Requests will be received but not decoded.
See also
vpNetwork::receive()
vpNetwork::receiveRequest()
vpNetwork::receiveRequestOnce()
vpNetwork::receiveRequestOnceFrom()
vpNetwork::receiveAndDecodeRequest()
vpNetwork::receiveAndDecodeRequestFrom()
vpNetwork::receiveAndDecodeRequestOnce()
vpNetwork::receiveAndDecodeRequestOnceFrom()
Parameters
receptorEmitting: Index of the receptor emitting the message

Definition at line 402 of file vpNetwork.cpp.

References vpNetwork::receiveRequestFrom(), and vpNetwork::request_list.

int vpNetwork::receiveAndDecodeRequestOnce ( )
inherited

Receives a message once (in the limit of the Maximum message size value). This message can represent an entire request or not. Several calls to this function might be necessary to get the entire request. If it represents an entire request, it decodes the request.

Warning
Requests will be received but not decoded.
See also
vpNetwork::receive()
vpNetwork::receiveRequestFrom()
vpNetwork::receiveRequest()
vpNetwork::receiveRequestOnceFrom()
vpNetwork::receiveAndDecodeRequest()
vpNetwork::receiveAndDecodeRequestFrom()
vpNetwork::receiveAndDecodeRequestOnce()
vpNetwork::receiveAndDecodeRequestOnceFrom()
Returns
The number of bytes received, -1 if an error occured.

Definition at line 432 of file vpNetwork.cpp.

References vpNetwork::receiveRequestOnce(), and vpNetwork::request_list.

int vpNetwork::receiveAndDecodeRequestOnceFrom ( const unsigned int &  receptorEmitting)
inherited

Receives a message once (in the limit of the Maximum message size value), from a specific emitter. This message can represent an entire request or not. Several calls to this function might be necessary to get the entire request. If it represents an entire request, it decodes the request.

Warning
Requests will be received but not decoded.
See also
vpNetwork::receive()
vpNetwork::receiveRequestFrom()
vpNetwork::receiveRequest()
vpNetwork::receiveRequestOnce()
vpNetwork::receiveAndDecodeRequest()
vpNetwork::receiveAndDecodeRequestFrom()
vpNetwork::receiveAndDecodeRequestOnce()
vpNetwork::receiveAndDecodeRequestOnceFrom()
Parameters
receptorEmitting: Index of the receptor emitting the message.
Returns
The number of bytes received, -1 if an error occured.

Definition at line 462 of file vpNetwork.cpp.

References vpNetwork::beginning, vpNetwork::currentMessageReceived, vpNetwork::end, vpNetwork::max_size_message, vpNetwork::param_sep, vpNetwork::readFileDescriptor, vpNetwork::receiveRequestOnceFrom(), vpNetwork::receptor_list, vpNetwork::request_list, vpNetwork::separator, vpNetwork::socketMax, vpNetwork::tv, vpNetwork::tv_sec, vpNetwork::tv_usec, vpNetwork::verboseMode, vpERROR_TRACE, and vpTRACE.

template<typename T >
int vpNetwork::receiveFrom ( T *  object,
const unsigned int &  receptorEmitting,
const unsigned int &  sizeOfObject = sizeof(T) 
)
inherited

Receives a object from a receptor, by specifying its size or not. Note that a received message can correspond to a deconnection signal.

Warning
Using this function means that you know what kind of object you are suppose to receive, and when you are suppose to receive. If the emitter has several receptors. It might be a problem, and in that case you better use the "request" mode.
See also
vpNetwork::getReceptorIndex()
vpNetwork::receiveRequestFrom()
vpNetwork::receiveRequestOnceFrom()
vpNetwork::receiveAndDecodeRequestFrom()
vpNetwork::receiveAndDecodeRequestOnceFrom()
Parameters
object: Received object.
receptorEmitting: Index of the receptor emitting the message.
sizeOfObject: Size of the received object.
Returns
the number of bytes received, or -1 if an error occured.

Definition at line 365 of file vpNetwork.h.

References vpERROR_TRACE, and vpTRACE.

std::vector< int > vpNetwork::receiveRequest ( )
inherited
std::vector< int > vpNetwork::receiveRequestFrom ( const unsigned int &  receptorEmitting)
inherited

Receives requests, from a specific emitter, untils there is request to receive.

Warning
Requests will be received but not decoded.
See also
vpNetwork::receive()
vpNetwork::receiveRequest()
vpNetwork::receiveRequestOnce()
vpNetwork::receiveRequestOnceFrom()
vpNetwork::receiveAndDecodeRequest()
vpNetwork::receiveAndDecodeRequestFrom()
vpNetwork::receiveAndDecodeRequestOnce()
vpNetwork::receiveAndDecodeRequestOnceFrom()
Parameters
receptorEmitting: Index of the receptor emitting the message

Definition at line 304 of file vpNetwork.cpp.

Referenced by vpNetwork::receiveAndDecodeRequestFrom().

int vpNetwork::receiveRequestOnce ( )
inherited

Receives a message once (in the limit of the Maximum message size value). This message can represent an entire request or not. Several calls to this function might be necessary to get the entire request.

Warning
Requests will be received but not decoded.
See also
vpNetwork::receive()
vpNetwork::receiveRequestFrom()
vpNetwork::receiveRequest()
vpNetwork::receiveRequestOnceFrom()
vpNetwork::receiveAndDecodeRequest()
vpNetwork::receiveAndDecodeRequestFrom()
vpNetwork::receiveAndDecodeRequestOnce()
vpNetwork::receiveAndDecodeRequestOnceFrom()
Returns
The number of bytes received, -1 if an error occured.

Definition at line 328 of file vpNetwork.cpp.

Referenced by vpNetwork::receiveAndDecodeRequestOnce().

int vpNetwork::receiveRequestOnceFrom ( const unsigned int &  receptorEmitting)
inherited

Receives a message once (in the limit of the Maximum message size value), from a specific emitter. This message can represent an entire request or not. Several calls to this function might be necessary to get the entire request.

Warning
Requests will be received but not decoded.
See also
vpNetwork::receive()
vpNetwork::receiveRequestFrom()
vpNetwork::receiveRequest()
vpNetwork::receiveRequestOnce()
vpNetwork::receiveAndDecodeRequest()
vpNetwork::receiveAndDecodeRequestFrom()
vpNetwork::receiveAndDecodeRequestOnce()
vpNetwork::receiveAndDecodeRequestOnceFrom()
Parameters
receptorEmitting: Index of the receptor emitting the message.
Returns
The number of bytes received, -1 if an error occured.

Definition at line 355 of file vpNetwork.cpp.

Referenced by vpNetwork::receiveAndDecodeRequestOnceFrom().

void vpNetwork::removeDecodingRequest ( const char *  id)
inherited

Delete a decoding request from the emitter.

See also
vpNetwork::addDecodingRequest()
Parameters
id: Id of the request to delete.

Definition at line 108 of file vpNetwork.cpp.

References vpNetwork::request_list.

template<typename T >
int vpNetwork::send ( T *  object,
const int unsigned &  sizeOfObject = sizeof(T) 
)
inherited
Examples:
testClient.cpp, and testServer.cpp.
template<typename T >
int vpNetwork::send ( T *  object,
const unsigned int &  sizeOfObject 
)
inherited

Send an object. The size of the received object is suppose to be the size of its type. Note that sending object containing pointers, virtual methods, etc, won't probably work.

Warning
Using this function means that, in the other side of the network, it knows what kind of object it is suppose to receive, and when it is suppose to receive. If the emitter has several receptors. It might be a problem, and in that case you better use the "request" option.
See also
vpNetwork::sendTo()
vpNetwork::sendRequest()
vpNetwork::sendRequestTo()
vpNetwork::sendAndEncodeRequest()
vpNetwork::sendAndEncodeRequestTo()
Parameters
object: Received object.
sizeOfObject: Size of the object
Returns
The number of bytes sent, or -1 if an error happened.

Definition at line 437 of file vpNetwork.h.

References vpTRACE.

int vpNetwork::sendAndEncodeRequest ( vpRequest req)
inherited

Send and encode a request to the first receptor in the list.

See also
vpNetwork::sendRequestTo()
vpNetwork::sendAndEncodeRequest()
vpNetwork::sendAndEncodeRequestTo()
vpNetwork::send()
vpNetwork::sendTo()
Parameters
req: Request to send.
Returns
The number of bytes that have been sent, -1 if an error occured.

Definition at line 241 of file vpNetwork.cpp.

References vpRequest::encode(), and vpNetwork::sendRequest().

int vpNetwork::sendAndEncodeRequestTo ( vpRequest req,
const unsigned int &  dest 
)
inherited

Send and encode a request to a specific receptor.

See also
vpNetwork::sendRequest()
vpNetwork::sendAndEncodeRequest()
vpNetwork::sendAndEncodeRequestTo()
vpNetwork::send()
vpNetwork::sendTo()
Parameters
req: Request to send.
dest: Index of the receptor receiving the request.
Returns
The number of bytes that have been sent, -1 if an error occured.

Definition at line 261 of file vpNetwork.cpp.

References vpRequest::encode(), and vpNetwork::sendRequestTo().

int vpNetwork::sendRequest ( vpRequest req)
inherited

Send a request to the first receptor in the list.

See also
vpNetwork::sendRequestTo()
vpNetwork::sendAndEncodeRequest()
vpNetwork::sendAndEncodeRequestTo()
vpNetwork::send()
vpNetwork::sendTo()
Parameters
req: Request to send.
Returns
The number of bytes that have been sent, -1 if an error occured.

Definition at line 173 of file vpNetwork.cpp.

References vpNetwork::sendRequestTo().

Referenced by vpNetwork::sendAndEncodeRequest().

int vpNetwork::sendRequestTo ( vpRequest req,
const unsigned int &  dest 
)
inherited

Send a request to a specific receptor.

See also
vpNetwork::sendRequest()
vpNetwork::sendAndEncodeRequest()
vpNetwork::sendAndEncodeRequestTo()
vpNetwork::send()
vpNetwork::sendTo()
Parameters
req: Request to send.
dest: Index of the receptor receiving the request.
Returns
The number of bytes that have been sent, -1 if an error occured.

Definition at line 189 of file vpNetwork.cpp.

References vpNetwork::beginning, vpNetwork::end, vpRequest::getId(), vpNetwork::param_sep, vpNetwork::receptor_list, vpNetwork::separator, vpRequest::size(), vpNetwork::verboseMode, and vpTRACE.

Referenced by vpNetwork::sendAndEncodeRequestTo(), and vpNetwork::sendRequest().

template<typename T >
int vpNetwork::sendTo ( T *  object,
const unsigned int &  dest,
const unsigned int &  sizeOfObject = sizeof(T) 
)
inherited

Send an object. The size has to be specified.

Warning
Using this function means that, in the other side of the network, it knows what kind of object it is suppose to receive, and when it is suppose to receive. If the emitter has several receptors. It might be a problem, and in that case you better use the "request" option.
See also
vpNetwork::getReceptorIndex()
vpNetwork::send()
vpNetwork::sendRequest()
vpNetwork::sendRequestTo()
vpNetwork::sendAndEncodeRequest()
vpNetwork::sendAndEncodeRequestTo()
Parameters
object: Object to send.
dest: Index of the receptor that you are sending the object.
sizeOfObject: Size of the object.
Returns
The number of bytes sent, or -1 if an error happened.

Definition at line 481 of file vpNetwork.h.

References vpTRACE.

void vpServer::setMaxNumberOfClients ( unsigned int &  l)
inline

Set the maximum number of clients that can be connected to the server.

See also
vpServer::getMaxNumberOfClients()
Parameters
l: Maximum number of clients.

Definition at line 222 of file vpServer.h.

void vpNetwork::setMaxSizeReceivedMessage ( const unsigned int &  s)
inlineinherited

Change the maximum size that the emitter can receive (in request mode).

See also
vpNetwork::getMaxSizeReceivedMessage()
Parameters
s: new maximum size value.

Definition at line 229 of file vpNetwork.h.

void vpNetwork::setTimeoutSec ( const long &  sec)
inlineinherited

Change the time the emitter spend to check if he receives a message from a receptor. Initially this value is set to 10usec.

See also
vpNetwork::setTimeoutUSec()
Parameters
sec: new value in second.

Definition at line 239 of file vpNetwork.h.

void vpNetwork::setTimeoutUSec ( const long &  usec)
inlineinherited

Change the time the emitter spend to check if he receives a message from a receptor. Initially this value is set to 10usec.

See also
vpNetwork::setTimeoutSec()
Parameters
usec: new value in micro second.

Definition at line 249 of file vpNetwork.h.

void vpNetwork::setVerbose ( const bool &  mode)
inlineinherited

Set the verbose mode.

Parameters
mode: Change the verbose mode. True to turn on, False to turn off.

Definition at line 256 of file vpNetwork.h.

bool vpServer::start ( )

Enable the server to wait for clients (on the limit of the maximum limit).

Returns
True if the server has started, false otherwise.
Examples:
testServer.cpp.

Definition at line 148 of file vpServer.cpp.

References vpNetwork::emitter, and vpERROR_TRACE.

Referenced by checkForConnections().

Member Data Documentation

std::string vpNetwork::beginning
protectedinherited
std::string vpNetwork::currentMessageReceived
protectedinherited

Definition at line 147 of file vpNetwork.h.

Referenced by vpNetwork::receiveAndDecodeRequestOnceFrom().

vpEmitter vpNetwork::emitter
protectedinherited

Definition at line 129 of file vpNetwork.h.

Referenced by checkForConnections(), start(), vpServer(), and ~vpServer().

std::string vpNetwork::end
protectedinherited
unsigned int vpNetwork::max_size_message
protectedinherited

Definition at line 141 of file vpNetwork.h.

Referenced by vpNetwork::receiveAndDecodeRequestOnceFrom().

std::string vpNetwork::param_sep
protectedinherited
fd_set vpNetwork::readFileDescriptor
protectedinherited

Definition at line 131 of file vpNetwork.h.

Referenced by checkForConnections(), and vpNetwork::receiveAndDecodeRequestOnceFrom().

std::string vpNetwork::separator
protectedinherited
int vpNetwork::socketMax
protectedinherited

Definition at line 133 of file vpNetwork.h.

Referenced by checkForConnections(), and vpNetwork::receiveAndDecodeRequestOnceFrom().

struct timeval vpNetwork::tv
protectedinherited
long vpNetwork::tv_sec
protectedinherited
long vpNetwork::tv_usec
protectedinherited
bool vpNetwork::verboseMode
protectedinherited