ViSP  2.7.0
vpNetwork.h
1 /****************************************************************************
2  *
3  * $Id: vpNetwork.h 4137 2013-02-14 06:56:53Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * TCP Network
36  *
37  * Authors:
38  * Aurelien Yol
39  *
40  *****************************************************************************/
41 
42 #ifndef vpNetwork_H
43 #define vpNetwork_H
44 
45 #include <visp/vpConfig.h>
46 #include <visp/vpRequest.h>
47 
48 #include <vector>
49 #include <stdio.h>
50 #include <string.h>
51 #include <iostream>
52 
53 #ifdef UNIX
54 # include <unistd.h>
55 # include <sys/socket.h>
56 # include <netinet/in.h>
57 # include <arpa/inet.h>
58 # include <netdb.h>
59 #else
60 # include<io.h>
61 //# include<winsock.h>
62 # include<winsock2.h>
63 //# pragma comment(lib, "ws2_32.lib") // Done by CMake in main CMakeLists.txt
64 #endif
65 
66 
83 class VISP_EXPORT vpNetwork
84 {
85 protected:
86 
87  struct vpReceptor{
88 #ifdef UNIX
91 #else
92  SOCKET socketFileDescriptorReceptor;
93  int receptorAddressSize;
94 #endif
95  struct sockaddr_in receptorAddress;
96  std::string receptorIP;
97  };
98 
99  struct vpEmitter{
100  struct sockaddr_in emitterAdress;
101 #ifdef UNIX
103 #else
104  SOCKET socketFileDescriptorEmitter;
105 #endif
106  };
107 
108  //######## PARAMETERS ########
109  //# #
110  //############################
111 
113  std::vector<vpReceptor> receptor_list;
115 #ifdef UNIX
117 #else
118  SOCKET socketMax;
119 #endif
120 
121  //Message Handling
122  std::vector<vpRequest*> request_list;
123 
124  unsigned int max_size_message;
125  std::string separator;
126  std::string beginning;
127  std::string end;
128  std::string param_sep;
129 
131 
132  struct timeval tv;
133  long tv_sec;
134  long tv_usec;
135 
137 
138 private:
139 
140  std::vector<int> _handleRequests();
141  int _handleFirstRequest();
142 
143  void _receiveRequest();
144  void _receiveRequestFrom(const unsigned int &receptorEmitting);
145  int _receiveRequestOnce();
146  int _receiveRequestOnceFrom(const unsigned int &receptorEmitting);
147 
148 public:
149 
150  vpNetwork();
151  virtual ~vpNetwork();
152 
153  void addDecodingRequest(vpRequest *);
154 
155  int getReceptorIndex(const char *name);
156 
164  std::string getRequestIdFromIndex(const int &ind){
165  if(ind >= (int)request_list.size() || ind < 0)
166  return "";
167  return request_list[(unsigned)ind]->getId();
168  }
169 
177  unsigned int getMaxSizeReceivedMessage(){ return max_size_message; }
178 
179  void print(const char *id = "");
180 
181  template<typename T>
182  int receive(T* object, const unsigned int &sizeOfObject = sizeof(T));
183  template<typename T>
184  int receiveFrom(T* object, const unsigned int &receptorEmitting, const unsigned int &sizeOfObject = sizeof(T));
185 
186  std::vector<int> receiveRequest();
187  std::vector<int> receiveRequestFrom(const unsigned int &receptorEmitting);
188  int receiveRequestOnce();
189  int receiveRequestOnceFrom(const unsigned int &receptorEmitting);
190 
191  std::vector<int> receiveAndDecodeRequest();
192  std::vector<int> receiveAndDecodeRequestFrom(const unsigned int &receptorEmitting);
193  int receiveAndDecodeRequestOnce();
194  int receiveAndDecodeRequestOnceFrom(const unsigned int &receptorEmitting);
195 
196  void removeDecodingRequest(const char *);
197 
198  template<typename T>
199  int send(T* object, const int unsigned &sizeOfObject = sizeof(T));
200  template<typename T>
201  int sendTo(T* object, const unsigned int &dest, const unsigned int &sizeOfObject = sizeof(T));
202 
203  int sendRequest(vpRequest &req);
204  int sendRequestTo(vpRequest &req, const unsigned int &dest);
205 
206  int sendAndEncodeRequest(vpRequest &req);
207  int sendAndEncodeRequestTo(vpRequest &req, const unsigned int &dest);
208 
216  void setMaxSizeReceivedMessage(const unsigned int &s){ max_size_message = s;}
217 
226  void setTimeoutSec(const long &sec){ tv_sec = sec; }
227 
236  void setTimeoutUSec(const long &usec){ tv_usec = usec; }
237 
243  void setVerbose(const bool &mode){ verboseMode = mode; }
244 };
245 
246 //######## Definition of Template Functions ########
247 //# #
248 //##################################################
249 
269 template<typename T>
270 int vpNetwork::receive(T* object, const unsigned int &sizeOfObject)
271 {
272  if(receptor_list.size() == 0)
273  {
274  if(verboseMode)
275  vpTRACE( "No receptor" );
276  return -1;
277  }
278 
279  tv.tv_sec = tv_sec;
280  tv.tv_usec = tv_usec;
281 
282  FD_ZERO(&readFileDescriptor);
283 
284  for(unsigned int i=0; i<receptor_list.size(); i++){
285  FD_SET((unsigned int)receptor_list[i].socketFileDescriptorReceptor,&readFileDescriptor);
286 
287  if(i == 0)
288  socketMax = receptor_list[i].socketFileDescriptorReceptor;
289 
290  if(socketMax < receptor_list[i].socketFileDescriptorReceptor) socketMax = receptor_list[i].socketFileDescriptorReceptor;
291  }
292 
293  int value = select((int)socketMax+1,&readFileDescriptor,NULL,NULL,&tv);
294  int numbytes = 0;
295 
296  if(value == -1){
297  if(verboseMode)
298  vpERROR_TRACE( "Select error" );
299  return -1;
300  }
301  else if(value == 0){
302  //Timeout
303  return 0;
304  }
305  else{
306  for(unsigned int i=0; i<receptor_list.size(); i++){
307  if(FD_ISSET((unsigned int)receptor_list[i].socketFileDescriptorReceptor,&readFileDescriptor)){
308 #ifdef UNIX
309  numbytes = recv(receptor_list[i].socketFileDescriptorReceptor, (char*)(void*)object, sizeOfObject, 0);
310 #else
311  numbytes = recv((unsigned int)receptor_list[i].socketFileDescriptorReceptor, (char*)(void*)object, (int)sizeOfObject, 0);
312 #endif
313  if(numbytes <= 0)
314  {
315  std::cout << "Disconnected : " << inet_ntoa(receptor_list[i].receptorAddress.sin_addr) << std::endl;
316  receptor_list.erase(receptor_list.begin()+(int)i);
317  return numbytes;
318  }
319 
320  break;
321  }
322  }
323  }
324 
325  return numbytes;
326 }
327 
349 template<typename T>
350 int vpNetwork::receiveFrom(T* object, const unsigned int &receptorEmitting, const unsigned int &sizeOfObject)
351 {
352  if(receptor_list.size() == 0 || receptorEmitting > (int)receptor_list.size()-1 )
353  {
354  if(verboseMode)
355  vpTRACE( "No receptor at the specified index" );
356  return -1;
357  }
358 
359  tv.tv_sec = tv_sec;
360  tv.tv_usec = tv_usec;
361 
362  FD_ZERO(&readFileDescriptor);
363 
364  socketMax = receptor_list[receptorEmitting].socketFileDescriptorReceptor;
365  FD_SET((unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor,&readFileDescriptor);
366 
367  int value = select((int)socketMax+1,&readFileDescriptor,NULL,NULL,&tv);
368  int numbytes = 0;
369 
370  if(value == -1){
371  if(verboseMode)
372  vpERROR_TRACE( "Select error" );
373  return -1;
374  }
375  else if(value == 0){
376  //timeout
377  return 0;
378  }
379  else{
380  if(FD_ISSET((unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor,&readFileDescriptor)){
381 #ifdef UNIX
382  numbytes = recv(receptor_list[receptorEmitting].socketFileDescriptorReceptor, (char*)(void*)object, sizeOfObject, 0);
383 #else
384  numbytes = recv((unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor, (char*)(void*)object, (int)sizeOfObject, 0);
385 #endif
386  if(numbytes <= 0)
387  {
388  std::cout << "Disconnected : " << inet_ntoa(receptor_list[receptorEmitting].receptorAddress.sin_addr) << std::endl;
389  receptor_list.erase(receptor_list.begin()+(int)receptorEmitting);
390  return numbytes;
391  }
392  }
393  }
394 
395  return numbytes;
396 }
397 
418 template<typename T>
419 int vpNetwork::send(T* object, const unsigned int &sizeOfObject)
420 {
421  if(receptor_list.size() == 0)
422  {
423  if(verboseMode)
424  vpTRACE( "No receptor !" );
425  return 0;
426  }
427 
428  int flags = 0;
429 #if ! defined(APPLE) && ! defined(WIN32)
430  flags = MSG_NOSIGNAL; // Only for Linux
431 #endif
432 
433 #ifdef UNIX
434  return sendto(receptor_list[0].socketFileDescriptorReceptor, (const char*)(void*)object, sizeOfObject,
435  flags, (sockaddr*) &receptor_list[0].receptorAddress,receptor_list[0].receptorAddressSize);
436 #else
437  return sendto(receptor_list[0].socketFileDescriptorReceptor, (const char*)(void*)object, (int)sizeOfObject,
438  flags, (sockaddr*) &receptor_list[0].receptorAddress,receptor_list[0].receptorAddressSize);
439 #endif
440 
441 }
442 
464 template<typename T>
465 int vpNetwork::sendTo(T* object, const unsigned int &dest, const unsigned int &sizeOfObject)
466 {
467  if(receptor_list.size() == 0 || dest > (int)receptor_list.size()-1 )
468  {
469  if(verboseMode)
470  vpTRACE( "No receptor at the specified index." );
471  return 0;
472  }
473 
474  int flags = 0;
475 #if ! defined(APPLE) && ! defined(WIN32)
476  flags = MSG_NOSIGNAL; // Only for Linux
477 #endif
478 
479 #ifdef UNIX
480  return sendto(receptor_list[dest].socketFileDescriptorReceptor, (const char*)(void*)object, sizeOfObject,
481  flags, (sockaddr*) &receptor_list[dest].receptorAddress,receptor_list[dest].receptorAddressSize);
482 #else
483  return sendto(receptor_list[dest].socketFileDescriptorReceptor, (const char*)(void*)object, (int)sizeOfObject,
484  flags, (sockaddr*) &receptor_list[dest].receptorAddress,receptor_list[dest].receptorAddressSize);
485 #endif
486 }
487 
488 #endif
unsigned int max_size_message
Definition: vpNetwork.h:124
std::string separator
Definition: vpNetwork.h:125
void setMaxSizeReceivedMessage(const unsigned int &s)
Definition: vpNetwork.h:216
std::string beginning
Definition: vpNetwork.h:126
This the request that will transit on the network.
Definition: vpRequest.h:134
std::vector< vpRequest * > request_list
Definition: vpNetwork.h:122
socklen_t receptorAddressSize
Definition: vpNetwork.h:90
std::string getRequestIdFromIndex(const int &ind)
Definition: vpNetwork.h:164
fd_set readFileDescriptor
Definition: vpNetwork.h:114
int socketFileDescriptorEmitter
Definition: vpNetwork.h:102
#define vpERROR_TRACE
Definition: vpDebug.h:379
#define vpTRACE
Definition: vpDebug.h:401
This class represents a Transmission Control Protocol (TCP) network.
Definition: vpNetwork.h:83
int socketFileDescriptorReceptor
Definition: vpNetwork.h:89
int send(T *object, const int unsigned &sizeOfObject=sizeof(T))
int socketMax
Definition: vpNetwork.h:116
unsigned int getMaxSizeReceivedMessage()
Definition: vpNetwork.h:177
std::string end
Definition: vpNetwork.h:127
vpEmitter emitter
Definition: vpNetwork.h:112
int receive(T *object, const unsigned int &sizeOfObject=sizeof(T))
Definition: vpNetwork.h:270
void setTimeoutUSec(const long &usec)
Definition: vpNetwork.h:236
long tv_sec
Definition: vpNetwork.h:133
bool verboseMode
Definition: vpNetwork.h:136
struct timeval tv
Definition: vpNetwork.h:132
std::vector< vpReceptor > receptor_list
Definition: vpNetwork.h:113
long tv_usec
Definition: vpNetwork.h:134
void setVerbose(const bool &mode)
Definition: vpNetwork.h:243
void setTimeoutSec(const long &sec)
Definition: vpNetwork.h:226
std::string currentMessageReceived
Definition: vpNetwork.h:130
int sendTo(T *object, const unsigned int &dest, const unsigned int &sizeOfObject=sizeof(T))
Definition: vpNetwork.h:465
std::string param_sep
Definition: vpNetwork.h:128
std::string receptorIP
Definition: vpNetwork.h:96
int receiveFrom(T *object, const unsigned int &receptorEmitting, const unsigned int &sizeOfObject=sizeof(T))
Definition: vpNetwork.h:350