iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BlackboardNetIce.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 
37 #ifndef INCLUDE_NRT_CORE_BLACKBOARD_NETWORK_ICE_BLACKBOARDNETICE_H
38 #define INCLUDE_NRT_CORE_BLACKBOARD_NETWORK_ICE_BLACKBOARDNETICE_H
39 
42 #include <Ice/Ice.h>
43 #include <boost/thread.hpp> // for shared_mutex, shared_lock and friends
44 #include <thread>
45 
46 namespace nrt
47 {
48  class BlackboardNetIce;
49 
50  namespace NetIce
51  {
52  // ######################################################################
53  // BlackboardMasterI
54  // ######################################################################
55  class BlackboardMasterI : public BlackboardMaster
56  {
57  public:
58  void registerClientInfo(nrt::NetIce::blackboardInfo const & info, Ice::Current const & c);
59 
60  void notifyAboutDeadBlackboard(std::string const & bbuid, Ice::Current const & c);
61 
62  //! Get the UID (as a string) of the Blackboard federation master
63  void masterbbuid(std::string & mbbuid, Ice::Current const & c);
64 
65  private:
66  void dispatchBlackboardMap();
67 
68  std::mutex itsMutex;
69  std::map<std::string /* bbuid */, nrt::NetIce::blackboardInfo> itsBlackboardMap;
70  };
71 
72  // ######################################################################
73  // BlackboardClientI
74  // ######################################################################
75  class BlackboardClientI : public BlackboardClient
76  {
77  public:
78  BlackboardClientI();
79 
80  void updateRegistration(blackboardSeq const & blackboards, Ice::Current const &);
81 
82  void remotePost(std::string const & bytes, std::string const & msgtype, std::string const & rmodulepportname,
83  std::string const & transactionkey, Ice::Long const numcallbacks, Ice::Current const &);
84 
85  void remoteCallback(std::string const & lmodulesportname, std::string const & transactionkey,
86  std::string & retstr, Ice::Current const &);
87 
88  void remoteCheck(std::string const & msgtype, std::string const & rmodulecportname, Ice::Byte cpolicy,
89  std::string & data, Ice::Current const &);
90 
91  void processNotifyParamChanged(std::string const & module, Ice::Byte state, std::string const & paramsummary,
92  Ice::Current const &);
93 
94  void processRemoteSetParam(std::string const & descriptor, std::string const & value,
95  Ice::Current const &);
96 
97  void processRemoteSetModuleParam(std::string const & module, std::string const & reativedescriptor,
98  std::string const & value, Ice::Current const &);
99 
100  void processRemoteCommand(std::string const & outcmd, std::string & retcmd, Ice::Current const &);
101 
102  private:
103  friend class nrt::BlackboardNetIce;
104 
105  std::mutex itsMutex;
106 
107  std::map<std::string /* rbbuid */,
108  std::function<void
109  (std::shared_ptr<std::string const> msgstr, std::string const msgtype,
110  std::string const rmodulepportname, std::string const transactionkey,
111  size_t const numcallbacks)>
112  >
113  itsRemotePostHandlerMappings;
114 
115  std::map<std::string /* rbbuid */,
116  std::function<std::shared_ptr<std::string const>
117  (std::string const lmodulesportname, std::string const transactionkey)>
118  >
119  itsCallbackMappings;
120 
121  std::map<std::string /* rbbuid */,
122  std::function<std::shared_ptr<std::string const>
123  (std::string const msgtype, std::string const rmodulecportname,
124  nrt::MessageCheckerPolicy const mcp)>
125  >
126  itsCheckCallMappings;
127 
128  std::map<std::string /* rbbuid */,
129  std::function<void(std::string const & module, nrt::ParameterState const state,
130  nrt::ParameterSummary const & ps)>
131  >
132  itsParamChangedListenerMappings;
133 
134  std::map<std::string /* bbuid */,
135  nrt::NetIce::BlackboardClientPrx
136  >
137  itsRemoteClientPrx;
138 
139  void remoteExecute(std::function<void()> func, std::string const & context,
140  nrt::NetIce::BlackboardMasterPrx masterPrx, std::string const & remoteBBuid);
141  void executeForRemote(std::function<void()> func, std::string const & context);
142 
143  };
144  } // nrt::NetIce
145 
146 
147  // ######################################################################
148  class BlackboardNetIce : public nrt::BlackboardNetHandler
149  {
150  public:
151 
152  //! Constructor
153  BlackboardNetIce();
154 
155  //! Start a blackboard master
156  /*! If a port == 0 is given, then try to find an open port and return it. */
157  int startMaster(int const port);
158 
159  //! Start a blackboard client
160  void startClient(std::string const & masterIP, int const masterPort);
161 
162  //! Stop a blackboard client
163  void stopClient();
164 
165  //! Let all blackboards on the network know about our posts/checks/subscriptions
166  void registerClientBlackboard(nrt::blackboard::BlackboardSummary const & bs);
167 
168  //! Get the UID (as a string) of the Blackboard federation master
169  std::string masterbbuid();
170 
171  //! Get a remote post handler
172  nrt::blackboard::MessageSubscriberRemotePostHandler
173  getRemotePostHandler(std::string const & bbuid, std::string const & msgtype, std::string const & modulepportname);
174 
175  //! Get a remote callback
176  nrt::blackboard::MessageSubscriberRemoteCallback
177  getRemoteCallback(std::string const & bbuid, std::string const & modulesportname);
178 
179  //! Get a remote check function
180  nrt::blackboard::MessageCheckerRemoteCall
181  getRemoteCheckCall(std::string const & bbuid, std::string const & msgtype, std::string const & modulecportname);
182 
183  //! Send a notification of local Parameter changed to a remote Blackboard
184  void sendNotifyParamChanged(std::string const & rbbuid, std::string const & module,
185  nrt::ParameterState const state, nrt::ParameterSummary const & ps);
186 
187  //! Set a parameter value on a remote blackboard
188  void setParam(std::string const & bbuid, std::string const & descriptor, std::string const & value);
189 
190  //! Set a parameter value on a remote blackboard
191  void setModuleParam(std::string const & bbuid, std::string const & module,
192  std::string const & relativedescriptor, std::string const & value);
193 
194  //! Run a command on a remote Blackboard and get results back
195  /*! The results are placed back in the args of the outgoing command, after deleting the outgoing args */
196  virtual void runRemoteCommand(std::string const & bbuid, nrt::blackboard::RemoteCommand & cmd);
197 
198  private:
199  Ice::CommunicatorPtr ic;
200  nrt::NetIce::BlackboardMasterPrx itsMasterPrx;
201  nrt::NetIce::BlackboardClientPrx itsClientPrx;
202  nrt::NetIce::BlackboardClientI *itsClient;
203 
204  boost::shared_mutex itsMutex;
205  };
206 }
207 
208 #endif // INCLUDE_NRT_CORE_BLACKBOARD_NETWORK_ICE_BLACKBOARDNETICE_H
209