Revision f150aab5 server/src/MiddlewareRSB.cpp

View differences:

server/src/MiddlewareRSB.cpp
1 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
*/
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 28

  
29 29
#include "MiddlewareRSB.h"
30 30

  
......
33 33
#include <rsb/converter/ProtocolBufferConverter.h>
34 34
#include <cstdio>
35 35

  
36
//CallbackWrapper:
36
// CallbackWrapper:
37 37
#include "RSB/GazeCallbackWrapper.h"
38 38
#include "RSB/MouthCallbackWrapper.h"
39 39
#include "RSB/SpeechCallbackWrapper.h"
......
45 45
using namespace rsb;
46 46
using namespace rsb::patterns;
47 47

  
48

  
49
MiddlewareRSB::MiddlewareRSB(Arbiter *arbiter, std::string scope) : Middleware(arbiter, scope){
50
    init();
48
MiddlewareRSB::MiddlewareRSB(Arbiter* arbiter, std::string scope) : Middleware(arbiter, scope) {
49
	init();
51 50
}
52 51

  
53
MiddlewareRSB::~MiddlewareRSB(){
54

  
52
MiddlewareRSB::~MiddlewareRSB() {
55 53
}
56 54

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

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

  
64
    //converter for EmotionState
65
    try {
66
    rsb::converter::Converter<string>::Ptr emotionStateConverter(new rsb::converter::ProtocolBufferConverter<rst::animation::EmotionExpression>());
67
    rsb::converter::converterRepository<string>()->registerConverter(emotionStateConverter);
68
    } catch (...) {}
69

  
70
    //converter for SoundChunk
71
    try {
72
    rsb::converter::Converter<string>::Ptr SoundChunkConverter(new rsb::converter::ProtocolBufferConverter<rst::audition::SoundChunk>());
73
    rsb::converter::converterRepository<string>()->registerConverter(SoundChunkConverter);
74
    } catch (...) {}
75

  
76
    //converter for Utterance
77
    try {
78
    rsb::converter::Converter<string>::Ptr UtteranceConverter(new rsb::converter::ProtocolBufferConverter<rst::audition::Utterance>());
79
    rsb::converter::converterRepository<string>()->registerConverter(UtteranceConverter);
80
    } catch (...) {}
81

  
82
    //converter for GazeTarget
83
    try {
84
    rsb::converter::Converter<string>::Ptr gazeTargetConverter(new rsb::converter::ProtocolBufferConverter<rst::animation::BinocularHeadGaze>());
85
    rsb::converter::converterRepository<string>()->registerConverter(gazeTargetConverter);
86
    } catch (...) {}
87

  
88
    //converter for MouthTarget
89
    //rsb::converter::Converter<string>::Ptr mouthTargetConverter(new rsb::converter::ProtocolBufferConverter<rst::robot::MouthTarget>());
90
    //rsb::converter::converterRepository<string>()->registerConverter(mouthTargetConverter);
91

  
92
    //converter for Animation
93
    try {
94
    rsb::converter::Converter<string>::Ptr animationConverter(new rsb::converter::ProtocolBufferConverter<rst::animation::HeadAnimation>());
95
    rsb::converter::converterRepository<string>()->registerConverter(animationConverter);
96
    } catch (...) {}
97

  
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");
59
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");
129 142
}
130 143

  
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);
144
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);
134 147
}
135 148

  
136
void MiddlewareRSB::init_callbacks(){
137
    printf("> registering callbacks (rpc methods)\n");
149
void MiddlewareRSB::init_callbacks() {
150
	printf("> registering callbacks (rpc methods)\n");
138 151

  
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
    ///init_callback("mouth",     LocalServer::CallbackPtr(new MouthCallbackWrapper(this)));
152
	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)));
143 156

  
144
    EmotionCallbackWrapper *emotion_cb = new EmotionCallbackWrapper(this);
145
    init_callback("defaultEmotion", LocalServer::CallbackPtr(emotion_cb));
146
    init_callback("currentEmotion", LocalServer::CallbackPtr(emotion_cb));
157
	EmotionCallbackWrapper* emotion_cb = new EmotionCallbackWrapper(this);
158
	init_callback("defaultEmotion", LocalServer::CallbackPtr(emotion_cb));
159
	init_callback("currentEmotion", LocalServer::CallbackPtr(emotion_cb));
147 160
}
148 161

  
149
//call a tts system to convert a string to an utterance
150
boost::shared_ptr<Utterance> MiddlewareRSB::tts_call(string text){
151
    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
        RemoteServer::DataFuture<rst::audition::Utterance> future_ptr = tts_server->callAsync<rst::audition::Utterance>("create_utterance", request);
159

  
160
        //try to fetch the result
161
        boost::shared_ptr<rst::audition::Utterance> utterance_ptr = future_ptr.get(tts_timeout);
162

  
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

  
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
    printf("> failed... got no utterance\n");
175
    boost::shared_ptr<Utterance> utterance(new Utterance());
176
    return utterance;
162
// call a tts system to convert a string to an utterance
163
boost::shared_ptr<Utterance> MiddlewareRSB::tts_call(string text) {
164
	double tts_timeout = 1.0; // seconds. DO NOT CHANGE THIS!
165

  
166
	// build request
167
	boost::shared_ptr<std::string> request(new string(text));
168

  
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
		boost::shared_ptr<rst::audition::Utterance> utterance_ptr = future_ptr.get(tts_timeout);
176

  
177
		// done, return utterance ptr
178
		boost::shared_ptr<Utterance> utterance(new UtteranceRSB(*(utterance_ptr.get())));
179
		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
	boost::shared_ptr<Utterance> utterance(new Utterance());
191
	return utterance;
177 192
}
178 193

  
179 194
#endif

Also available in: Unified diff