43 #include <visp/vpNetwork.h>
46 : emitter(), receptor_list(), readFileDescriptor(), socketMax(0), request_list(),
47 max_size_message(999999), separator(
"[*@*]"), beginning(
"[*start*]"), end(
"[*end*]"),
48 param_sep(
"[*|*]"), currentMessageReceived(), tv(), tv_sec(0), tv_usec(10),
59 WSAStartup(MAKEWORD(2,0), &WSAData);
83 bool alreadyHas =
false;
92 std::cout <<
"Server already has one request with the similar ID. Request hasn't been added." << std::endl;
125 std::cout <<
id << i <<
" : " << inet_ntoa(
receptor_list[i].receptorAddress.sin_addr) << std::endl;
138 struct hostent *server = gethostbyname(name);
140 if ( server == NULL )
142 std::string noSuchHostMessage(
"ERROR, " );
143 noSuchHostMessage.append( name );
144 noSuchHostMessage.append(
": no such host\n" );
145 vpERROR_TRACE( noSuchHostMessage.c_str(),
"vpNetwork::getReceptorIndex()" );
149 std::string ip = inet_ntoa(*(in_addr *)server->h_addr);
196 if(size == 0 || dest > (
unsigned)sizeMinusOne)
199 vpTRACE(
"Cannot Send Request! Bad Index" );
208 for(
unsigned int i = 1 ; i < req.
size() ; i++){
217 #if defined(__linux__)
218 flags = MSG_NOSIGNAL;
221 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
222 int value = sendto(
receptor_list[dest].socketFileDescriptorReceptor, message.c_str(), message.size(), flags,
225 int value = sendto((
unsigned)
receptor_list[dest].socketFileDescriptorReceptor, message.c_str(), (int)message.size(), flags,
288 return _handleRequests();
309 _receiveRequestFrom(receptorEmitting);
310 return _handleRequests();
333 _receiveRequestOnce();
334 return _handleFirstRequest();
359 _receiveRequestOnceFrom(receptorEmitting);
360 return _handleFirstRequest();
380 for(
unsigned int i = 0 ; i < res.size() ; i++)
406 for(
unsigned int i = 0 ; i < res.size() ; i++)
487 std::vector<int> vpNetwork::_handleRequests()
489 std::vector<int> resIndex;
490 int index = _handleFirstRequest();
494 resIndex.push_back(index);
495 index = _handleFirstRequest();
511 int vpNetwork::_handleFirstRequest()
517 if (indStart == std::string::npos && indSep == std::string::npos && indEnd == std::string::npos)
528 if(indStart == std::string::npos || indSep == std::string::npos || indEnd == std::string::npos)
531 if(indEnd < indStart)
540 if(indStart2 != std::string::npos && indStart2 < indEnd)
548 size_t deb = indStart +
beginning.size();
557 bool hasBeenFound =
false;
571 vpTRACE(
"No request corresponds to the received message");
575 size_t indDebParam = indSep +
separator.size();
579 while(indEndParam != std::string::npos)
582 request_list[(unsigned)indRequest]->addParameter(param);
583 indDebParam = indEndParam+
param_sep.size();
588 request_list[(unsigned)indRequest]->addParameter(param);
608 void vpNetwork::_receiveRequest()
610 while(_receiveRequestOnce() > 0) {};
629 void vpNetwork::_receiveRequestFrom(
const unsigned int &receptorEmitting)
631 while(_receiveRequestOnceFrom(receptorEmitting) > 0) {};
652 int vpNetwork::_receiveRequestOnce()
690 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
698 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[i].receptorAddress.sin_addr) << std::endl;
703 else if(numbytes > 0){
704 std::string returnVal(buf, (
unsigned int)numbytes);
736 int vpNetwork::_receiveRequestOnceFrom(
const unsigned int &receptorEmitting)
740 if(size == 0 || receptorEmitting > (
unsigned)sizeMinusOne )
743 vpTRACE(
"No receptor at the specified index!" );
769 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
776 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[receptorEmitting].receptorAddress.sin_addr) << std::endl;
781 else if(numbytes > 0){
782 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 *)