|
YARP
Yet Another Robot Platform
|
|
Go to the documentation of this file.
33 void* Port::needImplementation()
const
38 Port*
self =
const_cast<Port*
>(
this);
40 yCAssert(PORT, self->implementation !=
nullptr);
42 return self->implementation;
46 #define IMPL() (*reinterpret_cast<yarp::os::impl::PortCoreAdapter*>(needImplementation()))
56 if (implementation !=
nullptr) {
61 implementation =
nullptr;
72 implementation = port.implementation;
89 return open(contact, registerName,
nullptr);
97 yCError(PORT,
"YARP not initialized; create a yarp::os::Network object before using ports");
101 std::string n = contact2.
getName();
104 std::string nenv = std::string(
"YARP_RENAME") + conf.
getSafeString(n);
112 if (n.empty() && contact2.
getPort() <= 0) {
114 registerName =
false;
119 if (!nc.getNestedName().empty()) {
120 if (nc.getNodeName().empty()) {
121 Nodes& nodes = NameClient::getNameClient().getNodes();
124 if (!node_name.empty()) {
131 if (currentCore !=
nullptr) {
132 currentCore->
active =
false;
133 if (!n.empty() && (n[0] !=
'/' || currentCore->
includeNode) && n[0] !=
'=' && n !=
"..." && n.substr(0, 3) !=
"...") {
134 if (fakeName ==
nullptr) {
135 Nodes& nodes = NameClient::getNameClient().getNodes();
137 if (!node_name.empty()) {
138 n = (n[0] ==
'/' ?
"" :
"/") + n +
"@" + node_name;
143 if (!n.empty() && n[0] !=
'/' && n[0] !=
'=' && n !=
"..." && n.substr(0, 3) !=
"...") {
144 if (fakeName ==
nullptr) {
145 yCError(PORT,
"Port name '%s' needs to start with a '/' character", n.c_str());
149 if (!n.empty() && n !=
"..." && n[0] !=
'=' && n.substr(0, 3) !=
"...") {
150 if (fakeName ==
nullptr) {
152 if (!prefix.empty()) {
158 if (currentCore !=
nullptr) {
176 yCError(PORT,
"Error: Port '%s' is not committed to being either an input or output port.", n.c_str());
177 yCError(PORT,
"YARP does not mind, but we are trying to register with a name server that does.");
178 yCError(PORT,
"You can call Port::setWriteOnly() or Port::setReadOnly(), OR rename the port.");
181 yCError(PORT,
"For an output port, call it: %s (+ adds data)", nc2.
toString().c_str());
183 yCError(PORT,
"For an input port, call it: %s (- takes data)", nc2.
toString().c_str());
191 if ((currentCore !=
nullptr) && currentCore->
isOpened()) {
214 implementation = newCore;
216 currentCore = newCore;
217 currentCore->
active =
false;
225 yCDebug(PORT,
"local network allocation needed");
234 address.setNestedContact(contact2.
getNested());
238 registerName =
false;
257 if (
getType().getNameOnWire() != ntyp) {
262 if (registerName && !local) {
267 success = (address.isValid() || local) && (fakeName ==
nullptr);
271 Nodes& nodes = NameClient::getNameClient().getNodes();
272 nodes.
prepare(address.getRegName());
286 std::string blame =
"invalid address";
288 success = core.
listen(address, registerName);
289 blame =
"address conflict";
291 success = core.
start();
292 blame =
"manager did not start";
297 if (registerName && local) {
301 contact2.
setName(address.getRegName());
310 if (address.getRegName().empty()) {
312 "Anonymous port active at %s",
313 address.toURI().c_str());
316 "Port %s active at %s",
317 address.getRegName().c_str(),
318 address.toURI().c_str());
322 if (fakeName !=
nullptr) {
324 blame =
"unmanaged port failed to start";
328 yCError(PORT,
"Port %s failed to activate%s%s (%s)",
329 (address.isValid() ? (address.getRegName().c_str()) : (contact2.
getName().c_str())),
330 (address.isValid() ?
" at " :
""),
331 (address.isValid() ? address.toURI().c_str() :
""),
337 Nodes& nodes = NameClient::getNameClient().getNodes();
341 if (success && currentCore !=
nullptr) {
342 currentCore->
active =
true;
363 Nodes& nodes = NameClient::getNameClient().getNodes();
379 Nodes& nodes = NameClient::getNameClient().getNodes();
394 Nodes& nodes = NameClient::getNameClient().getNodes();
420 name = contact.
toURI();
423 return core.
addOutput(name,
nullptr,
nullptr,
true);
439 result = core.
send(writer,
nullptr, callback);
442 if (callback !=
nullptr) {
463 result = core.
send(writer, &reader, callback);
465 if (callback !=
nullptr) {
488 return core.
read(reader, willReply);
601 #define SET_FLAG(implementation, mask, val) \
602 IMPL().setFlags((IMPL().getFlags() & (~(mask))) + ((val) ? (mask) : 0))
607 IMPL().setWriteOnly();
615 IMPL().setReadOnly();
630 IMPL().setTimeout(timeout);
634 #ifndef YARP_NO_DEPRECATED // Since YARP 3.4
648 return IMPL().getType();
653 IMPL().promiseType(typ);
658 return IMPL().acquireProperties(readOnly);
663 IMPL().releaseProperties(prop);
668 IMPL().includeNodeInName(flag);
673 if (implementation ==
nullptr) {
676 return IMPL().active;
679 #ifndef YARP_NO_DEPRECATED // Since YARP 3.3
684 return IMPL().configCallbackLock(mutex);
691 return IMPL().configCallbackLock(mutex);
696 return IMPL().unconfigCallbackLock();
702 yCError(PORT,
"Cannot do lockCallback() without setCallbackLock() before opening port");
709 return IMPL().tryLockCallback();
714 IMPL().unlockCallback();
void close() override
Stop port activity.
bool send(const yarp::os::PortWriter &writer, yarp::os::PortReader *reader=nullptr, const yarp::os::PortWriter *callback=nullptr)
Send a normal message.
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
A simple collection of objects that can be described and transmitted in a portable way.
static Contact registerContact(const Contact &contact)
Register contact information with the name server.
bool replyAndDrop(PortWriter &writer) override
Same as reply(), but closes connection after reply.
void includeNodeInName(bool flag) override
Choose whether to prepend a node name (if one is available) to the port's name.
void releaseProperties(Property *prop) override
End access unstructured port properties.
void add(Contactable &contactable) override
add a Contactable to the Node specified in the contactable name (see NestedContact....
#define PORTCORE_IS_INPUT
void configAdminReader(PortReader &reader)
std::mutex * recCallbackLock
bool setCallbackLock(yarp::os::Mutex *mutex) override
Add a lock to use when invoking callbacks.
int getInputCount() override
Determine how many connections are arriving into this port.
std::string getEnvironment(const char *key, bool *found=nullptr)
Read a variable from the environment.
std::string getEnvelope()
bool start() override
Begin main thread.
PortReaderCreator * checkReadCreator()
bool tryLockCallback() override
Try to lock callbacks until unlockCallback() is called.
void configReadCreator(PortReaderCreator &creator)
std::string getSafeString(const std::string &txt)
int getOutputCount() override
Determine how many output connections this port has.
bool removeCallbackLock() override
Remove a lock on callbacks added with setCallbackLock()
void setAdminMode(bool adminMode=true)
Turn on/off "admin" mode.
std::string getName() const
void remove(Contactable &contactable) override
remove a Contactable from the Node specified in the contactable's name.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
A base class for objects that want information about port status changes.
virtual void onCompletion() const
This is called when the port has finished all writing operations.
static Type byNameOnWire(const char *name_on_wire)
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
void getReport(PortReport &reporter) override
Get information on the state of the port - connections etc.
void describe(void *id, yarp::os::OutputStream *os)
Produce a text description of the port and its connections.
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
int getVerbosity()
Get port verbosity level.
bool addOutput(const std::string &name) override
Add an output connection to the specified port.
A mini-server for network communication.
void setName(const std::string &name)
Set the name of this port.
void promiseType(const Type &typ)
std::string getNameOnWire() const
bool sharedOpen(Port &port)
Open a port wrapping an existing port.
void resetReporter() override
Remove the callback which is called upon any future connections and disconnections to/from the port.
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...
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.
void enableBackgroundWrite(bool backgroundFlag)
control whether writing from this port is done in the background.
void setReporter(PortReport &reporter) override
Set a callback to be called upon any future connections and disconnections to/from the port.
int join(double seconds=-1)
void setControlRegistration(bool flag)
Normally the port will unregister its name with the name server when shutting down.
static bool initialized()
Returns true if YARP has been fully initialized.
bool setTimeout(float timeout)
Set a timeout on network operations.
bool manualStart(const char *sourceName)
Start up the port, but without a main thread.
void setReaderCreator(PortReaderCreator &creator)
Set a creator for readers for port data.
void setReader(PortReader &reader) override
Set an external reader for port data.
bool read(ConnectionReader &reader) override
Callback for data.
bool isWriting()
Check if a message is currently being sent.
void resume() override
Put the port back in an operative state after interrupt() has been called.
~Port() override
Destructor.
#define yCAssert(component, x)
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
bool read(PortReader &reader, bool willReply=false) override
Read an object from the port.
bool reply(PortWriter &writer) override
Send an object as a reply to an object read from the port.
const Contact & getAddress() const
Get the address associated with the port.
static bool localNetworkAllocation()
Check where the name server in use expects processes to allocate their own network resources.
bool requireActiveName()
requireActiveName if there is no active node, creates a temporary one.
void resetReportCallback()
Reset the callback to be notified of changes in port status.
bool addOutput(const std::string &dest, void *id, yarp::os::OutputStream *os, bool onlyIfNeeded=false)
Add an output connection to this port.
void setRpcMode(bool expectRpc) override
Configure the port to be RPC only.
PortReader * checkAdminPortReader()
#define yCError(component,...)
bool getEnvelope(PortReader &envelope) override
Get the envelope information (e.g., a timestamp) from the last message received on the port.
bool lockCallback() override
Lock callbacks until unlockCallback() is called.
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
int getInputCount()
Check how many input connections there are.
#define yCInfo(component,...)
void setEnvelope(const std::string &envelope)
Set some envelope information to pass along with a message without actually being part of the message...
An interface to the operating system, including Port based communication.
#define yCDebug(component,...)
bool openFake(const std::string &name)
Start port without making it accessible from the network.
Property * acquireProperties(bool readOnly) override
Access unstructured port properties.
#define PORTCORE_IS_OUTPUT
Type getType() override
Get the type of data the port has committed to send/receive.
void setOutputMode(bool expectOutput) override
Configure the port to allow or forbid outputs.
void cat(Vector &a, const Vector &b)
void setReportCallback(yarp::os::PortReport *reporter)
Set a callback to be notified of changes in port status.
Contact where() const override
Returns information about how this port can be reached.
void interrupt() override
Interrupt any current reads or writes attached to the port.
RandScalar * implementation(void *t)
int getOutputCount()
Check how many output connections there are.
Basic wrapper for mutual exclusion.
bool isInterrupted() const
std::string getActiveName()
getActiveName getter for the currently active node's name
PortReader * checkPortReader()
void interrupt()
Prepare the port to be shut down.
void setReadHandler(yarp::os::PortReader &reader)
Set a callback for incoming data.
bool isWriting() override
Report whether the port is currently writing data.
Small helper class to help deal with legacy YARP configuration files.
void setInputMode(bool expectInput) override
Configure the port to allow or forbid inputs.
#define YARP_OS_LOG_COMPONENT(name, name_string)
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
void configWaitAfterSend(bool waitAfterSend)
The components from which ports and connections are built.
void unlockCallback() override
Unlock callbacks.
void configReader(PortReader &reader)
void promiseType(const Type &typ) override
Commit the port to a particular type of data.
int rename(const char *oldname, const char *newname)
Portable wrapper for the rename() function.
#define SET_FLAG(implementation, mask, val)
A class for storing options and configuration information.
void close() override
Shut down port.
bool listen(const Contact &address, bool shouldAnnounce=true)
Begin service at a given address.
void setAdminReader(PortReader &reader) override
Set an external reader for unrecognized administrative port messages.
bool isOpen() const
Check if the port has been opened.
bool reply(PortWriter &writer, bool drop, bool interrupted)
void resetPortName(const std::string &str)
void setVerbosity(int level)
Set whether the port should issue messages about its operations.