39 #include <visp3/core/vpNetwork.h> 42 : emitter(), receptor_list(), readFileDescriptor(), socketMax(0), request_list(), max_size_message(999999),
43 separator(
"[*@*]"), beginning(
"[*start*]"), end(
"[*end*]"), param_sep(
"[*|*]"), currentMessageReceived(), tv(),
44 tv_sec(0), tv_usec(10), verboseMode(false)
58 WSAStartup(MAKEWORD(2, 0), &WSAData);
82 bool alreadyHas =
false;
91 std::cout <<
"Server already has one request with the similar ID. " 92 "Request hasn't been added." 107 for (
unsigned int i = 0; i <
request_list.size(); i++) {
123 std::cout <<
id << i <<
" : " << inet_ntoa(
receptor_list[i].receptorAddress.sin_addr) << std::endl;
137 struct hostent *server = gethostbyname(name);
139 if (server == NULL) {
140 std::string noSuchHostMessage(
"ERROR, ");
141 noSuchHostMessage.append(name);
142 noSuchHostMessage.append(
": no such host\n");
143 vpERROR_TRACE(noSuchHostMessage.c_str(),
"vpNetwork::getReceptorIndex()");
147 std::string ip = inet_ntoa(*(in_addr *)server->h_addr);
190 if (size == 0 || dest > (
unsigned)sizeMinusOne) {
192 vpTRACE(
"Cannot Send Request! Bad Index");
198 if (req.
size() != 0) {
201 for (
unsigned int i = 1; i < req.
size(); i++) {
210 #if defined(__linux__) 211 flags = MSG_NOSIGNAL;
214 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 215 int value = (int)sendto(
receptor_list[dest].socketFileDescriptorReceptor, message.c_str(), message.size(), flags,
218 int value = sendto((
unsigned)
receptor_list[dest].socketFileDescriptorReceptor, message.c_str(), (int)message.size(),
281 return _handleRequests();
303 _receiveRequestFrom(receptorEmitting);
304 return _handleRequests();
327 _receiveRequestOnce();
328 return _handleFirstRequest();
354 _receiveRequestOnceFrom(receptorEmitting);
355 return _handleFirstRequest();
375 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();
488 while (index != -1) {
489 resIndex.push_back(index);
490 index = _handleFirstRequest();
506 int vpNetwork::_handleFirstRequest()
512 if (indStart == std::string::npos && indSep == std::string::npos && indEnd == std::string::npos) {
522 if (indStart == std::string::npos || indSep == std::string::npos || indEnd == std::string::npos)
525 if (indEnd < indStart) {
533 if (indStart2 != std::string::npos && indStart2 < indEnd) {
540 size_t deb = indStart +
beginning.size();
551 bool hasBeenFound =
false;
552 for (
unsigned int i = 0; i <
request_list.size(); i++) {
564 vpTRACE(
"No request corresponds to the received message");
568 size_t indDebParam = indSep +
separator.size();
572 while (indEndParam != std::string::npos || indEndParam < indEnd) {
574 request_list[(unsigned)indRequest]->addParameter(param);
575 indDebParam = indEndParam +
param_sep.size();
580 request_list[(unsigned)indRequest]->addParameter(param);
600 void vpNetwork::_receiveRequest()
602 while (_receiveRequestOnce() > 0) {
623 void vpNetwork::_receiveRequestFrom(
const unsigned int &receptorEmitting)
625 while (_receiveRequestOnceFrom(receptorEmitting) > 0) {
647 int vpNetwork::_receiveRequestOnce()
680 }
else if (value == 0) {
687 #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 std::string returnVal(buf, (
unsigned int)numbytes);
732 int vpNetwork::_receiveRequestOnceFrom(
const unsigned int &receptorEmitting)
736 if (size == 0 || receptorEmitting > (
unsigned)sizeMinusOne) {
738 vpTRACE(
"No receptor at the specified index!");
760 }
else if (value == 0) {
766 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 769 numbytes = recv((
unsigned int)
receptor_list[receptorEmitting].socketFileDescriptorReceptor, buf,
773 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[receptorEmitting].receptorAddress.sin_addr)
779 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.
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 *)
std::vector< vpRequest * > request_list