Statistics
| Branch: | Tag: | Revision:

hlrc / server / src / MiddlewareRSB.cpp @ ca4c481e

History | View | Annotate | Download (6.553 KB)

1
/*
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
MiddlewareRSB::MiddlewareRSB(Arbiter *arbiter, std::string scope) : Middleware(arbiter, scope){
49
    init();
50
}
51

    
52
MiddlewareRSB::~MiddlewareRSB(){
53

    
54
}
55

    
56
void MiddlewareRSB::tick(){
57
    //nothing to do
58
}
59

    
60
void MiddlewareRSB::init(){
61
    printf("> registering converters\n");
62

    
63
    //converter for EmotionState
64
    rsb::converter::Converter<string>::Ptr emotionStateConverter(new rsb::converter::ProtocolBufferConverter<rst::robot::EmotionState>());
65
    rsb::converter::converterRepository<string>()->registerConverter(emotionStateConverter);
66

    
67
    //converter for SoundChunk
68
    rsb::converter::Converter<string>::Ptr SoundChunkConverter(new rsb::converter::ProtocolBufferConverter<rst::audition::SoundChunk>());
69
    rsb::converter::converterRepository<string>()->registerConverter(SoundChunkConverter);
70

    
71
    //converter for Utterance
72
    rsb::converter::Converter<string>::Ptr UtteranceConverter(new rsb::converter::ProtocolBufferConverter<rst::audition::Utterance>());
73
    rsb::converter::converterRepository<string>()->registerConverter(UtteranceConverter);
74

    
75
    //converter for GazeTarget
76
    rsb::converter::Converter<string>::Ptr gazeTargetConverter(new rsb::converter::ProtocolBufferConverter<rst::robot::GazeTarget>());
77
    rsb::converter::converterRepository<string>()->registerConverter(gazeTargetConverter);
78

    
79
    //converter for MouthTarget
80
    rsb::converter::Converter<string>::Ptr mouthTargetConverter(new rsb::converter::ProtocolBufferConverter<rst::robot::MouthTarget>());
81
    rsb::converter::converterRepository<string>()->registerConverter(mouthTargetConverter);
82

    
83
    //converter for Animation
84
    rsb::converter::Converter<string>::Ptr animationConverter(new rsb::converter::ProtocolBufferConverter<rst::robot::Animation>());
85
    rsb::converter::converterRepository<string>()->registerConverter(animationConverter);
86

    
87
    //first get a factory instance that is used to create RSB domain objects
88
    Factory &factory = getFactory();
89

    
90
    //scope to listen on:
91
    string scope = base_scope + "/set/";
92

    
93
    //get server
94
    server = factory.createLocalServer(scope);
95

    
96
    //get tts server:
97
    string tts_scope = base_scope + "/tts_provider";
98
    printf("> registering remote server on %s\n",tts_scope.c_str());
99
    tts_server = factory.createRemoteServer(tts_scope);
100

    
101

    
102
    //create local server with parallel exec strategy (HACK!)
103
    rsb::ParticipantConfig config = factory.getDefaultParticipantConfig();
104
    rsc::runtime::Properties &properties = config.mutableEventReceivingStrategy().mutableOptions();
105
    properties.set<string>("parallelhandlercalls", "1");
106

    
107
    //now we can create a server with parallel execution:
108
    server_parallel = factory.createLocalServer(scope, config);
109

    
110
    //this server will now handle/call these methods in multiple threads:
111
    server_parallel->registerMethod("animation", LocalServer::CallbackPtr(new AnimationCallbackWrapper(this)));
112

    
113
    //initialise callbacks:
114
    init_callbacks();
115

    
116
    //informer->publish(gaze_target);
117
    printf("> MiddlewareRSB initialised\n");
118
}
119

    
120
void MiddlewareRSB::init_callback(string name, LocalServer::CallbackPtr callback_ptr){
121
    printf("> init_callback(%s%s)\n", server->getScope()->toString().c_str(), name.c_str());
122
    server->registerMethod(name, callback_ptr);
123
}
124

    
125
void MiddlewareRSB::init_callbacks(){
126
    printf("> registering callbacks (rpc methods)\n");
127

    
128
    init_callback("speech",    LocalServer::CallbackPtr(new SpeechCallbackWrapper(this)));
129
    init_callback("utterance", LocalServer::CallbackPtr(new UtteranceCallbackWrapper(this)));
130
    init_callback("gaze",      LocalServer::CallbackPtr(new GazeCallbackWrapper(this)));
131
    init_callback("mouth",     LocalServer::CallbackPtr(new MouthCallbackWrapper(this)));
132

    
133
    EmotionCallbackWrapper *emotion_cb = new EmotionCallbackWrapper(this);
134
    init_callback("defaultEmotion", LocalServer::CallbackPtr(emotion_cb));
135
    init_callback("currentEmotion", LocalServer::CallbackPtr(emotion_cb));
136
}
137

    
138
//call a tts system to convert a string to an utterance
139
boost::shared_ptr<Utterance> MiddlewareRSB::tts_call(string text){
140
    double tts_timeout = 1.0; //seconds. DO NOT CHANGE THIS!
141

    
142
    //build request
143
    boost::shared_ptr<std::string> request(new string(text));
144

    
145
    //try to fetch it asynchronously:
146
    try{
147
        RemoteServer::DataFuture<rst::audition::Utterance> future_ptr = tts_server->callAsync<rst::audition::Utterance>("create_utterance", request);
148

    
149
        //try to fetch the result
150
        boost::shared_ptr<rst::audition::Utterance> utterance_ptr = future_ptr.get(tts_timeout);
151

    
152
        //done, return utterance ptr
153
        boost::shared_ptr<Utterance> utterance(new UtteranceRSB(*(utterance_ptr.get())));
154
        printf("> done. got utterance (text=%s)\n",utterance->get_text().c_str());
155
        return utterance;
156

    
157
    }catch(rsc::threading::FutureTimeoutException e){
158
        printf("> error: tts_call timed out after %3.1f seconds.\n", tts_timeout);
159
    }catch(rsc::threading::FutureTaskExecutionException e){
160
        printf("> error: tts_call failed: %s\n", e.what());
161
    }
162

    
163
    printf("> failed... got no utterance\n");
164
    boost::shared_ptr<Utterance> utterance(new Utterance());
165
    return utterance;
166
}
167

    
168
#endif