YARP
Yet Another Robot Platform
Subscriber.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
8
10
12
13namespace {
14YARP_SERVERSQL_LOG_COMPONENT(SUBSCRIBER, "yarp.serversql.impl.Subscriber")
15} // namespace
16
17bool Subscriber::apply(yarp::os::Bottle& cmd,
18 yarp::os::Bottle& reply,
19 yarp::os::Bottle& event,
20 const yarp::os::Contact& remote)
21{
22 YARP_UNUSED(event);
23 YARP_UNUSED(remote);
24
25 std::string tag = cmd.get(0).asString();
26 bool ok = false;
27 if (tag == "subscribe" ||
28 tag == "unsubscribe" ||
29 tag == "announce" ||
30 tag == "topic" ||
31 tag == "untopic" ||
32 tag == "type") {
33 yCInfo(SUBSCRIBER, "-> %s", cmd.toString().c_str());
34 }
35 if (tag == "subscribe") {
36 std::string src = cmd.get(1).asString();
37 std::string dest = cmd.get(2).asString();
38 std::string mode = cmd.get(3).asString();
39 if (!dest.empty()) {
40 ok = addSubscription(src, dest, mode);
41 reply.clear();
42 reply.addVocab32(ok ? yarp::os::createVocab32('o', 'k')
43 : yarp::os::createVocab32('f', 'a', 'i', 'l'));
44 return ok;
45 }
46
47 // list subscriptions
48 listSubscriptions(src, reply);
49 return true;
50 }
51 if (tag == "unsubscribe") {
52 ok = removeSubscription(cmd.get(1).asString(),
53 cmd.get(2).asString());
54 reply.clear();
55 reply.addVocab32(ok ? yarp::os::createVocab32('o', 'k')
56 : yarp::os::createVocab32('f', 'a', 'i', 'l'));
57 return ok;
58 }
59 if (tag == "announce") {
60 if (cmd.get(2).isInt32()) {
61 welcome(cmd.get(1).asString(), cmd.get(2).asInt32() ? 1 : 0);
62 } else {
63 welcome(cmd.get(1).asString(), true);
64 }
65 reply.clear();
66 reply.addVocab32(yarp::os::createVocab32('o', 'k'));
67 return true;
68 }
69 if (tag == "topic") {
70 if (cmd.size() >= 2) {
71 bool result = setTopic(cmd.get(1).asString(),
72 cmd.get(2).asString(),
73 true);
74 reply.clear();
75 reply.addVocab32(replyCode(result));
76 return true;
77 }
78
79 reply.clear();
80 listTopics(reply);
81 return true;
82 }
83 if (tag == "type") {
84 if (cmd.size() == 4) {
85 bool result = setType(cmd.get(1).asString(),
86 cmd.get(2).asString(),
87 cmd.get(3).asString());
88 reply.clear();
89 reply.addVocab32(replyCode(result));
90 return true;
91 }
92 if (cmd.size() == 3) {
93 std::string result =
94 getType(cmd.get(1).asString(),
95 cmd.get(2).asString());
96 reply.clear();
97 if (result.empty()) {
98 reply.addVocab32(replyCode(false));
99 } else {
100 reply.addString(cmd.get(0).asString());
101 reply.addString(cmd.get(1).asString());
102 reply.addString(cmd.get(2).asString());
103 reply.addString(result);
104 }
105 return true;
106 }
107 reply.clear();
108 reply.addVocab32(replyCode(false));
109 return true;
110 }
111 if (tag == "untopic") {
112 bool result = setTopic(cmd.get(1).asString(), "", false);
113 reply.clear();
114 reply.addVocab32(replyCode(result));
115 return true;
116 }
117 return ok;
118}
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:64
void addVocab32(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:164
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:251
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:246
void clear()
Empties the bottle of any objects it contains.
Definition: Bottle.cpp:121
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:170
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:211
Represents how to reach a part of a YARP network.
Definition: Contact.h:33
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:204
virtual bool isInt32() const
Checks if value is a 32-bit integer.
Definition: Value.cpp:132
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
Abstract interface for maintaining persistent connections.
Definition: Subscriber.h:26
virtual bool listTopics(yarp::os::Bottle &topics)=0
virtual bool listSubscriptions(const std::string &src, yarp::os::Bottle &reply)=0
virtual bool setTopic(const std::string &port, const std::string &structure, bool active)=0
virtual bool removeSubscription(const std::string &src, const std::string &dest)=0
virtual bool addSubscription(const std::string &src, const std::string &dest, const std::string &mode)=0
virtual std::string getType(const std::string &family, const std::string &structure)=0
virtual bool welcome(const std::string &port, int activity)=0
virtual bool setType(const std::string &family, const std::string &structure, const std::string &value)=0
#define yCInfo(component,...)
Definition: LogComponent.h:171
#define YARP_SERVERSQL_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:29
constexpr yarp::conf::vocab32_t createVocab32(char a, char b=0, char c=0, char d=0)
Create a vocab from chars.
Definition: Vocab.h:27
#define YARP_UNUSED(var)
Definition: api.h:162