Commit 314b7de6 authored by legoc's avatar legoc
Browse files

Reviewed This

parent 26f101b6
......@@ -18,15 +18,6 @@ package fr.ill.ics.cameo.base;
import java.util.ArrayList;
import java.util.List;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import fr.ill.ics.cameo.base.impl.ThisImpl;
import fr.ill.ics.cameo.messages.JSON;
import fr.ill.ics.cameo.messages.Messages;
import fr.ill.ics.cameo.strings.Endpoint;
public class Application {
......@@ -34,272 +25,6 @@ public class Application {
void handle();
}
public static class This {
static ThisImpl impl;
private static Server server;
private static Server starterServer;
public static class Com {
private Server server;
private int applicationId;
Com(Server server, int applicationId) {
this.server = server;
this.applicationId = applicationId;
}
public Context getContext() {
return server.getContext();
}
public void storeKeyValue(String key, String value) {
server.storeKeyValue(applicationId, key, value);
}
public String getKeyValue(String key) throws UndefinedKeyException {
try {
return server.getKeyValue(applicationId, key);
}
catch (UndefinedApplicationException e) {
// Should not happen in This.
e.printStackTrace();
}
return null;
}
public void removeKey(String key) throws UndefinedKeyException {
try {
server.removeKey(applicationId, key);
}
catch (UndefinedApplicationException e) {
// Should not happen in This.
e.printStackTrace();
}
}
public int requestPort() {
try {
return server.requestPort(applicationId);
}
catch (UndefinedApplicationException e) {
// Should not happen in This.
e.printStackTrace();
}
return -1;
}
public void setPortUnavailable(int port) {
try {
server.setPortUnavailable(applicationId, port);
}
catch (UndefinedApplicationException e) {
// Should not happen in This.
e.printStackTrace();
}
}
public void releasePort(int port) {
try {
server.releasePort(applicationId, port);
}
catch (UndefinedApplicationException e) {
// Should not happen in This.
e.printStackTrace();
}
}
public JSONObject requestJSON(JSONObject request) {
return server.requestJSON(request);
}
public RequestSocket createRequestSocket(String endpoint) {
return server.createRequestSocket(endpoint);
}
/**
* Method provided by convenience to simplify the parsing of JSON messages.
* @param message
* @return
*/
public JSONObject parse(byte[] message) {
try {
return server.parse(message);
}
catch (ParseException e) {
throw new UnexpectedException("Cannot parse message");
}
}
//TODO Remove when use of key values is done.
public void removePort(String name) {
JSONObject response = server.requestJSON(Messages.createRemovePortV0Request(applicationId, name));
int port = JSON.getInt(response, Messages.RequestResponse.VALUE);
if (port == -1) {
System.err.println("Cannot remove port " + name);
}
}
}
private static Com com;
static private void initServer() {
server = impl.getServer();
server.registerEventListener(impl.getEventListener());
if (impl.getStarterEndpoint() != null) {
starterServer = new Server(impl.getStarterEndpoint());
}
com = new Com(impl.getServer(), impl.getId());
}
static public void init(String[] args) {
impl = new ThisImpl(args);
initServer();
}
static public void init(String name, String endpoint) {
impl = new ThisImpl(name, endpoint);
initServer();
}
static public String getName() {
if (impl == null) {
return null;
}
return impl.getName();
}
static public int getId() {
if (impl == null) {
return 0;
}
return impl.getId();
}
public int getTimeout() {
return impl.getTimeout();
}
public void setTimeout(int timeout) {
impl.setTimeout(timeout);
}
static public Endpoint getEndpoint() {
if (impl == null) {
return null;
}
return impl.getEndpoint();
}
static public Server getServer() {
return server;
}
static public Server getStarterServer() {
return starterServer;
}
static public Com getCom() {
return com;
}
static public boolean isAvailable(int timeout) {
return impl.isAvailable(timeout);
}
static public boolean isAvailable() {
return impl.isAvailable(10000);
}
static public void cancelWaitings() {
impl.cancelWaitings();
}
static public void terminate() {
impl.terminate();
server.terminate();
if (starterServer != null) {
starterServer.terminate();
}
}
static public void setResult(byte[] data) {
impl.setResult(data);
}
static public void setResult(String data) {
impl.setResult(data);
}
/**
* Sets the owner application RUNNING.
* @return
* @throws StateException, ConnectionTimeout
*/
static public boolean setRunning() {
return impl.setRunning();
}
/**
* Returns true if the application is in STOPPING state. Can be used when the application is already polling.
* @return
*/
static public boolean isStopping() {
return impl.isStopping();
}
/**
* Sets the stop handler with stopping time that overrides the one that may be defined in the configuration of the server.
* @param handler
*/
static public void handleStop(final Handler handler, int stoppingTime) {
impl.createStopHandler(handler, stoppingTime);
}
/**
* Sets the stop handler with default stopping time.
* @param handler
*/
static public void handleStop(final Handler handler) {
handleStop(handler, -1);
}
/**
*
* @return
*/
static public Instance connectToStarter(int options) {
if (starterServer == null) {
return null;
}
// Iterate the instances to find the id
List<Instance> instances = starterServer.connectAll(impl.getStarterName(), 0);
for (Instance i : instances) {
if (i.getId() == impl.getStarterId()) {
return i;
}
}
return null;
}
/**
*
* @return
*/
static public Instance connectToStarter() {
return connectToStarter(0);
}
}
/**
* describe states of application
*
......
......@@ -18,15 +18,14 @@ package fr.ill.ics.cameo.base;
import fr.ill.ics.cameo.base.Application.Handler;
import fr.ill.ics.cameo.base.Application.State;
import fr.ill.ics.cameo.base.impl.ThisImpl;
public class HandlerRunner {
private ThisImpl application;
private This application;
private Handler handler;
private Thread thread = null;
public HandlerRunner(ThisImpl application, Handler handler) {
public HandlerRunner(This application, Handler handler) {
this.application = application;
this.handler = handler;
}
......
......@@ -29,7 +29,6 @@ import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import fr.ill.ics.cameo.base.Application.State;
import fr.ill.ics.cameo.base.Application.This;
import fr.ill.ics.cameo.base.impl.Response;
import fr.ill.ics.cameo.base.impl.zmq.ContextZmq;
import fr.ill.ics.cameo.messages.JSON;
......
/*
* 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.base;
package fr.ill.ics.cameo.base.impl;
import java.util.List;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import fr.ill.ics.cameo.ProcessHandlerImpl;
import fr.ill.ics.cameo.base.Application;
import fr.ill.ics.cameo.base.Application.Handler;
import fr.ill.ics.cameo.base.Application.State;
import fr.ill.ics.cameo.base.CancelEvent;
import fr.ill.ics.cameo.base.ConnectionTimeout;
import fr.ill.ics.cameo.base.Event;
import fr.ill.ics.cameo.base.EventListener;
import fr.ill.ics.cameo.base.HandlerRunner;
import fr.ill.ics.cameo.base.InvalidArgumentException;
import fr.ill.ics.cameo.base.Server;
import fr.ill.ics.cameo.base.StatusEvent;
import fr.ill.ics.cameo.base.UnexpectedException;
import fr.ill.ics.cameo.base.UnmanagedApplicationException;
import fr.ill.ics.cameo.base.WaitingSet;
import fr.ill.ics.cameo.messages.JSON;
import fr.ill.ics.cameo.messages.Messages;
import fr.ill.ics.cameo.strings.Endpoint;
public class ThisImpl {
public class This {
static This impl;
private Endpoint serverEndpoint;
private String name;
private int id = -1;
......@@ -50,13 +26,297 @@ public class ThisImpl {
private String starterName;
private int starterId;
private Server server;
// Definition of a EventListener member.
private EventListener eventListener = new EventListener();
private HandlerRunner stopHandler;
private WaitingSet waitingSet = new WaitingSet();
private Server server;
private Server starterServer;
public static class Com {
private Server server;
private int applicationId;
Com(Server server, int applicationId) {
this.server = server;
this.applicationId = applicationId;
}
public Context getContext() {
return server.getContext();
}
public void storeKeyValue(String key, String value) {
server.storeKeyValue(applicationId, key, value);
}
public String getKeyValue(String key) throws UndefinedKeyException {
try {
return server.getKeyValue(applicationId, key);
}
catch (UndefinedApplicationException e) {
// Should not happen in This.
e.printStackTrace();
}
return null;
}
public void removeKey(String key) throws UndefinedKeyException {
try {
server.removeKey(applicationId, key);
}
catch (UndefinedApplicationException e) {
// Should not happen in This.
e.printStackTrace();
}
}
public int requestPort() {
try {
return server.requestPort(applicationId);
}
catch (UndefinedApplicationException e) {
// Should not happen in This.
e.printStackTrace();
}
return -1;
}
public void setPortUnavailable(int port) {
try {
server.setPortUnavailable(applicationId, port);
}
catch (UndefinedApplicationException e) {
// Should not happen in This.
e.printStackTrace();
}
}
public void releasePort(int port) {
try {
server.releasePort(applicationId, port);
}
catch (UndefinedApplicationException e) {
// Should not happen in This.
e.printStackTrace();
}
}
public JSONObject requestJSON(JSONObject request) {
return server.requestJSON(request);
}
public RequestSocket createRequestSocket(String endpoint) {
return server.createRequestSocket(endpoint);
}
/**
* Method provided by convenience to simplify the parsing of JSON messages.
* @param message
* @return
*/
public JSONObject parse(byte[] message) {
try {
return server.parse(message);
}
catch (ParseException e) {
throw new UnexpectedException("Cannot parse message");
}
}
//TODO Remove when use of key values is done.
public void removePort(String name) {
JSONObject response = server.requestJSON(Messages.createRemovePortV0Request(applicationId, name));
int port = JSON.getInt(response, Messages.RequestResponse.VALUE);
if (port == -1) {
System.err.println("Cannot remove port " + name);
}
}
}
private static Com com;
void initServer() {
server.registerEventListener(impl.getEventListener());
if (impl.getStarterEndpoint() != null) {
starterServer = new Server(impl.getStarterEndpoint());
}
com = new Com(impl.getServer(), impl.getId());
}
static public void init(String[] args) {
impl = new This(args);
impl.initServer();
}
static public void init(String name, String endpoint) {
impl = new This(name, endpoint);
impl.initServer();
}
static public String getName() {
if (impl == null) {
return null;
}
return impl.name;
}
static public int getId() {
if (impl == null) {
return 0;
}
return impl.id;
}
public int getTimeout() {
if (impl == null) {
return 0;
}
return impl.server.getTimeout();
}
public void setTimeout(int timeout) {
if (impl == null) {
return;
}
impl.server.setTimeout(timeout);
}
static public Endpoint getEndpoint() {
if (impl == null) {
return null;
}
return impl.serverEndpoint;
}
static public Server getServer() {
if (impl == null) {
return null;
}
return impl.server;
}
static public Server getStarterServer() {
if (impl == null) {
return null;
}
return impl.starterServer;
}
static public Com getCom() {
return com;
}
static public boolean isAvailable(int timeout) {
if (impl == null) {
return false;
}
return impl.server.isAvailable(timeout);
}
static public boolean isAvailable() {
return isAvailable(10000);
}
static public void cancelWaitings() {
if (impl == null) {
return;
}
impl.waitingSet.cancelAll();
}
static public void terminate() {
impl.terminateAll();
}
static public void setResult(byte[] data) {
JSONObject response = impl.server.requestJSON(Messages.createSetResultRequest(getId()), data);
int value = JSON.getInt(response, Messages.RequestResponse.VALUE);
if (value == -1) {
throw new UnexpectedException("Cannot set result");
}
}
static public void setResult(String data) {
setResult(Messages.serialize(data));
}
/**