YARP
Yet Another Robot Platform
Port.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <yarp/os/Port.h>
8 
9 #include <yarp/conf/system.h>
10 #include <yarp/conf/environment.h>
11 
12 #include <yarp/os/Bottle.h>
13 #include <yarp/os/Contact.h>
14 #include <yarp/os/Network.h>
15 #include <yarp/os/Portable.h>
16 #include <yarp/os/Time.h>
20 #include <yarp/os/impl/PortCore.h>
22 
23 using namespace yarp::os::impl;
24 using namespace yarp::os;
25 
26 namespace {
27 YARP_OS_LOG_COMPONENT(PORT, "yarp.os.Port")
28 } // namespace
29 
30 void* Port::needImplementation() const
31 {
32  if (implementation != nullptr) {
33  return implementation;
34  }
35  Port* self = const_cast<Port*>(this);
36  self->implementation = new yarp::os::impl::PortCoreAdapter(*self);
37  yCAssert(PORT, self->implementation != nullptr);
38  self->owned = true;
39  return self->implementation;
40 }
41 
42 // implementation is a PortCoreAdapter
43 #define IMPL() (*reinterpret_cast<yarp::os::impl::PortCoreAdapter*>(needImplementation()))
44 
46  implementation(nullptr),
47  owned(false)
48 {
49 }
50 
52 {
53  if (implementation != nullptr) {
54  close();
55  if (owned) {
56  delete (static_cast<PortCoreAdapter*>(implementation));
57  }
58  implementation = nullptr;
59  owned = false;
60  }
61 }
62 
64 {
65  close();
66  if (owned) {
67  delete (static_cast<PortCoreAdapter*>(implementation));
68  }
69  implementation = port.implementation;
70  owned = false;
71  return true;
72 }
73 
74 bool Port::openFake(const std::string& name)
75 {
76  return open(Contact(name), false, name.c_str());
77 }
78 
79 bool Port::open(const std::string& name)
80 {
81  return open(Contact(name));
82 }
83 
84 bool Port::open(const Contact& contact, bool registerName)
85 {
86  return open(contact, registerName, nullptr);
87 }
88 
89 bool Port::open(const Contact& contact, bool registerName, const char* fakeName)
90 {
91  Contact contact2 = contact;
92 
93  if (!NetworkBase::initialized()) {
94  yCError(PORT, "YARP not initialized; create a yarp::os::Network object before using ports");
95  return false;
96  }
97 
98  std::string n = contact2.getName();
99 
100  NameConfig conf;
101  std::string nenv = std::string("YARP_RENAME") + conf.getSafeString(n);
102  std::string rename = yarp::conf::environment::get_string(nenv);
103  if (!rename.empty()) {
104  n = rename;
105  contact2.setName(n);
106  }
107 
108  bool local = false;
109  if (n.empty() && contact2.getPort() <= 0) {
110  local = true;
111  registerName = false;
112  n = "...";
113  }
114 
115  NestedContact nc(n);
116  if (!nc.getNestedName().empty()) {
117  if (nc.getNodeName().empty()) {
118  Nodes& nodes = NameClient::getNameClient().getNodes();
119  nodes.requireActiveName();
120  std::string node_name = nodes.getActiveName();
121  if (!node_name.empty()) {
122  n = n + node_name;
123  }
124  }
125  }
126 
127  PortCoreAdapter* currentCore = &(IMPL());
128  if (currentCore != nullptr) {
129  currentCore->active = false;
130  if (!n.empty() && (n[0] != '/' || currentCore->includeNode) && n[0] != '=' && n != "..." && n.substr(0, 3) != "...") {
131  if (fakeName == nullptr) {
132  Nodes& nodes = NameClient::getNameClient().getNodes();
133  std::string node_name = nodes.getActiveName();
134  if (!node_name.empty()) {
135  n = (n[0] == '/' ? "" : "/") + n + "@" + node_name;
136  }
137  }
138  }
139  }
140  if (!n.empty() && n[0] != '/' && n[0] != '=' && n != "..." && n.substr(0, 3) != "...") {
141  if (fakeName == nullptr) {
142  yCError(PORT, "Port name '%s' needs to start with a '/' character", n.c_str());
143  return false;
144  }
145  }
146  if (!n.empty() && n != "..." && n[0] != '=' && n.substr(0, 3) != "...") {
147  if (fakeName == nullptr) {
148  std::string prefix = yarp::conf::environment::get_string("YARP_PORT_PREFIX");
149  if (!prefix.empty()) {
150  n = prefix + n;
151  contact2.setName(n);
152  }
153  }
154  }
155  if (currentCore != nullptr) {
156  NestedContact nc;
157  nc.fromString(n);
158  if (!nc.getNestedName().empty()) {
159  if (nc.getCategory().empty()) {
160  // we need to add in a category
161  std::string cat;
162  if (currentCore->commitToRead) {
163  cat = "-";
164  } else if (currentCore->commitToWrite) {
165  cat = "+";
166  }
167  if (!cat.empty()) {
168  if (currentCore->commitToRpc) {
169  cat += "1";
170  }
171  contact2.setName(nc.getNestedName() + cat + "@" + nc.getNodeName());
172  } else {
173  yCError(PORT, "Error: Port '%s' is not committed to being either an input or output port.", n.c_str());
174  yCError(PORT, "YARP does not mind, but we are trying to register with a name server that does.");
175  yCError(PORT, "You can call Port::setWriteOnly() or Port::setReadOnly(), OR rename the port.");
176  NestedContact nc2 = nc;
177  nc2.setCategoryWrite();
178  yCError(PORT, "For an output port, call it: %s (+ adds data)", nc2.toString().c_str());
179  nc2.setCategoryRead();
180  yCError(PORT, "For an input port, call it: %s (- takes data)", nc2.toString().c_str());
181  return false;
182  }
183  }
184  }
185  }
186 
187  // Allow for open() to be called safely many times on the same Port
188  if ((currentCore != nullptr) && currentCore->isOpened()) {
189  auto* newCore = new PortCoreAdapter(*this);
190  yCAssert(PORT, newCore != nullptr);
191  // copy state that should survive in a new open()
192  if (currentCore->checkPortReader() != nullptr) {
193  newCore->configReader(*(currentCore->checkPortReader()));
194  }
195  if (currentCore->checkAdminPortReader() != nullptr) {
196  newCore->configAdminReader(*(currentCore->checkAdminPortReader()));
197  }
198  if (currentCore->checkReadCreator() != nullptr) {
199  newCore->configReadCreator(*(currentCore->checkReadCreator()));
200  }
201  if (currentCore->checkWaitAfterSend() >= 0) {
202  newCore->configWaitAfterSend(currentCore->checkWaitAfterSend() != 0);
203  }
204  if (currentCore->haveCallbackLock) {
205  newCore->configCallbackLock(currentCore->recCallbackLock);
206  }
207  close();
208  if (owned) {
209  delete (static_cast<PortCoreAdapter*>(implementation));
210  }
211  implementation = newCore;
212  owned = true;
213  currentCore = newCore;
214  currentCore->active = false;
215  }
216 
217  PortCoreAdapter& core = IMPL();
218 
219  core.openable();
220 
221  if (NetworkBase::localNetworkAllocation() && contact2.getPort() <= 0) {
222  yCDebug(PORT, "local network allocation needed");
223  local = true;
224  }
225 
226  bool success = true;
227  Contact address(contact2.getName(),
228  contact2.getCarrier(),
229  contact2.getHost(),
230  contact2.getPort());
231  address.setNestedContact(contact2.getNested());
232 
233  core.setReadHandler(core);
234  if (contact2.getPort() > 0 && !contact2.getHost().empty()) {
235  registerName = false;
236  }
237 
238  std::string ntyp = getType().getNameOnWire();
239  if (ntyp.empty()) {
240  NestedContact nc;
241  nc.fromString(n);
242  if (!nc.getTypeName().empty()) {
243  ntyp = nc.getTypeName();
244  }
245  }
246  if (ntyp.empty()) {
247  ntyp = getType().getName();
248  }
249  if (!ntyp.empty()) {
250  NestedContact nc;
251  nc.fromString(contact2.getName());
252  nc.setTypeName(ntyp);
253  contact2.setNestedContact(nc);
254  if (getType().getNameOnWire() != ntyp) {
255  core.promiseType(Type::byNameOnWire(ntyp.c_str()));
256  }
257  }
258 
259  if (registerName && !local) {
260  address = NetworkBase::registerContact(contact2);
261  }
262 
263  core.setControlRegistration(registerName);
264  success = (address.isValid() || local) && (fakeName == nullptr);
265 
266  if (success) {
267  // create a node if needed
268  Nodes& nodes = NameClient::getNameClient().getNodes();
269  nodes.prepare(address.getRegName());
270  }
271 
272  // If we are a service client, go ahead and connect
273  if (success) {
274  NestedContact nc;
275  nc.fromString(address.getName());
276  if (!nc.getNestedName().empty()) {
277  if (nc.getCategory() == "+1") {
278  addOutput(nc.getNestedName());
279  }
280  }
281  }
282 
283  std::string blame = "invalid address";
284  if (success) {
285  success = core.listen(address, registerName);
286  blame = "address conflict";
287  if (success) {
288  success = core.start();
289  blame = "manager did not start";
290  }
291  }
292  if (success) {
293  address = core.getAddress();
294  if (registerName && local) {
295  contact2.setSocket(address.getCarrier(),
296  address.getHost(),
297  address.getPort());
298  contact2.setName(address.getRegName());
299  Contact newName = NetworkBase::registerContact(contact2);
300  core.resetPortName(newName.getName());
301  address = core.getAddress();
302  } else if (core.getAddress().getRegName().empty() && !registerName) {
303  core.resetPortName(core.getAddress().toURI(false));
304  core.setName(core.getAddress().getRegName());
305  }
306 
307  if (address.getRegName().empty()) {
308  yCInfo(PORT,
309  "Anonymous port active at %s",
310  address.toURI().c_str());
311  } else {
312  yCInfo(PORT,
313  "Port %s active at %s",
314  address.getRegName().c_str(),
315  address.toURI().c_str());
316  }
317  }
318 
319  if (fakeName != nullptr) {
320  success = core.manualStart(fakeName);
321  blame = "unmanaged port failed to start";
322  }
323 
324  if (!success) {
325  yCError(PORT, "Port %s failed to activate%s%s (%s)",
326  (address.isValid() ? (address.getRegName().c_str()) : (contact2.getName().c_str())),
327  (address.isValid() ? " at " : ""),
328  (address.isValid() ? address.toURI().c_str() : ""),
329  blame.c_str());
330  }
331 
332  if (success) {
333  // create a node if needed
334  Nodes& nodes = NameClient::getNameClient().getNodes();
335  nodes.add(*this);
336  }
337 
338  if (success && currentCore != nullptr) {
339  currentCore->active = true;
340  }
341  return success;
342 }
343 
344 bool Port::addOutput(const std::string& name)
345 {
346  return addOutput(Contact(name));
347 }
348 
349 bool Port::addOutput(const std::string& name, const std::string& carrier)
350 {
351  return addOutput(Contact(name, carrier));
352 }
353 
355 {
356  if (!owned) {
357  return;
358  }
359 
360  Nodes& nodes = NameClient::getNameClient().getNodes();
361  nodes.remove(*this);
362 
363  PortCoreAdapter& core = IMPL();
364  core.finishReading();
365  core.finishWriting();
366  core.close();
367  core.join();
368  core.active = false;
369 
370  // In fact, open flag means "ever opened", so don't reset it
371  // core.setOpened(false);
372 }
373 
375 {
376  Nodes& nodes = NameClient::getNameClient().getNodes();
377  nodes.remove(*this);
378 
379  PortCoreAdapter& core = IMPL();
380  core.interrupt();
381 }
382 
384 {
385  PortCoreAdapter& core = IMPL();
386  if (!core.isInterrupted()) {
387  // prevent resuming when the port is not interrupted
388  return;
389  }
390  core.resumeFull();
391  Nodes& nodes = NameClient::getNameClient().getNodes();
392  nodes.add(*this);
393 }
394 
395 
397 {
398  PortCoreAdapter& core = IMPL();
399  return core.getAddress();
400 }
401 
402 
403 bool Port::addOutput(const Contact& contact)
404 {
405  PortCoreAdapter& core = IMPL();
406  if (core.commitToRead) {
407  return false;
408  }
409  if (core.isInterrupted()) {
410  return false;
411  }
412  core.alertOnWrite();
413  std::string name;
414  if (contact.getPort() <= 0) {
415  name = contact.toString();
416  } else {
417  name = contact.toURI();
418  }
419  if (!core.isListening()) {
420  return core.addOutput(name, nullptr, nullptr, true);
421  }
422  Contact me = where();
423  return NetworkBase::connect(me.getName(), name);
424 }
425 
426 
427 bool Port::write(const PortWriter& writer, const PortWriter* callback) const
428 {
429  PortCoreAdapter& core = IMPL();
430  if (core.isInterrupted()) {
431  return false;
432  }
433  core.alertOnWrite();
434  bool result = false;
435  //WritableAdapter adapter(writer);
436  result = core.send(writer, nullptr, callback);
437  //writer.onCompletion();
438  if (!result) {
439  if (callback != nullptr) {
440  callback->onCompletion();
441  } else {
442  writer.onCompletion();
443  }
444  // leave result false
445  }
446  return result;
447 }
448 
449 bool Port::write(const PortWriter& writer,
450  PortReader& reader,
451  const PortWriter* callback) const
452 {
453  PortCoreAdapter& core = IMPL();
454  if (core.isInterrupted()) {
455  return false;
456  }
457  core.alertOnRpc();
458  core.alertOnWrite();
459  bool result = false;
460  result = core.send(writer, &reader, callback);
461  if (!result) {
462  if (callback != nullptr) {
463  callback->onCompletion();
464  } else {
465  writer.onCompletion();
466  }
467  // leave result false
468  }
469  return result;
470 }
471 
472 bool Port::read(PortReader& reader, bool willReply)
473 {
474  if (!isOpen()) {
475  return false;
476  }
477  PortCoreAdapter& core = IMPL();
478  if (willReply) {
479  core.alertOnRpc();
480  }
481  core.alertOnRead();
482  if (core.isInterrupted()) {
483  return false;
484  }
485  return core.read(reader, willReply);
486 }
487 
488 
489 bool Port::reply(PortWriter& writer)
490 {
491  PortCoreAdapter& core = IMPL();
492  return core.reply(writer, false, core.isInterrupted());
493 }
494 
496 {
497  PortCoreAdapter& core = IMPL();
498  return core.reply(writer, true, core.isInterrupted());
499 }
500 
501 
503 {
504  PortCoreAdapter& core = IMPL();
505  core.alertOnRead();
506  core.configReader(reader);
507 }
508 
510 {
511  PortCoreAdapter& core = IMPL();
512  core.configAdminReader(reader);
513 }
514 
515 
517 {
518  PortCoreAdapter& core = IMPL();
519  core.alertOnRead();
520  core.configReadCreator(creator);
521 }
522 
523 
524 void Port::enableBackgroundWrite(bool backgroundFlag)
525 {
526  PortCoreAdapter& core = IMPL();
527  core.configWaitAfterSend(!backgroundFlag);
528 }
529 
530 
532 {
533  PortCoreAdapter& core = IMPL();
534  return core.isWriting();
535 }
536 
537 
539 {
540  PortCoreAdapter& core = IMPL();
541  return core.setEnvelope(envelope);
542 }
543 
544 
546 {
547  PortCoreAdapter& core = IMPL();
548  return core.getEnvelope(envelope);
549 }
550 
552 {
553  PortCoreAdapter& core = IMPL();
554  core.alertOnRead();
555  return core.getInputCount();
556 }
557 
559 {
560  PortCoreAdapter& core = IMPL();
561  core.alertOnWrite();
562  return core.getOutputCount();
563 }
564 
566 {
567  PortCoreAdapter& core = IMPL();
568  core.describe(reporter);
569 }
570 
571 
573 {
574  PortCoreAdapter& core = IMPL();
575  core.setReportCallback(&reporter);
576 }
577 
578 
580 {
581  PortCoreAdapter& core = IMPL();
582  core.resetReportCallback();
583 }
584 
585 
586 void Port::setAdminMode(bool adminMode)
587 {
588  if (adminMode) {
589  Bottle b("__ADMIN");
590  setEnvelope(b);
591  } else {
592  Bottle b;
593  setEnvelope(b);
594  }
595 }
596 
597 
598 #define SET_FLAG(implementation, mask, val) \
599  IMPL().setFlags((IMPL().getFlags() & (~(mask))) + ((val) ? (mask) : 0))
600 
601 void Port::setInputMode(bool expectInput)
602 {
603  if (!expectInput) {
604  IMPL().setWriteOnly();
605  }
606  SET_FLAG(implementation, PORTCORE_IS_INPUT, expectInput);
607 }
608 
609 void Port::setOutputMode(bool expectOutput)
610 {
611  if (!expectOutput) {
612  IMPL().setReadOnly();
613  }
614  SET_FLAG(implementation, PORTCORE_IS_OUTPUT, expectOutput);
615 }
616 
617 void Port::setRpcMode(bool expectRpc)
618 {
619  if (expectRpc) {
620  IMPL().setRpc();
621  }
622  SET_FLAG(implementation, PORTCORE_IS_RPC, expectRpc);
623 }
624 
625 bool Port::setTimeout(float timeout)
626 {
627  IMPL().setTimeout(timeout);
628  return true;
629 }
630 
631 #ifndef YARP_NO_DEPRECATED // Since YARP 3.4
632 void Port::setVerbosity(int level)
633 {
634  YARP_UNUSED(level);
635 }
636 
638 {
639  return 0;
640 }
641 #endif
642 
644 {
645  return IMPL().getType();
646 }
647 
648 void Port::promiseType(const Type& typ)
649 {
650  IMPL().promiseType(typ);
651 }
652 
654 {
655  return IMPL().acquireProperties(readOnly);
656 }
657 
659 {
660  IMPL().releaseProperties(prop);
661 }
662 
663 void Port::includeNodeInName(bool flag)
664 {
665  IMPL().includeNodeInName(flag);
666 }
667 
668 bool Port::isOpen() const
669 {
670  if (implementation == nullptr) {
671  return false;
672  }
673  return IMPL().active;
674 }
675 
676 #ifndef YARP_NO_DEPRECATED // Since YARP 3.3
680 {
681  return IMPL().configCallbackLock(mutex);
682 }
684 #endif
685 
686 bool Port::setCallbackLock(std::mutex* mutex)
687 {
688  return IMPL().configCallbackLock(mutex);
689 }
690 
692 {
693  return IMPL().unconfigCallbackLock();
694 }
695 
697 {
698  if (!IMPL().lockCallback()) {
699  yCError(PORT,"Cannot do lockCallback() without setCallbackLock() before opening port");
700  }
701  return true;
702 }
703 
705 {
706  return IMPL().tryLockCallback();
707 }
708 
710 {
711  IMPL().unlockCallback();
712 }
void cat(Vector &a, const Vector &b)
#define PORTCORE_IS_INPUT
Definition: PortCore.h:48
#define PORTCORE_IS_RPC
Definition: PortCore.h:47
#define PORTCORE_IS_OUTPUT
Definition: PortCore.h:49
#define IMPL()
Definition: Port.cpp:43
#define SET_FLAG(implementation, mask, val)
Definition: Port.cpp:598
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:17
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
Represents how to reach a part of a YARP network.
Definition: Contact.h:36
const NestedContact & getNested() const
Get the NestedContact containing extra information for this Contact.
Definition: Contact.cpp:261
void setNestedContact(const yarp::os::NestedContact &nestedContact)
Sets the NestedContact containing extra information for this Contact.
Definition: Contact.cpp:266
std::string getName() const
Get the name associated with this Contact.
Definition: Contact.cpp:205
std::string getRegName() const
Get the name associated with this Contact.
Definition: Contact.cpp:217
std::string toString() const
Get a textual representation of the Contact.
Definition: Contact.cpp:303
std::string toURI(bool includeCarrier=true) const
Get a representation of the Contact as a URI.
Definition: Contact.cpp:313
int getPort() const
Get the port number associated with this Contact for socket communication.
Definition: Contact.cpp:239
void setSocket(const std::string &carrier, const std::string &hostname, int port)
Set information to a Contact about how to reach it using socket communication.
Definition: Contact.cpp:288
void setName(const std::string &name)
Set the name associated with this Contact.
Definition: Contact.cpp:222
std::string getCarrier() const
Get the carrier associated with this Contact for socket communication.
Definition: Contact.cpp:250
std::string getHost() const
Get the host name associated with this Contact for socket communication.
Definition: Contact.cpp:228
Basic wrapper for mutual exclusion.
Definition: Mutex.h:32
A placeholder for rich contact information.
Definition: NestedContact.h:24
bool fromString(const std::string &nFullName)
std::string getNodeName() const
std::string toString() const
std::string getNestedName() const
void setTypeName(const std::string &nWireType)
std::string getTypeName() const
std::string getCategory() const
static bool initialized()
Returns true if YARP has been fully initialized.
Definition: Network.cpp:1389
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
Definition: Network.cpp:682
static Contact registerContact(const Contact &contact)
Register contact information with the name server.
Definition: Network.cpp:1017
static bool localNetworkAllocation()
Check where the name server in use expects processes to allocate their own network resources.
Definition: Network.cpp:1953
The Nodes class.
Definition: Nodes.h:32
void remove(Contactable &contactable) override
remove a Contactable from the Node specified in the contactable's name.
Definition: Nodes.cpp:275
std::string getActiveName()
getActiveName getter for the currently active node's name
Definition: Nodes.cpp:325
bool requireActiveName()
requireActiveName if there is no active node, creates a temporary one.
Definition: Nodes.cpp:330
void add(Contactable &contactable) override
add a Contactable to the Node specified in the contactable name (see NestedContact....
Definition: Nodes.cpp:270
void prepare(const std::string &name)
prepare checks for the existence of the node specified in the name parameter.
Definition: Nodes.cpp:310
A creator for readers.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:25
A base class for objects that want information about port status changes.
Definition: PortReport.h:28
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:24
virtual void onCompletion() const
This is called when the port has finished all writing operations.
Definition: PortWriter.cpp:13
A mini-server for network communication.
Definition: Port.h:47
void setReaderCreator(PortReaderCreator &creator)
Set a creator for readers for port data.
Definition: Port.cpp:516
void enableBackgroundWrite(bool backgroundFlag)
control whether writing from this port is done in the background.
Definition: Port.cpp:524
int getOutputCount() override
Determine how many output connections this port has.
Definition: Port.cpp:558
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition: Port.cpp:427
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition: Port.cpp:502
Contact where() const override
Returns information about how this port can be reached.
Definition: Port.cpp:396
bool getEnvelope(PortReader &envelope) override
Get the envelope information (e.g., a timestamp) from the last message received on the port.
Definition: Port.cpp:545
Type getType() override
Get the type of data the port has committed to send/receive.
Definition: Port.cpp:643
void setRpcMode(bool expectRpc) override
Configure the port to be RPC only.
Definition: Port.cpp:617
Port()
Constructor.
Definition: Port.cpp:45
bool removeCallbackLock() override
Remove a lock on callbacks added with setCallbackLock()
Definition: Port.cpp:691
bool sharedOpen(Port &port)
Open a port wrapping an existing port.
Definition: Port.cpp:63
void setAdminMode(bool adminMode=true)
Turn on/off "admin" mode.
Definition: Port.cpp:586
void setAdminReader(PortReader &reader) override
Set an external reader for unrecognized administrative port messages.
Definition: Port.cpp:509
int getVerbosity()
Get port verbosity level.
Definition: Port.cpp:637
void resume() override
Put the port back in an operative state after interrupt() has been called.
Definition: Port.cpp:383
bool tryLockCallback() override
Try to lock callbacks until unlockCallback() is called.
Definition: Port.cpp:704
bool addOutput(const std::string &name) override
Add an output connection to the specified port.
Definition: Port.cpp:344
bool read(PortReader &reader, bool willReply=false) override
Read an object from the port.
Definition: Port.cpp:472
~Port() override
Destructor.
Definition: Port.cpp:51
bool replyAndDrop(PortWriter &writer) override
Same as reply(), but closes connection after reply.
Definition: Port.cpp:495
void setInputMode(bool expectInput) override
Configure the port to allow or forbid inputs.
Definition: Port.cpp:601
int getInputCount() override
Determine how many connections are arriving into this port.
Definition: Port.cpp:551
void setReporter(PortReport &reporter) override
Set a callback to be called upon any future connections and disconnections to/from the port.
Definition: Port.cpp:572
void setVerbosity(int level)
Set whether the port should issue messages about its operations.
Definition: Port.cpp:632
bool setTimeout(float timeout)
Set a timeout on network operations.
Definition: Port.cpp:625
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition: Port.cpp:374
bool lockCallback() override
Lock callbacks until unlockCallback() is called.
Definition: Port.cpp:696
void includeNodeInName(bool flag) override
Choose whether to prepend a node name (if one is available) to the port's name.
Definition: Port.cpp:663
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
Definition: Port.cpp:538
void releaseProperties(Property *prop) override
End access unstructured port properties.
Definition: Port.cpp:658
void getReport(PortReport &reporter) override
Get information on the state of the port - connections etc.
Definition: Port.cpp:565
bool openFake(const std::string &name)
Start port without making it accessible from the network.
Definition: Port.cpp:74
void resetReporter() override
Remove the callback which is called upon any future connections and disconnections to/from the port.
Definition: Port.cpp:579
void close() override
Stop port activity.
Definition: Port.cpp:354
void promiseType(const Type &typ) override
Commit the port to a particular type of data.
Definition: Port.cpp:648
bool reply(PortWriter &writer) override
Send an object as a reply to an object read from the port.
Definition: Port.cpp:489
bool setCallbackLock(yarp::os::Mutex *mutex) override
Add a lock to use when invoking callbacks.
Definition: Port.cpp:679
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:79
bool isWriting() override
Report whether the port is currently writing data.
Definition: Port.cpp:531
Property * acquireProperties(bool readOnly) override
Access unstructured port properties.
Definition: Port.cpp:653
void setOutputMode(bool expectOutput) override
Configure the port to allow or forbid outputs.
Definition: Port.cpp:609
bool isOpen() const
Check if the port has been opened.
Definition: Port.cpp:668
void unlockCallback() override
Unlock callbacks.
Definition: Port.cpp:709
A class for storing options and configuration information.
Definition: Property.h:34
static Type byNameOnWire(const char *name_on_wire)
Definition: Type.cpp:186
std::string getNameOnWire() const
Definition: Type.cpp:144
std::string getName() const
Definition: Type.cpp:139
Small helper class to help deal with legacy YARP configuration files.
Definition: NameConfig.h:25
std::string getSafeString(const std::string &txt)
Definition: NameConfig.cpp:109
PortReaderCreator * checkReadCreator()
void configWaitAfterSend(bool waitAfterSend)
void configReadCreator(PortReaderCreator &creator)
void configAdminReader(PortReader &reader)
bool reply(PortWriter &writer, bool drop, bool interrupted)
void configReader(PortReader &reader)
bool read(ConnectionReader &reader) override
Callback for data.
void resetPortName(const std::string &str)
Definition: PortCore.cpp:2991
std::string getEnvelope()
Definition: PortCore.cpp:1471
int getOutputCount()
Check how many output connections there are.
Definition: PortCore.cpp:1423
void setReportCallback(yarp::os::PortReport *reporter)
Set a callback to be notified of changes in port status.
Definition: PortCore.cpp:1171
bool start() override
Begin main thread.
Definition: PortCore.cpp:277
void interrupt()
Prepare the port to be shut down.
Definition: PortCore.cpp:331
void setEnvelope(const std::string &envelope)
Set some envelope information to pass along with a message without actually being part of the message...
Definition: PortCore.cpp:1457
bool isInterrupted() const
Definition: PortCore.cpp:3016
bool listen(const Contact &address, bool shouldAnnounce=true)
Begin service at a given address.
Definition: PortCore.cpp:68
bool isListening() const
Definition: PortCore.cpp:3006
void setControlRegistration(bool flag)
Normally the port will unregister its name with the name server when shutting down.
Definition: PortCore.cpp:3001
int getInputCount()
Check how many input connections there are.
Definition: PortCore.cpp:1414
bool manualStart(const char *sourceName)
Start up the port, but without a main thread.
Definition: PortCore.cpp:308
void setReadHandler(yarp::os::PortReader &reader)
Set a callback for incoming data.
Definition: PortCore.cpp:140
void promiseType(const Type &typ)
Definition: PortCore.cpp:3142
void close() override
Shut down port.
Definition: PortCore.cpp:264
const Contact & getAddress() const
Get the address associated with the port.
Definition: PortCore.cpp:2986
void describe(void *id, yarp::os::OutputStream *os)
Produce a text description of the port and its connections.
Definition: PortCore.cpp:1048
bool isWriting()
Check if a message is currently being sent.
Definition: PortCore.cpp:1395
bool send(const yarp::os::PortWriter &writer, yarp::os::PortReader *reader=nullptr, const yarp::os::PortWriter *callback=nullptr)
Send a normal message.
Definition: PortCore.cpp:1248
void resetReportCallback()
Reset the callback to be notified of changes in port status.
Definition: PortCore.cpp:1179
void setName(const std::string &name)
Set the name of this port.
Definition: PortCore.cpp:2966
bool addOutput(const std::string &dest, void *id, yarp::os::OutputStream *os, bool onlyIfNeeded=false)
Add an output connection to this port.
Definition: PortCore.cpp:842
int join(double seconds=-1)
Definition: ThreadImpl.cpp:120
#define yCInfo(component,...)
Definition: LogComponent.h:132
#define yCError(component,...)
Definition: LogComponent.h:154
#define yCAssert(component, x)
Definition: LogComponent.h:169
#define yCDebug(component,...)
Definition: LogComponent.h:109
#define YARP_OS_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:35
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:68
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
int rename(const char *oldname, const char *newname)
Portable wrapper for the rename() function.
Definition: Os.cpp:80
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:332
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:331
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:333
#define YARP_UNUSED(var)
Definition: api.h:162