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
20
21#include <cstdio>
22#include <string>
23#include <algorithm>
24
25namespace yarp::name {
26class NameServerConnectionHandler;
27}
28
29
35{
36private:
37 NameService *service;
38public:
40 {
41 this->service = service;
42 }
43
44 bool read(yarp::os::ConnectionReader& reader) override
45 {
46 return apply(reader, nullptr);
47 }
48
49 virtual bool apply(yarp::os::ConnectionReader& reader,
51 bool lock = true)
52 {
54 yarp::os::Bottle reply;
55 yarp::os::Bottle event;
56 bool ok = cmd.read(reader);
57 if (!ok) {
58 return false;
59 }
60 yarp::os::Contact remote;
61 remote = reader.getRemoteContact();
62 if (lock) {
63 service->lock();
64 }
65 service->apply(cmd,reply,event,remote);
66 for (size_t i=0; i<event.size(); i++) {
67 yarp::os::Bottle *e = event.get(i).asList();
68 if (e != nullptr) {
69 service->onEvent(*e);
70 }
71 }
72 if (lock) {
73 service->unlock();
74 }
75 if (writer == nullptr) {
76 writer = reader.getWriter();
77 }
78 if (writer != nullptr) {
79 //printf("sending reply %s\n", reply.toString().c_str());
80 if (reply.get(0).toString()=="old") {
81 // support old name server messages
82 for (size_t i=1; i<reply.size(); i++) {
83 yarp::os::Value& v = reply.get(i);
84 if (v.isList()) {
85 // old name server messages don't have quotes,
86 // so we strip them.
87 std::string si = v.asList()->toString();
88 si.erase(std::remove(si.begin(), si.end(), '\"'), si.end());
89 if (si.length()>0) {
90 writer->appendText(si);
91 }
92 } else {
93 if (v.isString()) {
94 writer->appendText(v.asString());
95 } else {
97 b.add(v);
98 b.write(*writer);
99 }
100 }
101 }
102 writer->appendText("*** end of message");
103 } else {
104 reply.write(*writer);
105 }
106 }
107 return true;
108 }
109};
110
111
112#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:24
virtual void onEvent(yarp::os::Bottle &event)
Definition: NameService.h:33
virtual void lock()
Definition: NameService.h:35
virtual void unlock()
Definition: NameService.h:36
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:64
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 ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
virtual Contact getRemoteContact() const =0
Gets information about who is supplying the data being read, if that information is available.
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:33
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:24
A single value (typically within a Bottle).
Definition: Value.h:43
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