... | ... | @@ -406,4 +406,114 @@ public class JavaApp { |
|
|
|
|
|
In our example, the *starter* app should be the running application *App1*. But be careful, the application could have terminated since it sent the request to start *App2*. In C++ and Python, the function is also *connectToStarter()* member of the *This* object.
|
|
|
|
|
|
Once you have a reference to the *starter* you can decide to stop it or use it to connect a communication object. |
|
|
\ No newline at end of file |
|
|
Once you have a reference to the *starter* you can decide to stop it or use it to connect a communication object.
|
|
|
|
|
|
# Standard error and output
|
|
|
|
|
|
The standard error and output can also be printed by the console application or retrieved by the API. For that the attribute *stream* of the application tag must have be set to *yes*. An example with the C++ API:
|
|
|
```cpp
|
|
|
// Start the app with the OUTPUTSTREAM option.
|
|
|
std::unique_ptr<cameo::application::Instance> app = server.start("App2", cameo::OUTPUTSTREAM);
|
|
|
|
|
|
// Get the outputstream socket.
|
|
|
std::shared_ptr<cameo::OutputStreamSocket> socket = app->getOutputStreamSocket();
|
|
|
|
|
|
// Define a thread to retrieve the output.
|
|
|
std::thread outputThread([&] {
|
|
|
while (true) {
|
|
|
std::optional<Output> output = socket->receive();
|
|
|
if (output) {
|
|
|
std::cout << output.value().getMessage() << std::endl;
|
|
|
}
|
|
|
else {
|
|
|
// The socket has terminated normally or was canceled.
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// Wait for 1s.
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
|
|
|
|
// Stop the output stream socket.
|
|
|
socket->cancel();
|
|
|
|
|
|
// Wait for the end of the thread.
|
|
|
outputThread.join();
|
|
|
```
|
|
|
|
|
|
Starting the application with the option *OUTPUTSTREAM* creates an output stream socket so that all the stream can be retrieved.
|
|
|
|
|
|
Same example in Java:
|
|
|
```java
|
|
|
// Start the app with the OUTPUTSTREAM option.
|
|
|
Instance app = server.start("App2", Option.OUTPUTSTREAM);
|
|
|
|
|
|
// Get the outputstream socket.
|
|
|
OutputStreamSocket socket = app.getOutputStreamSocket();
|
|
|
|
|
|
// Define a thread to retrieve the output.
|
|
|
Thread outputThread = new Thread(() -> {
|
|
|
while (true) {
|
|
|
Output output = socket.receive();
|
|
|
if (output != null) {
|
|
|
System.out.println(output.getMessage());
|
|
|
}
|
|
|
else {
|
|
|
// The socket has terminated normally or was canceled.
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
outputThread.start();
|
|
|
|
|
|
// Wait for 1s.
|
|
|
try {
|
|
|
Thread.sleep(1000);
|
|
|
|
|
|
// Stop the output stream socket.
|
|
|
socket.cancel();
|
|
|
|
|
|
// Wait for the end of the thread.
|
|
|
outputThread.join();
|
|
|
|
|
|
}
|
|
|
catch (InterruptedException e) {
|
|
|
}
|
|
|
```
|
|
|
|
|
|
And the Python example:
|
|
|
```python
|
|
|
import threading
|
|
|
|
|
|
# Define the function retrieving the output.
|
|
|
def printOutput(socket):
|
|
|
while True:
|
|
|
output = socket.receive()
|
|
|
if output:
|
|
|
print(output.getMessage())
|
|
|
else:
|
|
|
# The socket has terminated normally or was canceled.
|
|
|
return
|
|
|
|
|
|
# Start the app with the OUTPUTSTREAM option.
|
|
|
app = server.start("streampy", cameopy.OUTPUTSTREAM)
|
|
|
|
|
|
# Get the outputstream socket.
|
|
|
socket = app.getOutputStreamSocket()
|
|
|
|
|
|
# Start the thread.
|
|
|
t = threading.Thread(target=printOutput, args=(socket,))
|
|
|
t.start()
|
|
|
|
|
|
# Wait for 1s.
|
|
|
time.sleep(1)
|
|
|
|
|
|
# Stop the output stream socket.
|
|
|
socket.cancel()
|
|
|
|
|
|
# Wait for the end of the thread.
|
|
|
t.join()
|
|
|
```
|
|
|
|
|
|
Retrieving the output may not be only restricted to debug. If you need to integrate an external program that only has output, control could be done by analyzing the output stream. |
|
|
\ No newline at end of file |