30 void* Port::needImplementation()
const
35 Port*
self =
const_cast<Port*
>(
this);
37 yCAssert(PORT, self->implementation !=
nullptr);
39 return self->implementation;
43 #define IMPL() (*reinterpret_cast<yarp::os::impl::PortCoreAdapter*>(needImplementation()))
53 if (implementation !=
nullptr) {
58 implementation =
nullptr;
69 implementation = port.implementation;
86 return open(contact, registerName,
nullptr);
94 yCError(PORT,
"YARP not initialized; create a yarp::os::Network object before using ports");
98 std::string n = contact2.
getName();
101 std::string nenv = std::string(
"YARP_RENAME") + conf.
getSafeString(n);
109 if (n.empty() && contact2.
getPort() <= 0) {
111 registerName =
false;
116 if (!nc.getNestedName().empty()) {
117 if (nc.getNodeName().empty()) {
118 Nodes& nodes = NameClient::getNameClient().getNodes();
121 if (!node_name.empty()) {
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();
134 if (!node_name.empty()) {
135 n = (n[0] ==
'/' ?
"" :
"/") + n +
"@" + node_name;
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());
146 if (!n.empty() && n !=
"..." && n[0] !=
'=' && n.substr(0, 3) !=
"...") {
147 if (fakeName ==
nullptr) {
149 if (!prefix.empty()) {
155 if (currentCore !=
nullptr) {
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.");
178 yCError(PORT,
"For an output port, call it: %s (+ adds data)", nc2.
toString().c_str());
180 yCError(PORT,
"For an input port, call it: %s (- takes data)", nc2.
toString().c_str());
188 if ((currentCore !=
nullptr) && currentCore->
isOpened()) {
211 implementation = newCore;
213 currentCore = newCore;
214 currentCore->
active =
false;
222 yCDebug(PORT,
"local network allocation needed");
231 address.setNestedContact(contact2.
getNested());
235 registerName =
false;
254 if (
getType().getNameOnWire() != ntyp) {
259 if (registerName && !local) {
264 success = (address.isValid() ||
local) && (fakeName ==
nullptr);
268 Nodes& nodes = NameClient::getNameClient().getNodes();
269 nodes.
prepare(address.getRegName());
283 std::string blame =
"invalid address";
285 success = core.
listen(address, registerName);
286 blame =
"address conflict";
288 success = core.
start();
289 blame =
"manager did not start";
294 if (registerName && local) {
298 contact2.
setName(address.getRegName());
307 if (address.getRegName().empty()) {
309 "Anonymous port active at %s",
310 address.toURI().c_str());
313 "Port %s active at %s",
314 address.getRegName().c_str(),
315 address.toURI().c_str());
319 if (fakeName !=
nullptr) {
321 blame =
"unmanaged port failed to start";
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() :
""),
334 Nodes& nodes = NameClient::getNameClient().getNodes();
338 if (success && currentCore !=
nullptr) {
339 currentCore->
active =
true;
360 Nodes& nodes = NameClient::getNameClient().getNodes();
376 Nodes& nodes = NameClient::getNameClient().getNodes();
391 Nodes& nodes = NameClient::getNameClient().getNodes();
417 name = contact.
toURI();
420 return core.
addOutput(name,
nullptr,
nullptr,
true);
436 result = core.
send(writer,
nullptr, callback);
439 if (callback !=
nullptr) {
460 result = core.
send(writer, &reader, callback);
462 if (callback !=
nullptr) {
485 return core.
read(reader, willReply);
598 #define SET_FLAG(implementation, mask, val) \
599 IMPL().setFlags((IMPL().getFlags() & (~(mask))) + ((val) ? (mask) : 0))
604 IMPL().setWriteOnly();
612 IMPL().setReadOnly();
627 IMPL().setTimeout(timeout);
631 #ifndef YARP_NO_DEPRECATED
645 return IMPL().getType();
650 IMPL().promiseType(typ);
655 return IMPL().acquireProperties(readOnly);
660 IMPL().releaseProperties(prop);
665 IMPL().includeNodeInName(flag);
670 if (implementation ==
nullptr) {
673 return IMPL().active;
676 #ifndef YARP_NO_DEPRECATED
681 return IMPL().configCallbackLock(mutex);
688 return IMPL().configCallbackLock(mutex);
693 return IMPL().unconfigCallbackLock();
699 yCError(PORT,
"Cannot do lockCallback() without setCallbackLock() before opening port");
706 return IMPL().tryLockCallback();
711 IMPL().unlockCallback();
void cat(Vector &a, const Vector &b)
#define PORTCORE_IS_INPUT
#define PORTCORE_IS_OUTPUT
#define SET_FLAG(implementation, mask, val)
RandScalar * implementation(void *t)
A simple collection of objects that can be described and transmitted in a portable way.
Basic wrapper for mutual exclusion.
static bool initialized()
Returns true if YARP has been fully initialized.
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.
static Contact registerContact(const Contact &contact)
Register contact information with the name server.
static bool localNetworkAllocation()
Check where the name server in use expects processes to allocate their own network resources.
void remove(Contactable &contactable) override
remove a Contactable from the Node specified in the contactable's name.
std::string getActiveName()
getActiveName getter for the currently active node's name
bool requireActiveName()
requireActiveName if there is no active node, creates a temporary one.
void add(Contactable &contactable) override
add a Contactable to the Node specified in the contactable name (see NestedContact....
void prepare(const std::string &name)
prepare checks for the existence of the node specified in the name parameter.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
A base class for objects that want information about port status changes.
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
virtual void onCompletion() const
This is called when the port has finished all writing operations.
A mini-server for network communication.
void setReaderCreator(PortReaderCreator &creator)
Set a creator for readers for port data.
void enableBackgroundWrite(bool backgroundFlag)
control whether writing from this port is done in the background.
int getOutputCount() override
Determine how many output connections this port has.
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
void setReader(PortReader &reader) override
Set an external reader for port data.
Contact where() const override
Returns information about how this port can be reached.
bool getEnvelope(PortReader &envelope) override
Get the envelope information (e.g., a timestamp) from the last message received on the port.
Type getType() override
Get the type of data the port has committed to send/receive.
void setRpcMode(bool expectRpc) override
Configure the port to be RPC only.
bool removeCallbackLock() override
Remove a lock on callbacks added with setCallbackLock()
bool sharedOpen(Port &port)
Open a port wrapping an existing port.
void setAdminMode(bool adminMode=true)
Turn on/off "admin" mode.
void setAdminReader(PortReader &reader) override
Set an external reader for unrecognized administrative port messages.
int getVerbosity()
Get port verbosity level.
void resume() override
Put the port back in an operative state after interrupt() has been called.
bool tryLockCallback() override
Try to lock callbacks until unlockCallback() is called.
bool addOutput(const std::string &name) override
Add an output connection to the specified port.
bool read(PortReader &reader, bool willReply=false) override
Read an object from the port.
~Port() override
Destructor.
bool replyAndDrop(PortWriter &writer) override
Same as reply(), but closes connection after reply.
void setInputMode(bool expectInput) override
Configure the port to allow or forbid inputs.
int getInputCount() override
Determine how many connections are arriving into this port.
void setReporter(PortReport &reporter) override
Set a callback to be called upon any future connections and disconnections to/from the port.
void setVerbosity(int level)
Set whether the port should issue messages about its operations.
bool setTimeout(float timeout)
Set a timeout on network operations.
void interrupt() override
Interrupt any current reads or writes attached to the port.
bool lockCallback() override
Lock callbacks until unlockCallback() is called.
void includeNodeInName(bool flag) override
Choose whether to prepend a node name (if one is available) to the port's name.
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
void releaseProperties(Property *prop) override
End access unstructured port properties.
void getReport(PortReport &reporter) override
Get information on the state of the port - connections etc.
bool openFake(const std::string &name)
Start port without making it accessible from the network.
void resetReporter() override
Remove the callback which is called upon any future connections and disconnections to/from the port.
void close() override
Stop port activity.
void promiseType(const Type &typ) override
Commit the port to a particular type of data.
bool reply(PortWriter &writer) override
Send an object as a reply to an object read from the port.
bool setCallbackLock(yarp::os::Mutex *mutex) override
Add a lock to use when invoking callbacks.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
bool isWriting() override
Report whether the port is currently writing data.
Property * acquireProperties(bool readOnly) override
Access unstructured port properties.
void setOutputMode(bool expectOutput) override
Configure the port to allow or forbid outputs.
bool isOpen() const
Check if the port has been opened.
void unlockCallback() override
Unlock callbacks.
A class for storing options and configuration information.
static Type byNameOnWire(const char *name_on_wire)
std::string getNameOnWire() const
std::string getName() const
Small helper class to help deal with legacy YARP configuration files.
std::string getSafeString(const std::string &txt)
PortReaderCreator * checkReadCreator()
void configWaitAfterSend(bool waitAfterSend)
void configReadCreator(PortReaderCreator &creator)
void configAdminReader(PortReader &reader)
bool reply(PortWriter &writer, bool drop, bool interrupted)
PortReader * checkAdminPortReader()
std::mutex * recCallbackLock
PortReader * checkPortReader()
void configReader(PortReader &reader)
bool read(ConnectionReader &reader) override
Callback for data.
void resetPortName(const std::string &str)
std::string getEnvelope()
int getOutputCount()
Check how many output connections there are.
void setReportCallback(yarp::os::PortReport *reporter)
Set a callback to be notified of changes in port status.
bool start() override
Begin main thread.
void interrupt()
Prepare the port to be shut down.
void setEnvelope(const std::string &envelope)
Set some envelope information to pass along with a message without actually being part of the message...
bool isInterrupted() const
bool listen(const Contact &address, bool shouldAnnounce=true)
Begin service at a given address.
void setControlRegistration(bool flag)
Normally the port will unregister its name with the name server when shutting down.
int getInputCount()
Check how many input connections there are.
bool manualStart(const char *sourceName)
Start up the port, but without a main thread.
void setReadHandler(yarp::os::PortReader &reader)
Set a callback for incoming data.
void promiseType(const Type &typ)
void close() override
Shut down port.
const Contact & getAddress() const
Get the address associated with the port.
void describe(void *id, yarp::os::OutputStream *os)
Produce a text description of the port and its connections.
bool isWriting()
Check if a message is currently being sent.
bool send(const yarp::os::PortWriter &writer, yarp::os::PortReader *reader=nullptr, const yarp::os::PortWriter *callback=nullptr)
Send a normal message.
void resetReportCallback()
Reset the callback to be notified of changes in port status.
void setName(const std::string &name)
Set the name of this port.
bool addOutput(const std::string &dest, void *id, yarp::os::OutputStream *os, bool onlyIfNeeded=false)
Add an output connection to this port.
int join(double seconds=-1)
#define yCInfo(component,...)
#define yCError(component,...)
#define yCAssert(component, x)
#define yCDebug(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
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.
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.