39 #include <visp3/core/vpNetwork.h>
42 : emitter(), receptor_list(), readFileDescriptor(), socketMax(0), request_list(),
43 max_size_message(999999), separator(
"[*@*]"), beginning(
"[*start*]"), end(
"[*end*]"),
44 param_sep(
"[*|*]"), currentMessageReceived(), tv(), tv_sec(0), tv_usec(10),
55 WSAStartup(MAKEWORD(2,0), &WSAData);
79 bool alreadyHas =
false;
88 std::cout <<
"Server already has one request with the similar ID. Request hasn't been added." << std::endl;
121 std::cout <<
id << i <<
" : " << inet_ntoa(
receptor_list[i].receptorAddress.sin_addr) << std::endl;
134 struct hostent *server = gethostbyname(name);
136 if ( server == NULL )
138 std::string noSuchHostMessage(
"ERROR, " );
139 noSuchHostMessage.append( name );
140 noSuchHostMessage.append(
": no such host\n" );
141 vpERROR_TRACE( noSuchHostMessage.c_str(),
"vpNetwork::getReceptorIndex()" );
145 std::string ip = inet_ntoa(*(in_addr *)server->h_addr);
192 if(size == 0 || dest > (
unsigned)sizeMinusOne)
195 vpTRACE(
"Cannot Send Request! Bad Index" );
204 for(
unsigned int i = 1 ; i < req.
size() ; i++){
213 #if defined(__linux__)
214 flags = MSG_NOSIGNAL;
217 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
218 int value = sendto(
receptor_list[dest].socketFileDescriptorReceptor, message.c_str(), message.size(), flags,
221 int value = sendto((
unsigned)
receptor_list[dest].socketFileDescriptorReceptor, message.c_str(), (int)message.size(), flags,
284 return _handleRequests();
305 _receiveRequestFrom(receptorEmitting);
306 return _handleRequests();
329 _receiveRequestOnce();
330 return _handleFirstRequest();
355 _receiveRequestOnceFrom(receptorEmitting);
356 return _handleFirstRequest();
376 for(
unsigned int i = 0 ; i < res.size() ; i++)
402 for(
unsigned int i = 0 ; i < res.size() ; i++)
483 std::vector<int> vpNetwork::_handleRequests()
485 std::vector<int> resIndex;
486 int index = _handleFirstRequest();
490 resIndex.push_back(index);
491 index = _handleFirstRequest();
507 int vpNetwork::_handleFirstRequest()
513 if (indStart == std::string::npos && indSep == std::string::npos && indEnd == std::string::npos)
524 if(indStart == std::string::npos || indSep == std::string::npos || indEnd == std::string::npos)
527 if(indEnd < indStart)
536 if(indStart2 != std::string::npos && indStart2 < indEnd)
544 size_t deb = indStart +
beginning.size();
553 bool hasBeenFound =
false;
567 vpTRACE(
"No request corresponds to the received message");
571 size_t indDebParam = indSep +
separator.size();
575 while(indEndParam != std::string::npos || indEndParam < indEnd)
578 request_list[(unsigned)indRequest]->addParameter(param);
579 indDebParam = indEndParam+
param_sep.size();
584 request_list[(unsigned)indRequest]->addParameter(param);
604 void vpNetwork::_receiveRequest()
606 while(_receiveRequestOnce() > 0) {};
625 void vpNetwork::_receiveRequestFrom(
const unsigned int &receptorEmitting)
627 while(_receiveRequestOnceFrom(receptorEmitting) > 0) {};
648 int vpNetwork::_receiveRequestOnce()
686 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
694 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[i].receptorAddress.sin_addr) << std::endl;
699 else if(numbytes > 0){
700 std::string returnVal(buf, (
unsigned int)numbytes);
732 int vpNetwork::_receiveRequestOnceFrom(
const unsigned int &receptorEmitting)
736 if(size == 0 || receptorEmitting > (
unsigned)sizeMinusOne )
739 vpTRACE(
"No receptor at the specified index!" );
765 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
772 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[receptorEmitting].receptorAddress.sin_addr) << std::endl;
777 else if(numbytes > 0){
778 std::string returnVal(buf, (
unsigned int)numbytes);
unsigned int max_size_message
int receiveRequestOnceFrom(const unsigned int &receptorEmitting)
This the request that will transit on the network.
std::vector< vpRequest * > request_list
fd_set readFileDescriptor
void print(const char *id="")
int receiveAndDecodeRequestOnceFrom(const unsigned int &receptorEmitting)
int sendRequestTo(vpRequest &req, const unsigned int &dest)
std::vector< int > receiveRequestFrom(const unsigned int &receptorEmitting)
std::vector< int > receiveAndDecodeRequestFrom(const unsigned int &receptorEmitting)
std::vector< int > receiveAndDecodeRequest()
int sendAndEncodeRequestTo(vpRequest &req, const unsigned int &dest)
int receiveAndDecodeRequestOnce()
int sendAndEncodeRequest(vpRequest &req)
std::vector< int > receiveRequest()
std::vector< vpReceptor > receptor_list
int sendRequest(vpRequest &req)
void removeDecodingRequest(const char *)
std::string currentMessageReceived
int getReceptorIndex(const char *name)
void addDecodingRequest(vpRequest *)