YARP
Yet Another Robot Platform
WireLink.cpp
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 #include <yarp/os/WireLink.h>
7 
8 #include <yarp/os/ContactStyle.h>
10 #include <yarp/os/MessageStack.h>
11 #include <yarp/os/PortReader.h>
12 #include <yarp/os/PortWriter.h>
14 
15 using yarp::os::WireLink;
16 
17 
19 {
20 public:
22 
27  bool replies{true};
28  bool can_write{false};
29  bool can_read{false};
30 
32  const yarp::os::ContactStyle& style);
33 
34  void reset();
35 };
36 
37 
38 WireLink::Private::Private() = default;
39 
41  const yarp::os::ContactStyle& style)
42 {
43  reset();
44  this->port = &port;
45  replies = style.expectReply;
46  return true;
47 }
48 
50 {
51  reader = nullptr;
52  port = nullptr;
53  replies = true;
54  can_write = false;
55  can_read = false;
56 }
57 
58 
59 WireLink::WireLink() :
60  mPriv(new Private)
61 {
62 }
63 
65 {
66  delete mPriv;
67 }
68 
69 bool WireLink::isValid() const
70 {
71  return mPriv->port != nullptr || mPriv->reader != nullptr;
72 }
73 
75 {
76  mPriv->owner = &owner;
77  return true;
78 }
79 
81 {
83  mPriv->attach(port, style);
84  mPriv->can_write = true;
85  mPriv->can_read = false;
86  return true;
87 }
88 
90 {
91  mPriv->reset();
92  mPriv->reader = &reader;
93  mPriv->can_write = true;
94  mPriv->can_read = false;
95  return true;
96 }
97 
99 {
101  mPriv->attach(port, style);
102  port.setReader(*mPriv->owner);
103  mPriv->can_write = false;
104  mPriv->can_read = true;
105  return true;
106 }
107 
108 bool WireLink::setStreamingMode(bool streaming)
109 {
110  mPriv->replies = !streaming;
111  return true;
112 }
113 
115 {
116  if (mPriv->reader != nullptr) {
117  DummyConnector con;
118  writer.write(con.getWriter());
119  return mPriv->reader->read(con.getReader());
120  }
121  if (!isValid()) {
122  return false;
123  }
124  return mPriv->port->write(writer);
125 }
126 
128 {
129  if (mPriv->reader != nullptr) {
130  DummyConnector con;
131  writer.write(con.getWriter());
132  bool ok = mPriv->reader->read(con.getReader());
133  reader.read(con.getReader());
134  return ok;
135  }
136  if (!isValid()) {
137  return false;
138  }
139  if (!mPriv->replies) {
140  mPriv->port->write(writer);
141  return false;
142  }
143  return mPriv->port->write(writer, reader);
144 }
145 
146 bool WireLink::callback(yarp::os::PortWriter& writer, yarp::os::PortReader& reader, const std::string& tag)
147 {
148  mPriv->stack.attach(reader);
149  mPriv->stack.stack(writer, tag);
150  return true;
151 }
152 
153 bool WireLink::canWrite() const
154 {
155  return mPriv->can_write;
156 }
157 
158 bool WireLink::canRead() const
159 {
160  return mPriv->can_read;
161 }
Preferences for how to communicate with a contact.
Definition: ContactStyle.h:24
bool expectReply
Specify whether you expect a reply to a message.
Definition: ContactStyle.h:58
virtual void setReader(PortReader &reader)=0
Set an external reader for port data.
A dummy connection to test yarp::os::Portable implementations.
ConnectionWriter & getWriter()
Get the dummy ConnectionWriter loaded with whatever was written the ConnectionWriter since it was las...
ConnectionReader & getReader(ConnectionWriter *replyWriter=nullptr)
Get the dummy ConnectionReader loaded with whatever was written the ConnectionWriter since it was las...
Maintain a stack of messages to send asynchronously.
Definition: MessageStack.h:20
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:25
virtual bool read(ConnectionReader &reader)=0
Read this object from a network connection.
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:24
virtual bool write(ConnectionWriter &writer) const =0
Write this object to a network connection.
An abstract unbuffered port.