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