iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BlackboardHelpers.H
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
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 */
34 
35 
36 #ifndef INCLUDE_NRT_CORE_BLACKBOARD_DETAILS_BLACKBOARDHELPERS_H
37 #define INCLUDE_NRT_CORE_BLACKBOARD_DETAILS_BLACKBOARDHELPERS_H
38 
43 #include <nrt/External/cereal/types/map.hpp>
44 #include <nrt/External/cereal/types/set.hpp>
45 #include <nrt/External/cereal/types/vector.hpp>
46 #include <nrt/External/cereal/types/string.hpp>
47 #include <nrt/External/cereal/types/utility.hpp>
48 #include <set>
49 #include <deque>
50 #include <iosfwd>
51 
52 namespace nrt
53 {
54  class Blackboard;
55 
56  // ######################################################################
57  //! Type for Blackboard unique ID data
58  /*! This used internally by Blackboard ONLY to keep track of blackboards across distributed systems. Users should not
59  be concerned with this. Because Blackboard is a Singleton, we will initialize our internals upon the first call to
60  str(). */
62  {
63  public:
64  //! As a string for printing debug messages and transmitting over networks
65  std::string const & str() const;
66 
67  private:
68  std::string strID; // the ID as a string
69  };
70 
71  // define some helper structs in our own reserved namespace
72  namespace blackboard
73  {
74  // ######################################################################
75  //! Data which we store about each message on our Blackboard
76  struct MessageData
77  {
78  //! Default constructor
80 
81  //! Constructor
82  MessageData(std::shared_ptr<nrt::MessageBase const> msg_) : msg(msg_) { } // checked is empty
83 
84  std::shared_ptr<nrt::MessageBase const> msg;
85  std::set<std::string /* module|checkinguid */> checked;
86  };
87 
88  // ######################################################################
89  //! Data which we store about each message received as part of a remote post transaction
91  {
92  //! Default constructor
94 
95  //! Construct from an nrt::Message
96  RemotePostMessageData(std::shared_ptr<nrt::MessageBase const> msg_, size_t const numcb_) :
97  msg(msg_), numcb(numcb_) { }
98 
99  //! Construct with no message (void case); then our msg pointer is invalid
100  RemotePostMessageData(size_t const numcb_) :
101  msg(), numcb(numcb_) { }
102 
103  //! The message
104  std::shared_ptr<nrt::MessageBase const> msg;
105 
106  //! The number of callbacks
107  size_t numcb;
108  };
109 
110  // ######################################################################
111  //! A callback wrapped for use to serve a remote post, plus the associated module mutex
113  {
114  //! Default constructor
116 
117  //! Constructor
118  CallbackForRemoteData(MessageSubscriberCallbackForRemote & cb_, boost::shared_mutex * mtx_) :
119  cb(cb_), mtx(mtx_) { }
120 
121  //! Callback
122  MessageSubscriberCallbackForRemote cb;
123 
124  //! Mutex
125  boost::shared_mutex *mtx;
126  };
127 
128  // ######################################################################
129  // Callbacks and remote check function
130 
131  //! Serve remote check function
132  typedef void (nrt::Blackboard::*serveRemoteCheckFunc)
133  (std::string const & modulecportname, nrt::MessageCheckerPolicy mcp, std::string & bytes);
134 
135  //! Serve remote post function
136  typedef void (nrt::Blackboard::*serveRemotePostFunc)
137  (std::string const & bytes, std::string const & modulepostype, std::string const & transactionkey,
138  size_t const numcallbacks);
139 
140  //! Serve remote callback function
141  typedef void(nrt::Blackboard::*serveRemoteCallbackFunc)
142  (std::string const & modulesubscritype, std::string const & transactionkey, std::string & retstr);
143 
144  // ######################################################################
145  // Summary info about our posters, subscribers and checkers, to send to blackboard master
146  struct BlackboardSummary
147  {
148  nrt::blackboard::BlackboardManagerSummary bbms; //!< Our BlackboardManager's summary
149 
150  std::map<std::string /* namespc */, std::map<std::string /* module */, ModuleSummary> > modules;
151 
152  // NOTE: only the master has those. Any content here for non-master blackboards will be ignored // FIXME
153  std::map<std::string /* namespc */, std::map<std::string /* connec */, ConnectorSummary> > pconnectors;
154  std::map<std::string /* namespc */, std::map<std::string /* connec */, ConnectorSummary> > cconnectors;
155  std::map<std::string /* namespc */, std::map<std::string /* connec */, ConnectorSummary> > sconnectors;
156 
157  std::map<std::string /* namespc */, MacroModuleSummary> namespaces; //!< all namespaces, including empty ones
158 
159  //! Serialization
160  template <class Archive> void serialize(Archive & ar)
161  { ar(bbms, modules, pconnectors, cconnectors, sconnectors, namespaces); }
162  };
163 
164  // ######################################################################
165  struct ConnectionSearchNode
166  {
167  enum LinkType { LinkPos, LinkSub, LinkChk };
168 
169  ConnectionSearchNode(LinkType const linktype_, std::string const & bbuid_, std::string const & conn_,
170  std::string const & topic_, nrt::blackboard::NamespaceSummary * ns_);
171  LinkType linktype;
172  std::string bbuid;
173  std::string conn;
174  std::string topic;
175 
177  };
178 
179  // Note: we use deque here instead of stack because we need to be able to iterate over the closed queue
180  typedef typename std::deque<std::shared_ptr<nrt::blackboard::ConnectionSearchNode> > ConnectionSearchQueue;
181 
182  // ######################################################################
183  //! Data about a module or macromodule (namespace) that the blackboard master keeps track of
184  struct GUIdata
185  {
186  //! Initialize
187  GUIdata(int x_ = 0, int y_ = 0);
188 
189  int x, y; //<! Coordinates of that module/macromodule in the GUI
190 
191  //! Serialization
192  template <class Archive> void serialize(Archive & ar)
193  { ar( x, y ); }
194  };
195 
196  //! Output stream operator overload (mostly for debug messages)
197  std::ostream & operator<<(std::ostream & os, nrt::blackboard::GUIdata const & gd);
198 
199  }; // namespace blackboard
200 
201 }; // namespace nrt
202 
203 #endif // INCLUDE_NRT_CORE_BLACKBOARD_DETAILS_BLACKBOARDHELPERS_H
204