Application.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore::core
19 * @author Nils Adermann (naderman at naderman dot de)
20 * @author Kai Welke (welke at kit dot edu)
21 * @author Jan Issac (jan dot issac at gmail dot com)
22 * @date 2010
23 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
24 * GNU General Public License
25 */
26 
27 #pragma once
28 
29 #include "../ArmarXManager.h"
30 #include "../Component.h" // for Component
31 #include "../logging/Logging.h"
32 #include "../system/ArmarXDataPath.h"
33 #include "../system/ImportExport.h" // for ARMARXCORE_IMPORT_EXPORT
34 #include "../system/cmake/CMakePackageFinder.h"
35 #include "ApplicationOptions.h"
36 #include "ApplicationNetworkStats.h" // for ApplicationNetworkStatsPtr
37 #include "ArmarXCore/core/ArmarXFwd.h" // for ArmarXManagerPtr
40 #include "ArmarXCore/core/exceptions/Exception.h" // for LocalException
41 #include "properties/PropertyUser.h" // for PropertyDefinitionsPtr, etc
42 
43 #include <Ice/Application.h> // for Application
44 #include <Ice/BuiltinSequences.h> // for StringSeq
45 #include <Ice/PropertiesF.h> // for PropertiesPtr
46 #include <IceUtil/Handle.h> // for Handle, HandleBase
47 
48 #include <string> // for string
49 #include <vector> // for vector
50 #include <mutex>
51 
52 
53 namespace Ice
54 {
55  struct InitializationData;
56 } // namespace Ice
57 
58 #define xstr(s) stringify(s)
59 #define stringify(s) #s
60 
66 namespace std
67 {
68  class thread;
69 }
70 
71 namespace armarx
72 {
73  // forward declarations
74  class ManagedIceObjectRegistryInterface;
75  class ThreadPool;
76  using ThreadPoolPtr = std::shared_ptr<ThreadPool>;
79 
84 
88  class Application;
89 
94 
103  {
104  public:
108  ApplicationPropertyDefinitions(std::string prefix);
109  };
110 
194  public Ice::Application,
195  public PropertyUser
196  {
197  public:
201  Application();
209  template <class T>
211  {
212  std::unique_lock lock(instanceMutex);
213 
214  if (instance)
215  {
216  throw LocalException("Application instance already created");
217  }
218 
219  instance = ApplicationPtr(new T());
220 
221  return instance;
222  }
223 
230  static ApplicationPtr getInstance();
231 
232  static void setInstance(ApplicationPtr const& inst);
233 
234  static std::string GetVersion()
235  {
236 #ifdef ARMARX_VERSION
237  return xstr(ARMARX_VERSION);
238 #else
239  return "undefined";
240 #endif
241  }
242 
246  int run(int argc, char* argv[]) override;
247 
254  void setName(const std::string& name);
255 
261  std::string getName() const;
262 
268  void interruptCallback(int signal) override;
269 
270  void registerDataPathsFromDependencies(std::string dependencies);
271 
277  std::vector<std::string> getDefaultPackageNames();
278 
279  static std::string GetArmarXConfigDefaultPath(bool envVarExpanded = true);
280  static Ice::StringSeq GetDefaultsPaths();
281  static void LoadDefaultConfig(Ice::PropertiesPtr properties);
282  static const std::string& GetProjectName();
283  static const Ice::StringSeq& GetProjectDependencies();
284 
289  void setIceProperties(Ice::PropertiesPtr properties) override;
290 
291  void updateIceProperties(const Ice::PropertyDict& properties) override;
292 
293  void icePropertiesUpdated(const std::set<std::string>& changedProperties) override;
294  static const std::string ArmarXUserConfigDirEnvVar;
295 
296  const ThreadPoolPtr& getThreadPool() const;
297  bool getForbidThreadCreation() const;
298  void setForbidThreadCreation(bool value);
299  void enableLibLoading(bool enable = true);
300 
301  void storeCommandLineArguments(int argc, char* argv[]);
302  const std::vector<std::string>& getCommandLineArguments() const;
303  protected:
307  PropertyDefinitionsPtr createPropertyDefinitions() override;
308 
315  virtual void setup(const ManagedIceObjectRegistryInterfacePtr& registry, Ice::PropertiesPtr properties) = 0;
316 
330  virtual int exec(const ArmarXManagerPtr& armarXManager);
331 
339  int doMain(int argc, char* argv[], const Ice::InitializationData& initData, Ice::Int i) override;
340 
341  void loadDefaultConfig(int argc, char* argv[], const Ice::InitializationData& initData);
342 
343  void loadDependentProjectDatapaths();
344 
352  virtual std::string getDomainName();
353 
357  ArmarXManagerPtr getArmarXManager();
358 
359 
360 
364  void showHelp(ApplicationOptions::Options& options);
365 
369  Ice::PropertiesPtr parseOptionsMergeProperties(int argc, char* argv[]);
370 
371 
376  static void HandlerInterrupt(int sig);
377 
378 
383  static void HandlerFault(int sig);
384 
385  private:
386 
387  void loadLibrariesFromProperties();
388 
392  static ApplicationPtr instance;
393 
397  static std::mutex instanceMutex;
398 
402  std::string applicationName;
403 
407  ArmarXManagerPtr armarXManager;
408 
409  ApplicationNetworkStatsPtr applicationNetworkStats;
410 
411  std::shared_ptr<std::thread> shutdownThread;
412 
413  static std::string ProjectName;
414  static Ice::StringSeq ProjectDependendencies;
415 
416  ThreadPoolPtr threadPool;
417  bool forbidThreadCreation = false;
418  bool libLoadingEnabled = false;
419 
420  std::vector<std::string> commandLineArguments;
421  };
422 
424  {
425  // Application interface
426  protected:
427  void setup(const ManagedIceObjectRegistryInterfacePtr& registry, Ice::PropertiesPtr properties) override
428  {
429  }
430  };
431 }
432 
434 {
435  template<class ComponentT, class AppT>
441  class SimpleSingleComponentApp : virtual public AppT
442  {
443  void setup(const ManagedIceObjectRegistryInterfacePtr& registry, Ice::PropertiesPtr properties) override
444  {
445  registry->addObject(Component::create<ComponentT>(properties, "", appConfigDomain));
446  }
447 
448  public:
449  std::string appConfigName;
450  std::string appConfigDomain;
451  };
452 }
453 
454 namespace armarx
455 {
482  template<class ComponentT, class AppT = armarx::Application>
483  int runSimpleComponentApp(int argc, char* argv[], std::string appName, const std::string& configName = "", const std::string& configDomain = "ArmarX", bool enableLibLoading = false)
484  {
485  armarx::ApplicationPtr app = AppT::template createInstance<internal::SimpleSingleComponentApp<ComponentT, AppT> >();
486 
488  sApp->appConfigDomain = configDomain;
489  sApp->appConfigName = configName;
490  app->enableLibLoading(enableLibLoading);
491  app->setName(appName);
492  app->storeCommandLineArguments(argc, argv);
493  return app->main(argc, argv);
494  }
495 
500  {
501  public:
502  MultipleComponentsApplication(std::vector<ComponentPtr> components) :
503  components(components) {}
504 
505  private:
506  std::vector<ComponentPtr> components;
507 
508  // Application interface
509  protected:
510  void setup(const ManagedIceObjectRegistryInterfacePtr& registry, Ice::PropertiesPtr properties) override
511  {
512  for (ComponentPtr comp : components)
513  {
514  registry->addObject(comp);
515  }
516  }
517  };
518 
519  template<class ComponentType>
520  std::vector<ComponentPtr> createComponents(ComponentType comp)
521  {
522  return {comp};
523  }
524 
525  template<class ComponentType, class... ComponentTypes >
526  std::vector<ComponentPtr> createComponents(ComponentType comp, ComponentTypes... components)
527  {
528  std::vector<ComponentPtr> result {comp};
529  std::vector<ComponentPtr> tempResult = createComponents(components...);
530  result.insert(result.end(), tempResult.begin(), tempResult.end());
531  return result;
532  }
533 
534  template<class... ComponentTypes >
535  std::vector<ComponentPtr> createComponentsUtil(ComponentTypes&& ... components)
536  {
537  std::vector<ComponentPtr> result;
538  std::vector<ComponentPtr> tempResult = createComponents(components...);
539  result.insert(result.end(), tempResult.begin(), tempResult.end());
540  return result;
541  }
542 
559  template<class... ComponentTypes >
560  std::tuple<armarx::ApplicationPtr, std::vector<ComponentPtr> > runMultipleComponentsApp(int argc, char* argv[], std::string appName, const std::string& configDomain = "ArmarX")
561  {
562  Ice::PropertiesPtr props = Ice::createProperties(argc, argv);
563  std::vector<ComponentPtr> comps = createComponentsUtil(Component::create<ComponentTypes>(props, "", configDomain)...);
565 
566  app->setName(appName);
567  return std::make_tuple(app, comps);
568  }
569 
570 }
571 
armarx::Application::createInstance
static ApplicationPtr createInstance()
Creates the one application instance of type T.
Definition: Application.h:210
armarx::internal::SimpleSingleComponentApp::appConfigName
std::string appConfigName
Definition: Application.h:449
armarx::internal
Definition: Application.h:433
armarx::runMultipleComponentsApp
std::tuple< armarx::ApplicationPtr, std::vector< ComponentPtr > > runMultipleComponentsApp(int argc, char *argv[], std::string appName, const std::string &configDomain="ArmarX")
Convenience function to create an app with multiple components easily.
Definition: Application.h:560
ApplicationOptions.h
armarx::ThreadPoolPtr
std::shared_ptr< ThreadPool > ThreadPoolPtr
Definition: Application.h:76
xstr
#define xstr(s)
Definition: Application.h:58
IceInternal::Handle< ::Ice::Properties >
armarx::ApplicationNetworkStats
The ApplicationNetworkStats class implements the Ice::Instrumentation::CommunicatorObserver interface...
Definition: ApplicationNetworkStats.h:51
armarx::createComponentsUtil
std::vector< ComponentPtr > createComponentsUtil(ComponentTypes &&... components)
Definition: Application.h:535
armarx::createComponents
std::vector< ComponentPtr > createComponents(ComponentType comp)
Definition: Application.h:520
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::MultipleComponentsApplication::setup
void setup(const ManagedIceObjectRegistryInterfacePtr &registry, Ice::PropertiesPtr properties) override
Setup method to be implemented by user applications.
Definition: Application.h:510
Ice::createProperties
Ice::PropertiesPtr createProperties()
PropertyUser.h
armarx::DummyApplication
Definition: Application.h:423
armarx::ApplicationOptions::Options
Stucture containing the parsed options of the application.
Definition: ApplicationOptions.h:76
ApplicationNetworkStats.h
armarx::runSimpleComponentApp
int runSimpleComponentApp(int argc, char *argv[], std::string appName, const std::string &configName="", const std::string &configDomain="ArmarX", bool enableLibLoading=false)
Creates and runs an application (of AppT) for the given component (ComponentT).
Definition: Application.h:483
armarx::ApplicationPtr
IceUtil::Handle< Application > ApplicationPtr
Definition: Application.h:93
armarx::PropertyDefinitionContainer
PropertyDefinitionContainer.
Definition: PropertyDefinitionContainer.h:53
armarx::Application
Baseclass for all ArmarX applications.
Definition: Application.h:193
armarx::ApplicationPropertyDefinitions
Application property definition container.
Definition: Application.h:101
armarx::internal::SimpleSingleComponentApp::appConfigDomain
std::string appConfigDomain
Definition: Application.h:450
armarx::internal::SimpleSingleComponentApp
Used by runSimpleComponentApp<ComponentT, AppT>. This class can't be defined in function,...
Definition: Application.h:441
ArmarXFwd.h
Ice
Definition: DBTypes.cpp:64
armarx::DummyApplication::setup
void setup(const ManagedIceObjectRegistryInterfacePtr &registry, Ice::PropertiesPtr properties) override
Setup method to be implemented by user applications.
Definition: Application.h:427
PropertyDefinitionContainer.h
std
Definition: Application.h:66
IceUtil::Handle< ApplicationNetworkStats >
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
ManagedIceObjectRegistryInterface.h
armarx::Application::GetVersion
static std::string GetVersion()
Definition: Application.h:234
armarx::MultipleComponentsApplication::MultipleComponentsApplication
MultipleComponentsApplication(std::vector< ComponentPtr > components)
Definition: Application.h:502
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
armarx::MultipleComponentsApplication
Do not use! Use armarx::runMultipleComponentsApp()
Definition: Application.h:499
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::ApplicationOptions::showHelp
void showHelp(ApplicationPtr application, ArmarXDummyManagerPtr dummyManager, Options options, Ice::PropertiesPtr properties, std::ostream &out=std::cout)
Prints help according to the format selection in options.
Definition: ApplicationOptions.cpp:169
armarx::PropertyUser
Abstract PropertyUser class.
Definition: PropertyUser.h:62
armarx::Application::ArmarXUserConfigDirEnvVar
static const std::string ArmarXUserConfigDirEnvVar
Definition: Application.h:294
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
Exception.h