YARP
Yet Another Robot Platform
NameServerManager.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_NAMESERVERMANAGER_INC
11 #define YARPDB_NAMESERVERMANAGER_INC
12 
14 
16 #include <mutex>
17 #include <yarp/os/Port.h>
18 
19 
20 namespace yarp {
21  namespace name {
22  class NameServerManager;
23  }
24 }
25 
33 private:
34  NameService& ns;
35  yarp::os::Port *port;
36  std::mutex mutex;
37 public:
39  yarp::os::Port *port = NULL) : ns(ns),
40  port(port), mutex() {
41  }
42 
43  void setPort(yarp::os::Port& port) {
44  this->port = &port;
45  }
46 
47  void onEvent(yarp::os::Bottle& event) override {
48  ns.onEvent(event);
49  if (port!=NULL) {
50  port->write(event);
51  }
52  }
53 
54  yarp::os::PortReader *create() const override {
55  return new NameServerConnectionHandler(const_cast<NameServerManager*>(this));
56  }
57 
58  void lock() override {
59  mutex.lock();
60  }
61 
62  void unlock() override {
63  mutex.unlock();
64  }
65 
66  virtual bool apply(yarp::os::Bottle& cmd,
67  yarp::os::Bottle& reply,
68  yarp::os::Bottle& event,
69  const yarp::os::Contact& remote) override {
70  bool ok = ns.apply(cmd,reply,event,remote);
71  return ok;
72  }
73 };
74 
75 #endif
Manage a single connection to the name server.
Manage the name server.
void setPort(yarp::os::Port &port)
void onEvent(yarp::os::Bottle &event) override
virtual bool apply(yarp::os::Bottle &cmd, yarp::os::Bottle &reply, yarp::os::Bottle &event, const yarp::os::Contact &remote) override
yarp::os::PortReader * create() const override
Factory for PortReader objects.
NameServerManager(NameService &ns, yarp::os::Port *port=NULL)
Abstract interface for a name server operator.
Definition: NameService.h:39
virtual void onEvent(yarp::os::Bottle &event)
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:76
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
A creator for readers.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
A mini-server for network communication.
Definition: Port.h:50
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition: Port.cpp:430
The main, catch-all namespace for YARP.
Definition: environment.h:25