YARP
Yet Another Robot Platform
WireLink.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include <yarp/os/WireLink.h>
10 
11 #include <yarp/os/ContactStyle.h>
12 #include <yarp/os/DummyConnector.h>
13 #include <yarp/os/MessageStack.h>
14 #include <yarp/os/PortReader.h>
15 #include <yarp/os/PortWriter.h>
17 
18 using yarp::os::WireLink;
19 
20 
22 {
23 public:
25 
30  bool replies{true};
31  bool can_write{false};
32  bool can_read{false};
33 
35  const yarp::os::ContactStyle& style);
36 
37  void reset();
38 };
39 
40 
41 WireLink::Private::Private() = default;
42 
44  const yarp::os::ContactStyle& style)
45 {
46  reset();
47  this->port = &port;
48  replies = style.expectReply;
49  return true;
50 }
51 
53 {
54  reader = nullptr;
55  port = nullptr;
56  replies = true;
57  can_write = false;
58  can_read = false;
59 }
60 
61 
62 WireLink::WireLink() :
63  mPriv(new Private)
64 {
65 }
66 
68 {
69  delete mPriv;
70 }
71 
72 bool WireLink::isValid() const
73 {
74  return mPriv->port != nullptr || mPriv->reader != nullptr;
75 }
76 
78 {
79  mPriv->owner = &owner;
80  return true;
81 }
82 
84 {
86  mPriv->attach(port, style);
87  mPriv->can_write = true;
88  mPriv->can_read = false;
89  return true;
90 }
91 
93 {
94  mPriv->reset();
95  mPriv->reader = &reader;
96  mPriv->can_write = true;
97  mPriv->can_read = false;
98  return true;
99 }
100 
102 {
104  mPriv->attach(port, style);
105  port.setReader(*mPriv->owner);
106  mPriv->can_write = false;
107  mPriv->can_read = true;
108  return true;
109 }
110 
111 bool WireLink::setStreamingMode(bool streaming)
112 {
113  mPriv->replies = !streaming;
114  return true;
115 }
116 
118 {
119  if (mPriv->reader != nullptr) {
120  DummyConnector con;
121  writer.write(con.getWriter());
122  return mPriv->reader->read(con.getReader());
123  }
124  if (!isValid()) {
125  return false;
126  }
127  return mPriv->port->write(writer);
128 }
129 
131 {
132  if (mPriv->reader != nullptr) {
133  DummyConnector con;
134  writer.write(con.getWriter());
135  bool ok = mPriv->reader->read(con.getReader());
136  reader.read(con.getReader());
137  return ok;
138  }
139  if (!isValid()) {
140  return false;
141  }
142  if (!mPriv->replies) {
143  mPriv->port->write(writer);
144  return false;
145  }
146  return mPriv->port->write(writer, reader);
147 }
148 
149 bool WireLink::callback(yarp::os::PortWriter& writer, yarp::os::PortReader& reader, const std::string& tag)
150 {
151  mPriv->stack.attach(reader);
152  mPriv->stack.stack(writer, tag);
153  return true;
154 }
155 
156 bool WireLink::canWrite() const
157 {
158  return mPriv->can_write;
159 }
160 
161 bool WireLink::canRead() const
162 {
163  return mPriv->can_read;
164 }
yarp::os::DummyConnector
A dummy connection to test yarp::os::Portable implementations.
Definition: DummyConnector.h:35
MessageStack.h
yarp::os::PortReader::read
virtual bool read(ConnectionReader &reader)=0
Read this object from a network connection.
yarp::os::ContactStyle
Preferences for how to communicate with a contact.
Definition: ContactStyle.h:27
ContactStyle.h
yarp::os::UnbufferedContactable
An abstract unbuffered port.
Definition: UnbufferedContactable.h:22
yarp::os::DummyConnector::getReader
ConnectionReader & getReader(ConnectionWriter *replyWriter=nullptr)
Get the dummy ConnectionReader loaded with whatever was written the ConnectionWriter since it was las...
Definition: DummyConnector.cpp:117
yarp::os::PortWriter
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:27
PortWriter.h
yarp::os::ContactStyle::expectReply
bool expectReply
Specify whether you expect a reply to a message.
Definition: ContactStyle.h:61
yarp::os::PortReader
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
DummyConnector.h
yarp::os::DummyConnector::getWriter
ConnectionWriter & getWriter()
Get the dummy ConnectionWriter loaded with whatever was written the ConnectionWriter since it was las...
Definition: DummyConnector.cpp:112
yarp::os::Contactable::setReader
virtual void setReader(PortReader &reader)=0
Set an external reader for port data.
UnbufferedContactable.h
yarp::os::PortWriter::write
virtual bool write(ConnectionWriter &writer) const =0
Write this object to a network connection.
PortReader.h
yarp::os::MessageStack
Maintain a stack of messages to send asynchronously.
Definition: MessageStack.h:23