Statistics
| Branch: | Tag: | Revision:

hlrc / server / src / MiddlewareRSB.cpp @ d4dfb462

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