... | ... | @@ -8,137 +8,6 @@ Cameo is a very lightweight application server, very easy to install and usable |
|
|
* [Implementation](implementation)
|
|
|
* [Example of code](example-of-code)
|
|
|
|
|
|
# Example of code
|
|
|
We present a sample of code where two Cameo applications interact with a publisher/subscriber pattern. We define:
|
|
|
* a Java application Publisher that publishes to a single subscriber.
|
|
|
* a C++ application Subscriber which starts the Publisher application on the local Cameo server and subscribes to it.
|
|
|
|
|
|
Code for the C++ Publisher application:
|
|
|
```cpp
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
|
// Initialise the nappli application represented by This.
|
|
|
application::This::init(argc, argv);
|
|
|
|
|
|
// New block to ensure nappli objects are terminated before the application.
|
|
|
{
|
|
|
// Declare a publisher.
|
|
|
std::auto_ptr<application::Publisher> publisher;
|
|
|
|
|
|
try {
|
|
|
// Define the publisher with name "pub" that accepts a single subscriber.
|
|
|
publisher = application::This::publish("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.
|
|
|
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:
|
|
|
```java
|
|
|
public class SubscriberApplication {
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
// Initialise the Cameo application represented by This.
|
|
|
Application.This.init(args);
|
|
|
|
|
|
// Get the local Cameo server (the one that started This).
|
|
|
Server server = Application.This.getServer();
|
|
|
|
|
|
// Declare the subscriber.
|
|
|
Application.Subscriber subscriber = null;
|
|
|
|
|
|
try {
|
|
|
// Start the publisher application with name "pubcpp" and get a reference to the running application with the Instance object.
|
|
|
Application.Instance publisherApplication = server.start("pubcpp");
|
|
|
|
|
|
// Check that the application really started.
|
|
|
if (!publisherApplication.exists()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// Subscriber 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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 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 services and subscriber in Java.
|
|
|
subscriber.terminate();
|
|
|
Application.This.terminate();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
```
|
|
|
|
|
|
Define the code is not enough to run Cameo applications. We need to configure a Cameo server with the following configuration file:
|
|
|
<pre><code class="xml">
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
|
|
<config port="7000" max_applications="20" log_directory="logs" debug="ON" polling_time="100">
|
|
|
|
|
|
<applications>
|
|
|
|
|
|
<!-- The C++ application with name pubcpp -->
|
|
|
<application name="pubcpp" stream="yes" log_directory="logs">
|
|
|
<start executable="pubcppapp"/>
|
|
|
</application>
|
|
|
|
|
|
|
|
|
<!-- The Java application with name subpubjava -->
|
|
|
<application name="subpubjava" stream="yes">
|
|
|
<start executable="java" args="-classpath bin.jar:jeromq-0.3.6.jar:protobuf-java-2.5.0.jar fr.ill.ics.nappli.test.SubscriberApplication"/>
|
|
|
</application>
|
|
|
|
|
|
</applications>
|
|
|
|
|
|
</config>
|
|
|
</code></pre>
|
|
|
A Cameo server configuration file contains the list of applications that can be started with their name, executable and other attributes. For example we can specify that the application will stream its standard and error outputs.
|
|
|
Now we have to execute the *subpubjava* application. That can be realized manually by using the Cameo console:
|
|
|
<pre>
|
|
|
> cmo -e tcp://machine:7000 exec subpubjava
|
|
|
</pre>
|
|
|
|
|
|
The server running with the endpoint tcp://machine:7000 starts *subpubjava*.
|
|
|
|
|
|
# Installation
|
|
|
* [Dependencies](dependencies)
|
|
|
* [JZMQ or JeroMQ ?](JZMQorJeroMQ)
|
... | ... | |