YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
PortCore.h
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#ifndef YARP_OS_IMPL_PORTCORE_H
8#define YARP_OS_IMPL_PORTCORE_H
9
10#include <yarp/os/Carriers.h>
11#include <yarp/os/Contact.h>
12#include <yarp/os/Contactable.h>
14#include <yarp/os/PortReader.h>
16#include <yarp/os/PortReport.h>
17#include <yarp/os/PortWriter.h>
18#include <yarp/os/Property.h>
19#include <yarp/os/Type.h>
20#include <yarp/os/Vocab.h>
24
25#include <atomic>
26#include <condition_variable>
27#include <mutex>
28#include <vector>
29
30namespace yarp::os::impl {
31
32class PortCoreUnit;
33
34#define PORTCORE_SEND_NORMAL (1)
35#define PORTCORE_SEND_LOG (2)
36
37// some flags for restricting port behavior
38#define PORTCORE_IS_NULL (0)
39#define PORTCORE_IS_RPC (1)
40#define PORTCORE_IS_INPUT (2)
41#define PORTCORE_IS_OUTPUT (4)
42
102{
103public:
105 outputModifier(nullptr),
106 inputModifier(nullptr)
107 {
108 }
109
111 {
112 releaseOutModifier();
113 releaseInModifier();
114 }
115
117 {
118 if (outputModifier != nullptr) {
119 outputModifier->close();
120 delete outputModifier;
121 outputModifier = nullptr;
122 }
123 }
124
126 {
127 if (inputModifier != nullptr) {
128 inputModifier->close();
129 delete inputModifier;
130 inputModifier = nullptr;
131 }
132 }
133
134public:
137 std::mutex outputMutex;
138 std::mutex inputMutex;
139};
140
142 public ThreadImpl,
144{
145public:
150
154 ~PortCore();
155
165 bool addOutput(const std::string& dest,
166 void* id,
168 bool onlyIfNeeded = false);
169
173 void addOutput(OutputProtocol* op);
174
182 bool removeInput(const std::string& src,
183 void* id,
185
193 bool removeOutput(const std::string& dest,
194 void* id,
196
203 bool removeIO(const Route& route, bool synch = false);
204
210 void describe(void* id, yarp::os::OutputStream* os);
211
216 void describe(yarp::os::PortReport& reporter);
217
224 bool readBlock(ConnectionReader& reader,
225 void* id,
227
234 bool adminBlock(ConnectionReader& reader,
235 void* id);
236
241 void setName(const std::string& name);
242
246 std::string getName();
247
253 void setEnvelope(const std::string& envelope);
254
258 bool setEnvelope(yarp::os::PortWriter& envelope);
259
260 std::string getEnvelope();
261
265 bool getEnvelope(yarp::os::PortReader& envelope);
266
273 void report(const yarp::os::PortInfo& info);
274
282 void reportUnit(PortCoreUnit* unit, bool active);
283
287 void setFlags(unsigned int flags)
288 {
289 this->m_flags = flags;
290 }
291
293 {
294 this->m_contactable = contactable;
295 }
296
300 unsigned int getFlags()
301 {
302 return m_flags;
303 }
304
308 bool listen(const Contact& address, bool shouldAnnounce = true);
309
313 bool isWriting();
314
318 int getInputCount();
319
323 int getOutputCount();
324
328 void setReadHandler(yarp::os::PortReader& reader);
329
333 void setAdminReadHandler(yarp::os::PortReader& reader);
334
338 void setReadCreator(yarp::os::PortReaderCreator& creator);
339
345 {
346 this->m_waitBeforeSend = waitBeforeSend;
347 }
348
354 {
355 this->m_waitAfterSend = waitAfterSend;
356 }
357
361 bool read(yarp::os::ConnectionReader& reader) override
362 {
363 // does nothing by default
364 YARP_UNUSED(reader);
365 return true;
366 }
367
371 bool start() override;
372
376 bool manualStart(const char* sourceName);
377
384 bool send(const yarp::os::PortWriter& writer,
385 yarp::os::PortReader* reader = nullptr,
386 const yarp::os::PortWriter* callback = nullptr);
387
394 bool sendHelper(const yarp::os::PortWriter& writer,
395 int mode,
396 yarp::os::PortReader* reader = nullptr,
397 const yarp::os::PortWriter* callback = nullptr);
398
402 void close() override;
403
407 void run() override;
408
412 int getEventCount();
413
417 const Contact& getAddress() const;
418
419 void resetPortName(const std::string& str);
420
424 yarp::os::PortReaderCreator* getReadCreator();
425
429 void notifyCompletion(void* tracker);
430
435 void setControlRegistration(bool flag);
436
440 void interrupt();
441
445 void resume();
446
450 void setReportCallback(yarp::os::PortReport* reporter);
451
455 void resetReportCallback();
456
461 bool isListening() const;
462
467 bool isManual() const;
468
472 bool isInterrupted() const;
473
474 void setTimeout(float timeout);
475
476 bool setCallbackLock(std::mutex* mutex = nullptr);
477
478 bool removeCallbackLock();
479
480 bool lockCallback();
481
482 bool tryLockCallback();
483
484 void unlockCallback();
485
486 yarp::os::impl::PortDataModifier& getPortModifier();
487
488 void checkType(PortReader& reader);
489
490 yarp::os::Type getType();
491
492 void promiseType(const Type& typ);
493
494private:
495 // main internal PortCore state and operations
496 std::vector<PortCoreUnit *> m_units;
497 std::mutex m_stateMutex;
498 std::condition_variable m_stateCv;
499 std::mutex m_packetMutex;
500 std::condition_variable m_connectionChangeCv;
501 Face* m_face {nullptr};
502 std::string m_name;
503 yarp::os::Contact m_address;
504 yarp::os::PortReader *m_reader {nullptr};
505 yarp::os::PortReader *m_adminReader {nullptr};
506 yarp::os::PortReaderCreator *m_readableCreator {nullptr};
507 yarp::os::PortReport *m_eventReporter {nullptr};
508 std::atomic<bool> m_listening {false};
509 std::atomic<bool> m_running {false};
510 std::atomic<bool> m_starting {false};
511 std::atomic<bool> m_closing {false};
512 std::atomic<bool> m_finished {false};
513 bool m_finishing {false};
514 bool m_waitBeforeSend {true};
515 bool m_waitAfterSend {true};
516 bool m_controlRegistration {true};
517 bool m_interruptable {true};
518 bool m_interrupted {false};
519 bool m_manual {false};
520 int m_events {0};
521 int m_connectionListeners {0};
522 int m_inputCount {0};
523 int m_outputCount {0};
524 int m_dataOutputCount {0};
525 unsigned int m_flags {PORTCORE_IS_INPUT | PORTCORE_IS_OUTPUT};
526 bool m_logNeeded {false};
527 PortCorePackets m_packets {};
528 std::string m_envelope;
529 float m_timeout {-1};
530 int m_counter {1};
531 yarp::os::Contactable *m_contactable {nullptr};
532 std::mutex* m_mutex {nullptr};
533 bool m_mutexOwned {false};
534 BufferedConnectionWriter m_envelopeWriter {true};
535
536 std::mutex m_typeMutex;
537 bool m_checkedType {false};
538 Type m_type;
539
540 // port data modifier
542
543 // set IP packet TOS
544 bool setTypeOfService(PortCoreUnit* unit, int tos);
545
546 // get IP packet TOS
547 int getTypeOfService(PortCoreUnit* unit);
548
549 // set the scheduling properties of all threads
550 // within the process scope.
551 bool setProcessSchedulingParam(int priority = -1, int policy = -1);
552
553 // attach a portmonitor plugin to the port or to a specific connection
554 bool attachPortMonitor(const std::string& portmonitor_options, bool isOutput, std::string& errMsg);
555
556 // detach the portmonitor from the port or specific connection
557 bool detachPortMonitor(bool isOutput);
558
559 // set the parameter for the portmonitor of the port (if any)
560 bool setParamPortMonitor(const yarp::os::Property& param, bool isOutput, std::string& errMsg);
561
562 // get the parameters from the portmonitor of the port (if any)
563 bool getParamPortMonitor(yarp::os::Property& param, bool isOutput, std::string& errMsg);
564
565 void closeMain();
566
567 bool isUnit(const Route& route, int index);
568
569 // only called in "finished" phase
570 void closeUnits();
571
572 // called anytime, garbage collects terminated units
573 void cleanUnits(bool blocking = true);
574
575 // only called by the manager
576 void reapUnits();
577
578 // only called in "running" phase
579 void addInput(InputProtocol* ip);
580
581 bool removeUnit(const Route& route, bool synch = false, bool* except = nullptr);
582
583 int getNextIndex();
584};
585
586} // namespace yarp::os::impl
587
588#endif // YARP_OS_IMPL_PORTCORE_H
FeatureMode mode
#define PORTCORE_IS_INPUT
Definition PortCore.h:40
#define PORTCORE_IS_OUTPUT
Definition PortCore.h:41
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition Carrier.h:44
An interface for reading from a network connection.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
An abstract port.
Definition Contactable.h:28
The initial point-of-contact with a port.
Definition Face.h:20
The output side of an active connection between two ports.
Simple specification of the minimum functions needed from output streams.
Information about a port connection or event.
Definition PortInfo.h:25
A creator for readers.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition PortReader.h:24
A base class for objects that want information about port status changes.
Definition PortReport.h:25
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition PortWriter.h:23
A class for storing options and configuration information.
Definition Property.h:32
Information about a connection between two ports.
Definition Route.h:28
This manages a single threaded resource related to a single input or output connection.
void setFlags(unsigned int flags)
Configure the port to meet certain restrictions in behavior.
Definition PortCore.h:287
void setWaitAfterSend(bool waitAfterSend)
After sending a message, should we wait for it to be sent to all destinations before returning?
Definition PortCore.h:353
void setWaitBeforeSend(bool waitBeforeSend)
Upon being asked to send a message, should we wait for any existing message to be sent to all destina...
Definition PortCore.h:344
void setContactable(Contactable *contactable)
Definition PortCore.h:292
bool read(yarp::os::ConnectionReader &reader) override
Callback for data.
Definition PortCore.h:361
unsigned int getFlags()
Check current configuration of port.
Definition PortCore.h:300
This is the heart of a yarp port.
Definition PortCore.h:102
yarp::os::Carrier * outputModifier
Definition PortCore.h:135
yarp::os::Carrier * inputModifier
Definition PortCore.h:136
An abstraction for a thread of execution.
Definition ThreadImpl.h:21
The components from which ports and connections are built.
#define YARP_UNUSED(var)
Definition api.h:162
#define YARP_os_impl_API
Definition api.h:46