34 #include <visp3/core/vpConfig.h>
38 #if defined(WINRT) || defined(_WIN32)
39 #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
40 #define _WINSOCK_DEPRECATED_NO_WARNINGS
45 #ifdef VISP_HAVE_FUNC_INET_NTOP
47 #include <visp3/core/vpNetwork.h>
48 #include <visp3/core/vpDebug.h>
51 : emitter(), receptor_list(), readFileDescriptor(), socketMax(0), request_list(), max_size_message(999999),
52 separator(
"[*@*]"), beginning(
"[*start*]"), end(
"[*end*]"), param_sep(
"[*|*]"), currentMessageReceived(), tv(),
53 tv_sec(0), tv_usec(10), verboseMode(false)
56 #ifdef TARGET_OS_IPHONE
67 WSAStartup(MAKEWORD(2, 0), &WSAData);
91 bool alreadyHas =
false;
100 std::cout <<
"Server already has one request with the similar ID. "
101 "Request hasn't been added."
116 for (
unsigned int i = 0; i <
request_list.size(); i++) {
132 std::cout <<
id << i <<
" : " << inet_ntoa(
receptor_list[i].receptorAddress.sin_addr) << std::endl;
146 struct hostent *server = gethostbyname(name);
148 if (server ==
nullptr) {
149 std::string noSuchHostMessage(
"ERROR, ");
150 noSuchHostMessage.append(name);
151 noSuchHostMessage.append(
": no such host\n");
152 vpERROR_TRACE(noSuchHostMessage.c_str(),
"vpNetwork::getReceptorIndex()");
156 std::string ip = inet_ntoa(*(in_addr *)server->h_addr);
199 if (size == 0 || dest > (
unsigned)sizeMinusOne) {
201 vpTRACE(
"Cannot Send Request! Bad Index");
207 if (req.
size() != 0) {
210 for (
unsigned int i = 1; i < req.
size(); i++) {
219 #if defined(__linux__)
220 flags = MSG_NOSIGNAL;
223 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
224 int value = (int)sendto(
receptor_list[dest].socketFileDescriptorReceptor, message.c_str(), message.size(), flags,
227 int value = sendto((
unsigned)
receptor_list[dest].socketFileDescriptorReceptor, message.c_str(), (
int)message.size(),
289 privReceiveRequest();
290 return privHandleRequests();
312 privReceiveRequestFrom(receptorEmitting);
313 return privHandleRequests();
336 privReceiveRequestOnce();
337 return privHandleFirstRequest();
363 privReceiveRequestOnceFrom(receptorEmitting);
364 return privHandleFirstRequest();
384 for (
unsigned int i = 0; i < res.size(); i++)
411 for (
unsigned int i = 0; i < res.size(); i++) {
492 std::vector<int> vpNetwork::privHandleRequests()
494 std::vector<int> resIndex;
495 int index = privHandleFirstRequest();
497 while (index != -1) {
498 resIndex.push_back(index);
499 index = privHandleFirstRequest();
515 int vpNetwork::privHandleFirstRequest()
521 if (indStart == std::string::npos && indSep == std::string::npos && indEnd == std::string::npos) {
526 vpTRACE(
"Incorrect message");
531 if (indStart == std::string::npos || indSep == std::string::npos || indEnd == std::string::npos)
534 if (indEnd < indStart) {
536 vpTRACE(
"Incorrect message");
542 if (indStart2 != std::string::npos && indStart2 < indEnd) {
544 vpTRACE(
"Incorrect message");
549 size_t deb = indStart +
beginning.size();
560 bool hasBeenFound =
false;
561 for (
unsigned int i = 0; i <
request_list.size(); i++) {
573 vpTRACE(
"No request corresponds to the received message");
577 size_t indDebParam = indSep +
separator.size();
581 while (indEndParam != std::string::npos || indEndParam < indEnd) {
583 request_list[(unsigned)indRequest]->addParameter(param);
584 indDebParam = indEndParam +
param_sep.size();
589 request_list[(unsigned)indRequest]->addParameter(param);
609 void vpNetwork::privReceiveRequest()
611 while (privReceiveRequestOnce() > 0) {
632 void vpNetwork::privReceiveRequestFrom(
const unsigned int &receptorEmitting)
634 while (privReceiveRequestOnceFrom(receptorEmitting) > 0) {
656 int vpNetwork::privReceiveRequestOnce()
660 vpTRACE(
"No Receptor!");
665 #ifdef TARGET_OS_IPHONE
687 vpERROR_TRACE(
"Select error");
690 else if (value == 0) {
698 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
705 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[i].receptorAddress.sin_addr) << std::endl;
711 std::string returnVal(buf, (
unsigned int)numbytes);
744 int vpNetwork::privReceiveRequestOnceFrom(
const unsigned int &receptorEmitting)
748 if (size == 0 || receptorEmitting > (
unsigned)sizeMinusOne) {
750 vpTRACE(
"No receptor at the specified index!");
755 #ifdef TARGET_OS_IPHONE
770 vpERROR_TRACE(
"Select error");
773 else if (value == 0) {
780 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
783 numbytes = recv((
unsigned int)
receptor_list[receptorEmitting].socketFileDescriptorReceptor, buf,
787 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[receptorEmitting].receptorAddress.sin_addr)
794 std::string returnVal(buf, (
unsigned int)numbytes);
804 #elif !defined(VISP_BUILD_SHARED_LIBS)
806 void dummy_vpNetwork() { };
std::vector< int > receiveAndDecodeRequestFrom(const unsigned int &receptorEmitting)
int sendRequest(vpRequest &req)
fd_set readFileDescriptor
std::string currentMessageReceived
int sendAndEncodeRequestTo(vpRequest &req, const unsigned int &dest)
std::vector< vpRequest * > request_list
int sendAndEncodeRequest(vpRequest &req)
int receiveAndDecodeRequestOnce()
std::vector< int > receiveRequest()
int sendRequestTo(vpRequest &req, const unsigned int &dest)
std::vector< vpReceptor > receptor_list
void addDecodingRequest(vpRequest *)
std::vector< int > receiveRequestFrom(const unsigned int &receptorEmitting)
std::vector< int > receiveAndDecodeRequest()
void removeDecodingRequest(const char *)
int receiveAndDecodeRequestOnceFrom(const unsigned int &receptorEmitting)
void print(const char *id="")
int receiveRequestOnceFrom(const unsigned int &receptorEmitting)
int getReceptorIndex(const char *name)
unsigned int max_size_message
This the request that will transit on the network.
std::string getId() const
unsigned int size() const