... | ... | @@ -79,37 +79,33 @@ 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) {
|
|
|
// Receive the simple request.
|
|
|
std::unique_ptr<coms::basic::Request> request = responder->receive();
|
|
|
if (!request) {
|
|
|
std::cout << "Responder is canceled" << std::endl;
|
|
|
break;
|
|
|
}
|
|
|
// Loop on the requests.
|
|
|
while (true) {
|
|
|
// Receive the simple request.
|
|
|
std::unique_ptr<coms::basic::Request> request = responder->receive();
|
|
|
if (!request) {
|
|
|
std::cout << "Responder is canceled" << std::endl;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
// Print the request data as string.
|
|
|
std::cout << "Received request " << request->get() << std::endl;
|
|
|
// Print the request data as string.
|
|
|
std::cout << "Received request " << request->get() << std::endl;
|
|
|
|
|
|
// Reply a string to the requester.
|
|
|
request->reply("Done");
|
|
|
// 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,28 +198,38 @@ Default value is 0 and in that case *Publisher.init()* immediately returns. Then |
|
|
|
|
|
Let's give an example of a subcriber in C++:
|
|
|
```cpp
|
|
|
// Connect to the app PubApp which hosts a publisher.
|
|
|
std::unique_ptr<application::App> publisherApp = server.connect("PubApp");
|
|
|
try {
|
|
|
// Connect to the app PubApp which hosts a publisher.
|
|
|
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");
|
|
|
// Create a subscriber to the application.
|
|
|
std::unique_ptr<coms::Subscriber> subscriber = coms::Subscriber::create(*publisherApp, "publisher");
|
|
|
|
|
|
// Initialize the subscriber.
|
|
|
subscriber.init();
|
|
|
// Initialize the subscriber.
|
|
|
subscriber.init();
|
|
|
|
|
|
// Receive data.
|
|
|
while (true) {
|
|
|
std::optional<std::string> message = subscriber->receive();
|
|
|
// Receive data.
|
|
|
while (true) {
|
|
|
std::optional<std::string> message = subscriber->receive();
|
|
|
|
|
|
// If there is no value then the subscriber will not receive messages any more.
|
|
|
if (!message.has_value()) {
|
|
|
break;
|
|
|
// If there is no value then the subscriber will not receive messages any more.
|
|
|
if (!message.has_value()) {
|
|
|
break;
|
|
|
}
|
|
|
std::cout << "Received " << message.value() << std::endl;
|
|
|
}
|
|
|
catch (const InitException& e) {
|
|
|
std::cout << "Subscriber cannot be created" << std::endl;
|
|
|
}
|
|
|
std::cout << "Received " << message.value() << 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. |