replicode
library.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 _library_h_
77 #define _library_h_
78 
79 #include "module_register.h"
80 #include "class_register.h"
81 #include "module_node.h"
82 #include "module.h"
83 
84 
85 #define MBRANE_MESSAGE_CLASSES "mBrane_message_classes.h"
86 
87 // C##_CID needed for use in switches (instead of the non constant expression user_class::CID())
88 // C##_metaData forces the intialization of C::_MetaData
89 #define MBRANE_MESSAGE_CLASS(C) static const uint16_t C##_CID=(uint16)__COUNTER__;static const uint64_t C##_metaData=ClassRegister::Load<C>(C##_CID);
90 #define MBRANE_STREAM_DATA_CLASS(C) static const uint16_t C##_CID=(uint16)__COUNTER__;static const uint64_t C##_metaData=ClassRegister::Load<C>(C##_CID);
91 #include LIBRARY_CLASSES
92 
93 #define CLASS_ID(C) C##_CID
94 
95 
96 template<class U> class LibraryModule:
97  public Object<Memory, module::_Module, U>
98 {
99 protected:
100  static const uint16_t _CID;
101  LibraryModule(bool canMigrate = true): Object<Memory, module::_Module, U>()
102  {
103  this->_canMigrate = canMigrate;
104  }
105 public:
106  static const uint16_t CID()
107  {
108  return _CID;
109  }
110  static module::_Module *New()
111  {
112  return new U();
113  }
114  virtual ~LibraryModule() {}
115  void _start()
116  {
117  this->_ready = true;
118  module::Node::Get()->trace(module::Node::EXECUTION) << "> Info: Module " << this->_cid << "|" << this->_id << " started" << std::endl;
119  ((U *)this)->start();
120  }
121  void _stop()
122  {
123  ((U *)this)->stop();
124  this->_ready = false;
125  module::Node::Get()->trace(module::Node::EXECUTION) << "> Info: Module " << this->_cid << "|" << this->_id << " stopped" << std::endl;
126  }
127  void notify(_Payload *p)
128  {
129  switch (p->cid()) {
130 #undef MBRANE_MESSAGE_CLASS
131 #undef MBRANE_STREAM_DATA_CLASS
132 #define MBRANE_MESSAGE_CLASS(C) case C##_CID: ((U *)this)->react((C *)p); return;
133 #define MBRANE_STREAM_DATA_CLASS(C)
134 #include "classes.h"
135 
136  default:
137  return;
138  }
139  }
140  void notify(uint16_t sid, _Payload *p)
141  {
142  switch (p->cid()) {
143 #undef MBRANE_MESSAGE_CLASS
144 #undef MBRANE_STREAM_DATA_CLASS
145 #define MBRANE_MESSAGE_CLASS(C)
146 #define MBRANE_STREAM_DATA_CLASS(C) case CLASS_ID(C): ((U *)this)->react(sid,(C *)p); return;
147 #include "classes.h"
148 
149  default:
150  return;
151  }
152  }
153  module::_Module::Decision dispatch(_Payload *p)
154  {
155  switch (p->cid()) {
156 #undef MBRANE_MESSAGE_CLASS
157 #undef MBRANE_STREAM_DATA_CLASS
158 #define MBRANE_MESSAGE_CLASS(C) case C##_CID: return ((U *)this)->decide((C *)p);
159 #define MBRANE_STREAM_DATA_CLASS(C) case C##_CID: return ((U *)this)->decide((C *)p);
160 #include "classes.h"
161 
162  default:
163  return module::_Module::DISCARD;
164  }
165  }
166 };
167 template<class U> const uint16_t Module<U>::_CID = ModuleRegister::Load(New, U::ClassName);
168 
169 
170 // for retrieving CIDs from names (in specs)
171 #undef MBRANE_MESSAGE_CLASS
172 #undef MBRANE_STREAM_DATA_CLASS
173 #define MBRANE_MESSAGE_CLASS(C) static const uint16_t C##_name=ClassRegister::Load(#C);
174 #define MBRANE_STREAM_DATA_CLASS(C) static const uint16_t C##_name=ClassRegister::Load(#C);
175 #include MBRANE_MESSAGE_CLASSES
176 #include LIBRARY_CLASSES
177 
178 
179 #define MODULE_CLASS_BEGIN(C,S) \
180 class C: \
181 public S{ \
182 public: \
183  static const char *ClassName; \
184  C():S(){} \
185  ~C(){} \
186  Decision decide(KillModule *p){ \
187  return WAIT; \
188  } \
189  void react(KillModule *p){ \
190  delete this; \
191  }
192 
193 #define MODULE_CLASS_END(C) \
194 }; \
195 const char *C::ClassName=#C; \
196 extern "C"{ \
197  mBrane::sdk::module::_Module * cdecl New##C(){ return new C(); } \
198  const uint16_t C##_CID(){ return C::CID(); } \
199 }
200 
201 
202 #endif
Definition: library.h:96
void _start()
Definition: library.h:115
uint16_t _cid
Definition: module.h:110
void notify(uint16_t sid, _Payload *p)
Definition: library.h:140
_Module()
Definition: module.cpp:88
uint16_t _id
Definition: module.h:111
bool _canMigrate
Definition: module.h:112
LibraryModule(bool canMigrate=true)
Definition: library.h:101
static module::_Module * New()
Definition: library.h:110
module::_Module::Decision dispatch(_Payload *p)
Definition: library.h:153
static const uint16_t _CID
Definition: library.h:100
void notify(_Payload *p)
Definition: library.h:127
Definition: application.h:98
bool canMigrate()
Definition: module.cpp:102
static const uint16_t CID()
Definition: library.h:106
virtual ~LibraryModule()
Definition: library.h:114
NetworkInterface * Load(XMLNode &n, mdaemon::Node *node)
Definition: tcp.cpp:83
void _stop()
Definition: library.h:121
bool _ready
Definition: module.h:114