Commit e310ab37 authored by legoc's avatar legoc

Added client server example in Java.

parent f5cfd98e
......@@ -124,7 +124,6 @@ int main(int argc, char *argv[]) {
cout << "synchronization with the subscriber synced ? " << synced << endl;
}
// Test the killing of the application
{
cout << "starting publoopcpp for killing" << endl;
......@@ -152,6 +151,60 @@ int main(int argc, char *argv[]) {
killThread->join();
}
// Test the cancelling of a subscriber
{
cout << "starting publoopcpp for testing cancel of a subscriber" << endl;
// Use a shared_ptr to use it in the thread and the main thread.
shared_ptr<application::Instance> pubLoopApplication(server.start("publoopcpp"));
// Create a subscriber
auto_ptr<application::Subscriber> subscriber = application::Subscriber::create(*pubLoopApplication, "publisher");
// Start thread
auto_ptr<thread> cancelThread(new thread(&cancelAll));
// Receiving data
string data;
while (subscriber->receive(data)) {
cout << "received " << data << endl;
}
cout << "subscriber end of stream " << subscriber->hasEnded() << endl;
// Start thread
auto_ptr<thread> killThread(new thread(bind(&killInstance, pubLoopApplication)));
application::State state = pubLoopApplication->waitFor();
cout << "end of publoopcpp with state " << application::toString(state) << endl;
cancelThread->join();
killThread->join();
}
// Test the cancelling of a requester
{
cout << "starting respcpp for testing cancel of a requester" << endl;
// // Use a shared_ptr to use it in the thread and the main thread.
// shared_ptr<application::Instance> responderApplication(server.start("respcpp"));
//
// // Create a requester
// auto_ptr<application::Requester> requester = application::Requester::create(*responderApplication, "responder");
//
// // Start thread
// auto_ptr<thread> cancelThread(new thread(&cancelAll));
//
// // Receiving data
//
// while (subscriber->receive(data)) {
// cout << "received " << data << endl;
// }
//
// cout << "subscriber end of stream " << subscriber->hasEnded() << endl;
}
}
return 0;
......
/*
* Copyright 2015 Institut Laue-Langevin
*
* Licensed under the EUPL, Version 1.1 only (the "License");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
package fr.ill.ics.cameo.examples.clientserver;
import fr.ill.ics.cameo.Application;
import fr.ill.ics.cameo.RemoteException;
import fr.ill.ics.cameo.Server;
import fr.ill.ics.cameo.impl.Buffer;
public class RequesterClient {
public static void main(String[] args) {
Application.This.init(args);
// The request message is the first argument.
String requestMessage = args[0];
// Get the local Cameo server.
Server server = Application.This.getServer();
if (Application.This.isAvailable() && server.isAvailable()) {
System.out.println("connected server " + server);
} else {
System.exit(-1);
}
try {
// Connect to the server.
Application.Instance responderServer = server.connect("responder-server");
System.out.println("Application " + responderServer + " has state " + Application.State.toString(responderServer.now()));
// Create a requester.
Application.Requester requester = Application.Requester.create(responderServer, "responder");
System.out.println("Created requester " + requester);
// Send a simple message as string.
requester.send(requestMessage);
System.out.println("Response is " + requester.receiveString());
// Terminate the requester.
requester.terminate();
} catch (RemoteException e) {
System.out.println("Requester error:" + e);
} finally {
// Do not forget to terminate This.
Application.This.terminate();
}
System.out.println("Finished the application");
}
}
\ No newline at end of file
/*
* Copyright 2015 Institut Laue-Langevin
*
* Licensed under the EUPL, Version 1.1 only (the "License");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
package fr.ill.ics.cameo.examples.clientserver;
import fr.ill.ics.cameo.Application;
import fr.ill.ics.cameo.RemoteException;
import fr.ill.ics.cameo.impl.Buffer;
public class ResponderServer {
public static void main(String[] args) {
Application.This.init(args);
if (Application.This.isAvailable()) {
System.out.println("connected");
}
try {
System.out.println("creating responder");
// create the publisher
Application.Responder responder = Application.Responder.create("responder");
// Set the state.
Application.This.setRunning();
// Loop on the requests.
while (true) {
// Receive the simple request.
Application.Request request = responder.receive();
System.out.println("received request " + request.get());
// Reply.
request.reply("done");
// Terminate the request object.
request.terminate();
}
} catch (RemoteException e) {
System.out.println("responder error");
} finally {
// Do not forget to terminate This.
Application.This.terminate();
}
System.out.println("finished the application");
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<config port="8000" max_applications="20" debug="ON" polling_time="100">
<applications>
<application name="responder-server" starting_time="0" retries="0" stopping_time="1" stream="yes" multiple="no" restart="no">
<start executable="java" args="-classpath target/cameo-tests-jeromq-0.0.1-full.jar fr.ill.ics.cameo.examples.clientserver.ResponderServer"/>
</application>
<application name="requester-client" starting_time="0" retries="0" stopping_time="1" stream="yes" multiple="no" restart="no">
<start executable="java" args="-classpath target/cameo-tests-jeromq-0.0.1-full.jar fr.ill.ics.cameo.examples.clientserver.RequesterClient"/>
</application>
</applications>
</config>
<?xml version="1.0" encoding="UTF-8"?>
<config port="8000" max_applications="20" debug="ON" polling_time="100">
<applications>
<application name="responder-server" starting_time="0" retries="0" stopping_time="1" stream="yes" multiple="no" restart="no">
<start executable="java" args="-classpath target/cameo-tests-jzmq-0.0.1-full.jar fr.ill.ics.cameo.examples.clientserver.ResponderServer"/>
</application>
<application name="requester-client" starting_time="0" retries="0" stopping_time="1" stream="yes" multiple="no" restart="no">
<start executable="java" args="-classpath target/cameo-tests-jzmq-0.0.1-full.jar fr.ill.ics.cameo.examples.clientserver.RequesterClient"/>
</application>
</applications>
</config>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment