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/XMLDomModifier.h>
63 
64 #include <fw/Authority.h>
65 #include <fw/AuthorityManager.h>
66 #include <fw/Channel.h>
67 #include <fw/ChannelManager.h>
68 #include <fw/NameRegistry.h>
69 
70 #include <loader/Loader.h>
71 
73 #define MIRA_FW mira::Framework::instance()
74 
75 namespace mira {
76 
78 
79 // forward decls.
80 struct IntSignal;
81 class ErrorService;
82 class Framework;
83 class FrameworkTransformer;
84 class RemoteModule;
85 class UnitManager;
86 
88 namespace Private {
89 
91 
99 class MIRA_FRAMEWORK_EXPORT FrameworkStartup
100 {
101 public:
102  FrameworkStartup(int argc, char** argv);
103  FrameworkStartup(const std::vector<std::string>& args);
104 
105 protected:
106 
107  void initialize();
108  ProgramOptions mOptions;
109  typedef std::vector<std::string> CommandLineStrings;
110  CommandLineStrings mCommandLineVariables;
111  CommandLineStrings mCommandLineKnownFrameworks;
112 };
113 
115 
116 class FrameworkAuthority : public Authority
117 {
118 public:
119  FrameworkAuthority(Framework* framework, const ResourceName& ns,
120  const std::string& name, Flags flags = Authority::NORMAL)
121  : Authority(ns, name, flags), mFramework(framework)
122  {}
123 
124 public:
125  boost::shared_ptr<PropertyNode> getProperties();
126 
127 protected:
128  Framework* mFramework;
129 };
130 
132 
133 }
135 
137 
149 class MIRA_FRAMEWORK_EXPORT Framework : public Private::FrameworkStartup,
150  public ExplicitSingleton<Framework>
151 {
152 public:
153 
161  Framework(int argc, char** argv, bool startImmediately = false);
168  Framework(const std::vector<std::string>& args, bool startImmediately = false);
169 
170  virtual ~Framework();
171 
173  template<typename Reflector>
174  void reflect(Reflector& r)
175  {
176  serialization::VersionType version = r.version(2, this);
177 
178  if (version >= 2)
179  r.property("RemoteModule", mRemoteModule, "The remote module", boost::shared_ptr<RemoteModule>());
180 
181  r.method("getUptime", &Framework::getUptime, this, "Get the time since the framework was started");
182 
183  r.method("terminateProcess", &Framework::requestTermination, this,
184  "Terminate framework (and thus, the process). Use with care!",
185  "exitcode", "the exitcode returned by the process", 0u);
186 
187  r.interface("IVariableRegistry");
188  r.method("getVariables", &Framework::getVariables, this,
189  "Query variables");
190 
191  r.interface("ILibraryRegistry");
192  r.method("getLibraries",
194  "Query libraries");
195  r.method("getLoadedLibraries",
196  boost::function<LibraryRegistry::Register()>([](){
198  for (auto it = reg.begin(); it != reg.end(); ) {
199  if (it->second.mIsLoaded)
200  ++it;
201  else {
202  auto succ = it; ++succ;
203  reg.erase(it);
204  it = succ;
205  }
206  }
207  return reg;
208  }),
209  "Query only loaded libraries");
210 
211  r.interface("IConfigurationLoader");
212  r.method("loadConfig",
213  boost::function<void(const XMLDom&)>(
214  [&](const XMLDom& xml) { this->load(const_cast<XMLDom&>(xml)); } ),
215  "Load a configuration",
216  "xml", "XML document", XMLDom());
217 
218  }
219 
220 public:
221  boost::shared_ptr<PropertyNode> getProperties() {
222  return mProperties;
223  }
224 
225 public:
226 
233  void load(XMLDom& xml);
234 
241  void load(const std::string& configFile);
242 
247  void load();
248 
253  void start();
254 
258  bool isStarted() const
259  {
260  return mIsStarted;
261  }
262 
267  {
268  return Time::now() - mStartTime;
269  }
270 
276  virtual int exec();
277 
286  virtual void requestTermination(int exitcode=0);
287 
291  virtual bool isTerminationRequested() const;
292 
298  int getTerminationExitCode() const;
299 
308  int run();
309 
310 public:
311 
314 
316  NameRegistry& getNameRegistry() { return mNames; }
317 
319  AuthorityManager& getAuthorityManager() { return mAuthorityManager; }
320 
322  ChannelManager& getChannelManager() { return mChannelManager; }
323 
325  boost::shared_ptr<FrameworkTransformer> getTransformer() { return mTransformer; }
326 
328  ConfigurationLoader& getConfigurationLoader() { return mConfigurationLoader; }
329 
331  boost::shared_ptr<UnitManager> getUnitManager() { return mUnitManager; }
332 
334  RPCManager& getRPCManager() { return mRPCManager; }
335 
337  boost::shared_ptr<ErrorService> getErrorService() { return mErrorServiceModule; }
338 
340  boost::shared_ptr<RemoteModule> getRemoteModule() { return mRemoteModule; }
341 
343 
349  std::string getID() const { return mAuthority->getID(); }
350 
355  std::string getGlobalID() const { return mAuthority->getGlobalID(); }
356 
361  XMLVariablesMap& getVariables() { return mXMLPreprocessor.variables; }
362 
363  XMLDomPreprocessor& getXMLDomPreprocessor() { return mXMLPreprocessor; }
364 
371  {
372  return ScopedAccess<ProtecteeDatabase>(&mMetaDatabase);
373  }
374 
375  bool isInExec() const { return mInExec; }
376 
377 protected:
378 
379  friend class Private::FrameworkStartup;
380 
381  void initialize();
382  void finalize();
383  void ctrlCHandler(const IntSignal& sig);
384 
385  static void errorHandler(const IntSignal& sig);
386  static bool enterLeaveErrorHandler(bool enter);
387 
388 protected:
389 
393  bool mInExec;
395 
396  // The authority of the framework (used for all services of the framework
397  // as well as providing a process thread for submodules)
398  boost::shared_ptr<Private::FrameworkAuthority> mAuthority;
399 
401  boost::shared_ptr<PropertyNode> mProperties;
402 
403  // our components:
405  boost::shared_ptr<RemoteModule> mRemoteModule;
406 
407  // DO NOT CHANGE THE ORDER of the following components, the order of
408  // destruction is important for producing correct error messages when
409  // destroying the framework and errors occur. If the order is not correct,
410  // it could result in misleading error messages.
411  RPCManager mRPCManager; // create RPCManager and ChannelManager first
412  ChannelManager mChannelManager; // to bring up the communication components
413  // that may be used by the components below.
415  boost::shared_ptr<FrameworkTransformer> mTransformer;
416 
417  boost::shared_ptr<UnitManager> mUnitManager;
418 
420 
421  boost::shared_ptr<ErrorService> mErrorServiceModule;
422  std::string mName;
423 
427 
429 };
430 
432 
433 }
434 
435 #include <fw/impl/Authority.hpp>
436 #include <fw/impl/ConcreteChannel.hpp>
437 
438 #endif
Authority class that is used to access the functionality of the framework.
XMLDom mConfigDom
Definition: Framework.h:425
void reflect(Reflector &r)
Reflect method for serialization.
Definition: Framework.h:174
A STL conform wrapper for libxml2 to read XML files as DOM.
Definition: XMLDom.h:73
Typedefs for OS independent basic data types.
std::string mName
Definition: Framework.h:422
boost::shared_ptr< ErrorService > getErrorService()
Returns the pointer to the persistent error service.
Definition: Framework.h:337
Manager class for all framework channels.
std::string getGlobalID() const
Return the fully qualified global id of this framework (includes namespace)
Definition: Framework.h:355
Normally authorities must have a unique name.
Definition: Authority.h:103
Manager class for all authorities in a framework.
ConfigurationLoader & getConfigurationLoader()
Returns the reference to the configuration file loader.
Definition: Framework.h:328
bool mInExec
Definition: Framework.h:393
boost::shared_ptr< FrameworkTransformer > getTransformer()
Returns the pointer to the transform framework.
Definition: Framework.h:325
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Only for backward compatibility.
Stores aliases for namespaces and allows to resolve local names to global fully qualified names...
Definition: NameRegistry.h:66
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:421
int mTerminationExitCode
Definition: Framework.h:391
ConfigurationLoader mConfigurationLoader
Definition: Framework.h:419
Structure used in signal handlers to pass the signal and callstack.
Definition: SignalHandler.h:67
uint8 VersionType
Definition: ReflectorInterface.h:72
std::string getID() const
Returns the ID of this framework.
Definition: Framework.h:349
bool mTerminationRequested
Definition: Framework.h:390
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:421
RootPropertyNode mPropertiesRoot
Definition: Framework.h:400
class MIRA_FRAMEWORK_EXPORT Authority
forward declaration
Definition: RemoteConnection.h:74
Configuration loader for loading XML application configuration files.
bool isInExec() const
Definition: Framework.h:375
Duration getUptime() const
Return duration since started.
Definition: Framework.h:266
std::map< std::string, LibraryInfo > Register
Definition: LibraryRegistry.h:119
Registry for shared libraries.
AuthorityManager mAuthorityManager
Definition: Framework.h:414
bool isStarted() const
Return true if framework is started.
Definition: Framework.h:258
bool mRemoteDisabled
Definition: Framework.h:394
ProtecteeMixin< MetaTypeDatabase > ProtecteeDatabase
Definition: Framework.h:365
ChannelManager mChannelManager
Definition: Framework.h:412
Resolving names of channels and authorities in namespaces.
boost::shared_ptr< RemoteModule > mRemoteModule
Definition: Framework.h:405
bool mIsStarted
Definition: Framework.h:392
#define MIRA_FRAMEWORK_EXPORT
Definition: FrameworkExports.h:61
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:104
This class represents the core element of a modular application.
Definition: Framework.h:149
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:91
ProtecteeDatabase mMetaDatabase
Definition: Framework.h:426
ScopedAccess< ProtecteeDatabase > getMetaDatabase()
Return the meta database that contains all known meta information in this framework.
Definition: Framework.h:370
RPCManager & getRPCManager()
Returns the reference to the manager singleton for registered RPC services.
Definition: Framework.h:334
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:417
Provided for convenience.
Definition: Singleton.h:580
boost::shared_ptr< PropertyNode > getProperties()
Definition: Framework.h:221
boost::shared_ptr< FrameworkTransformer > mTransformer
Definition: Framework.h:415
boost::shared_ptr< Private::FrameworkAuthority > mAuthority
Definition: Framework.h:398
Framework channel classes.
NameRegistry & getNameRegistry()
Returns the reference to the name registry.
Definition: Framework.h:316
AuthorityManager & getAuthorityManager()
Returns the reference to the manager singleton for registered authorities.
Definition: Framework.h:319
ChannelManager & getChannelManager()
Returns the reference to the manager singleton for channels.
Definition: Framework.h:322
XMLDomPreprocessor & getXMLDomPreprocessor()
Definition: Framework.h:363
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:106
Time mStartTime
Definition: Framework.h:428
boost::shared_ptr< RemoteModule > getRemoteModule()
Returns the pointer to the remote module.
Definition: Framework.h:340
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:361
static Time now() static Time eternity()
Returns the current utc based time.
Definition: Time.h:484
NameRegistry mNames
Definition: Framework.h:404
boost::shared_ptr< UnitManager > getUnitManager()
Returns the reference to the unit manager.
Definition: Framework.h:331
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.
XMLDomPreprocessor mXMLPreprocessor
Definition: Framework.h:424
boost::shared_ptr< PropertyNode > mProperties
Definition: Framework.h:401
This class is for internal use only.
Definition: RPCManager.h:96
Class for loading, parsing, modifying and interpreting application configuration files.
Definition: Loader.h:181
static Register getRegister()
Get access to library register.
Definition: LibraryRegistry.h:145
RPCManager mRPCManager
Definition: Framework.h:411
Contains non-intrusive reflects for XMLDom documents.