MIRA
Framework.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 
48 #ifndef _MIRA_FRAMEWORK_H_
49 #define _MIRA_FRAMEWORK_H_
50 
51 #include <serialization/adapters/std/list>
52 #include <serialization/adapters/std/map>
53 #include <serialization/adapters/boost/shared_ptr.hpp>
54 
55 #include <utils/ProgramOptions.h>
56 #include <platform/Types.h>
57 #include <rpc/RPCManager.h>
58 #include <thread/ScopedAccess.h>
59 #include <utils/Singleton.h>
60 #include <xml/XMLDomReflect.h>
61 #include <xml/XMLDomPreprocessor.h>
63 #include <thread/ThreadMonitor.h>
64 
65 #include <fw/Authority.h>
66 #include <fw/AuthorityManager.h>
67 #include <fw/Channel.h>
68 #include <fw/ChannelManager.h>
69 #include <fw/NameRegistry.h>
70 
71 #include <loader/Loader.h>
72 
74 #define MIRA_FW mira::Framework::instance()
75 
76 namespace mira {
77 
79 
80 // forward decls.
81 struct IntSignal;
82 class ErrorService;
83 class Framework;
84 class FrameworkTransformer;
85 class RemoteModule;
86 class UnitManager;
87 
89 namespace Private {
90 
92 
100 class FrameworkStartup
101 {
102 public:
103  FrameworkStartup(int argc, char** argv);
104  FrameworkStartup(const std::vector<std::string>& args);
105 
106 protected:
107 
108  void initialize();
109  ProgramOptions mOptions;
110  typedef std::vector<std::string> CommandLineStrings;
111  CommandLineStrings mCommandLineVariables;
112  CommandLineStrings mCommandLineKnownFrameworks;
113 };
114 
116 
117 class FrameworkAuthority : public Authority
118 {
119 public:
120  FrameworkAuthority(Framework* framework, const ResourceName& ns,
121  const std::string& name, Flags flags = Authority::NORMAL)
122  : Authority(ns, name, flags), mFramework(framework)
123  {}
124 
125 public:
126  boost::shared_ptr<PropertyNode> getProperties();
127 
128 protected:
129  Framework* mFramework;
130 };
131 
133 
134 }
136 
138 
150 class Framework : public Private::FrameworkStartup,
151  public ExplicitSingleton<Framework>
152 {
153 public:
154 
162  Framework(int argc, char** argv, bool startImmediately = false);
169  Framework(const std::vector<std::string>& args, bool startImmediately = false);
170 
171  virtual ~Framework();
172 
174  template<typename Reflector>
175  void reflect(Reflector& r)
176  {
177  serialization::VersionType version = r.version(2, this);
178 
179  if (version >= 2)
180  r.property("RemoteModule", mRemoteModule, "The remote module", boost::shared_ptr<RemoteModule>());
181 
182  r.method("getUptime", &Framework::getUptime, this, "Get the time since the framework was started");
183 
184  r.method("terminateProcess", &Framework::requestTermination, this,
185  "Terminate framework (and thus, the process). Use with care!",
186  "exitcode", "the exitcode returned by the process", 0u);
187 
188  r.interface("IVariableRegistry");
189  r.method("getVariables", &Framework::getVariables, this,
190  "Query variables");
191 
192  r.interface("ILibraryRegistry");
193  r.method("getLibraries",
195  "Query libraries");
196  r.method("getLoadedLibraries",
197  boost::function<LibraryRegistry::Register()>([](){
199  for (auto it = reg.begin(); it != reg.end(); ) {
200  if (it->second.mIsLoaded)
201  ++it;
202  else
203  it = reg.erase(it);
204  }
205  return reg;
206  }),
207  "Query only loaded libraries");
208 
209  r.interface("IConfigurationLoader");
210  r.method("loadConfig",
211  boost::function<void(const XMLDom&)>(
212  [&](const XMLDom& xml) { this->load(const_cast<XMLDom&>(xml)); } ),
213  "Load a configuration",
214  "xml", "XML document", XMLDom());
215 
216  r.interface("IThreadMonitor");
217  r.method("getThreadInformation",
218  boost::function<ThreadMonitor::BasicThreadInfoVector()>(
219  [] { return ThreadMonitor::instance().getBasicThreadInformation(); } ),
220  "Query thread information");
221 
222  }
223 
224 public:
225  boost::shared_ptr<PropertyNode> getProperties() {
226  return mProperties;
227  }
228 
229 public:
230 
237  void load(XMLDom& xml);
238 
245  void load(const std::string& configFile);
246 
251  void load();
252 
257  void start();
258 
262  bool isStarted() const
263  {
264  return mIsStarted;
265  }
266 
271  {
272  return Time::now() - mStartTime;
273  }
274 
280  virtual int exec();
281 
290  virtual void requestTermination(int exitcode=0);
291 
295  virtual bool isTerminationRequested() const;
296 
302  int getTerminationExitCode() const;
303 
312  int run();
313 
314 public:
315 
318 
321 
324 
327 
329  boost::shared_ptr<FrameworkTransformer> getTransformer() { return mTransformer; }
330 
333 
335  boost::shared_ptr<UnitManager> getUnitManager() { return mUnitManager; }
336 
339 
341  boost::shared_ptr<ErrorService> getErrorService() { return mErrorServiceModule; }
342 
344  boost::shared_ptr<RemoteModule> getRemoteModule() { return mRemoteModule; }
345 
347 
353  std::string getID() const { return mAuthority->getID(); }
354 
359  std::string getGlobalID() const { return mAuthority->getGlobalID(); }
360 
366 
368 
375  {
377  }
378 
379  bool isInExec() const { return mInExec; }
380 
381 protected:
382 
384 
385  void initialize();
386  void finalize();
387  void ctrlCHandler(const IntSignal& sig);
388 
389  static void errorHandler(const IntSignal& sig);
390  static bool enterLeaveErrorHandler(bool enter);
391 
392 protected:
393 
397  bool mInExec;
399 
400  // The authority of the framework (used for all services of the framework
401  // as well as providing a process thread for submodules)
402  boost::shared_ptr<Private::FrameworkAuthority> mAuthority;
403 
405  boost::shared_ptr<PropertyNode> mProperties;
406 
407  // our components:
409  boost::shared_ptr<RemoteModule> mRemoteModule;
410 
411  // DO NOT CHANGE THE ORDER of the following components, the order of
412  // destruction is important for producing correct error messages when
413  // destroying the framework and errors occur. If the order is not correct,
414  // it could result in misleading error messages.
415  RPCManager mRPCManager; // create RPCManager and ChannelManager first
416  ChannelManager mChannelManager; // to bring up the communication components
417  // that may be used by the components below.
419  boost::shared_ptr<FrameworkTransformer> mTransformer;
420 
421  boost::shared_ptr<UnitManager> mUnitManager;
422 
424 
425  boost::shared_ptr<ErrorService> mErrorServiceModule;
426  std::string mName;
427 
431 
433 };
434 
436 
437 }
438 
439 #include <fw/impl/Authority.hpp>
440 #include <fw/impl/ConcreteChannel.hpp>
441 
442 #endif
virtual int exec()
Executes the framework and blocks until the framework is terminated, e.g.
Authority class that is used to access the functionality of the framework.
XMLDom mConfigDom
Definition: Framework.h:429
void reflect(Reflector &r)
Reflect method for serialization.
Definition: Framework.h:175
A STL conform wrapper for libxml2 to read XML files as DOM.
Definition: XMLDom.h:74
std::string mName
Definition: Framework.h:426
boost::shared_ptr< ErrorService > getErrorService()
Returns the pointer to the persistent error service.
Definition: Framework.h:341
Manager class for all framework channels.
std::string getGlobalID() const
Return the fully qualified global id of this framework (includes namespace)
Definition: Framework.h:359
Normally authorities must have a unique name.
Definition: Authority.h:104
Manager class for all authorities in a framework.
ConfigurationLoader & getConfigurationLoader()
Returns the reference to the configuration file loader.
Definition: Framework.h:332
static Type & instance()
Returns a reference to the singleton instance.
Definition: Singleton.h:508
bool mInExec
Definition: Framework.h:397
boost::shared_ptr< FrameworkTransformer > getTransformer()
Returns the pointer to the transform framework.
Definition: Framework.h:329
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
A thread monitor, which collects information about the resources of all running threads of the curren...
std::vector< BasicThreadInfo > BasicThreadInfoVector
Vector of serializable thread info.
Definition: ThreadMonitor.h:236
friend class Private::FrameworkStartup
Definition: Framework.h:383
Stores aliases for namespaces and allows to resolve local names to global fully qualified names...
Definition: NameRegistry.h:66
Preprocessor for XML dom documents.
Grants thread-safe access to an object (the Protectee) that should be protected from concurrent acces...
Definition: ScopedAccess.h:119
boost::shared_ptr< ErrorService > mErrorServiceModule
Definition: Framework.h:425
int mTerminationExitCode
Definition: Framework.h:395
Framework(int argc, char **argv, bool startImmediately=false)
Constructor that takes command line arguments and a flag.
static void errorHandler(const IntSignal &sig)
ConfigurationLoader mConfigurationLoader
Definition: Framework.h:423
Structure used in signal handlers to pass the signal and callstack.
Definition: SignalHandler.h:67
static bool enterLeaveErrorHandler(bool enter)
uint8 VersionType
Definition: ReflectorInterface.h:72
std::string getID() const
Returns the ID of this framework.
Definition: Framework.h:353
bool mTerminationRequested
Definition: Framework.h:394
int run()
Calls the above load() and start() methods according to the command line parameters that were passed ...
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:418
RootPropertyNode mPropertiesRoot
Definition: Framework.h:404
Configuration loader for loading XML application configuration files.
bool isInExec() const
Definition: Framework.h:379
Duration getUptime() const
Return duration since started.
Definition: Framework.h:270
std::map< std::string, LibraryInfo > Register
Definition: LibraryRegistry.h:154
Registry for shared libraries.
AuthorityManager mAuthorityManager
Definition: Framework.h:418
bool isStarted() const
Return true if framework is started.
Definition: Framework.h:262
bool mRemoteDisabled
Definition: Framework.h:398
ProtecteeMixin< MetaTypeDatabase > ProtecteeDatabase
Definition: Framework.h:369
ChannelManager mChannelManager
Definition: Framework.h:416
Resolving names of channels and authorities in namespaces.
boost::shared_ptr< RemoteModule > mRemoteModule
Definition: Framework.h:409
bool mIsStarted
Definition: Framework.h:396
A singleton class that can be freely configured using policies that control the creation, instantiation, lifetime and thread-safety.
Use this class to represent time durations.
Definition: Time.h:106
void ctrlCHandler(const IntSignal &sig)
This class represents the core element of a modular application.
Definition: Framework.h:150
A special node that acts only as (empty) root node for a property tree.
Definition: PropertyNode.h:448
std::map< std::string, XMLVariableValue > XMLVariablesMap
Definition: XMLDomPreprocessor.h:92
ProtecteeDatabase mMetaDatabase
Definition: Framework.h:430
ScopedAccess< ProtecteeDatabase > getMetaDatabase()
Return the meta database that contains all known meta information in this framework.
Definition: Framework.h:374
RPCManager & getRPCManager()
Returns the reference to the manager singleton for registered RPC services.
Definition: Framework.h:338
virtual void requestTermination(int exitcode=0)
Requests the termination of the framework and hence the whole application.
boost::shared_ptr< UnitManager > mUnitManager
Definition: Framework.h:421
Provided for convenience.
Definition: Singleton.h:544
static Time now()
Returns the current utc based time.
Definition: Time.h:481
XMLVariablesMap variables
Definition: XMLDomPreprocessor.h:121
boost::shared_ptr< PropertyNode > getProperties()
Definition: Framework.h:225
boost::shared_ptr< FrameworkTransformer > mTransformer
Definition: Framework.h:419
boost::shared_ptr< Private::FrameworkAuthority > mAuthority
Definition: Framework.h:402
Framework channel classes.
NameRegistry & getNameRegistry()
Returns the reference to the name registry.
Definition: Framework.h:320
AuthorityManager & getAuthorityManager()
Returns the reference to the manager singleton for registered authorities.
Definition: Framework.h:323
virtual bool isTerminationRequested() const
Returns whether the termination of the framework is reqested.
ChannelManager & getChannelManager()
Returns the reference to the manager singleton for channels.
Definition: Framework.h:326
XMLDomPreprocessor & getXMLDomPreprocessor()
Definition: Framework.h:367
Central instance that stores all created Authorities.
Definition: AuthorityManager.h:130
Preprocesses XML documents and resolves all special tags like , <if>, <warning> and so on...
Definition: XMLDomPreprocessor.h:109
Time mStartTime
Definition: Framework.h:432
boost::shared_ptr< RemoteModule > getRemoteModule()
Returns the pointer to the remote module.
Definition: Framework.h:344
Class for accessing command line parameters.
Contains internal RPCManager class.
XMLVariablesMap & getVariables()
Returns the list of variables that are registered via command line or config file.
Definition: Framework.h:365
void start()
Starts the framework and it&#39;s remote component (if one was configured or created) and returns immedia...
NameRegistry mNames
Definition: Framework.h:408
boost::shared_ptr< UnitManager > getUnitManager()
Returns the reference to the unit manager.
Definition: Framework.h:335
void load()
Loads configuration files given on command line if any by calling above load(const std::string& confi...
An exception that occurred whenever a channel does not exist.
Definition: ChannelManager.h:75
Grants thread-safe access to an object that should be protected from concurrent access.
int getTerminationExitCode() const
Returns the exit code as specified by requestTermination(), or 0 if requestTermination() was not call...
XMLDomPreprocessor mXMLPreprocessor
Definition: Framework.h:428
boost::shared_ptr< PropertyNode > mProperties
Definition: Framework.h:405
This class is for internal use only.
Definition: RPCManager.h:96
Class for loading, parsing, modifying and interpreting application configuration files.
Definition: Loader.h:179
static Register getRegister()
Get access to library register.
Definition: LibraryRegistry.h:180
virtual ~Framework()
RPCManager mRPCManager
Definition: Framework.h:415
Contains non-intrusive reflects for XMLDom documents.