YARP
Yet Another Robot Platform
NameServerConnectionHandler.h
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 
7 #ifndef YARPDB_NAMESERVERCONNECTIONHANDLER_INC
8 #define YARPDB_NAMESERVERCONNECTIONHANDLER_INC
9 
10 #include <yarp/name/api.h>
11 #include <yarp/os/PortReader.h>
14 #include <yarp/os/Bottle.h>
15 #include <yarp/os/Contact.h>
16 #include <yarp/os/Time.h>
17 #include <yarp/os/Value.h>
18 
19 #include <yarp/name/NameService.h>
20 
21 #include <cstdio>
22 #include <string>
23 #include <algorithm>
24 
25 namespace yarp {
26  namespace name {
27  class NameServerConnectionHandler;
28  }
29 }
30 
31 
37 {
38 private:
39  NameService *service;
40 public:
42  {
43  this->service = service;
44  }
45 
46  bool read(yarp::os::ConnectionReader& reader) override
47  {
48  return apply(reader, nullptr);
49  }
50 
51  virtual bool apply(yarp::os::ConnectionReader& reader,
53  bool lock = true)
54  {
55  yarp::os::Bottle cmd;
56  yarp::os::Bottle reply;
57  yarp::os::Bottle event;
58  bool ok = cmd.read(reader);
59  if (!ok) {
60  return false;
61  }
62  yarp::os::Contact remote;
63  remote = reader.getRemoteContact();
64  if (lock) {
65  service->lock();
66  }
67  service->apply(cmd,reply,event,remote);
68  for (size_t i=0; i<event.size(); i++) {
69  yarp::os::Bottle *e = event.get(i).asList();
70  if (e != nullptr) {
71  service->onEvent(*e);
72  }
73  }
74  if (lock) {
75  service->unlock();
76  }
77  if (writer == nullptr) {
78  writer = reader.getWriter();
79  }
80  if (writer != nullptr) {
81  //printf("sending reply %s\n", reply.toString().c_str());
82  if (reply.get(0).toString()=="old") {
83  // support old name server messages
84  for (size_t i=1; i<reply.size(); i++) {
85  yarp::os::Value& v = reply.get(i);
86  if (v.isList()) {
87  // old name server messages don't have quotes,
88  // so we strip them.
89  std::string si = v.asList()->toString();
90  si.erase(std::remove(si.begin(), si.end(), '\"'), si.end());
91  if (si.length()>0) {
92  writer->appendText(si);
93  }
94  } else {
95  if (v.isString()) {
96  writer->appendText(v.asString());
97  } else {
99  b.add(v);
100  b.write(*writer);
101  }
102  }
103  }
104  writer->appendText("*** end of message");
105  } else {
106  reply.write(*writer);
107  }
108  }
109  return true;
110  }
111 };
112 
113 
114 #endif
Manage a single connection to the name server.
bool read(yarp::os::ConnectionReader &reader) override
Read this object from a network connection.
virtual bool apply(yarp::os::ConnectionReader &reader, yarp::os::ConnectionWriter *writer, bool lock=true)
Abstract interface for a name server operator.
Definition: NameService.h:36
virtual void onEvent(yarp::os::Bottle &event)
Definition: NameService.h:45
virtual void lock()
Definition: NameService.h:47
virtual void unlock()
Definition: NameService.h:48
virtual bool apply(yarp::os::Bottle &cmd, yarp::os::Bottle &reply, yarp::os::Bottle &event, const yarp::os::Contact &remote)=0
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
Definition: Bottle.cpp:336
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:251
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition: Bottle.cpp:240
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:246
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition: Bottle.cpp:230
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:211
An interface for reading from a network connection.
virtual Contact getRemoteContact() const =0
Gets information about who is supplying the data being read, if that information is available.
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
An interface for writing to a network connection.
virtual void appendText(const std::string &str, const char terminate='\n')=0
Send a terminated string to the network connection.
Represents how to reach a part of a YARP network.
Definition: Contact.h:36
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:25
A single value (typically within a Bottle).
Definition: Value.h:45
virtual bool isString() const
Checks if value is a string.
Definition: Value.cpp:156
virtual bool isList() const
Checks if value is a list.
Definition: Value.cpp:162
virtual Bottle * asList() const
Get list value.
Definition: Value.cpp:240
std::string toString() const override
Return a standard text representation of the content of the object.
Definition: Value.cpp:356
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
The main, catch-all namespace for YARP.
Definition: dirs.h:16