... | ... | @@ -12,42 +12,39 @@ Code for the C++ Publisher application: |
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
|
// Initialise the Cameo application represented by This.
|
|
|
cameo::application::This::init(argc, argv);
|
|
|
|
|
|
// New block to ensure Cameo objects are terminated before the application.
|
|
|
{
|
|
|
// Declare a publisher.
|
|
|
std::unique_ptr<cameo::coms::Publisher> publisher;
|
|
|
|
|
|
try {
|
|
|
// Create the publisher with name "pub" that accepts a single subscriber.
|
|
|
publisher = cameo::coms::Publisher::create("pub", 1);
|
|
|
|
|
|
// Synchronize with the subscriber.
|
|
|
publisher->waitForSubscribers();
|
|
|
|
|
|
// Once here, we are sure that the subscriber is ready
|
|
|
// and will be able to receive all the messages.
|
|
|
}
|
|
|
catch (const PublisherCreationException& e) {
|
|
|
// The publisher cannot be created.
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
// Force the state RUNNING.
|
|
|
cameo::application::This::setRunning();
|
|
|
|
|
|
// We can send data to the unique subscriber.
|
|
|
publisher->send("hello");
|
|
|
publisher->send("world");
|
|
|
publisher->send("!");
|
|
|
|
|
|
// Send the end of the stream.
|
|
|
publisher->sendEnd();
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
// Initialise the Cameo application represented by This.
|
|
|
cameo::application::This::init(argc, argv);
|
|
|
|
|
|
// Declare a publisher.
|
|
|
std::unique_ptr<cameo::coms::Publisher> publisher;
|
|
|
|
|
|
try {
|
|
|
// Create the publisher with name "pub" that accepts a single subscriber.
|
|
|
publisher = cameo::coms::Publisher::create("pub", 1);
|
|
|
|
|
|
// Synchronize with the subscriber.
|
|
|
publisher->waitForSubscribers();
|
|
|
|
|
|
// Once here, we are sure that the subscriber is ready
|
|
|
// and will be able to receive all the messages.
|
|
|
}
|
|
|
catch (const PublisherCreationException& e) {
|
|
|
// The publisher cannot be created.
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
// Force the state RUNNING.
|
|
|
cameo::application::This::setRunning();
|
|
|
|
|
|
// We can send data to the unique subscriber.
|
|
|
publisher->send("hello");
|
|
|
publisher->send("world");
|
|
|
publisher->send("!");
|
|
|
|
|
|
// Send the end of the stream.
|
|
|
publisher->sendEnd();
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
```
|
|
|
Code for the Java Subscriber application:
|
... | ... | @@ -60,54 +57,54 @@ import fr.ill.ics.cameo.coms.Subscriber; |
|
|
|
|
|
public class SubscriberApplication {
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
// Initialise the Cameo application represented by This.
|
|
|
This.init(args);
|
|
|
// Initialise the Cameo application represented by This.
|
|
|
This.init(args);
|
|
|
|
|
|
// Get the local Cameo server (the one that started This).
|
|
|
Server server = This.getServer();
|
|
|
// Get the local Cameo server (the one that started This).
|
|
|
Server server = This.getServer();
|
|
|
|
|
|
// Declare the subscriber.
|
|
|
Subscriber subscriber = null;
|
|
|
// Declare the subscriber.
|
|
|
Subscriber subscriber = null;
|
|
|
|
|
|
try {
|
|
|
// Start the publisher application with name "pubcpp"
|
|
|
// and get a reference to the running application with the Instance object.
|
|
|
Instance publisherApplication = server.start("pubcpp");
|
|
|
|
|
|
// Check that the application really started.
|
|
|
if (!publisherApplication.exists()) {
|
|
|
return;
|
|
|
}
|
|
|
try {
|
|
|
// Start the publisher application with name "pubcpp"
|
|
|
// and get a reference to the running application with the Instance object.
|
|
|
Instance publisherApplication = server.start("pubcpp");
|
|
|
|
|
|
// Check that the application really started.
|
|
|
if (!publisherApplication.exists()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// Subscribe to the publisher with name "pub".
|
|
|
subscriber = publisherApplication.subscribe("pub");
|
|
|
// Subscribe to the publisher with name "pub".
|
|
|
subscriber = publisherApplication.subscribe("pub");
|
|
|
|
|
|
// We are ready to receive data.
|
|
|
while (true) {
|
|
|
// Receive string encoded messages.
|
|
|
String data = subscriber.receiveString();
|
|
|
if (data != null) {
|
|
|
System.out.println("received " + data);
|
|
|
} else {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
// We are ready to receive data.
|
|
|
while (true) {
|
|
|
// Receive string encoded messages.
|
|
|
String data = subscriber.receiveString();
|
|
|
if (data != null) {
|
|
|
System.out.println("received " + data);
|
|
|
}
|
|
|
else {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// Finished to receive the data.
|
|
|
// We can wait for the termination of the "pubcpp" application.
|
|
|
Application.State state = publisherApplication.waitFor();
|
|
|
|
|
|
// At this point, the "pubcpp" application is terminated and its terminal state is state.
|
|
|
}
|
|
|
finally {
|
|
|
// Do not forget to terminate the subscriber and This in Java.
|
|
|
subscriber.terminate();
|
|
|
This.terminate();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// Finished to receive the data.
|
|
|
// We can wait for the termination of the "pubcpp" application.
|
|
|
Application.State state = publisherApplication.waitFor();
|
|
|
|
|
|
// At this point, the "pubcpp" application is terminated and its terminal state is state.
|
|
|
}
|
|
|
finally {
|
|
|
// Do not forget to terminate the subscriber and This in Java.
|
|
|
subscriber.terminate();
|
|
|
This.terminate();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
... | ... | @@ -117,22 +114,14 @@ You can notice the call to *start()* with the "pubcpp" name that is not already |
|
|
Here is a possible configuration file *config.xml*:
|
|
|
```xml
|
|
|
<config port="7000">
|
|
|
|
|
|
<applications>
|
|
|
|
|
|
<!-- The C++ application with name pubcpp -->
|
|
|
<application name="pubcpp" log_directory="logs">
|
|
|
<start executable="pubcppapp"/>
|
|
|
</application>
|
|
|
|
|
|
|
|
|
<!-- The Java application with name subpubjava -->
|
|
|
<application name="subpubjava" log_directory="logs">
|
|
|
<start executable="java" args="-classpath tests.jar test.SubscriberApplication"/>
|
|
|
</application>
|
|
|
|
|
|
</applications>
|
|
|
|
|
|
<applications>
|
|
|
<application name="pubcpp" log_directory="logs">
|
|
|
<start executable="pubcppapp"/>
|
|
|
</application>
|
|
|
<application name="subpubjava" log_directory="logs">
|
|
|
<start executable="java" args="-classpath tests.jar test.SubscriberApplication"/>
|
|
|
</application>
|
|
|
</applications>
|
|
|
</config>
|
|
|
```
|
|
|
A Cameo server configuration file contains:
|
... | ... | |