replicode
payload.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_payload_h
77 #define mBrane_sdk_payload_h
78 
79 #include "object.h"
80 #include "config.h"
81 
82 
83 namespace mBrane
84 {
85 namespace sdk
86 {
87 
88 namespace payloads
89 {
90 class _Message;
91 class _StreamData;
92 }
93 
94 // Base interface for payloads
95 class mBrane_dll __Payload:
96  public _Object
97 {
98 protected:
99  __Payload();
100 public:
101  virtual ~__Payload();
102  virtual uint16_t cid() const = 0;
103  virtual void init(); // called upon reception
104  virtual size_t size() const = 0; // returns the size of the whole instance; called upon sending
105  virtual uint16_t ptrCount() const; // number of pointers to payloads
106  virtual __Payload *getPtr(uint16_t i) const; // iterates the pointers to payloads
107  virtual void setPtr(uint16_t i, __Payload *p); // iterates the pointers to payloads
108 
109  virtual bool isShared() const; // called upon reception and transmission.
110  virtual bool isConstant() const; // called upon reception and transmission.
111 };
112 
113 // Convenience for writing getPtr and setPtr
114 #define PTR(Class,Member) (__Payload *)(((uint8_t *)this)+offsetof(Class,Member));
115 
116 namespace payloads
117 {
118 class _StreamData;
119 class _Message;
120 }
121 
122 class mBrane_dll _Payload:
123  public __Payload
124 {
125 public:
126  typedef enum {
127  CONTROL = 0,
128  DATA = 1,
129  STREAM = 2
130  } Category;
131 private:
134 protected:
135  uint64_t _node_recv_ts; // not transmitted
136  uint64_t _recv_ts; // not transmitted
137  uint64_t _metaData; // [oid(32)|cid(16)|reserved(14)|category(2)]; oid==0x00FFFFFF means non shared object, or shared object not sent yet.
138  uint64_t _node_send_ts;
139  uint64_t _send_ts;
140  _Payload();
141 public:
142  virtual ~_Payload();
143  Category category() const;
144  uint64_t &node_send_ts(); // send timestamp: time of emission from a node
145  uint64_t &node_recv_ts(); // recv timestamp: time of reception by a node
146  uint64_t &send_ts(); // send timestamp: time of emission from a module (< than node_send_ts)
147  uint64_t &recv_ts(); // recv timestamp: time of reception by a module (> than node_recv_ts)
148  // down_casting; return NULL by default
149  virtual payloads::_Message *as_Message();
150  virtual payloads::_StreamData *as_StreamData();
151  // caching.
152  void setOID(uint8_t NID); // for shared objects.
153  void setOID(); // for constant objects.
154  uint32_t getOID() const; // full OID: 32 bits.
155  uint32_t getID() const; // object ID: 24 bits.
156  uint8_t getNID() const; // 0x80 for constant objects.
157 };
158 
159 class mBrane_dll _RPayload:
160  public __Payload
161 {
162 protected:
163  uint64_t _metaData; // [reserved(32)|cid(16)|reserved(16)]
164  _RPayload();
165 public:
166  virtual ~_RPayload();
167 };
168 
169 // Base class for all payloads.
170 template<class P, class U, class M> class ___Payload: // P: payload class, U: final class, M: memory
171  public Object<M, P, U>
172 {
173 protected:
174  // convenience for accessing the Memory from subclasses unaware of M
175  static void *Alloc(uint32_t requested_size, uint32_t &normalized_size);
176  static void Dealloc(uint32_t requested_size, void *o);
177  ___Payload();
178 public:
179  static uint64_t _MetaData;
180  static void *New(uint32_t size); // to initialize the _vftable on recv(); size used for non-standard cases (like Storage<T>), i.e. when the actual size is not sizeof the class
181  static uint16_t CID();
182  static size_t Offset(); // to metadata from this
183  void *operator new(size_t s);
184  void operator delete(void *o);
185  virtual ~___Payload();
186  uint16_t cid() const;
187  virtual size_t size() const; // default; returns sizeof(U)
188 };
189 
190 template<class U, class M> class Payload:
191  public ___Payload<_Payload, U, M>
192 {
193 protected:
194  Payload();
195 public:
196  virtual ~Payload();
197 };
198 
199 // Template variant of the well-known DP. Adapts C to Payload<U,M>.
200 // Usage: class Some3rdPartyClass{ ... };
201 // class Some3rdPartyClassAdapted:public PayloadAdapter<Some3rdPartyClass,Some3rdPartyClassAdapted>{ ... };
202 template<class C, class U, class M> class PayloadAdapter:
203  public C,
204  public Payload<U, M>
205 {
206 protected:
207  PayloadAdapter();
208 public:
209  virtual ~PayloadAdapter();
210 };
211 
212 // Standard raw payload (no transmission information) to embed in any payload.
213 template<class U, class M> class RPayload:
214  public ___Payload<_RPayload, U, M>
215 {
216 protected:
217  RPayload();
218 public:
219  virtual ~RPayload();
220 };
221 
222 // Template variant of the well-known DP. Adapts C to RPayload<U,M>.
223 template<class C, class U, class M> class RPayloadAdapter:
224  public C,
225  public RPayload<U, M>
226 {
227 protected:
228  RPayloadAdapter();
229 public:
230  virtual ~RPayloadAdapter();
231 };
232 }
233 }
234 
235 
236 #include "payload.tpl.cpp"
237 
238 
239 #endif
uint64_t _send_ts
Definition: payload.h:139
Definition: message.h:131
virtual ~PayloadAdapter()
Definition: payload.tpl.cpp:168
uint64_t _metaData
Definition: payload.h:137
Definition: payload.h:95
static uint16_t CID()
Definition: payload.tpl.cpp:91
Definition: base.h:121
uint64_t _recv_ts
Definition: payload.h:136
static void * New(uint32_t size)
Definition: payload.tpl.cpp:86
Definition: payload.h:223
static uint32_t LastConstantOID
Definition: payload.h:132
Definition: payload.h:170
Definition: array.h:84
Definition: payload.h:202
Definition: payload.h:190
static void Dealloc(uint32_t requested_size, void *o)
Definition: payload.tpl.cpp:101
uint64_t _node_send_ts
Definition: payload.h:138
Category
Definition: payload.h:126
uint64_t _node_recv_ts
Definition: payload.h:135
RPayloadAdapter()
Definition: payload.tpl.cpp:174
Definition: payload.h:122
virtual ~___Payload()
Definition: payload.tpl.cpp:128
virtual ~RPayloadAdapter()
Definition: payload.tpl.cpp:178
virtual ~Payload()
Definition: payload.tpl.cpp:148
RPayload()
Definition: payload.tpl.cpp:154
Definition: object.h:101
uint16_t cid() const
Definition: payload.tpl.cpp:132
Definition: payload.h:213
Definition: payload.h:159
Payload()
Definition: payload.tpl.cpp:144
___Payload()
Definition: payload.tpl.cpp:123
virtual ~RPayload()
Definition: payload.tpl.cpp:158
Definition: message.h:106
virtual size_t size() const
Definition: payload.tpl.cpp:137
static uint64_t _MetaData
Definition: payload.h:179
PayloadAdapter()
Definition: payload.tpl.cpp:164
uint64_t _metaData
Definition: payload.h:163
static size_t Offset()
Definition: payload.tpl.cpp:118
static void * Alloc(uint32_t requested_size, uint32_t &normalized_size)
Definition: payload.tpl.cpp:96
static uint32_t LastSharedOID
Definition: payload.h:133