replicode
message.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_message_h
77 #define mBrane_sdk_message_h
78 
79 #include "payload.h"
80 #include "memory.h"
81 #include "module_node.h"
82 
83 
84 // Root classes for defining applicative message classes
85 namespace mBrane
86 {
87 namespace sdk
88 {
89 
90 class CommChannel;
91 
92 namespace payloads
93 {
94 
95 template<class U> class ControlMessage:
96  public Payload<U, Memory>
97 {
98 protected:
100 public:
101  virtual ~ControlMessage();
102 };
103 
105 
106 class mBrane_dll _StreamData
107 {
108 protected:
109  uint16_t _sid; // stream identifer
110  _StreamData(uint16_t sid);
111 public:
112  virtual ~_StreamData();
113  uint16_t &sid();
114 };
115 
117 
118 template<class U, class M> class StreamData:
119  public Payload<U, M>,
120  public _StreamData
121 {
122 protected:
123  StreamData(uint16_t sid = 0);
124 public:
125  virtual ~StreamData();
127 };
128 
130 
131 class mBrane_dll _Message
132 {
133 protected:
137  _Message();
138 public:
139  virtual ~_Message();
140  uint16_t &senderModule_cid();
141  uint16_t &senderModule_id();
142  uint16_t &senderNodeID();
143 };
144 
146 
147 template<class U, class M> class Message:
148  public Payload<U, M>,
149  public _Message
150 {
151 protected:
152  Message();
153 public:
154  virtual ~Message();
155  _Message *as_Message();
156 };
157 
159 
160 // Shared objects are cached. The first 32 bits of their metadata is NID(7 bits)|ID(24 bits). The most significant bit of the OID is 0 (1 for constants).
161 // Shared objects are supposed not to change once created.
162 // Constant objects are duplicated on each node.
163 template<class U, class M> class SharedObject:
164  public Message<U, M>
165 {
166 protected:
167  void decRef(); // notifies the node when the ref count drops to 1: doomed for deletion.
168  SharedObject();
169 public:
170  virtual ~SharedObject();
171  bool isShared();
172  bool isConstant();
173 };
174 
176 
177 // CStorage stands for contiguous storage.
178 // S. superclass, T: type of the data to be stored contiguously.
179 // Typical use: data compacted dynamically, i.e. whose size is not an integral constant (e.g. that could not parameterize a template), e.g. archives, compressed images, etc.
180 // Usage sample:
181 // template<class U> class CoreData:public Message<U,Memory>{...};
182 // class ACStorage:public CStorage<CoreData<ACStorage,int32_t> >{...};
183 // ACStorage *acs=new(32) ACStorage(); // acs contains an array of 32 int32.
184 // Do not declare any data in subclasses of CStorage: such subclasses shall only contain logic, e.g. functions to exploit CoreData (if any) and the array of Ts.
185 template<class S, typename T> class CStorage:
186  public S
187 {
188 protected:
189  uint32_t _size; // of the whole instance (not normalized)
190  uint32_t _capacity; // max number of elements in the array
191  T *_data; // points to ((T *)(((uint8_t *)this)+offsetof(CStorage<S,T>,_data)+sizeof(T *)));
192  CStorage();
193 public:
194  static void *New(uint32_t size); // total size of the instance; called upon sending
195  void *operator new(size_t s); // unused; need for compilation of ___Payload<U,P,M>::New
196  void *operator new(size_t s, uint32_t capacity); // overrides Object<U,M>::new
197  void operator delete(void *o); // overrides Object<U,M>::delete
198  virtual ~CStorage();
199  size_t size() const;
200  uint32_t getCapacity() const;
201  T &data(uint32_t i);
202  T &data(uint32_t i) const;
203  T *data();
204 };
205 }
206 }
207 }
208 
209 
210 #include "message.tpl.cpp"
211 
212 
213 #endif
Definition: message.h:131
virtual ~CStorage()
Definition: message.tpl.cpp:199
virtual ~StreamData()
Definition: message.tpl.cpp:99
uint32_t getCapacity() const
Definition: message.tpl.cpp:208
uint32_t _capacity
Definition: message.h:190
Message()
Definition: message.tpl.cpp:110
T * _data
Definition: message.h:191
virtual ~ControlMessage()
Definition: message.tpl.cpp:88
Definition: array.h:84
uint16_t _senderModuleCID
Definition: message.h:134
bool isConstant()
Definition: message.tpl.cpp:159
size_t size() const
Definition: message.tpl.cpp:203
uint32_t _size
Definition: message.h:189
Definition: message.h:163
Definition: payload.h:190
virtual ~SharedObject()
Definition: message.tpl.cpp:131
Definition: message.h:118
Definition: message.h:147
uint16_t _sid
Definition: message.h:109
static void * New(uint32_t size)
Definition: message.tpl.cpp:166
_StreamData * as_StreamData()
Definition: message.tpl.cpp:103
Definition: message.h:185
uint16_t _senderModuleID
Definition: message.h:135
ControlMessage()
Definition: message.tpl.cpp:83
virtual ~Message()
Definition: message.tpl.cpp:115
_Message * as_Message()
Definition: message.tpl.cpp:119
uint16_t _senderNodeID
Definition: message.h:136
uint16_t & sid()
Definition: message.cpp:94
bool isShared()
Definition: message.tpl.cpp:154
StreamData(uint16_t sid=0)
Definition: message.tpl.cpp:94
Definition: message.h:106
T * data()
Definition: message.tpl.cpp:223
CStorage()
Definition: message.tpl.cpp:194
void decRef()
Definition: message.tpl.cpp:135
SharedObject()
Definition: message.tpl.cpp:126