Commit 6c4c545e authored by legoc's avatar legoc

Added C++ example for requester responder.

parent b996e494
......@@ -24,7 +24,9 @@ bin_PROGRAMS = \
testtimeout \
testconnectionchecker \
testveryfast \
teststoptimeout
teststoptimeout \
requesterclient \
responderserver
......@@ -218,3 +220,17 @@ teststoptimeout_SOURCES = \
teststoptimeout_CPPFLAGS = $(CT_CXXFLAGS)
teststoptimeout_LDFLAGS = $(CT_LDFLAGS)
teststoptimeout_LDADD = $(CT_LIBS)
requesterclient_SOURCES = \
examples/RequesterClient.cpp
requesterclient_CPPFLAGS = $(CT_CXXFLAGS)
requesterclient_LDFLAGS = $(CT_LDFLAGS)
requesterclient_LDADD = $(CT_LIBS)
responderserver_SOURCES = \
examples/ResponderServer.cpp
responderserver_CPPFLAGS = $(CT_CXXFLAGS)
responderserver_LDFLAGS = $(CT_LDFLAGS)
responderserver_LDADD = $(CT_LIBS)
/*
* 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.
*/
#include <cameo/cameo.h>
#include <iostream>
using namespace std;
using namespace cameo;
int main(int argc, char *argv[]) {
application::This::init(argc, argv);
// New block to ensure cameo objects are terminated before the application.
{
// The request message is the second argument.
string requestMessage;
if (argc > 1) {
requestMessage = argv[1];
}
// Get the local Cameo server.
Server& server = application::This::getServer();
if (application::This::isAvailable() && server.isAvailable()) {
cout << "Connected server " << server << endl;
}
// Connect to the server.
auto_ptr<application::Instance> responderServer = server.connect("responder-server");
cout << "Application " << *responderServer << " has state " << application::toString(responderServer->now()) << endl;
// Create a requester.
auto_ptr<application::Requester> requester = application::Requester::create(*responderServer, "responder");
cout << "Created requester " << *requester << endl;
if (requester.get() == 0) {
cout << "requester error" << endl;
return -1;
}
// Send a simple message as string.
requester->send(requestMessage);
string response;
requester->receive(response);
cout << "Response is " << response << endl;
cout << "Finished the application" << 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.
*/
#include <cameo/cameo.h>
#include <iostream>
using namespace std;
using namespace cameo;
int main(int argc, char *argv[]) {
application::This::init(argc, argv);
// New block to ensure cameo objects are terminated before the application.
{
if (application::This::isAvailable()) {
cout << "Connected" << endl;
}
auto_ptr<application::Responder> responder;
try {
responder = application::Responder::create("responder");
cout << "Created responder " << *responder << endl;
} catch (const ResponderCreationException& e) {
cout << "Responder error" << endl;
return -1;
}
application::This::setRunning();
// Loop on the requests.
while (true) {
// Receive the simple request.
auto_ptr<application::Request> request = responder->receive();
cout << "Received request " << request->get() << endl;
// Reply.
request->reply("done");
}
cout << "Finished the application" << endl;
}
return 0;
}
<?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="build/bin/responderserver"/>
</application>
<application name="requester-client" starting_time="0" retries="0" stopping_time="1" stream="yes" multiple="no" restart="no">
<start executable="build/bin/requesterclient"/>
</application>
</applications>
</config>
......@@ -35,7 +35,7 @@ public class RequesterClient {
Server server = Application.This.getServer();
if (Application.This.isAvailable() && server.isAvailable()) {
System.out.println("connected server " + server);
System.out.println("Connected server " + server);
} else {
System.exit(-1);
}
......
......@@ -28,14 +28,14 @@ public class ResponderServer {
Application.This.init(args);
if (Application.This.isAvailable()) {
System.out.println("connected");
System.out.println("Connected");
}
try {
System.out.println("creating responder");
// create the publisher
Application.Responder responder = Application.Responder.create("responder");
System.out.println("Created responder " + responder);
// Set the state.
Application.This.setRunning();
......@@ -45,7 +45,7 @@ public class ResponderServer {
// Receive the simple request.
Application.Request request = responder.receive();
System.out.println("received request " + request.get());
System.out.println("Received request " + request.get());
// Reply.
request.reply("done");
......@@ -55,14 +55,14 @@ public class ResponderServer {
}
} catch (RemoteException e) {
System.out.println("responder error");
System.out.println("Responder error");
} finally {
// Do not forget to terminate This.
Application.This.terminate();
}
System.out.println("finished the application");
System.out.println("Finished the application");
}
}
\ No newline at end of file
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