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