replicode
network_interface.h
Go to the documentation of this file.
1 /*
2 * HUMANOBS - mBrane
3 *
4 * Eric Nivel
5 * Center for Analysis and Design of Intelligent Agents
6 * Reykjavik University, Menntavegur 1, 101 Reykjavik, Iceland
7 * http://cadia.ru.is
8 * Copyright(c)2012
9 *
10 * This software was developed by the above copyright holder as part of
11 * the HUMANOBS EU research project, in collaboration with the
12 * following parties:
13 *
14 * Autonomous Systems Laboratory
15 * Technical University of Madrid, Spain
16 * http://www.aslab.org/
17 *
18 * Communicative Machines
19 * Edinburgh, United Kingdom
20 * http://www.cmlabs.com/
21 *
22 * Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
23 * University of Lugano and SUPSI, Switzerland
24 * http://www.idsia.ch/
25 *
26 * Institute of Cognitive Sciences and Technologies
27 * Consiglio Nazionale delle Ricerche, Italy
28 * http://www.istc.cnr.it/
29 *
30 * Dipartimento di Ingegneria Informatica
31 * University of Palermo, Italy
32 * http://roboticslab.dinfo.unipa.it/index.php/Main/HomePage
33 *
34 *
35 * --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, is permitted provided that the following conditions
39 * are met:
40 *
41 * - Redistributions of source code must retain the above copyright
42 * and collaboration notice, this list of conditions and the
43 * following disclaimer.
44 *
45 * - Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following
47 * disclaimer in the documentation and/or other materials provided
48 * with the distribution.
49 *
50 * - Neither the name of its copyright holders nor the names of its
51 * contributors may be used to endorse or promote products
52 * derived from this software without specific prior written permission.
53 *
54 * - CADIA Clause: The license granted in and to the software under this
55 * agreement is a limited-use license. The software may not be used in
56 * furtherance of:
57 * (i) intentionally causing bodily injury or severe emotional distress
58 * to any person;
59 * (ii) invading the personal privacy or violating the human rights of
60 * any person; or
61 * (iii) committing or preparing for any act of war.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
64 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
65 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
66 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
67 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
68 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
69 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
70 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
71 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
72 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
73 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74 */
75 
76 #ifndef mBrane_sdk_network_interface_h
77 #define mBrane_sdk_network_interface_h
78 
79 #include <CoreLibrary/utils.h>
80 #include <CoreLibrary/xml_parser.h>
81 
82 #include "mdaemon_node.h"
83 
84 
85 namespace mBrane
86 {
87 namespace sdk
88 {
89 
90 class CommChannel;
91 class ConnectedCommChannel;
92 class mBrane_dll NetworkInterface
93 {
94 public:
95  typedef enum {
96  UDP = 0,
97  TCP = 1,
98  RM = 2,
99  IB = 3 // MPI2
100  } Protocol;
101 protected:
103  NetworkInterface(Protocol _protocol);
104 public:
105  typedef NetworkInterface *(*Load)(XMLNode &, mdaemon::Node *n); // function exported by the shared library
106  virtual ~NetworkInterface();
107  Protocol protocol() const;
108  virtual bool operator ==(NetworkInterface &i) = 0;
109  virtual bool operator !=(NetworkInterface &i) = 0;
110  virtual bool canBroadcast() = 0; // as opposed to connected mode
111  virtual uint16_t start() = 0; // initialize the network interface; returns 0 if successful
112  virtual uint16_t stop() = 0; // the network interface; returns 0 if successful
113  virtual uint16_t getIDSize() = 0; // node net ID to be broadcast
114  virtual void fillID(uint8_t *ID) = 0; // with relevant parameters (different from Node::_ID; ex: IP addr and port)
115  virtual uint16_t newChannel(uint8_t *ID, CommChannel **channel) = 0; // create a new channel (bcast ID=local node net ID, or connected ID=remote node net ID); returns 0 if successful
116  virtual uint16_t acceptConnection(ConnectedCommChannel **channel, int32_t timeout, bool &timedout) = 0; // listen to connect attempts and creates a new channel accordingly; returns 0 if successful
117 };
118 
119 class _Payload;
120 class mBrane_dll CommChannel
121 {
122 protected:
123  CommChannel(); // initialization to be performed in subclasses' constructors
124  int16_t _send(__Payload *c, uint8_t destinationNID);
125  int16_t _recv(__Payload **c, uint8_t sourceNID);
126  std::mutex commSendMutex;
127  std::mutex commRecvMutex;
131 public:
132  virtual ~CommChannel(); // shutdown to be performed in subclasses' destructors
133  virtual int16_t send(uint8_t *b, size_t s) = 0; // return 0 if successfull, error code (>0) otherwise
134  virtual int16_t recv(uint8_t *b, size_t s, bool peek = false) = 0;
135  virtual bool isConnected() = 0;
136  virtual bool disconnect() = 0;
137  int16_t bufferedSend(uint8_t *b, size_t s, bool sendNow = false);
138  int16_t send(_Payload *p, uint8_t destinationNID); // return 0 if successfull, error code (>0) otherwise. destinationNID used only for connected comm channels.
139  int16_t recv(_Payload **p, uint8_t sourceNID);
140 };
141 
142 class mBrane_dll ConnectedCommChannel:
143  public CommChannel
144 {
145 protected:
147 public:
148  virtual ~ConnectedCommChannel();
149 };
150 
151 class mBrane_dll BroadcastCommChannel:
152  public CommChannel
153 {
154 protected:
156 public:
157  virtual ~BroadcastCommChannel();
158 };
159 }
160 }
161 
162 
163 #endif
Definition: payload.h:95
Protocol _protocol
Definition: network_interface.h:102
std::mutex commRecvMutex
Definition: network_interface.h:127
std::mutex commSendMutex
Definition: network_interface.h:126
uint8_t * sendBuffer
Definition: network_interface.h:128
uint32_t sendBufferPos
Definition: network_interface.h:130
Definition: array.h:84
uint32_t sendBufferLen
Definition: network_interface.h:129
void stop()
Definition: Perf_modules.h:99
Definition: payload.h:122
Definition: network_interface.h:92
Definition: xml_parser.h:172
void start()
Definition: Perf_modules.h:96
Definition: mdaemon_node.h:100
Definition: network_interface.h:151
Definition: network_interface.h:120
Definition: network_interface.h:142
Protocol
Definition: network_interface.h:95