YARP
Yet Another Robot Platform
NameServerConnectionHandler.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 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
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:39
virtual void onEvent(yarp::os::Bottle &event)
Definition: NameService.h:48
virtual void lock()
Definition: NameService.h:50
virtual void unlock()
Definition: NameService.h:51
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:76
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
Definition: Bottle.cpp:339
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:254
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition: Bottle.cpp:243
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition: Bottle.cpp:233
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:214
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:39
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
A single value (typically within a Bottle).
Definition: Value.h:47
virtual bool isString() const
Checks if value is a string.
Definition: Value.cpp:159
virtual bool isList() const
Checks if value is a list.
Definition: Value.cpp:165
virtual Bottle * asList() const
Get list value.
Definition: Value.cpp:243
std::string toString() const override
Return a standard text representation of the content of the object.
Definition: Value.cpp:359
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
The main, catch-all namespace for YARP.
Definition: environment.h:25