Commit f3db0ae2 authored by ics's avatar ics
Browse files

manage server stops

parent 1c33b539
......@@ -23,7 +23,9 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import fr.ill.ics.bridge.events.ServerEndedEvent;
import fr.ill.ics.bridge.events.ServerErrorEvent;
import fr.ill.ics.bridge.listeners.ServerEndedListener;
import fr.ill.ics.bridge.listeners.ServerErrorListener;
......@@ -33,12 +35,14 @@ import fr.ill.ics.bridge.listeners.ServerErrorListener;
*
*/
public class ErrorManager {
private static Map<String, ErrorManager> instances = new HashMap<String, ErrorManager>();
private List<ServerErrorListener> serverErrorListeners;
private static Map<String, ErrorManager> instances = new HashMap<String, ErrorManager>();
private List<ServerErrorListener> serverErrorListeners;
private List<ServerEndedListener> serverEndedListeners;
private ErrorManager() {
serverErrorListeners = new ArrayList<ServerErrorListener>();
serverEndedListeners = new ArrayList<ServerEndedListener>();
}
public static ErrorManager getInstance(String serverId) {
......@@ -48,34 +52,34 @@ public class ErrorManager {
}
return instances.get(serverId);
}
/**
* Add new listener for server error events
* @param listener
*/
public void addServerErrorListener(ServerErrorListener listener) {
synchronized(serverErrorListeners) {
if (!serverErrorListeners.contains(listener)) {
serverErrorListeners.add(listener);
}
}
}
/**
* Remove listener for server error events
* @param listener
*/
public void removeServerErrorListener(ServerErrorListener listener) {
synchronized(serverErrorListeners) {
if (serverErrorListeners.contains(listener)) {
serverErrorListeners.remove(listener);
}
}
}
/**
* Add new listener for server error events
* @param listener
*/
public void addServerErrorListener(ServerErrorListener listener) {
synchronized(serverErrorListeners) {
if (!serverErrorListeners.contains(listener)) {
serverErrorListeners.add(listener);
}
}
}
/**
* Remove listener for server error events
* @param listener
*/
public void removeServerErrorListener(ServerErrorListener listener) {
synchronized(serverErrorListeners) {
if (serverErrorListeners.contains(listener)) {
serverErrorListeners.remove(listener);
}
}
}
/**
* Notify all server error listeners
* @param anEvent
......@@ -84,11 +88,50 @@ public class ErrorManager {
// Make a copy of the list to allow the client to do and add/remove during a notification
ArrayList<ServerErrorListener> changeListeners;
synchronized (serverErrorListeners) {
changeListeners = new ArrayList(serverErrorListeners);
changeListeners = new ArrayList<ServerErrorListener>(serverErrorListeners);
}
for (int i = 0; i < changeListeners.size(); i++) {
ServerErrorListener aListener = changeListeners.get(i);
aListener.serverError(anEvent);
}
}
/**
* Add new listener for server ended events
* @param listener
*/
public void addServerEndedListener(ServerEndedListener listener) {
synchronized(serverEndedListeners) {
if (!serverEndedListeners.contains(listener)) {
serverEndedListeners.add(listener);
}
}
}
/**
* Remove listener for server ended events
* @param listener
*/
public void removeServerEndedListener(ServerEndedListener listener) {
synchronized(serverEndedListeners) {
if (serverEndedListeners.contains(listener)) {
serverEndedListeners.remove(listener);
}
}
}
public void notifyServerEndedListeners(ServerEndedEvent event) {
// Make a copy of the list to allow the client to do and add/remove during a notification
ArrayList<ServerEndedListener> changeListeners;
synchronized (serverEndedListeners) {
changeListeners = new ArrayList<ServerEndedListener>(serverEndedListeners);
}
for (int i = 0; i < changeListeners.size(); i++) {
ServerEndedListener aListener = changeListeners.get(i);
aListener.serverEnded(event);
}
}
}
\ No newline at end of file
......@@ -21,6 +21,9 @@ package fr.ill.ics.nscclient.serverconnection;
import java.util.HashMap;
import java.util.Map;
import fr.ill.ics.bridge.ErrorManager;
import fr.ill.ics.bridge.command.CommandZoneWrapper;
import fr.ill.ics.bridge.events.ServerEndedEvent;
import fr.ill.ics.cameo.Application;
/**
......@@ -32,28 +35,49 @@ public class ServerConnection {
private String serverId;
private String statusMessage;
private Application.Instance serverInstance;
private Thread connectedToServerThread;
private static Map<String, ServerConnection> instances = new HashMap<String, ServerConnection>();
/**
* Constructor. Starts with a server connecting state from which either
* a connection is established or a new server launched.
*/
private ServerConnection(String serverId) {
this.serverId = serverId;
statusMessage = "serverConnectionInitMessage";
}
public static ServerConnection getInstance(String serverId) {
if (!instances.containsKey(serverId)) {
ServerConnection instance = new ServerConnection(serverId);
instances.put(serverId, instance);
}
return instances.get(serverId);
}
private void startConnectedToServerThread() {
if (serverId == CommandZoneWrapper.SERVER_ID) {
// Check server status from Nomad client => if dead, kill client
connectedToServerThread = new Thread(new Runnable() {
@Override
public void run() {
int serverState = serverInstance.waitFor();
ServerEndedEvent event = new ServerEndedEvent(serverState);
ErrorManager.getInstance(serverId).notifyServerEndedListeners(event);
System.out.println("server state : " + Application.State.toString(serverState));
}
});
connectedToServerThread.start();
}
}
/**
* Performs necessary actions to maintain a connection with the server
......@@ -61,40 +85,55 @@ public class ServerConnection {
* @param serverId
*/
public boolean makeConnection() {
Application.Instance serverInstance = ServerInstance.getInstance().getApplicationInstance(serverId);
serverInstance = ServerInstance.getInstance().getApplicationInstance(serverId);
if (serverInstance == null || !serverInstance.exists()) {
System.out.println("The server does not exist");
serverInstance = ServerInstance.getInstance().startApplication(serverId);
if (serverInstance == null || !serverInstance.exists()) {
return false;
}
}
statusMessage = "serverConnectingMessage";
// Manage state in case the server exists but is shutting down or processing error.
System.out.println("Waiting for the state RUNNING of " + serverInstance);
serverInstance.waitFor(Application.State.RUNNING);
statusMessage = "serverConnectedMessage";
// Start the connected to server thread only in case of real server.
startConnectedToServerThread();
System.out.println("Server is running");
return true;
}
/**
* Returns a string message for the current state
*/
public String getStatusMessage() {
return statusMessage;
}
public void reset() {
// The "connected to server thread" exists in case of a real server.
if (connectedToServerThread != null) {
serverInstance.cancelWaitFor();
try {
connectedToServerThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
\ 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