replicode
module_descriptor.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_module_descriptor_h
77 #define mBrane_module_descriptor_h
78 
79 #include <Core/list.h>
80 #include <Core/module.h>
81 #include <CoreLibrary/xml_parser.h>
82 
83 #include "projection.h"
84 
85 
86 using namespace mBrane::sdk;
87 using namespace mBrane::sdk::module;
88 
89 namespace mBrane
90 {
91 
92 class NodeEntry;
94  public Object<Memory, _Object, ModuleEntry>
95 {
96 public:
100  ~ModuleEntry();
101 };
102 
104 {
105 private:
106  std::mutex mutex; // guards against Messaging::SendMessages.
107  uint32_t activationCount; // TODO: std::atomic
108 public:
109  static Array<Array<NodeEntry, 32>, 128> Main[2]; // 0: Data and Control: message class -> nodes -> modules, 1: Streams: stream id -> nodes -> modules
110  NodeEntry();
111  ~NodeEntry();
113  {
114  mutex.lock();
115  activationCount++;
116  mutex.unlock();
117  }
119  {
120  mutex.lock();
121  activationCount--;
122  mutex.unlock();
123  }
125  {
126  mutex.lock();
127  a = activationCount;
128  mutex.unlock();
129  }
131 };
132 
133 template<> class Projection<ModuleDescriptor>:
134  public _Projection<ModuleDescriptor, Projection<ModuleDescriptor>>
135 {
136 public:
137  Array<List<P<ModuleEntry>, 1024>::Iterator, 128> subscriptions[2]; // 0: indexed by message class ID (MCID), 1: indexed by stream ID (SID)
138  uint16_t subscriptionCount[2]; // idem
139  Projection(ModuleDescriptor *projected, Space *space);
140  ~Projection();
141  void activate();
142  void deactivate();
143  void setActivationLevel(float a);
144  void updateActivationCount(float t);
145  void addSubscription(uint8_t payloadType, uint16_t ID, List<P<ModuleEntry>, 1024>::Iterator i);
146  void removeSubscription(uint8_t payloadType, uint16_t ID);
147 };
148 
149 // Module proxy.
151  public Projectable<ModuleDescriptor>
152 {
153 private:
155  {
156  public:
159  };
161  {
162  public:
166  };
168  const char *name;
169 public:
171  static Array<Array<P<ModuleDescriptor>, 128>, 32> Config; // indexed by module descriptor class ID | ID; temporary: used at config time when node IDs are not known; tranfered in Node::start in Main.
172  static Array<Array<Array<P<ModuleDescriptor>, 128>, 32>, 8> Main; // indexed by host ID | module descriptor class ID | ID.
173  static ModuleDescriptor *New(XMLNode &n);
174  static void Init(uint8_t hostID); // resolves host name into ID, copies Config in Main, apply initial projections.
175  static uint16_t GetID(uint8_t hostID, uint16_t CID); // returns the first available slot in Main[hostID][CID].
176  static const char *GetName(uint16_t cid, uint16_t id); // returns the name of CID.
177  Host::host_name hostName; // resolved in hostID at Node::run() time
178  P<_Module> module; // NULL if remote
179  //_Module *module;
180  ModuleDescriptor(const char *hostName, _Module *m, uint16_t CID, const char *name); // invoked at Node::loadApplication() time.
181  ModuleDescriptor(uint8_t hostID, uint16_t CID, uint16_t ID); // invoked dynamically.
182  ~ModuleDescriptor();
183  void applyInitialProjections(uint8_t hostID);
184  const char *getName();
185  void _activate();
186  void _deactivate();
187  void addSubscription_message(uint8_t hostID, uint16_t spaceID, uint16_t MCID);
188  void addSubscription_stream(uint8_t hostID, uint16_t spaceID, uint16_t SID);
189  void removeSubscription_message(uint8_t hostID, uint16_t spaceID, uint16_t MCID);
190  void removeSubscription_stream(uint8_t hostID, uint16_t spaceID, uint16_t SID);
191  void removeSubscriptions_message(uint8_t hostID, uint16_t spaceID);
192  void removeSubscriptions_stream(uint8_t hostID, uint16_t spaceID);
193 };
194 }
195 
196 
197 #endif
void Init(const std::vector< int32_t > &numbers, const std::vector< std::string > &strings)
Definition: Perf.cpp:93
float activationLevel
Definition: module_descriptor.h:164
Definition: module_descriptor.h:93
void getActivation(uint32_t &a)
Definition: module_descriptor.h:124
uint16_t SID
Definition: module_descriptor.h:158
Host::host_name hostName
Definition: module_descriptor.h:177
void decActivation()
Definition: module_descriptor.h:118
Definition: array.h:84
Definition: projection.h:114
Definition: module_descriptor.h:103
const char * name
Definition: module_descriptor.h:168
Definition: list.h:94
Definition: base.h:94
ModuleDescriptor * descriptor
Definition: module_descriptor.h:98
Definition: module_descriptor.h:150
uint32_t activationCount
Definition: module_descriptor.h:107
Definition: module.cpp:85
Definition: array.h:86
NodeEntry * node
Definition: module_descriptor.h:97
static Array< Array< P< ModuleDescriptor >, 128 >, 32 > Config
Definition: module_descriptor.h:171
Definition: xml_parser.h:172
Array< _Projection, 32 > initialProjections
Definition: module_descriptor.h:167
List< P< ModuleEntry >, 1024 > modules
Definition: module_descriptor.h:130
void incActivation()
Definition: module_descriptor.h:112
Definition: object.h:101
static Array< Array< Array< P< ModuleDescriptor >, 128 >, 32 >, 8 > Main
Definition: module_descriptor.h:172
Definition: projection.h:91
P< _Module > module
Definition: module_descriptor.h:178
Definition: module_descriptor.h:160
Definition: projection.h:102
std::mutex mutex
Definition: module_descriptor.h:106
Definition: array.h:162
Definition: space.h:89
uint16_t spaceID
Definition: module_descriptor.h:163
Definition: module.h:99
uint16_t MCID
Definition: module_descriptor.h:157
Definition: module_descriptor.h:154
uint16_t CID
Definition: module_descriptor.h:170
Array< _Subscription, 8 > subscriptions
Definition: module_descriptor.h:165