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 <alcommon/albroker.h>
26#include <alcommon/albrokermanager.h>
27#include <alcommon/almodule.h>
28#include <alcore/altypes.h>
29#include <alproxies/alaudioplayerproxy.h>
30#include <alproxies/allauncherproxy.h>
31#include <alproxies/alloggerproxy.h>
32#include <baseapp/main_thread.h>
33#include <baseapp/run.h>
34#include <core/exception.h>
35#include <core/threading/thread.h>
36#include <plugin/manager.h>
37#include <utils/system/dynamic_module/module.h>
38
39#include <dlfcn.h>
40
41using namespace std;
42
43/** Nao Fawkes Module.
44 * This module is instantiated in NaoQi and embeds its own Fawkes instance.
45 */
46class NaoFawkesModule : public AL::ALModule
47{
48public:
49 /** Constructor.
50 * Initializes and starts the embedded Fawkes, and loads the nao plugin
51 * without precondition.
52 * @param broker NaoQi broker to use, will be forwarded to the nao plugin
53 * @param name name of the module (no idea why NaoQi wants to pass it
54 * as a parameter)
55 */
56 NaoFawkesModule(AL::ALPtr<AL::ALBroker> broker, const std::string &name)
57 : AL::ALModule(broker, name), broker(broker)
58 {
59 setModuleDescription("Fawkes integration module");
60
61 AL::ALPtr<AL::ALLoggerProxy> logger = broker->getLoggerProxy();
62
63 try {
64 logger->info("NaoQiFawkes", "*** Initializing embedded Fawkes");
65
66 // The module flags hack is required because otherwise NaoQi segfaults
67 // due to problems with boost static initialization after a module
68 // has been closed once, unfortunately that prevents loading a
69 // new version of a plugin without a restart.
70
71 fawkes::runtime::InitOptions init_options =
75 .net_service_name("NaoQi Fawkes on %h")
76 .loggers("console;syslog:NaoQiFawkes")
77 .load_plugins("naoqi,webview")
78 .default_plugin("nao_default");
79
80 if (fawkes::runtime::init(init_options) != 0) {
81 //throw AL::ALError(name, "ctor", "Initializing Fawkes failed");
82 logger->info("NaoQiFawkes", "--- Fawkes initialization failed");
83 play_sound(RESDIR "/sounds/naoshutdown.wav");
84 } else {
85 logger->info("NaoQiFawkes", "*** Starting embedded Fawkes");
86 fawkes::runtime::main_thread->full_start();
87 logger->info("NaoQiFawkes", "*** Embedded Fawkes initialization done");
88 play_sound(RESDIR "/sounds/naostartup.wav");
89 }
90 } catch (fawkes::Exception &e) {
91 std::string message;
92 for (fawkes::Exception::iterator i = e.begin(); i != e.end(); ++i) {
93 if (i != e.begin())
94 message += "\n";
95 message += *i;
96 }
97 logger->info("NaoQiFawkes", "--- Fawkes initialization failed, exception follows.");
98 logger->info("NaoQiFawkes", message);
99 play_sound(RESDIR "/sounds/naoshutdown.wav");
100 //throw AL::ALError(name, "ctor", e.what());
101 }
102 }
103
104 /** Destructor.
105 * Stops the Fawkes main thread and cleans up the embedded Fawkes.
106 */
108 {
109 fawkes::runtime::main_thread->cancel();
110 fawkes::runtime::main_thread->join();
111 fawkes::runtime::cleanup();
112 }
113
114 /** Play startup sound.
115 * @param filename name of file to play
116 */
117 void
118 play_sound(const char *filename)
119 {
120 // Is the auplayer running ?
121 try {
122 AL::ALPtr<AL::ALLauncherProxy> launcher(new AL::ALLauncherProxy(broker));
123 bool is_auplayer_available = launcher->isModulePresent("ALAudioPlayer");
124
125 if (is_auplayer_available) {
126 AL::ALPtr<AL::ALAudioPlayerProxy> auplayer(new AL::ALAudioPlayerProxy(broker));
127 auplayer->playFile(filename);
128 }
129 } catch (AL::ALError &e) {
130 } // ignored
131 }
132
133private:
134 AL::ALPtr<AL::ALBroker> broker;
135};
136
137#ifdef __cplusplus
138extern "C" {
139#endif
140
141int
142_createModule(AL::ALPtr<AL::ALBroker> broker)
143{
144 // init broker with the main broker inctance
145 // from the parent executable
146
147 AL::ALPtr<AL::ALLoggerProxy> logger = broker->getLoggerProxy();
148
149 logger->info("NaoQiFawkes", "*** Setting broker stuff");
150 AL::ALBrokerManager::setInstance(broker->fBrokerManager.lock());
151 AL::ALBrokerManager::getInstance()->addBroker(broker);
152
153 fawkes::naoqi::broker = broker;
154
155 // create modules instance
156 logger->info("NaoQiFawkes", "*** Instantiating Module");
157 AL::ALModule::createModule<NaoFawkesModule>(broker, "NaoFawkesModule");
158
159 return 0;
160}
161
162int
163_closeModule()
164{
165 // Delete module instance
166 return 0;
167}
168
169#ifdef __cplusplus
170}
171#endif
Nao Fawkes Module.
void play_sound(const char *filename)
Play startup sound.
NaoFawkesModule(AL::ALPtr< AL::ALBroker > broker, const std::string &name)
Constructor.
virtual ~NaoFawkesModule()
Destructor.
Message iterator for exceptions.
Definition: exception.h:73
Base class for exceptions in Fawkes.
Definition: exception.h:36
iterator end() noexcept
Get end iterator for messages.
Definition: exception.cpp:692
iterator begin() noexcept
Get iterator for messages.
Definition: exception.cpp:676
void full_start()
Start the thread and wait until once() completes.
@ MODULE_NODELETE
Do not unload the library during dlclose().
Definition: module.h:89
@ MODULE_FLAGS_DEFAULT
Default flags, these are MODULE_BIND_GLOBAL, MODULE_BIND_NOW and MODULE_BIND_DEEP.
Definition: module.h:46
void join()
Join the thread.
Definition: thread.cpp:597
void cancel()
Cancel a thread.
Definition: thread.cpp:646
Initialization options class.
Definition: init_options.h:34
InitOptions & loggers(const char *loggers)
Set loggers.
InitOptions & net_service_name(const char *service_name)
Set Fawkes network service name.
InitOptions & plugin_module_flags(Module::ModuleFlags flags)
Set module flags.
InitOptions & load_plugins(const char *plugin_list)
Set list of plugins to load during startup.
InitOptions & default_plugin(const char *default_plugin)
Set additional default plugin name.