... | ... | @@ -79,22 +79,14 @@ If you need to setup a request/response mechanism between different apps then us |
|
|
|
|
|
An example of a responder in a C++ application registered as *ResApp* in the Cameo server:
|
|
|
```cpp
|
|
|
// Define a responder.
|
|
|
std::unique_ptr<coms::basic::Responder> responder;
|
|
|
|
|
|
try {
|
|
|
// Create the responder with name "the-responder".
|
|
|
responder = coms::basic::Responder::create("the-responder");
|
|
|
std::unique_ptr<coms::basic::Responder> responder = coms::basic::Responder::create("the-responder");
|
|
|
|
|
|
// Initialize the responder.
|
|
|
responder->init();
|
|
|
|
|
|
std::cout << "Created responder " << *responder << std::endl;
|
|
|
}
|
|
|
catch (const coms::InitException& e) {
|
|
|
std::cout << "Responder cannot be created" << std::endl;
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
// Loop on the requests.
|
|
|
while (true) {
|
... | ... | @@ -110,6 +102,10 @@ std::cout << "Received request " << request->get() << std::endl; |
|
|
|
|
|
// Reply a string to the requester.
|
|
|
request->reply("Done");
|
|
|
}
|
|
|
catch (const coms::InitException& e) {
|
|
|
std::cout << "Responder error" << std::endl;
|
|
|
}
|
|
|
```
|
|
|
The responder is created with the name "the-responder" to identify it.
|
|
|
The pre-condition for the creation of the responder is that *This* must have been initialized.
|
... | ... | @@ -202,8 +198,9 @@ Default value is 0 and in that case *Publisher.init()* immediately returns. Then |
|
|
|
|
|
Let's give an example of a subcriber in C++:
|
|
|
```cpp
|
|
|
try {
|
|
|
// Connect to the app PubApp which hosts a publisher.
|
|
|
std::unique_ptr<application::App> publisherApp = server.connect("PubApp");
|
|
|
std::unique_ptr<App> publisherApp = server.connect("PubApp");
|
|
|
|
|
|
// Create a subscriber to the application.
|
|
|
std::unique_ptr<coms::Subscriber> subscriber = coms::Subscriber::create(*publisherApp, "publisher");
|
... | ... | @@ -221,9 +218,18 @@ while (true) { |
|
|
}
|
|
|
std::cout << "Received " << message.value() << std::endl;
|
|
|
}
|
|
|
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 same subscriber in Python:
|
|
|
```python
|
|
|
|
|
|
```
|
|
|
|
|
|
As for the requester/responder no port was provided to define the publisher and subscriber. but only a name. Moreover the Cameo implementation provides a bit of **synchronization** with the number of subscribers which can be very useful.
|
|
|
|
|
|
The communication patterns provided by Cameo are **high-level** and allow programmers to rapidly design a communication between apps. |