39 #include <visp3/core/vpNetwork.h>
42 #ifdef VISP_HAVE_FUNC_INET_NTOP
44 #include <visp3/core/vpDebug.h>
47 : emitter(), receptor_list(), readFileDescriptor(), socketMax(0), request_list(), max_size_message(999999),
48 separator(
"[*@*]"), beginning(
"[*start*]"), end(
"[*end*]"), param_sep(
"[*|*]"), currentMessageReceived(), tv(),
49 tv_sec(0), tv_usec(10), verboseMode(false)
52 #ifdef TARGET_OS_IPHONE
63 WSAStartup(MAKEWORD(2, 0), &WSAData);
87 bool alreadyHas =
false;
96 std::cout <<
"Server already has one request with the similar ID. "
97 "Request hasn't been added."
112 for (
unsigned int i = 0; i <
request_list.size(); i++) {
128 std::cout <<
id << i <<
" : " << inet_ntoa(
receptor_list[i].receptorAddress.sin_addr) << std::endl;
142 struct hostent *server = gethostbyname(name);
144 if (server ==
nullptr) {
145 std::string noSuchHostMessage(
"ERROR, ");
146 noSuchHostMessage.append(name);
147 noSuchHostMessage.append(
": no such host\n");
148 vpERROR_TRACE(noSuchHostMessage.c_str(),
"vpNetwork::getReceptorIndex()");
152 std::string ip = inet_ntoa(*(in_addr *)server->h_addr);
195 if (size == 0 || dest > (
unsigned)sizeMinusOne) {
197 vpTRACE(
"Cannot Send Request! Bad Index");
203 if (req.
size() != 0) {
206 for (
unsigned int i = 1; i < req.
size(); i++) {
215 #if defined(__linux__)
216 flags = MSG_NOSIGNAL;
219 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
220 int value = (int)sendto(
receptor_list[dest].socketFileDescriptorReceptor, message.c_str(), message.size(), flags,
223 int value = sendto((
unsigned)
receptor_list[dest].socketFileDescriptorReceptor, message.c_str(), (
int)message.size(),
285 privReceiveRequest();
286 return privHandleRequests();
308 privReceiveRequestFrom(receptorEmitting);
309 return privHandleRequests();
332 privReceiveRequestOnce();
333 return privHandleFirstRequest();
359 privReceiveRequestOnceFrom(receptorEmitting);
360 return privHandleFirstRequest();
380 for (
unsigned int i = 0; i < res.size(); i++)
407 for (
unsigned int i = 0; i < res.size(); i++) {
488 std::vector<int> vpNetwork::privHandleRequests()
490 std::vector<int> resIndex;
491 int index = privHandleFirstRequest();
493 while (index != -1) {
494 resIndex.push_back(index);
495 index = privHandleFirstRequest();
511 int vpNetwork::privHandleFirstRequest()
517 if (indStart == std::string::npos && indSep == std::string::npos && indEnd == std::string::npos) {
527 if (indStart == std::string::npos || indSep == std::string::npos || indEnd == std::string::npos)
530 if (indEnd < indStart) {
538 if (indStart2 != std::string::npos && indStart2 < indEnd) {
545 size_t deb = indStart +
beginning.size();
556 bool hasBeenFound =
false;
557 for (
unsigned int i = 0; i <
request_list.size(); i++) {
569 vpTRACE(
"No request corresponds to the received message");
573 size_t indDebParam = indSep +
separator.size();
577 while (indEndParam != std::string::npos || indEndParam < indEnd) {
579 request_list[(unsigned)indRequest]->addParameter(param);
580 indDebParam = indEndParam +
param_sep.size();
585 request_list[(unsigned)indRequest]->addParameter(param);
605 void vpNetwork::privReceiveRequest()
607 while (privReceiveRequestOnce() > 0) {
628 void vpNetwork::privReceiveRequestFrom(
const unsigned int &receptorEmitting)
630 while (privReceiveRequestOnceFrom(receptorEmitting) > 0) {
652 int vpNetwork::privReceiveRequestOnce()
661 #ifdef TARGET_OS_IPHONE
686 else if (value == 0) {
694 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
701 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[i].receptorAddress.sin_addr) << std::endl;
707 std::string returnVal(buf, (
unsigned int)numbytes);
740 int vpNetwork::privReceiveRequestOnceFrom(
const unsigned int &receptorEmitting)
744 if (size == 0 || receptorEmitting > (
unsigned)sizeMinusOne) {
746 vpTRACE(
"No receptor at the specified index!");
751 #ifdef TARGET_OS_IPHONE
769 else if (value == 0) {
776 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
779 numbytes = recv((
unsigned int)
receptor_list[receptorEmitting].socketFileDescriptorReceptor, buf,
783 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[receptorEmitting].receptorAddress.sin_addr)
790 std::string returnVal(buf, (
unsigned int)numbytes);
800 #elif !defined(VISP_BUILD_SHARED_LIBS)
802 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