Statistics
| Branch: | Tag: | Revision:

hlrc / server / src / MiddlewareRSB.cpp @ eb08b7c8

History | View | Annotate | Download (6.739 KB)

1 0c286af0 Simon Schulz
/*
2
* 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
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
//CallbackWrapper:
37
#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 3877047d Simon Schulz
49 0c286af0 Simon Schulz
MiddlewareRSB::MiddlewareRSB(Arbiter *arbiter, std::string scope) : Middleware(arbiter, scope){
50
    init();
51
}
52
53
MiddlewareRSB::~MiddlewareRSB(){
54
55
}
56
57
void MiddlewareRSB::tick(){
58
    //nothing to do
59
}
60
61
void MiddlewareRSB::init(){
62
    printf("> registering converters\n");
63
64
    //converter for EmotionState
65 89370c3a Patrick Holthaus
    try {
66 4bd3e852 Simon Schulz
    rsb::converter::Converter<string>::Ptr emotionStateConverter(new rsb::converter::ProtocolBufferConverter<rst::animation::EmotionExpression>());
67 0c286af0 Simon Schulz
    rsb::converter::converterRepository<string>()->registerConverter(emotionStateConverter);
68 89370c3a Patrick Holthaus
    } catch (...) {}
69 0c286af0 Simon Schulz
70
    //converter for SoundChunk
71 89370c3a Patrick Holthaus
    try {
72 0c286af0 Simon Schulz
    rsb::converter::Converter<string>::Ptr SoundChunkConverter(new rsb::converter::ProtocolBufferConverter<rst::audition::SoundChunk>());
73
    rsb::converter::converterRepository<string>()->registerConverter(SoundChunkConverter);
74 89370c3a Patrick Holthaus
    } catch (...) {}
75 0c286af0 Simon Schulz
76
    //converter for Utterance
77 89370c3a Patrick Holthaus
    try {
78 0c286af0 Simon Schulz
    rsb::converter::Converter<string>::Ptr UtteranceConverter(new rsb::converter::ProtocolBufferConverter<rst::audition::Utterance>());
79
    rsb::converter::converterRepository<string>()->registerConverter(UtteranceConverter);
80 89370c3a Patrick Holthaus
    } catch (...) {}
81 0c286af0 Simon Schulz
82
    //converter for GazeTarget
83 89370c3a Patrick Holthaus
    try {
84 4bd3e852 Simon Schulz
    rsb::converter::Converter<string>::Ptr gazeTargetConverter(new rsb::converter::ProtocolBufferConverter<rst::animation::BinocularHeadGaze>());
85 0c286af0 Simon Schulz
    rsb::converter::converterRepository<string>()->registerConverter(gazeTargetConverter);
86 89370c3a Patrick Holthaus
    } catch (...) {}
87 0c286af0 Simon Schulz
88
    //converter for MouthTarget
89 4bd3e852 Simon Schulz
    //rsb::converter::Converter<string>::Ptr mouthTargetConverter(new rsb::converter::ProtocolBufferConverter<rst::robot::MouthTarget>());
90
    //rsb::converter::converterRepository<string>()->registerConverter(mouthTargetConverter);
91 0c286af0 Simon Schulz
92
    //converter for Animation
93 89370c3a Patrick Holthaus
    try {
94 4bd3e852 Simon Schulz
    rsb::converter::Converter<string>::Ptr animationConverter(new rsb::converter::ProtocolBufferConverter<rst::animation::HeadAnimation>());
95 0c286af0 Simon Schulz
    rsb::converter::converterRepository<string>()->registerConverter(animationConverter);
96 89370c3a Patrick Holthaus
    } catch (...) {}
97 0c286af0 Simon Schulz
98
    //first get a factory instance that is used to create RSB domain objects
99
    Factory &factory = getFactory();
100
101
    //scope to listen on:
102
    string scope = base_scope + "/set/";
103
104
    //get server
105
    server = factory.createLocalServer(scope);
106
107
    //get tts server:
108
    string tts_scope = base_scope + "/tts_provider";
109
    printf("> registering remote server on %s\n",tts_scope.c_str());
110
    tts_server = factory.createRemoteServer(tts_scope);
111
112
113
    //create local server with parallel exec strategy (HACK!)
114
    rsb::ParticipantConfig config = factory.getDefaultParticipantConfig();
115
    rsc::runtime::Properties &properties = config.mutableEventReceivingStrategy().mutableOptions();
116
    properties.set<string>("parallelhandlercalls", "1");
117
118
    //now we can create a server with parallel execution:
119
    server_parallel = factory.createLocalServer(scope, config);
120
121
    //this server will now handle/call these methods in multiple threads:
122
    server_parallel->registerMethod("animation", LocalServer::CallbackPtr(new AnimationCallbackWrapper(this)));
123
124
    //initialise callbacks:
125
    init_callbacks();
126
127
    //informer->publish(gaze_target);
128
    printf("> MiddlewareRSB initialised\n");
129
}
130
131
void MiddlewareRSB::init_callback(string name, LocalServer::CallbackPtr callback_ptr){
132
    printf("> init_callback(%s%s)\n", server->getScope()->toString().c_str(), name.c_str());
133
    server->registerMethod(name, callback_ptr);
134
}
135
136
void MiddlewareRSB::init_callbacks(){
137
    printf("> registering callbacks (rpc methods)\n");
138
139
    init_callback("speech",    LocalServer::CallbackPtr(new SpeechCallbackWrapper(this)));
140
    init_callback("utterance", LocalServer::CallbackPtr(new UtteranceCallbackWrapper(this)));
141
    init_callback("gaze",      LocalServer::CallbackPtr(new GazeCallbackWrapper(this)));
142 4bd3e852 Simon Schulz
    ///init_callback("mouth",     LocalServer::CallbackPtr(new MouthCallbackWrapper(this)));
143 0c286af0 Simon Schulz
144
    EmotionCallbackWrapper *emotion_cb = new EmotionCallbackWrapper(this);
145
    init_callback("defaultEmotion", LocalServer::CallbackPtr(emotion_cb));
146
    init_callback("currentEmotion", LocalServer::CallbackPtr(emotion_cb));
147
}
148
149
//call a tts system to convert a string to an utterance
150 2e526a15 Simon Schulz
boost::shared_ptr<Utterance> MiddlewareRSB::tts_call(string text){
151 0c286af0 Simon Schulz
    double tts_timeout = 1.0; //seconds. DO NOT CHANGE THIS!
152
153
    //build request
154
    boost::shared_ptr<std::string> request(new string(text));
155
156
    //try to fetch it asynchronously:
157
    try{
158 ae368998 Robert Haschke
        RemoteServer::DataFuture<rst::audition::Utterance> future_ptr = tts_server->callAsync<rst::audition::Utterance>("create_utterance", request);
159 0c286af0 Simon Schulz
160
        //try to fetch the result
161
        boost::shared_ptr<rst::audition::Utterance> utterance_ptr = future_ptr.get(tts_timeout);
162 2e526a15 Simon Schulz
163
        //done, return utterance ptr
164
        boost::shared_ptr<Utterance> utterance(new UtteranceRSB(*(utterance_ptr.get())));
165
        printf("> done. got utterance (text=%s)\n",utterance->get_text().c_str());
166
        return utterance;
167 0c286af0 Simon Schulz
168
    }catch(rsc::threading::FutureTimeoutException e){
169
        printf("> error: tts_call timed out after %3.1f seconds.\n", tts_timeout);
170
    }catch(rsc::threading::FutureTaskExecutionException e){
171
        printf("> error: tts_call failed: %s\n", e.what());
172
    }
173
174 2e526a15 Simon Schulz
    printf("> failed... got no utterance\n");
175
    boost::shared_ptr<Utterance> utterance(new Utterance());
176 0c286af0 Simon Schulz
    return utterance;
177
}
178
179
#endif