YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
PortMonitor.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef PORTMONITOR_INC
7#define PORTMONITOR_INC
8
11#include <yarp/os/Election.h>
13#include <yarp/os/Things.h>
14
15#include "MonitorBinding.h"
16#include "MonitorEvent.h"
17
18#include <mutex>
19#include <memory>
20
21class PortMonitor;
22
27 public yarp::os::PeerRecord<PortMonitor>
28{
29public:
30 virtual ~PortMonitorGroup() {}
31 virtual bool acceptIncomingData(PortMonitor *source);
32};
33
34
35
52{
53
54public:
55 PortMonitor() = default;
56 PortMonitor(const PortMonitor&) = delete;
60
61 ~PortMonitor() override
62 {
63 if (!portName.empty()) {
64 getPeers().remove(portName,this);
65 }
66 for (auto it=binder.begin(); it!=binder.end(); ++it)
67 {
68 if (*it) {
69 delete *it;
70 *it = nullptr;
71 }
72 }
73 binder.clear();
74 }
75
76 void close() override
77 {
78 bReady = false;
79 if (!portName.empty()) {
80 getPeers().remove(portName, this);
81 portName.clear();
82 }
83 for (auto it = binder.begin(); it != binder.end(); ++it)
84 {
85 if (*it) {
86 delete *it;
87 *it = nullptr;
88 }
89 }
90 binder.clear();
91 }
92
93 Carrier *create() const override
94 {
95 return new PortMonitor();
96 }
97
98 std::string getName() const override
99 {
100 return "portmonitor";
101 }
102
103 std::string toString() const override
104 {
105 return "portmonitor_carrier";
106 }
107
108 bool configure(yarp::os::ConnectionState& proto) override;
109 bool configureFromProperty(yarp::os::Property& options) override;
110
111 //bool modifiesIncomingData() override;
112 bool acceptIncomingData(yarp::os::ConnectionReader& reader) override;
113
115
116 const yarp::os::PortWriter& modifyOutgoingData(const yarp::os::PortWriter& writer) override;
117
118 bool acceptOutgoingData(const yarp::os::PortWriter& writer) override;
119
121
122 void setCarrierParams(const yarp::os::Property& params) override;
123
124 void getCarrierParams(yarp::os::Property& params) const override;
125
126
127 void lock() const { mutex.lock(); }
128 void unlock() const { mutex.unlock(); }
129
131 {
132 if(!bReady) {
133 return nullptr;
134 }
135 if (binder.empty()) {
136 return nullptr;
137 }
138 return binder[0];
139 }
140
141public:
142 std::string portName;
143 std::string sourceName;
144
145private:
146 static std::unique_ptr<yarp::os::ElectionOf<PortMonitorGroup>> peers;
148
149
150private:
151 bool bReady {false};
153 yarp::os::ConnectionReader* localReader {nullptr};
154 yarp::os::Things thing;
155 std::vector<MonitorBinding*> binder;
156 PortMonitorGroup *group {nullptr};
157 mutable std::mutex mutex;
158};
159
160#endif //PORTMONITOR_INC
Manager for arbitration-aware inputs to a given port.
Definition PortMonitor.h:28
virtual ~PortMonitorGroup()
Definition PortMonitor.h:30
virtual bool acceptIncomingData(PortMonitor *source)
Allow to monitor and modify port data from Lua script Under development.
Definition PortMonitor.h:52
MonitorBinding * getBinder()
~PortMonitor() override
Definition PortMonitor.h:61
void lock() const
PortMonitor & operator=(const PortMonitor &)=delete
void getCarrierParams(yarp::os::Property &params) const override
Get carrier configuration and deliver it by port administrative commands.
yarp::os::ConnectionReader & modifyIncomingData(yarp::os::ConnectionReader &reader) override
Modify incoming payload data, if appropriate.
void unlock() const
const yarp::os::PortWriter & modifyOutgoingData(const yarp::os::PortWriter &writer) override
Modify outgoing payload data, if appropriate.
PortMonitor(const PortMonitor &)=delete
Carrier * create() const override
Factory method.
Definition PortMonitor.h:93
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
Definition PortMonitor.h:98
std::string toString() const override
Get name of carrier.
PortMonitor & operator=(PortMonitor &&)=delete
PortMonitor(PortMonitor &&)=delete
yarp::os::PortReader & modifyReply(yarp::os::PortReader &reader) override
Modify reply payload data, if appropriate.
void setCarrierParams(const yarp::os::Property &params) override
Configure carrier from port administrative commands.
bool configureFromProperty(yarp::os::Property &options) override
void close() override
Close the carrier.
Definition PortMonitor.h:76
bool acceptIncomingData(yarp::os::ConnectionReader &reader) override
Determine whether incoming data should be accepted.
std::string sourceName
bool acceptOutgoingData(const yarp::os::PortWriter &writer) override
Determine whether outgoing data should be accepted.
std::string portName
bool configure(yarp::os::ConnectionState &proto) override
Class PortMonitor.
PortMonitor()=default
A mini-server for performing network communication in the background.
An interface for reading from a network connection.
The basic state of a connection - route, streams in use, etc.
A dummy connection to test yarp::os::Portable implementations.
void remove(const std::string &key, typename PR::peer_type *entity)
Definition Election.h:98
A starter class for implementing simple modifying carriers.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition PortReader.h:24
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition PortWriter.h:23
A class for storing options and configuration information.
Definition Property.h:33
Base class for generic things.
Definition Things.h:18