... | ... | @@ -2,4 +2,63 @@ |
|
|
Controlling the Cameo apps is already a form of communication between the apps. We show here the common ways to make them communicate by using the communication patterns aka coms and the return value.
|
|
|
|
|
|
# Return value
|
|
|
First and easiest way to pass data from an app to another is to use the return value. When an app terminates it is possible to set a return value that will be published to all the instance references. |
|
|
\ No newline at end of file |
|
|
First and easiest way to pass data from an app to another is to use the return value. When an app terminates it is possible to set a return value that will be published to all the instance references. We can extend the Java application of *App2*:
|
|
|
|
|
|
```java
|
|
|
import fr.ill.ics.cameo.base.This;
|
|
|
|
|
|
public class JavaApp {
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
// Initialise the Cameo application represented by This.
|
|
|
This.init(args);
|
|
|
|
|
|
// Infinite printing loop.
|
|
|
int i = 0;
|
|
|
while (true) {
|
|
|
System.out.println("Printing " + i);
|
|
|
try {
|
|
|
Thread.sleep(100);
|
|
|
}
|
|
|
catch (InterruptedException e) {
|
|
|
}
|
|
|
i++;
|
|
|
}
|
|
|
|
|
|
// Set the result.
|
|
|
This.setResult("This is a result");
|
|
|
|
|
|
This.terminate();
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
In C++, we can get the result:
|
|
|
```cpp
|
|
|
#include <cameo/api/cameo.h>
|
|
|
#include <iostream>
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
|
// Define the reference to the Cameo server in B.
|
|
|
cameo::Server server("tcp://B:7000");
|
|
|
|
|
|
// Start the application "App2" and obtain a reference to the running app.
|
|
|
std::unique_ptr<cameo::application::Instance> app2 = server.start("App2");
|
|
|
|
|
|
// Wait for the end of the app and get the terminal state.
|
|
|
cameo::application::State state = app2->waitFor();
|
|
|
|
|
|
// Get the result.
|
|
|
std::optional<std::string> result = resultApplication->getResult();
|
|
|
if (result.has_value()) {
|
|
|
std::cout << "Result " << result.value() << std::endl;
|
|
|
}
|
|
|
else {
|
|
|
std::cout << "No result" << std::endl;
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
``` |