Commit 5080ce96 authored by legoc's avatar legoc
Browse files

Merge branch 'multi-client2' of...

Merge branch 'multi-client2' of https://code.ill.fr/instrument-control/nomad-command-system into multi-client2
parents 21e4d142 f11bb70f
......@@ -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
......@@ -30,6 +30,7 @@ import fr.ill.ics.util.ConfigManager;
*/
public class ChatEvent {
public final static String NOMAD = "nomad";
private LocalDateTime timestamp;
private String sender;
private String formattedSender;
......@@ -79,9 +80,20 @@ public class ChatEvent {
public String getFormattedSender() {
if (formattedSender == null) {
formattedSender = sender.replace("tcp://", "");
formattedSender = formattedSender.substring(0, formattedSender.indexOf(":"));
formattedSender = formatEndPoint(sender);
}
return formattedSender;
}
public boolean comesFromServer() {
return sender.equals(NOMAD);
}
public static String formatEndPoint(String endPoint) {
String formattedEndPoint = endPoint.replace("tcp://", "");
formattedEndPoint = formattedEndPoint.substring(0, formattedEndPoint.indexOf(":"));
return formattedEndPoint;
}
}
\ No newline at end of file
/*
* Nomad Instrument Control Software
*
* Copyright 2011 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.bridge.events;
import fr.ill.ics.cameo.Application;
public class ServerEndedEvent {
private int state;
public ServerEndedEvent(int state) {
this.state = state;
}
public int getState() {
return state;
}
public String getMessage() {
String messageKey = "";
// Switch depending on the real state of the server.
switch (getState()) {
case (Application.State.ERROR):
messageKey = "serverEndedOnErrorMessage";
case (Application.State.KILLED):
messageKey = "serverEndedOnKilledMessage";
case Application.State.SUCCESS:
messageKey = "serverEndedOnSuccessMessage";
case (Application.State.STOPPED):
messageKey = "serverEndedOnStoppedMessage";
}
return messageKey;
}
}
\ No newline at end of file
/*
* Nomad Instrument Control Software
*
* Copyright 2011 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.bridge.listeners;
import fr.ill.ics.bridge.events.ServerEndedEvent;
public interface ServerEndedListener {
public void serverEnded(ServerEndedEvent anEvent);
}
\ No newline at end of file
......@@ -1060,7 +1060,7 @@ public class ServantManagerAccessor {
return null;
}
public boolean hasRunningCommands(String userName, String message) {
public boolean hasRunningCommands() {
// Create the message type.
ServantManagerRequest.Message type = ServantManagerRequest.Message.newBuilder()
......
......@@ -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