replicode
application.h
Go to the documentation of this file.
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ HUMANOBS - mBrane
4 //_/_/
5 //_/_/ Eric Nivel
6 //_/_/ Center for Analysis and Design of Intelligent Agents
7 //_/_/ Reykjavik University, Menntavegur 1, 101 Reykjavik, Iceland
8 //_/_/ http://cadia.ru.is
9 //_/_/ Copyright(c)2012
10 //_/_/
11 //_/_/ This software was developed by the above copyright holder as part of
12 //_/_/ the HUMANOBS EU research project, in collaboration with the
13 //_/_/ following parties:
14 //_/_/
15 //_/_/ Autonomous Systems Laboratory
16 //_/_/ Technical University of Madrid, Spain
17 //_/_/ http://www.aslab.org/
18 //_/_/
19 //_/_/ Communicative Machines
20 //_/_/ Edinburgh, United Kingdom
21 //_/_/ http://www.cmlabs.com/
22 //_/_/
23 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
24 //_/_/ University of Lugano and SUPSI, Switzerland
25 //_/_/ http://www.idsia.ch/
26 //_/_/
27 //_/_/ Institute of Cognitive Sciences and Technologies
28 //_/_/ Consiglio Nazionale delle Ricerche, Italy
29 //_/_/ http://www.istc.cnr.it/
30 //_/_/
31 //_/_/ Dipartimento di Ingegneria Informatica
32 //_/_/ University of Palermo, Italy
33 //_/_/ http://roboticslab.dinfo.unipa.it/index.php/Main/HomePage
34 //_/_/
35 //_/_/
36 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
37 //_/_/
38 //_/_/ Redistribution and use in source and binary forms, with or without
39 //_/_/ modification, is permitted provided that the following conditions
40 //_/_/ are met:
41 //_/_/
42 //_/_/ - Redistributions of source code must retain the above copyright
43 //_/_/ and collaboration notice, this list of conditions and the
44 //_/_/ following disclaimer.
45 //_/_/
46 //_/_/ - Redistributions in binary form must reproduce the above copyright
47 //_/_/ notice, this list of conditions and the following
48 //_/_/ disclaimer in the documentation and/or other materials provided
49 //_/_/ with the distribution.
50 //_/_/
51 //_/_/ - Neither the name of its copyright holders nor the names of its
52 //_/_/ contributors may be used to endorse or promote products
53 //_/_/ derived from this software without specific prior written permission.
54 //_/_/
55 //_/_/ - CADIA Clause: The license granted in and to the software under this
56 //_/_/ agreement is a limited-use license. The software may not be used in
57 //_/_/ furtherance of:
58 //_/_/ (i) intentionally causing bodily injury or severe emotional distress
59 //_/_/ to any person;
60 //_/_/ (ii) invading the personal privacy or violating the human rights of
61 //_/_/ any person; or
62 //_/_/ (iii) committing or preparing for any act of war.
63 //_/_/
64 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
65 //_/_/ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
66 //_/_/ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
67 //_/_/ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
68 //_/_/ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
69 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
70 //_/_/ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
71 //_/_/ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
72 //_/_/ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
73 //_/_/ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
74 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75 //_/_/
76 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
77 
78 #ifndef _application_h_
79 #define _application_h_
80 
81 #include "module_register.h"
82 #include "class_register.h"
83 #include "module_node.h"
84 #include "module.h"
85 
86 
87 #define MBRANE_MESSAGE_CLASSES "mBrane_message_classes.h"
88 
89 // C##_CID needed for use in switches (instead of the non constant expression user_class::CID())
90 // the initialization of C##_metaData triggers the intialization of C::_MetaData
91 #define MBRANE_MESSAGE_CLASS(C) static const uint16_t C##_CID=(uint16_t)__COUNTER__;static const uint64_t C##_metaData=ClassRegister::Load<C>(C##_CID);
92 #define MBRANE_STREAM_DATA_CLASS(C) static const uint16_t C##_CID=(uint16_t)__COUNTER__;static const uint64_t C##_metaData=ClassRegister::Load<C>(C##_CID);
93 #include APPLICATION_CLASSES
94 
95 #define CLASS_ID(C) C##_CID
96 
97 
98 template<class U> class Module:
99  public mBrane::sdk::Object<Memory, module::_Module, U>
100 {
101 protected:
102  static const uint16_t _CID;
103  Module(bool canMigrate = true): Object<Memory, module::_Module, U>()
104  {
105  this->_canMigrate = canMigrate;
106  }
107 public:
108  static const uint16_t CID()
109  {
110  return _CID;
111  }
112  static module::_Module *New()
113  {
114  return new U();
115  }
116  virtual ~Module() {}
117  void _start()
118  {
119  this->_ready = true;
120  ((U *)this)->start();
121  module::Node::Get()->trace(module::Node::EXECUTION) << "> Info: Module " << this->_cid << "|" << this->_id << " started" << std::endl;
122  }
123  void _stop()
124  {
125  ((U *)this)->stop();
126  this->_ready = false;
127  module::Node::Get()->trace(module::Node::EXECUTION) << "> Info: Module " << this->_cid << "|" << this->_id << " stopped" << std::endl;
128  }
129  void notify(_Payload *p)
130  {
131  switch (p->cid()) {
132 #undef MBRANE_MESSAGE_CLASS
133 #undef MBRANE_STREAM_DATA_CLASS
134 #define MBRANE_MESSAGE_CLASS(C) case CLASS_ID(C): ((U *)this)->react((C *)p); return;
135 #define MBRANE_STREAM_DATA_CLASS(C)
136 #include "classes.h"
137 
138  default:
139  return;
140  }
141  }
142  void notify(uint16_t sid, _Payload *p)
143  {
144  switch (p->cid()) {
145 #undef MBRANE_MESSAGE_CLASS
146 #undef MBRANE_STREAM_DATA_CLASS
147 #define MBRANE_MESSAGE_CLASS(C)
148 #define MBRANE_STREAM_DATA_CLASS(C) case CLASS_ID(C): ((U *)this)->react(sid,(C *)p); return;
149 #include "classes.h"
150 
151  default:
152  return;
153  }
154  }
155  module::_Module::Decision dispatch(_Payload *p)
156  {
157  switch (p->cid()) {
158 #undef MBRANE_MESSAGE_CLASS
159 #undef MBRANE_STREAM_DATA_CLASS
160 #define MBRANE_MESSAGE_CLASS(C) case CLASS_ID(C): return ((U *)this)->decide((C *)p);
161 #define MBRANE_STREAM_DATA_CLASS(C) case CLASS_ID(C): return ((U *)this)->decide((C *)p);
162 #include "classes.h"
163 
164  default:
165  return module::_Module::DISCARD;
166  }
167  }
168 };
169 template<class U> const uint16_t Module<U>::_CID = ModuleRegister::Load(New, U::ClassName);
170 
171 // to use in user module cpp files; forces the intitialization of Module<U>::_CID
172 #define LOAD_MODULE(C) static uint16_t cid_##C=C::CID();
173 
174 // force _MetaData initilization in user mBrane_dll.
175 #undef MBRANE_MESSAGE_CLASS
176 #define MBRANE_MESSAGE_CLASS(C) static const uint64_t C##_force_init=C::_MetaData;
177 #include APPLICATION_CLASSES
178 
179 // for retrieving CIDs from names (in specs)
180 #undef MBRANE_MESSAGE_CLASS
181 #undef MBRANE_STREAM_DATA_CLASS
182 #define MBRANE_MESSAGE_CLASS(C) static const uint16_t C##_name=ClassRegister::Load(#C);
183 #define MBRANE_STREAM_DATA_CLASS(C) static const uint16_t C##_name=ClassRegister::Load(#C);
184 #ifndef LIBRARY_CLASSES
185 #include MBRANE_MESSAGE_CLASSES
186 #endif
187 #include APPLICATION_CLASSES
188 
189 
190 #define MODULE_CLASS_BEGIN(C,S) \
191 class C: \
192 public S{ \
193 public: \
194  static const char *ClassName; \
195  C():S(){} \
196  ~C(){} \
197  Decision decide(KillModule *p){ \
198  return WAIT; \
199  } \
200  void react(KillModule *p){ \
201  delete this; \
202  }
203 
204 #define MODULE_CLASS_END(C) \
205 }; \
206 const char *C::ClassName=#C; \
207 extern "C"{ \
208  mBrane::sdk::module::_Module * cdecl New##C(){ return new C(); } \
209  const uint16_t C##_CID(){ return C::CID(); } \
210 }
211 
212 
213 #endif
static const uint16_t _CID
Definition: application.h:102
uint16_t _cid
Definition: module.h:110
module::_Module::Decision dispatch(_Payload *p)
Definition: application.h:155
void notify(_Payload *p)
Definition: application.h:129
_Module()
Definition: module.cpp:88
uint16_t _id
Definition: module.h:111
static module::_Module * New()
Definition: application.h:112
bool _canMigrate
Definition: module.h:112
void notify(uint16_t sid, _Payload *p)
Definition: application.h:142
Definition: application.h:98
Module(bool canMigrate=true)
Definition: application.h:103
void _stop()
Definition: application.h:123
bool canMigrate()
Definition: module.cpp:102
void _start()
Definition: application.h:117
static const uint16_t CID()
Definition: application.h:108
virtual ~Module()
Definition: application.h:116
Definition: object.h:101
NetworkInterface * Load(XMLNode &n, mdaemon::Node *node)
Definition: tcp.cpp:83
bool _ready
Definition: module.h:114