Statistics
| Branch: | Tag: | Revision:

hlrc / server / src / MiddlewareRSB.cpp @ bd86bb92

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