iCub-main
Loading...
Searching...
No Matches
IRpcServer.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2006-2019 Istituto Italiano di Tecnologia (IIT)
3 * All rights reserved.
4 *
5 * This software may be modified and distributed under the terms of the
6 * BSD-3-Clause license. See the accompanying LICENSE file for details.
7 */
8
9// Autogenerated by Thrift Compiler (0.12.0-yarped)
10//
11// This is an automatically generated file.
12// It could get re-generated if the ALLOW_IDL_GENERATION flag is on.
13
14#include <IRpcServer.h>
15
16#include <yarp/os/idl/WireTypes.h>
17
19 public yarp::os::Portable
20{
21public:
23 bool write(yarp::os::ConnectionWriter& connection) const override;
24 bool read(yarp::os::ConnectionReader& connection) override;
25
26 thread_local static std::int32_t s_return_helper;
27};
28
29thread_local std::int32_t IRpcServer_get_answer_helper::s_return_helper = {};
30
35
36bool IRpcServer_get_answer_helper::write(yarp::os::ConnectionWriter& connection) const
37{
38 yarp::os::idl::WireWriter writer(connection);
39 if (!writer.writeListHeader(2)) {
40 return false;
41 }
42 if (!writer.writeTag("get_answer", 1, 2)) {
43 return false;
44 }
45 return true;
46}
47
48bool IRpcServer_get_answer_helper::read(yarp::os::ConnectionReader& connection)
49{
50 yarp::os::idl::WireReader reader(connection);
51 if (!reader.readListReturn()) {
52 return false;
53 }
54 if (!reader.readI32(s_return_helper)) {
55 reader.fail();
56 return false;
57 }
58 return true;
59}
60
62 public yarp::os::Portable
63{
64public:
65 explicit IRpcServer_set_answer_helper(const std::int32_t rightAnswer);
66 bool write(yarp::os::ConnectionWriter& connection) const override;
67 bool read(yarp::os::ConnectionReader& connection) override;
68
69 std::int32_t m_rightAnswer;
70
71 thread_local static bool s_return_helper;
72};
73
75
77 m_rightAnswer{rightAnswer}
78{
79 s_return_helper = {};
80}
81
82bool IRpcServer_set_answer_helper::write(yarp::os::ConnectionWriter& connection) const
83{
84 yarp::os::idl::WireWriter writer(connection);
85 if (!writer.writeListHeader(3)) {
86 return false;
87 }
88 if (!writer.writeTag("set_answer", 1, 2)) {
89 return false;
90 }
91 if (!writer.writeI32(m_rightAnswer)) {
92 return false;
93 }
94 return true;
95}
96
97bool IRpcServer_set_answer_helper::read(yarp::os::ConnectionReader& connection)
98{
99 yarp::os::idl::WireReader reader(connection);
100 if (!reader.readListReturn()) {
101 return false;
102 }
103 if (!reader.readBool(s_return_helper)) {
104 reader.fail();
105 return false;
106 }
107 return true;
108}
109
111 public yarp::os::Portable
112{
113public:
114 explicit IRpcServer_add_int_helper(const std::int32_t x);
115 bool write(yarp::os::ConnectionWriter& connection) const override;
116 bool read(yarp::os::ConnectionReader& connection) override;
117
118 std::int32_t m_x;
119
120 thread_local static std::int32_t s_return_helper;
121};
122
123thread_local std::int32_t IRpcServer_add_int_helper::s_return_helper = {};
124
126 m_x{x}
127{
128 s_return_helper = {};
129}
130
131bool IRpcServer_add_int_helper::write(yarp::os::ConnectionWriter& connection) const
132{
133 yarp::os::idl::WireWriter writer(connection);
134 if (!writer.writeListHeader(3)) {
135 return false;
136 }
137 if (!writer.writeTag("add_int", 1, 2)) {
138 return false;
139 }
140 if (!writer.writeI32(m_x)) {
141 return false;
142 }
143 return true;
144}
145
146bool IRpcServer_add_int_helper::read(yarp::os::ConnectionReader& connection)
147{
148 yarp::os::idl::WireReader reader(connection);
149 if (!reader.readListReturn()) {
150 return false;
151 }
152 if (!reader.readI32(s_return_helper)) {
153 reader.fail();
154 return false;
155 }
156 return true;
157}
158
160 public yarp::os::Portable
161{
162public:
163 explicit IRpcServer_start_helper();
164 bool write(yarp::os::ConnectionWriter& connection) const override;
165 bool read(yarp::os::ConnectionReader& connection) override;
166
167 thread_local static bool s_return_helper;
168};
169
171
176
177bool IRpcServer_start_helper::write(yarp::os::ConnectionWriter& connection) const
178{
179 yarp::os::idl::WireWriter writer(connection);
180 if (!writer.writeListHeader(1)) {
181 return false;
182 }
183 if (!writer.writeTag("start", 1, 1)) {
184 return false;
185 }
186 return true;
187}
188
189bool IRpcServer_start_helper::read(yarp::os::ConnectionReader& connection)
190{
191 yarp::os::idl::WireReader reader(connection);
192 if (!reader.readListReturn()) {
193 return false;
194 }
195 if (!reader.readBool(s_return_helper)) {
196 reader.fail();
197 return false;
198 }
199 return true;
200}
201
203 public yarp::os::Portable
204{
205public:
206 explicit IRpcServer_stop_helper();
207 bool write(yarp::os::ConnectionWriter& connection) const override;
208 bool read(yarp::os::ConnectionReader& connection) override;
209
210 thread_local static bool s_return_helper;
211};
212
214
219
220bool IRpcServer_stop_helper::write(yarp::os::ConnectionWriter& connection) const
221{
222 yarp::os::idl::WireWriter writer(connection);
223 if (!writer.writeListHeader(1)) {
224 return false;
225 }
226 if (!writer.writeTag("stop", 1, 1)) {
227 return false;
228 }
229 return true;
230}
231
232bool IRpcServer_stop_helper::read(yarp::os::ConnectionReader& connection)
233{
234 yarp::os::idl::WireReader reader(connection);
235 if (!reader.readListReturn()) {
236 return false;
237 }
238 if (!reader.readBool(s_return_helper)) {
239 reader.fail();
240 return false;
241 }
242 return true;
243}
244
246 public yarp::os::Portable
247{
248public:
250 bool write(yarp::os::ConnectionWriter& connection) const override;
251 bool read(yarp::os::ConnectionReader& connection) override;
252
253 thread_local static bool s_return_helper;
254};
255
257
262
263bool IRpcServer_is_running_helper::write(yarp::os::ConnectionWriter& connection) const
264{
265 yarp::os::idl::WireWriter writer(connection);
266 if (!writer.writeListHeader(2)) {
267 return false;
268 }
269 if (!writer.writeTag("is_running", 1, 2)) {
270 return false;
271 }
272 return true;
273}
274
275bool IRpcServer_is_running_helper::read(yarp::os::ConnectionReader& connection)
276{
277 yarp::os::idl::WireReader reader(connection);
278 if (!reader.readListReturn()) {
279 return false;
280 }
281 if (!reader.readBool(s_return_helper)) {
282 reader.fail();
283 return false;
284 }
285 return true;
286}
287
288// Constructor
290{
291 yarp().setOwner(*this);
292}
293
295{
297 if (!yarp().canWrite()) {
298 yError("Missing server method '%s'?", "std::int32_t IRpcServer::get_answer()");
299 }
300 bool ok = yarp().write(helper, helper);
301 return ok ? IRpcServer_get_answer_helper::s_return_helper : std::int32_t{};
302}
303
304bool IRpcServer::set_answer(const std::int32_t rightAnswer)
305{
306 IRpcServer_set_answer_helper helper{rightAnswer};
307 if (!yarp().canWrite()) {
308 yError("Missing server method '%s'?", "bool IRpcServer::set_answer(const std::int32_t rightAnswer)");
309 }
310 bool ok = yarp().write(helper, helper);
312}
313
314std::int32_t IRpcServer::add_int(const std::int32_t x)
315{
317 if (!yarp().canWrite()) {
318 yError("Missing server method '%s'?", "std::int32_t IRpcServer::add_int(const std::int32_t x)");
319 }
320 bool ok = yarp().write(helper, helper);
321 return ok ? IRpcServer_add_int_helper::s_return_helper : std::int32_t{};
322}
323
325{
327 if (!yarp().canWrite()) {
328 yError("Missing server method '%s'?", "bool IRpcServer::start()");
329 }
330 bool ok = yarp().write(helper, helper);
332}
333
335{
336 IRpcServer_stop_helper helper{};
337 if (!yarp().canWrite()) {
338 yError("Missing server method '%s'?", "bool IRpcServer::stop()");
339 }
340 bool ok = yarp().write(helper, helper);
342}
343
345{
347 if (!yarp().canWrite()) {
348 yError("Missing server method '%s'?", "bool IRpcServer::is_running()");
349 }
350 bool ok = yarp().write(helper, helper);
352}
353
354// help method
355std::vector<std::string> IRpcServer::help(const std::string& functionName)
356{
357 bool showAll = (functionName == "--all");
358 std::vector<std::string> helpString;
359 if (showAll) {
360 helpString.emplace_back("*** Available commands:");
361 helpString.emplace_back("get_answer");
362 helpString.emplace_back("set_answer");
363 helpString.emplace_back("add_int");
364 helpString.emplace_back("start");
365 helpString.emplace_back("stop");
366 helpString.emplace_back("is_running");
367 helpString.emplace_back("help");
368 } else {
369 if (functionName == "get_answer") {
370 helpString.emplace_back("std::int32_t get_answer() ");
371 helpString.emplace_back("Get answer from server ");
372 helpString.emplace_back("@return the answer ");
373 }
374 if (functionName == "set_answer") {
375 helpString.emplace_back("bool set_answer(const std::int32_t rightAnswer) ");
376 helpString.emplace_back("Set value for future answers. ");
377 helpString.emplace_back("@param rightAnswer new answer ");
378 helpString.emplace_back("@return true if connection was successful ");
379 }
380 if (functionName == "add_int") {
381 helpString.emplace_back("std::int32_t add_int(const std::int32_t x) ");
382 helpString.emplace_back("Add one integer to future answers. ");
383 helpString.emplace_back("@param x value to add ");
384 helpString.emplace_back("@return new value ");
385 }
386 if (functionName == "start") {
387 helpString.emplace_back("bool start() ");
388 helpString.emplace_back("Start service ");
389 helpString.emplace_back("@return true if service started correctly ");
390 }
391 if (functionName == "stop") {
392 helpString.emplace_back("bool stop() ");
393 helpString.emplace_back("Stop service ");
394 helpString.emplace_back("@return true if service stopped correctly ");
395 }
396 if (functionName == "is_running") {
397 helpString.emplace_back("bool is_running() ");
398 helpString.emplace_back("Check is service is running ");
399 helpString.emplace_back("@return true/false if service is/is not running ");
400 }
401 if (functionName == "help") {
402 helpString.emplace_back("std::vector<std::string> help(const std::string& functionName = \"--all\")");
403 helpString.emplace_back("Return list of available commands, or help message for a specific function");
404 helpString.emplace_back("@param functionName name of command for which to get a detailed description. If none or '--all' is provided, print list of available commands");
405 helpString.emplace_back("@return list of strings (one string per line)");
406 }
407 }
408 if (helpString.empty()) {
409 helpString.emplace_back("Command not found");
410 }
411 return helpString;
412}
413
414// read from ConnectionReader
415bool IRpcServer::read(yarp::os::ConnectionReader& connection)
416{
417 yarp::os::idl::WireReader reader(connection);
418 reader.expectAccept();
419 if (!reader.readListHeader()) {
420 reader.fail();
421 return false;
422 }
423
424 std::string tag = reader.readTag();
425 bool direct = (tag == "__direct__");
426 if (direct) {
427 tag = reader.readTag();
428 }
429 while (!reader.isError()) {
430 if (tag == "get_answer") {
432 yarp::os::idl::WireWriter writer(reader);
433 if (!writer.isNull()) {
434 if (!writer.writeListHeader(1)) {
435 return false;
436 }
437 if (!writer.writeI32(IRpcServer_get_answer_helper::s_return_helper)) {
438 return false;
439 }
440 }
441 reader.accept();
442 return true;
443 }
444 if (tag == "set_answer") {
445 std::int32_t rightAnswer;
446 if (!reader.readI32(rightAnswer)) {
447 reader.fail();
448 return false;
449 }
451 yarp::os::idl::WireWriter writer(reader);
452 if (!writer.isNull()) {
453 if (!writer.writeListHeader(1)) {
454 return false;
455 }
456 if (!writer.writeBool(IRpcServer_set_answer_helper::s_return_helper)) {
457 return false;
458 }
459 }
460 reader.accept();
461 return true;
462 }
463 if (tag == "add_int") {
464 std::int32_t x;
465 if (!reader.readI32(x)) {
466 reader.fail();
467 return false;
468 }
470 yarp::os::idl::WireWriter writer(reader);
471 if (!writer.isNull()) {
472 if (!writer.writeListHeader(1)) {
473 return false;
474 }
475 if (!writer.writeI32(IRpcServer_add_int_helper::s_return_helper)) {
476 return false;
477 }
478 }
479 reader.accept();
480 return true;
481 }
482 if (tag == "start") {
484 yarp::os::idl::WireWriter writer(reader);
485 if (!writer.isNull()) {
486 if (!writer.writeListHeader(1)) {
487 return false;
488 }
489 if (!writer.writeBool(IRpcServer_start_helper::s_return_helper)) {
490 return false;
491 }
492 }
493 reader.accept();
494 return true;
495 }
496 if (tag == "stop") {
498 yarp::os::idl::WireWriter writer(reader);
499 if (!writer.isNull()) {
500 if (!writer.writeListHeader(1)) {
501 return false;
502 }
503 if (!writer.writeBool(IRpcServer_stop_helper::s_return_helper)) {
504 return false;
505 }
506 }
507 reader.accept();
508 return true;
509 }
510 if (tag == "is_running") {
512 yarp::os::idl::WireWriter writer(reader);
513 if (!writer.isNull()) {
514 if (!writer.writeListHeader(1)) {
515 return false;
516 }
517 if (!writer.writeBool(IRpcServer_is_running_helper::s_return_helper)) {
518 return false;
519 }
520 }
521 reader.accept();
522 return true;
523 }
524 if (tag == "help") {
525 std::string functionName;
526 if (!reader.readString(functionName)) {
527 functionName = "--all";
528 }
529 auto help_strings = help(functionName);
530 yarp::os::idl::WireWriter writer(reader);
531 if (!writer.isNull()) {
532 if (!writer.writeListHeader(2)) {
533 return false;
534 }
535 if (!writer.writeTag("many", 1, 0)) {
536 return false;
537 }
538 if (!writer.writeListBegin(BOTTLE_TAG_INT32, static_cast<uint32_t>(help_strings.size()))) {
539 return false;
540 }
541 for (const auto& help_string : help_strings) {
542 if (!writer.writeString(help_string)) {
543 return false;
544 }
545 }
546 if (!writer.writeListEnd()) {
547 return false;
548 }
549 }
550 reader.accept();
551 return true;
552 }
553 if (reader.noMore()) {
554 reader.fail();
555 return false;
556 }
557 std::string next_tag = reader.readTag();
558 if (next_tag == "") {
559 break;
560 }
561 tag.append("_").append(next_tag);
562 }
563 return false;
564}
IRpcServer_add_int_helper(const std::int32_t x)
bool write(yarp::os::ConnectionWriter &connection) const override
bool read(yarp::os::ConnectionReader &connection) override
static thread_local std::int32_t s_return_helper
bool read(yarp::os::ConnectionReader &connection) override
bool write(yarp::os::ConnectionWriter &connection) const override
static thread_local std::int32_t s_return_helper
bool read(yarp::os::ConnectionReader &connection) override
bool write(yarp::os::ConnectionWriter &connection) const override
static thread_local bool s_return_helper
IRpcServer_set_answer_helper(const std::int32_t rightAnswer)
bool write(yarp::os::ConnectionWriter &connection) const override
bool read(yarp::os::ConnectionReader &connection) override
static thread_local bool s_return_helper
bool write(yarp::os::ConnectionWriter &connection) const override
bool read(yarp::os::ConnectionReader &connection) override
static thread_local bool s_return_helper
bool read(yarp::os::ConnectionReader &connection) override
bool write(yarp::os::ConnectionWriter &connection) const override
static thread_local bool s_return_helper
virtual bool stop()
Stop service.
virtual bool set_answer(const std::int32_t rightAnswer)
Set value for future answers.
virtual std::int32_t get_answer()
Get answer from server.
virtual std::int32_t add_int(const std::int32_t x)
Add one integer to future answers.
virtual bool is_running()
Check is service is running.
virtual std::vector< std::string > help(const std::string &functionName="--all")
bool read(yarp::os::ConnectionReader &connection) override
virtual bool start()
Start service.
Copyright (C) 2008 RobotCub Consortium.