39 #include <visp3/core/vpNetwork.h> 42 #ifdef VISP_HAVE_FUNC_INET_NTOP 45 : emitter(), receptor_list(), readFileDescriptor(), socketMax(0), request_list(), max_size_message(999999),
46 separator(
"[*@*]"), beginning(
"[*start*]"), end(
"[*end*]"), param_sep(
"[*|*]"), currentMessageReceived(), tv(),
47 tv_sec(0), tv_usec(10), verboseMode(false)
61 WSAStartup(MAKEWORD(2, 0), &WSAData);
85 bool alreadyHas =
false;
94 std::cout <<
"Server already has one request with the similar ID. " 95 "Request hasn't been added." 110 for (
unsigned int i = 0; i <
request_list.size(); i++) {
126 std::cout <<
id << i <<
" : " << inet_ntoa(
receptor_list[i].receptorAddress.sin_addr) << std::endl;
140 struct hostent *server = gethostbyname(name);
142 if (server == NULL) {
143 std::string noSuchHostMessage(
"ERROR, ");
144 noSuchHostMessage.append(name);
145 noSuchHostMessage.append(
": no such host\n");
146 vpERROR_TRACE(noSuchHostMessage.c_str(),
"vpNetwork::getReceptorIndex()");
150 std::string ip = inet_ntoa(*(in_addr *)server->h_addr);
193 if (size == 0 || dest > (
unsigned)sizeMinusOne) {
195 vpTRACE(
"Cannot Send Request! Bad Index");
201 if (req.
size() != 0) {
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 = (int)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(),
284 return _handleRequests();
306 _receiveRequestFrom(receptorEmitting);
307 return _handleRequests();
330 _receiveRequestOnce();
331 return _handleFirstRequest();
357 _receiveRequestOnceFrom(receptorEmitting);
358 return _handleFirstRequest();
378 for (
unsigned int i = 0; i < res.size(); i++)
405 for (
unsigned int i = 0; i < res.size(); i++) {
486 std::vector<int> vpNetwork::_handleRequests()
488 std::vector<int> resIndex;
489 int index = _handleFirstRequest();
491 while (index != -1) {
492 resIndex.push_back(index);
493 index = _handleFirstRequest();
509 int vpNetwork::_handleFirstRequest()
515 if (indStart == std::string::npos && indSep == std::string::npos && indEnd == std::string::npos) {
525 if (indStart == std::string::npos || indSep == std::string::npos || indEnd == std::string::npos)
528 if (indEnd < indStart) {
536 if (indStart2 != std::string::npos && indStart2 < indEnd) {
543 size_t deb = indStart +
beginning.size();
554 bool hasBeenFound =
false;
555 for (
unsigned int i = 0; i <
request_list.size(); i++) {
567 vpTRACE(
"No request corresponds to the received message");
571 size_t indDebParam = indSep +
separator.size();
575 while (indEndParam != std::string::npos || indEndParam < indEnd) {
577 request_list[(unsigned)indRequest]->addParameter(param);
578 indDebParam = indEndParam +
param_sep.size();
583 request_list[(unsigned)indRequest]->addParameter(param);
603 void vpNetwork::_receiveRequest()
605 while (_receiveRequestOnce() > 0) {
626 void vpNetwork::_receiveRequestFrom(
const unsigned int &receptorEmitting)
628 while (_receiveRequestOnceFrom(receptorEmitting) > 0) {
650 int vpNetwork::_receiveRequestOnce()
683 }
else if (value == 0) {
690 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 697 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[i].receptorAddress.sin_addr) << std::endl;
702 std::string returnVal(buf, (
unsigned int)numbytes);
735 int vpNetwork::_receiveRequestOnceFrom(
const unsigned int &receptorEmitting)
739 if (size == 0 || receptorEmitting > (
unsigned)sizeMinusOne) {
741 vpTRACE(
"No receptor at the specified index!");
763 }
else if (value == 0) {
769 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 772 numbytes = recv((
unsigned int)
receptor_list[receptorEmitting].socketFileDescriptorReceptor, buf,
776 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[receptorEmitting].receptorAddress.sin_addr)
782 std::string returnVal(buf, (
unsigned int)numbytes);
792 #elif !defined(VISP_BUILD_SHARED_LIBS) 794 void dummy_vpNetwork(){};
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