Statistics
| Branch: | Tag: | Revision:

hlrc / server / src / MiddlewareRSB.cpp @ d4c15d69

History | View | Annotate | Download (6.579 KB)

1 0c286af0 Simon Schulz
/*
2 f150aab5 Robert Haschke
 * This file is part of hlrc_server
3
 *
4
 * Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
5
 * http://opensource.cit-ec.de/projects/hlrc_server
6
 *
7
 * This file may be licensed under the terms of the
8
 * GNU General Public License Version 3 (the ``GPL''),
9
 * or (at your option) any later version.
10
 *
11
 * Software distributed under the License is distributed
12
 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13
 * express or implied. See the GPL for the specific language
14
 * governing rights and limitations.
15
 *
16
 * You should have received a copy of the GPL along with this
17
 * program. If not, go to http://www.gnu.org/licenses/gpl.html
18
 * or write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
 *
21
 * The development of this software was supported by the
22
 * Excellence Cluster EXC 277 Cognitive Interaction Technology.
23
 * The Excellence Cluster EXC 277 is a grant of the Deutsche
24
 * Forschungsgemeinschaft (DFG) in the context of the German
25
 * Excellence Initiative.
26
 *
27
 */
28 0c286af0 Simon Schulz
29
#include "MiddlewareRSB.h"
30
31
#ifdef RSB_SUPPORT
32
#include <rsb/converter/Repository.h>
33
#include <rsb/converter/ProtocolBufferConverter.h>
34
#include <cstdio>
35
36 f150aab5 Robert Haschke
// CallbackWrapper:
37 0c286af0 Simon Schulz
#include "RSB/GazeCallbackWrapper.h"
38
#include "RSB/MouthCallbackWrapper.h"
39
#include "RSB/SpeechCallbackWrapper.h"
40
#include "RSB/UtteranceCallbackWrapper.h"
41
#include "RSB/EmotionCallbackWrapper.h"
42
#include "RSB/AnimationCallbackWrapper.h"
43
#include "RSB/UtteranceRSB.h"
44
using namespace std;
45
using namespace rsb;
46
using namespace rsb::patterns;
47
48 f150aab5 Robert Haschke
MiddlewareRSB::MiddlewareRSB(Arbiter* arbiter, std::string scope) : Middleware(arbiter, scope) {
49
        init();
50 0c286af0 Simon Schulz
}
51
52 f150aab5 Robert Haschke
MiddlewareRSB::~MiddlewareRSB() {
53 0c286af0 Simon Schulz
}
54
55 f150aab5 Robert Haschke
void MiddlewareRSB::tick() {
56
        // nothing to do
57 0c286af0 Simon Schulz
}
58
59 f150aab5 Robert Haschke
void MiddlewareRSB::init() {
60
        printf("> registering converters\n");
61
62
        // converter for EmotionState
63
        try {
64
                rsb::converter::Converter<string>::Ptr emotionStateConverter(
65
                   new rsb::converter::ProtocolBufferConverter<rst::animation::EmotionExpression>());
66
                rsb::converter::converterRepository<string>()->registerConverter(emotionStateConverter);
67
        }
68
        catch (...) {
69
        }
70
71
        // converter for SoundChunk
72
        try {
73
                rsb::converter::Converter<string>::Ptr SoundChunkConverter(
74
                   new rsb::converter::ProtocolBufferConverter<rst::audition::SoundChunk>());
75
                rsb::converter::converterRepository<string>()->registerConverter(SoundChunkConverter);
76
        }
77
        catch (...) {
78
        }
79
80
        // converter for Utterance
81
        try {
82
                rsb::converter::Converter<string>::Ptr UtteranceConverter(
83
                   new rsb::converter::ProtocolBufferConverter<rst::audition::Utterance>());
84
                rsb::converter::converterRepository<string>()->registerConverter(UtteranceConverter);
85
        }
86
        catch (...) {
87
        }
88
89
        // converter for GazeTarget
90
        try {
91
                rsb::converter::Converter<string>::Ptr gazeTargetConverter(
92
                   new rsb::converter::ProtocolBufferConverter<rst::animation::BinocularHeadGaze>());
93
                rsb::converter::converterRepository<string>()->registerConverter(gazeTargetConverter);
94
        }
95
        catch (...) {
96
        }
97
98
        // converter for MouthTarget
99
        // rsb::converter::Converter<string>::Ptr mouthTargetConverter(new
100
        // rsb::converter::ProtocolBufferConverter<rst::robot::MouthTarget>());
101
        // rsb::converter::converterRepository<string>()->registerConverter(mouthTargetConverter);
102
103
        // converter for Animation
104
        try {
105
                rsb::converter::Converter<string>::Ptr animationConverter(
106
                   new rsb::converter::ProtocolBufferConverter<rst::animation::HeadAnimation>());
107
                rsb::converter::converterRepository<string>()->registerConverter(animationConverter);
108
        }
109
        catch (...) {
110
        }
111
112
        // first get a factory instance that is used to create RSB domain objects
113
        Factory& factory = getFactory();
114
115
        // scope to listen on:
116
        string scope = base_scope + "/set/";
117
118
        // get server
119
        server = factory.createLocalServer(scope);
120
121
        // get tts server:
122
        string tts_scope = base_scope + "/tts_provider";
123
        printf("> registering remote server on %s\n", tts_scope.c_str());
124
        tts_server = factory.createRemoteServer(tts_scope);
125
126
        // create local server with parallel exec strategy (HACK!)
127
        rsb::ParticipantConfig config = factory.getDefaultParticipantConfig();
128
        rsc::runtime::Properties& properties = config.mutableEventReceivingStrategy().mutableOptions();
129
        properties.set<string>("parallelhandlercalls", "1");
130
131
        // now we can create a server with parallel execution:
132
        server_parallel = factory.createLocalServer(scope, config);
133
134
        // this server will now handle/call these methods in multiple threads:
135
        server_parallel->registerMethod("animation", LocalServer::CallbackPtr(new AnimationCallbackWrapper(this)));
136
137
        // initialise callbacks:
138
        init_callbacks();
139
140
        // informer->publish(gaze_target);
141
        printf("> MiddlewareRSB initialised\n");
142 0c286af0 Simon Schulz
}
143
144 f150aab5 Robert Haschke
void MiddlewareRSB::init_callback(string name, LocalServer::CallbackPtr callback_ptr) {
145
        printf("> init_callback(%s%s)\n", server->getScope()->toString().c_str(), name.c_str());
146
        server->registerMethod(name, callback_ptr);
147 0c286af0 Simon Schulz
}
148
149 f150aab5 Robert Haschke
void MiddlewareRSB::init_callbacks() {
150
        printf("> registering callbacks (rpc methods)\n");
151 0c286af0 Simon Schulz
152 f150aab5 Robert Haschke
        init_callback("speech", LocalServer::CallbackPtr(new SpeechCallbackWrapper(this)));
153
        init_callback("utterance", LocalServer::CallbackPtr(new UtteranceCallbackWrapper(this)));
154
        init_callback("gaze", LocalServer::CallbackPtr(new GazeCallbackWrapper(this)));
155
        /// init_callback("mouth",     LocalServer::CallbackPtr(new MouthCallbackWrapper(this)));
156 0c286af0 Simon Schulz
157 f150aab5 Robert Haschke
        EmotionCallbackWrapper* emotion_cb = new EmotionCallbackWrapper(this);
158
        init_callback("defaultEmotion", LocalServer::CallbackPtr(emotion_cb));
159
        init_callback("currentEmotion", LocalServer::CallbackPtr(emotion_cb));
160 0c286af0 Simon Schulz
}
161
162 f150aab5 Robert Haschke
// call a tts system to convert a string to an utterance
163 482adb6d Robert Haschke
std::shared_ptr<Utterance> MiddlewareRSB::tts_call(string text) {
164 f150aab5 Robert Haschke
        double tts_timeout = 1.0; // seconds. DO NOT CHANGE THIS!
165
166
        // build request
167 482adb6d Robert Haschke
        std::shared_ptr<std::string> request(new string(text));
168 f150aab5 Robert Haschke
169
        // try to fetch it asynchronously:
170
        try {
171
                RemoteServer::DataFuture<rst::audition::Utterance> future_ptr =
172
                   tts_server->callAsync<rst::audition::Utterance>("create_utterance", request);
173
174
                // try to fetch the result
175 482adb6d Robert Haschke
                std::shared_ptr<rst::audition::Utterance> utterance_ptr = future_ptr.get(tts_timeout);
176 f150aab5 Robert Haschke
177
                // done, return utterance ptr
178 482adb6d Robert Haschke
                std::shared_ptr<Utterance> utterance(new UtteranceRSB(*(utterance_ptr.get())));
179 f150aab5 Robert Haschke
                printf("> done. got utterance (text=%s)\n", utterance->get_text().c_str());
180
                return utterance;
181
        }
182
        catch (rsc::threading::FutureTimeoutException e) {
183
                printf("> error: tts_call timed out after %3.1f seconds.\n", tts_timeout);
184
        }
185
        catch (rsc::threading::FutureTaskExecutionException e) {
186
                printf("> error: tts_call failed: %s\n", e.what());
187
        }
188
189
        printf("> failed... got no utterance\n");
190 482adb6d Robert Haschke
        std::shared_ptr<Utterance> utterance(new Utterance());
191 f150aab5 Robert Haschke
        return utterance;
192 0c286af0 Simon Schulz
}
193
194
#endif