iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-ResultsSyntax.C
Go to the documentation of this file.
1 /*! @file
2  @author Unknown
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
35 #include <nrt/Core/Model/Manager.H>
36 
37 class MessageSend : public nrt::MessageBase
38 { public: template<class Archive> inline void serialize(Archive& ar) { } };
39 
40 class MessageReturn : public nrt::MessageBase
41 {
42  public:
43  MessageReturn() {}
44  MessageReturn(std::string const & _id) : id(_id) { }
45  std::string id;
46  template<class Archive> inline void serialize(Archive& ar) { ar( id ); }
47 };
48 
49 
50 
51 
52 NRT_DECLARE_MESSAGEPOSTER_PORT(TheRequest, MessageSend, MessageReturn, "Post/Recieve Request");
53 NRT_DECLARE_MESSAGEPOSTER_PORT(AsyncPost, nrt::Message<std::string>, void, "An Async Message");
54 
55 class SendingModule : public nrt::Module,
56  public nrt::MessagePoster<TheRequest, AsyncPost>
57 {
58  public:
59  using nrt::Module::Module;
60 
61  void doPost()
62  {
63 
64  {
65  std::unique_ptr<nrt::Message<std::string>> asyncmsg(new nrt::Message<std::string>("hello world!"));
66  AsyncPost::post(asyncmsg);
67 
68  // #0 The following is the most explicit method which allows for maximum flexibility in exception handling
69  std::unique_ptr<MessageSend> msg(new MessageSend);
70  TheRequest::PostResultsType results = TheRequest::post(msg);
71  TheRequest::PostResultsType::iterator it = results.begin();
72  for(; it != results.end(); ++it)
73  {
74  try
75  {
76  TheRequest::PostResultsType::ResultFuture res = *it;
77  NRT_INFO("Method 0. Got A Result From " << res.get()->id);
78  }
80  { NRT_INFO("Method 0. Caught Exception: " << e.what()); }
81  }
82  }
83 
84  {
85  // #1 The following is a convenient way to do #0 using a range-based-for loop
86  std::unique_ptr<MessageSend> msg(new MessageSend);
87  for (typename TheRequest::PostResultsType::ResultFuture res : TheRequest::post(msg))
88  {
89  try
90  {
91  NRT_INFO("Method 1. Got A Result From " << res.get()->id);
92  }
94  { NRT_INFO("Method 1. Caught Exception: " << e.what()); }
95  }
96  }
97 
98  {
99  std::unique_ptr<MessageSend> msg(new MessageSend);
100  // #2 A more concise, short-hand for #1. This method requires the user to memorize only the ::post() syntax, and to remember
101  // to call res.get(). Notice that the auto resolves to a MessagePosterResults, _not_ a shared_ptr.
102  for(auto res : TheRequest::post(msg))
103  {
104  try
105  { NRT_INFO("Method 2. Got A Result From " << res.get()->id); }
107  { NRT_INFO("Method 2. Caught Exception: " << e.what()); }
108  }
109  }
110 
111  {
112  std::unique_ptr<MessageSend> msg(new MessageSend);
113  // #3 The simplest method sacrifices fine-grained exception handling/concurrency control for simplicity and clarity. Notice that we're relying on
114  // the typecasting operator of MessagePosterResult::Result.
115  try
116  {
117  for(std::shared_ptr<MessageReturn const> retmsg : TheRequest::post(msg))
118  {
119  NRT_INFO("Method 3. Got A Result From " << retmsg->id);
120  }
121  }
123  { NRT_INFO("Method 3. Caught Exception: " << e.what()); }
124  }
125 
126  };
127 };
128 
129 NRT_DECLARE_MESSAGESUBSCRIBER_PORT(TheResponse, MessageSend, MessageReturn, "Post/Recieve Request");
130 NRT_DECLARE_MESSAGECHECKER_PORT(AsyncCheck, nrt::Message<std::string>, "An Async Message");
131 
132 class RespondingModule : public nrt::Module,
133  public nrt::MessageSubscriber<TheResponse>,
134  public nrt::MessageChecker<AsyncCheck>
135 {
136  public:
137  RespondingModule(std::string const & id) : nrt::Module("Responder" + id) { }
138 
139  std::unique_ptr<MessageReturn> onMessage(TheResponse::InPtr m)
140  {
141  NRT_INFO(meta().instanceName << " recieved Request... Checking for a message");
142 
143  for(std::shared_ptr<nrt::Message<std::string> const> checkmsg : AsyncCheck::check(nrt::MessageCheckerPolicy::Any))
144  {
145  NRT_INFO("Got A Check'd Message: " << checkmsg->value());
146  }
147 
148  usleep(100000);
149  if (meta().instanceName == "Responder3")
150  throw nrt::exception::ModuleException(this, "", meta().instanceName + " doesn't like this request");
151  return std::unique_ptr<MessageReturn>(new MessageReturn(meta().instanceName));
152  }
153 };
154 
155 int main(int argc, char const* argv[])
156 {
157  // Get a handle onto our Blackboard:
159 
160  // Connect command-line args to Blackboard:
161  bb.setCommandLineArgs(argc, argv);
162 
163  auto s = bb.addModule<SendingModule>("sender");
164  s->TheRequest::setTopic("request");
165  s->AsyncPost::setTopic("async");
166 
167  auto r1 = bb.addModule<RespondingModule>("1");
168  r1->TheResponse::setTopicFilter("request");
169  r1->AsyncCheck::setTopicFilter("async");
170 
171  auto r2 = bb.addModule<RespondingModule>("2");
172  r2->TheResponse::setTopicFilter("request");
173  r2->AsyncCheck::setTopicFilter("async");
174 
175  auto r3 = bb.addModule<RespondingModule>("3");
176  r3->TheResponse::setTopicFilter("request");
177  r3->AsyncCheck::setTopicFilter("async");
178 
179  auto r4 = bb.addModule<RespondingModule>("4");
180  r4->TheResponse::setTopicFilter("request");
181  r4->AsyncCheck::setTopicFilter("async");
182 
183  bb.launch();
184 
185  NRT_DEBUG("Starting Post Sequences");
186  s->doPost();
187  NRT_DEBUG("Finished Post Sequences");
188  return 0;
189 }
190