Fawkes API  Fawkes Development Version
naofawkes_module.cpp
1 
2 /***************************************************************************
3  * naofawkes_module.cpp - NaoQi module for Fawkes integration
4  *
5  * Created: Thu Jul 03 17:59:29 2008
6  * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "naoqi_broker.h"
24 
25 #include <baseapp/run.h>
26 #include <baseapp/main_thread.h>
27 #include <core/exception.h>
28 #include <core/threading/thread.h>
29 #include <plugin/manager.h>
30 #include <utils/system/dynamic_module/module.h>
31 
32 #include <alcore/altypes.h>
33 #include <alcommon/albroker.h>
34 #include <alcommon/albrokermanager.h>
35 #include <alcommon/almodule.h>
36 #include <alproxies/allauncherproxy.h>
37 #include <alproxies/alaudioplayerproxy.h>
38 #include <alproxies/alloggerproxy.h>
39 
40 #include <dlfcn.h>
41 
42 using namespace std;
43 
44 
45 /** Nao Fawkes Module.
46  * This module is instantiated in NaoQi and embeds its own Fawkes instance.
47  */
48 class NaoFawkesModule : public AL::ALModule
49 {
50  public:
51 
52  /** Constructor.
53  * Initializes and starts the embedded Fawkes, and loads the nao plugin
54  * without precondition.
55  * @param broker NaoQi broker to use, will be forwarded to the nao plugin
56  * @param name name of the module (no idea why NaoQi wants to pass it
57  * as a parameter)
58  */
59  NaoFawkesModule(AL::ALPtr<AL::ALBroker> broker, const std::string &name)
60  : AL::ALModule(broker, name), broker(broker)
61  {
62  setModuleDescription("Fawkes integration module");
63 
64  AL::ALPtr<AL::ALLoggerProxy> logger = broker->getLoggerProxy();
65 
66  try {
67  logger->info("NaoQiFawkes", "*** Initializing embedded Fawkes");
68 
69  // The module flags hack is required because otherwise NaoQi segfaults
70  // due to problems with boost static initialization after a module
71  // has been closed once, unfortunately that prevents loading a
72  // new version of a plugin without a restart.
73 
74  fawkes::runtime::InitOptions init_options =
75  fawkes::runtime::InitOptions("naofawkes")
78  .net_service_name("NaoQi Fawkes on %h")
79  .loggers("console;syslog:NaoQiFawkes")
80  .load_plugins("naoqi,webview")
81  .default_plugin("nao_default");
82 
83  if (fawkes::runtime::init(init_options) != 0) {
84  //throw AL::ALError(name, "ctor", "Initializing Fawkes failed");
85  logger->info("NaoQiFawkes", "--- Fawkes initialization failed");
86  play_sound(RESDIR"/sounds/naoshutdown.wav");
87  } else {
88 
89  logger->info("NaoQiFawkes", "*** Starting embedded Fawkes");
90  fawkes::runtime::main_thread->full_start();
91  logger->info("NaoQiFawkes", "*** Embedded Fawkes initialization done");
92  play_sound(RESDIR"/sounds/naostartup.wav");
93  }
94  } catch (fawkes::Exception &e) {
95  std::string message;
96  for (fawkes::Exception::iterator i = e.begin(); i != e.end(); ++i) {
97  if (i != e.begin()) message += "\n";
98  message += *i;
99  }
100  logger->info("NaoQiFawkes",
101  "--- Fawkes initialization failed, exception follows.");
102  logger->info("NaoQiFawkes", message);
103  play_sound(RESDIR"/sounds/naoshutdown.wav");
104  //throw AL::ALError(name, "ctor", e.what());
105  }
106 
107  }
108 
109  /** Destructor.
110  * Stops the Fawkes main thread and cleans up the embedded Fawkes.
111  */
113  {
114  fawkes::runtime::main_thread->cancel();
115  fawkes::runtime::main_thread->join();
116  fawkes::runtime::cleanup();
117  }
118 
119  /** Play startup sound.
120  * @param filename name of file to play
121  */
122  void
123  play_sound(const char *filename)
124  {
125  // Is the auplayer running ?
126  try {
127  AL::ALPtr<AL::ALLauncherProxy> launcher(new AL::ALLauncherProxy(broker));
128  bool is_auplayer_available = launcher->isModulePresent("ALAudioPlayer");
129 
130  if (is_auplayer_available) {
131  AL::ALPtr<AL::ALAudioPlayerProxy>
132  auplayer(new AL::ALAudioPlayerProxy(broker));
133  auplayer->playFile(filename);
134  }
135  } catch (AL::ALError& e) {} // ignored
136  }
137  private:
138  AL::ALPtr<AL::ALBroker> broker;
139 };
140 
141 #ifdef __cplusplus
142 extern "C"
143 {
144 #endif
145 
146 
147 int
148 _createModule(AL::ALPtr<AL::ALBroker> broker)
149 {
150  // init broker with the main broker inctance
151  // from the parent executable
152 
153  AL::ALPtr<AL::ALLoggerProxy> logger = broker->getLoggerProxy();
154 
155  logger->info("NaoQiFawkes", "*** Setting broker stuff");
156  AL::ALBrokerManager::setInstance(broker->fBrokerManager.lock());
157  AL::ALBrokerManager::getInstance()->addBroker(broker);
158 
159  fawkes::naoqi::broker = broker;
160 
161  // create modules instance
162  logger->info("NaoQiFawkes", "*** Instantiating Module");
163  AL::ALModule::createModule<NaoFawkesModule>(broker, "NaoFawkesModule");
164 
165  return 0;
166 }
167 
168 int
169 _closeModule()
170 {
171  // Delete module instance
172  return 0;
173 }
174 
175 # ifdef __cplusplus
176 }
177 # endif
void full_start()
Start the thread and wait until once() completes.
virtual ~NaoFawkesModule()
Destructor.
InitOptions & default_plugin(const char *default_plugin)
Set additional default plugin name.
STL namespace.
InitOptions & load_plugins(const char *plugin_list)
Set list of plugins to load during startup.
Message iterator for exceptions.
Definition: exception.h:72
Initialization options class.
Definition: init_options.h:37
iterator end()
Get end iterator for messages.
Definition: exception.cpp:717
Base class for exceptions in Fawkes.
Definition: exception.h:36
Do not unload the library during dlclose().
Definition: module.h:89
iterator begin()
Get iterator for messages.
Definition: exception.cpp:700
Nao Fawkes Module.
InitOptions & plugin_module_flags(Module::ModuleFlags flags)
Set module flags.
NaoFawkesModule(AL::ALPtr< AL::ALBroker > broker, const std::string &name)
Constructor.
void cancel()
Cancel a thread.
Definition: thread.cpp:651
InitOptions & net_service_name(const char *service_name)
Set Fawkes network service name.
void play_sound(const char *filename)
Play startup sound.
InitOptions & loggers(const char *loggers)
Set loggers.
void join()
Join the thread.
Definition: thread.cpp:610
Default flags, these are MODULE_BIND_GLOBAL, MODULE_BIND_NOW and MODULE_BIND_DEEP.
Definition: module.h:46