AERA
tcp_io_device.h
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2025 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2025 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2025 Icelandic Institute for Intelligent Machines
9 //_/_/ Copyright (c) 2021 Leonard Eberding
10 //_/_/ http://www.iiim.is
11 //_/_/
12 //_/_/ Copyright (c) 2010-2012 Eric Nivel
13 //_/_/ Center for Analysis and Design of Intelligent Agents
14 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
15 //_/_/ http://cadia.ru.is
16 //_/_/
17 //_/_/ Part of this software was developed by Eric Nivel
18 //_/_/ in the HUMANOBS EU research project, which included
19 //_/_/ the following parties:
20 //_/_/
21 //_/_/ Autonomous Systems Laboratory
22 //_/_/ Technical University of Madrid, Spain
23 //_/_/ http://www.aslab.org/
24 //_/_/
25 //_/_/ Communicative Machines
26 //_/_/ Edinburgh, United Kingdom
27 //_/_/ http://www.cmlabs.com/
28 //_/_/
29 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
30 //_/_/ University of Lugano and SUPSI, Switzerland
31 //_/_/ http://www.idsia.ch/
32 //_/_/
33 //_/_/ Institute of Cognitive Sciences and Technologies
34 //_/_/ Consiglio Nazionale delle Ricerche, Italy
35 //_/_/ http://www.istc.cnr.it/
36 //_/_/
37 //_/_/ Dipartimento di Ingegneria Informatica
38 //_/_/ University of Palermo, Italy
39 //_/_/ http://diid.unipa.it/roboticslab/
40 //_/_/
41 //_/_/
42 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
43 //_/_/
44 //_/_/ Redistribution and use in source and binary forms, with or without
45 //_/_/ modification, is permitted provided that the following conditions
46 //_/_/ are met:
47 //_/_/ - Redistributions of source code must retain the above copyright
48 //_/_/ and collaboration notice, this list of conditions and the
49 //_/_/ following disclaimer.
50 //_/_/ - Redistributions in binary form must reproduce the above copyright
51 //_/_/ notice, this list of conditions and the following disclaimer
52 //_/_/ in the documentation and/or other materials provided with
53 //_/_/ the distribution.
54 //_/_/
55 //_/_/ - Neither the name of its copyright holders nor the names of its
56 //_/_/ contributors may be used to endorse or promote products
57 //_/_/ derived from this software without specific prior
58 //_/_/ written permission.
59 //_/_/
60 //_/_/ - CADIA Clause: The license granted in and to the software
61 //_/_/ under this agreement is a limited-use license.
62 //_/_/ The software may not be used in furtherance of:
63 //_/_/ (i) intentionally causing bodily injury or severe emotional
64 //_/_/ distress to any person;
65 //_/_/ (ii) invading the personal privacy or violating the human
66 //_/_/ rights of any person; or
67 //_/_/ (iii) committing or preparing for any act of war.
68 //_/_/
69 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
70 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
71 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
72 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
73 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
74 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
75 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
76 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
77 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
78 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
79 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
80 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
81 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
82 //_/_/ OF SUCH DAMAGE.
83 //_/_/
84 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
85 
86 #ifndef tcp_io_device_h
87 #define tcp_io_device_h
88 
89 #include "../r_exec/mem.h"
90 
91 #ifndef ENABLE_PROTOBUF
92 
93 namespace tcp_io_device {
94 
95  template<class O, class S> class TcpIoDevice :
96  public r_exec::MemExec<O, S> {
97 
98  public:
99 
100  TcpIoDevice();
101  TcpIoDevice(int number_of_servers, int number_of_clients, std::vector<std::pair<std::string, std::string> > server_configurations, std::vector<std::string> client_configurations);
102  ~TcpIoDevice();
103 
108  int initTCP();
109  };
110 }
111 
112 #else
113 
114 #pragma comment (lib, "Ws2_32.lib")
115 
116 #define PROTOBUF_USE_DLLS
117 
118 #include <map>
119 #include <string>
120 #include <stdlib.h>
121 #include <stdio.h>
122 #include <thread>
123 #include <bitset>
124 
125 #include "AERA_Protobuf/tcp_connection.h"
126 
127 namespace tcp_io_device {
128 
129  class MetaData;
130  class MsgData;
131 
132 
138  template<class O, class S> class TcpIoDevice :
139  public r_exec::MemExec<O, S> {
140 
141  public:
142 
143  TcpIoDevice(int number_of_servers, int number_of_clients, std::vector<std::pair<std::string, std::string> > server_configurations, std::vector<std::string> client_configurations);
144  ~TcpIoDevice();
145 
150  int initTCP();
151 
155  virtual bool load(const std::vector<r_code::Code*>* objects, uint32 stdin_oid, uint32 stdout_oid, uint32 self_oid);
156 
165  virtual r_code::Code* eject(r_code::Code* command);
166 
171  virtual void on_diagnostic_time_tick() { on_time_tick(); }
172 
173 
174  protected:
175  class _Thread : public Thread {
176  };
177 
178  int number_of_servers_;
179  int number_of_clients_;
180 
181  std::vector<std::pair<std::string, std::string> > server_configurations_;
182  std::vector<std::string> client_configurations_;
183 
184  std::vector<TCPConnection*> tcp_connections_;
185  std::shared_ptr<SafeQueue> receive_queue_;
186  std::shared_ptr<SafeQueue> send_queue_;
187 
188  std::map<int, std::string> id_mapping_;
189  std::map<std::string, r_code::Code*> entities_;
190  std::map<std::string, uint16> commands_;
191  std::map<std::string, r_code::Code*> objects_;
192 
193  std::map<std::string, MetaData> meta_data_map_;
194  std::map<std::string, MsgData> data_map_;
195 
196  Thread* timeTickThread_;
197  Timestamp lastInjectTime_;
198  Timestamp lastCommandTime_;
199 
200  bool started_;
201 
206  void startTimeTickThread();
207 
211  void on_time_tick();
212 
219  static thread_ret thread_function_call timeTickRun(void* args);
220 
228  tcp_io_device::MsgData constructMessageFromCommand(std::string cmd_identifier, std::string entity, r_code::Code* cmd);
229 
230  template<class T>
231  std::vector<T> getDataVec(r_code::Code* cmd, int start_index, int end_index, tcp_io_device::VariableDescription_DataType type);
232 
237  void handleMessage(std::unique_ptr<TCPMessage> msg);
238 
243  void handleDataMessage(std::unique_ptr<TCPMessage> data_msg);
244 
249  template<class V>
250  void injectDefault(r_code::Code* entity, r_code::Code* object, std::vector<V> vals, core::Timestamp time);
251 
252  void injectDefault(r_code::Code* entity, r_code::Code* object, std::vector<r_code::Code*> vals, core::Timestamp time);
253 
258  template<class V>
259  void injectSet(r_code::Code* entity, r_code::Code* object, std::vector<V> vals, core::Timestamp time);
260 
261  void injectSet(r_code::Code* entity, r_code::Code* object, std::vector<r_code::Code*> vals, core::Timestamp time);
262 
267  template<class V>
268  void injectOpCode(r_code::Code* entity, r_code::Code* object, std::vector<V> vals, core::Timestamp time, std::string opcode_handle);
269 
275  void handleSetupMessage(std::unique_ptr<TCPMessage> setup_msg);
276 
281  void sendDataMessage(tcp_io_device::MsgData msg_data);
282 
287  void sendMessage(std::unique_ptr<TCPMessage> msg);
288  };
289 } // namespace tcp_io_device
290 
291 #endif // !ENABLE_PROTOBUF
292 
293 #endif
tcp_io_device::TcpIoDevice::initTCP
int initTCP()
Definition: tcp_io_device.cpp:109
tcp_io_device::TcpIoDevice
Definition: tcp_io_device.h:96
r_code::Code
Definition: r_code/object.h:224
tcp_io_device::MsgData
Definition: AERA/IODevices/TCP/AERA_Protobuf/utils.h:225
r_exec::MemExec
Definition: mem.h:979