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)
50 #ifdef TARGET_OS_IPHONE
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__)))
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(),
283 privReceiveRequest();
284 return privHandleRequests();
306 privReceiveRequestFrom(receptorEmitting);
307 return privHandleRequests();
330 privReceiveRequestOnce();
331 return privHandleFirstRequest();
357 privReceiveRequestOnceFrom(receptorEmitting);
358 return privHandleFirstRequest();
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::privHandleRequests()
488 std::vector<int> resIndex;
489 int index = privHandleFirstRequest();
491 while (index != -1) {
492 resIndex.push_back(index);
493 index = privHandleFirstRequest();
509 int vpNetwork::privHandleFirstRequest()
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::privReceiveRequest()
605 while (privReceiveRequestOnce() > 0) {
626 void vpNetwork::privReceiveRequestFrom(
const unsigned int &receptorEmitting)
628 while (privReceiveRequestOnceFrom(receptorEmitting) > 0) {
650 int vpNetwork::privReceiveRequestOnce()
659 #ifdef TARGET_OS_IPHONE
684 else if (value == 0) {
692 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
699 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[i].receptorAddress.sin_addr) << std::endl;
705 std::string returnVal(buf, (
unsigned int)numbytes);
738 int vpNetwork::privReceiveRequestOnceFrom(
const unsigned int &receptorEmitting)
742 if (size == 0 || receptorEmitting > (
unsigned)sizeMinusOne) {
744 vpTRACE(
"No receptor at the specified index!");
749 #ifdef TARGET_OS_IPHONE
767 else if (value == 0) {
774 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
777 numbytes = recv((
unsigned int)
receptor_list[receptorEmitting].socketFileDescriptorReceptor, buf,
781 std::cout <<
"Disconnected : " << inet_ntoa(
receptor_list[receptorEmitting].receptorAddress.sin_addr)
788 std::string returnVal(buf, (
unsigned int)numbytes);
798 #elif !defined(VISP_BUILD_SHARED_LIBS)
800 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.