MIRA
AuthorityManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_AUTHORITYMANAGER_H_
48 #define _MIRA_AUTHORITYMANAGER_H_
49 
50 #include <map>
51 
52 #include <fw/Authority.h>
53 
54 namespace mira {
55 
57 
67 {
68 private:
69  struct Shared
70  {
71  Shared(AbstractAuthority* iAuthority, boost::shared_mutex& mutex) :
72  authority(iAuthority),
73  mLock(mutex) {}
74 
75  ~Shared() {
76  mLock.unlock();
77  }
78 
80  private:
81  boost::shared_lock<boost::shared_mutex> mLock;
82  };
83 
84 public:
85 
87  SharedAuthority(AbstractAuthority* iAuthority, boost::shared_mutex& mutex) :
88  mShared(new Shared(iAuthority, mutex)) {}
89 
91  operator AbstractAuthority*() { return mShared->authority; }
92 
94  AbstractAuthority* authority() { return mShared->authority; }
95 
96 private:
97 
98  boost::shared_ptr<Shared> mShared;
99 
100 };
101 
108 {
109  template<typename Reflector>
110  void reflect(Reflector& r)
111  {
112  r.member("Value", value, "The property value (if query succeeded)");
113  r.member("SerializedException", exception,
114  "Buffer containing the serialized exception (or empty)");
115  r.member("ErrorMessage", message,
116  "The error message from the exception (or empty)");
117  }
118 
119  json::Value value; // main: the json-serialized property value (if no exception)
120  Buffer<uint8> exception; // binary-serialized exception
121  std::string message; // exception message
122 };
123 
131 {
132 public:
133 
135 
136  ~AuthorityManager();
137 
139  template<typename Reflector>
140  void reflect(Reflector& r)
141  {
142  r.interface("IAuthorityManager");
143  r.method("hasAuthority", &AuthorityManager::hasAuthority, this,
144  "Returns if an authority with the given global id exists.",
145  "id", "authority id", "/MyAuthority");
146  r.method("isLocal", &AuthorityManager::isLocal, this,
147  "Returns true if authority with given id is a local one "
148  "(The instance lives in this framework/process).",
149  "id", "authority id", "/MyAuthority");
150  r.method("getAuthorities", &AuthorityManager::getAuthorities, this,
151  "Returns a list of existing authorities.");
152  }
153 
154 public:
155 
160  void addAuthority(AbstractAuthority* authority);
161 
165  void removeAuthority(AbstractAuthority* authority);
166 
170  void start(const std::string& id) const;
171 
175  void stop(const std::string& id) const;
176 
181  bool hasAuthority(const std::string& id) const;
182 
187  bool isRunning(const std::string& id) const;
188 
193  bool isLocal(const std::string& id) const;
194 
199  bool hasUnrecoverableFailure(const std::string& id) const;
200 
205  StatusManager::StatusMap getStatusMap(const std::string& id) const;
210  AuthorityDescription getDescription(const std::string& id) const;
211 
217  std::set<std::string> getPublishedChannels(const std::string& id) const;
222  std::set<std::string> getSubscribedChannels(const std::string& id) const;
223 
228  boost::shared_ptr<PropertyNode> getProperties(const std::string& id) const;
229 
234  std::set<std::string> getAuthorities() const;
235 
240  SharedAuthority getAuthority(const std::string& id);
241 
242 private:
243 
244  struct LockableAuthority
245  {
246  LockableAuthority(AbstractAuthority* a) : authority(a) {}
247 
248  AbstractAuthority* authority;
249  boost::shared_mutex mutex;
250  };
251  typedef boost::shared_ptr<LockableAuthority> LockableAuthorityPtr;
252  typedef std::map<std::string, LockableAuthorityPtr> AuthorityMap;
253 
255  AuthorityMap mAuthorities;
256 
258  mutable boost::mutex mAuthoritiesMutex;
259 
260 private:
261 
273  LockableAuthorityPtr findAuthority(const std::string& id) const;
274 
275 private:
276 
277  // authority of the AuthorityManager (must be last member!!)
278  Authority mAuthority;
279 };
280 
282 
283 }
284 
285 #endif
Authority class that is used to access the functionality of the framework.
bool hasAuthority(const std::string &id) const
Returns if an authority with the given global id exists.
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
void reflect(Reflector &r)
Definition: AuthorityManager.h:110
std::multimap< std::string, Status > StatusMap
Definition: Status.h:295
class MIRA_FRAMEWORK_EXPORT Authority
forward declaration
Definition: RemoteConnection.h:74
This is the descriptive part of an authority.
Definition: AuthorityDescription.h:61
json::Value value
Definition: AuthorityManager.h:119
#define MIRA_FRAMEWORK_EXPORT
Definition: FrameworkExports.h:61
void reflect(Reflector &r)
Reflect method for serialization.
Definition: AuthorityManager.h:140
Buffer< uint8 > exception
Definition: AuthorityManager.h:120
json_spirit::mValue Value
A value is an abstract description of data in JSON (underlying data can either be one of the JSON bas...
Definition: JSON.h:176
Central instance that stores all created Authorities.
Definition: AuthorityManager.h:130
A structure used for returning the complete result of a (remote) property query, including potential ...
Definition: AuthorityManager.h:107
SharedAuthority(AbstractAuthority *iAuthority, boost::shared_mutex &mutex)
There is no need to call this constructor manually.
Definition: AuthorityManager.h:87
AbstractAuthority * authority()
access the wrapped AbstractAuthority pointer
Definition: AuthorityManager.h:94
This is the abstract base class of local and remote authority classes.
Definition: AbstractAuthority.h:76
std::set< std::string > getAuthorities() const
Returns a list of existing authorities.
A wrapper providing locked/shared access to an AbstractAuthority pointer (used by AuthorityManager::g...
Definition: AuthorityManager.h:66
std::string message
Definition: AuthorityManager.h:121
bool isLocal(const std::string &id) const
Returns true if authority with given id is a local one (The instance lives in this framework/process)...