replicode
pingpong_modules.h
Go to the documentation of this file.
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ HUMANOBS - mBrane - PingPong
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 pingpong_modules_h
79 #define pingpong_modules_h
80 
81 #include "pingpong.h"
82 #include "../Core/module_node.h"
83 
84 #define N module::Node
85 #define NODE module::Node::Get()
86 #define OUTPUT NODE->trace(N::APPLICATION)
87 
88 class pong;
90 void start()
91 {
92 }
93 void stop()
94 {
95 }
96 template<class T> Decision decide(T *p)
97 {
98  return WAIT;
99 }
100 template<class T> void react(T *p) // to messages
101 {
102  OUTPUT << "ping got a message..." << std::endl;
103 }
105 {
106  OUTPUT << "ping got SystemReady" << std::endl;
107  /* NODE->send(this,new Ball(0),N::LOCAL);
108  NODE->deleteModule(1,0);
109  NODE->send(this,new Ball(1),N::LOCAL);
110  NODE->newModule(this,Module<pong>::CID()); // module_id=0; ping does not catch notifications on new modules, so we have to know the new module's id, see below.
111  NODE->activateModule(this,Module<pong>::CID(),0,1,1.0); // the correct way to do that is to have it done in the new module: this solves the issue above, at the expense of removing the initial subscriptions in the configuration file.
112  NODE->subscribeMessage(this,Module<pong>::CID(),0,1,CLASS_ID(Ball));// idem.
113  NODE->activateSpace(this,0,1,0);
114  NODE->activateSpace(this,0,1,1);
115  NODE->send(this,new Ball(2),N::LOCAL);
116  NODE->newSpace(this); // space_id=3.
117  NODE->activateSpace(this,0,3,1);
118  NODE->setSpaceThreshold(this,3,0.5);
119  NODE->newModule(this,Module<pong>::CID()); // module_id=1.
120  NODE->send(this,new Ball(3),N::LOCAL);
121  NODE->subscribeMessage(this,Module<pong>::CID(),1,3,CLASS_ID(Ball));
122  NODE->unsubscribeMessage(this,Module<pong>::CID(),1,3,CLASS_ID(Ball));
123  NODE->subscribeMessage(this,Module<pong>::CID(),1,3,CLASS_ID(Ball));
124  NODE->activateModule(this,Module<pong>::CID(),1,3,1.0);
125  NODE->send(this,new Ball(4),N::LOCAL);
126  NODE->deleteSpace(3);
127  NODE->send(this,new Ball(5),N::LOCAL);
128  NODE->unsubscribeMessage(this,Module<pong>::CID(),0,1,CLASS_ID(Ball));
129  */
130  NODE->send(this, new Type2(), N::LOCAL);
131 }
132 void react(Ball *p)
133 {
134  OUTPUT << "ping got the ball " << p->id << std::endl;
135 }
136 void react(Type1 *p)
137 {
138  static uint32_t i = 0;
139  static uint64_t _begin = Time::Get();
140 
141  if (++i == 10) {
142  uint64_t delta = Time::Get() - _begin;
143  float t = ((float)delta) / 1000000; // convert from us to ms (1000) per message (1000)
144  std::cout << "------------ " << t << std::endl;
145  return;
146  }
147 
148  OUTPUT << "ping got Type1" << std::endl;
149  NODE->send(this, new Type2(), N::LOCAL);
150 }
151 void react(Type2 *p)
152 {
153  OUTPUT << "ping got Type2" << std::endl;
154 }
155 template<class T> void react(uint16_t sid, T *p) // to stream data
156 {
157  OUTPUT << "ping got data on stream " << sid << std::endl;
158 }
159 MODULE_CLASS_END(ping)
160 
161 MODULE_CLASS_BEGIN(pong, Module<pong>)
162 void start()
163 {
164 }
165 void stop()
166 {
167 }
168 template<class T> Decision decide(T *p)
169 {
170  return WAIT;
171 }
172 template<class T> void react(T *p) // to messages
173 {
174  OUTPUT << "pong " << _id << " got a message..." << std::endl;
175 }
176 void react(SystemReady *p)
177 {
178  OUTPUT << "pong got SystemReady" << std::endl;
179 }
180 void react(Ball *p)
181 {
182  OUTPUT << "pong " << _id << " got the ball " << p->id << std::endl;
183 }
184 void react(Type1 *p)
185 {
186  OUTPUT << "pong got Type1" << std::endl;
187 }
188 void react(Type2 *p)
189 {
190  OUTPUT << "pong got Type2" << std::endl;
191  NODE->send(this, new Type1(), N::LOCAL);
192 }
193 template<class T> void react(uint16_t sid, T *p) // to stream data
194 {
195  OUTPUT << "pong got data on stream " << sid << std::endl;
196 }
197 MODULE_CLASS_END(pong)
198 
199 
200 #endif /* pingpong_modules_h */
Definition: Perf_class.h:96
void start()
Definition: pingpong_modules.h:90
#define MODULE_CLASS_END(C)
Definition: application.h:204
int32_t id
Definition: Perf_class.h:99
Definition: control_messages.h:89
Definition: application.h:98
void stop()
Definition: pingpong_modules.h:93
Definition: Perf_class.h:111
#define NODE
Definition: pingpong_modules.h:85
#define MODULE_CLASS_BEGIN(C, S)
Definition: application.h:190
Decision decide(T *p)
Definition: pingpong_modules.h:96
Definition: Perf_class.h:110
void react(T *p)
Definition: pingpong_modules.h:100
#define OUTPUT
Definition: pingpong_modules.h:86