... | ... | @@ -179,7 +179,7 @@ try { |
|
|
Publisher publisher = Publisher.create("the-publisher");
|
|
|
|
|
|
// Set the number of subscribers to wait to 1.
|
|
|
publisher->setWaitForSubscribers(1);
|
|
|
publisher.setWaitForSubscribers(1);
|
|
|
|
|
|
// Synchronize with the subscriber(s). Wait for the subscriber to connect.
|
|
|
publisher.init();
|
... | ... | @@ -198,9 +198,9 @@ catch (InitException e) { |
|
|
```
|
|
|
|
|
|
Here again, *This* must have been initialized before creating the publisher. We provide a synchronization feature with the second argument of the *Publisher.create()* method: the number of subscribers can be provided. The call to *Publisher.init()* is blocking until the required number of subscribers has been reached. This allows to have subscribers that will receive all the messages: The publisher is **synchronized**.
|
|
|
Default value is 0 and in that case *Publisher.init()* immediately returns. Then the publisher is **not** synchronized and some messages may be lost.
|
|
|
Default value is 0 and in that case *Publisher.init()* immediately returns. Then the publisher is **not** synchronized and some messages may be lost. There is another option for the publisher: synchronized subscribers. It is set using *setSyncSubscribers()* instead of *setWaitForSubscribers()* so that the publisher is not waiting for the subscribers but the subscribers are effectively connected at the end of their *init()*.
|
|
|
|
|
|
Let's give an example of a subcriber in C++:
|
|
|
Let's give an example of a subscriber in C++:
|
|
|
```cpp
|
|
|
try {
|
|
|
// Connect to the app PubApp which hosts a publisher.
|
... | ... | @@ -210,7 +210,7 @@ try { |
|
|
std::unique_ptr<coms::Subscriber> subscriber = coms::Subscriber::create(*publisherApp, "publisher");
|
|
|
|
|
|
// Initialize the subscriber.
|
|
|
subscriber.init();
|
|
|
subscriber->init();
|
|
|
|
|
|
// Receive data.
|
|
|
while (true) {
|
... | ... | @@ -227,7 +227,7 @@ catch (const InitException& e) { |
|
|
std::cout << "Subscriber cannot be created" << std::endl;
|
|
|
}
|
|
|
```
|
|
|
The subscriber is created by connecting to the publisher named "the-publisher" living in the *PubApp* application. Once connected, the subscriber will receive the messages until an emty message arrives. Here again the pre-condition for the creation of the subscriber is that *This* must have been initialized.
|
|
|
The subscriber is created by connecting to the publisher named "the-publisher" living in the *PubApp* application. Once connected, the subscriber will receive the messages until an empty message arrives. Here again the pre-condition for the creation of the subscriber is that *This* must have been initialized.
|
|
|
|
|
|
The same subscriber in Python:
|
|
|
```python
|
... | ... | |