Commit e5571088 authored by yannick legoc's avatar yannick legoc
Browse files

Moved access to nomad server instance in a dedicated class.

parent 4300cea4
......@@ -22,7 +22,7 @@ import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import fr.ill.ics.nscclient.log.LogSubcriberImpl;
import fr.ill.ics.nscclient.log.LogSubscriber;
import fr.ill.ics.nscclient.notification.DataNotificationClient;
import fr.ill.ics.nscclient.notification.commandzone.CommandZoneEventClient;
import fr.ill.ics.nscclient.survey.SurveySubcriberImpl;
......@@ -48,10 +48,10 @@ public class ChangeManager {
CommandZoneEventClient.getInstance().readAndDispatch();
// Iterating the map of log subscribers
Map<String, LogSubcriberImpl> logSubscriberMap = LogSubcriberImpl.getInstances();
Iterator<Entry<String, LogSubcriberImpl>> it = logSubscriberMap.entrySet().iterator();
Map<String, LogSubscriber> logSubscriberMap = LogSubscriber.getInstances();
Iterator<Entry<String, LogSubscriber>> it = logSubscriberMap.entrySet().iterator();
while (it.hasNext()) {
LogSubcriberImpl logSubscriber = it.next().getValue();
LogSubscriber logSubscriber = it.next().getValue();
logSubscriber.readAndDispatch();
}
......
......@@ -23,7 +23,7 @@ import java.util.Map;
import fr.ill.ics.bridge.listeners.ServerLogEventListener;
import fr.ill.ics.bridge.listeners.ServerLogXMLListener;
import fr.ill.ics.nscclient.log.LogSubcriberImpl;
import fr.ill.ics.nscclient.log.LogSubscriber;
public class LogEventSender {
......@@ -48,18 +48,18 @@ public class LogEventSender {
* @param listener
*/
public void addLogEventListener(ServerLogEventListener listener) {
LogSubcriberImpl.getInstance(serverId).addLogEventListener(listener);
LogSubscriber.getInstance(serverId).addLogEventListener(listener);
}
public void removeLogEventListener(ServerLogEventListener listener) {
LogSubcriberImpl.getInstance(serverId).removeLogEventListener(listener);
LogSubscriber.getInstance(serverId).removeLogEventListener(listener);
}
public void addLogXMLListener(ServerLogXMLListener listener) {
LogSubcriberImpl.getInstance(serverId).addLogXMLListener(listener);
LogSubscriber.getInstance(serverId).addLogXMLListener(listener);
}
public void removeLogXMLListener(ServerLogXMLListener listener) {
LogSubcriberImpl.getInstance(serverId).removeLogXMLListener(listener);
LogSubscriber.getInstance(serverId).removeLogXMLListener(listener);
}
}
\ No newline at end of file
......@@ -38,9 +38,10 @@ import fr.ill.ics.cameo.Application;
import fr.ill.ics.cameo.ConnectionTimeout;
import fr.ill.ics.cameo.Server;
import fr.ill.ics.nomadserver.notification.NotificationMessage;
import fr.ill.ics.nscclient.serverconnection.ServerInstance;
import fr.ill.ics.util.ConfigManager;
public class LogSubcriberImpl {
public class LogSubscriber {
private Set<ServerLogEventListener> logEventListeners = new LinkedHashSet<ServerLogEventListener>();
private Set<ServerLogEvent> pendingChanges = new LinkedHashSet<ServerLogEvent>();
......@@ -54,24 +55,24 @@ public class LogSubcriberImpl {
private Application.Subscriber subscriber;
private Thread subscriberThread;
private static Map<String, LogSubcriberImpl> instances = new HashMap<String, LogSubcriberImpl>();
private static Map<String, LogSubscriber> instances = new HashMap<String, LogSubscriber>();
private String serverId;
private LogSubcriberImpl(String serverId) {
private LogSubscriber(String serverId) {
this.serverId = serverId;
}
public static LogSubcriberImpl getInstance(String serverId) {
public static LogSubscriber getInstance(String serverId) {
if (!instances.containsKey(serverId)) {
LogSubcriberImpl instance = new LogSubcriberImpl(serverId);
LogSubscriber instance = new LogSubscriber(serverId);
instances.put(serverId, instance);
}
return instances.get(serverId);
}
public static Map<String, LogSubcriberImpl> getInstances() {
public static Map<String, LogSubscriber> getInstances() {
return instances;
}
......@@ -137,57 +138,23 @@ public class LogSubcriberImpl {
}
public void init() {
// Connect nomad server.
Application.Instance nomad = ServerInstance.getInstance().getApplicationInstance(serverId);
// Get the server endpoint.
String nomadServerEndpoint = ConfigManager.getInstance().getNomadServerEndpoint();
if (nomad == null) {
System.err.println("Problem to connect to the nomad server " + serverId);
return;
}
// Connect to the server.
Server server = new Server(nomadServerEndpoint);
// Define the log publisher.
String logPublisherName = "log_publisher";
// Connect nomad server.
Application.Instance nomad = null;
// Do not connect to the same application if it is simulated.
if (!serverId.equals(CommandZoneWrapper.SERVER_ID)) {
// Add the server id to the publisher name.
logPublisherName += serverId;
try {
// Find the simulated instance.
List<Application.Instance> nomadApps = server.connectAll("nssim");
for (Application.Instance app : nomadApps) {
if (app.exists() && app.getId() == Integer.parseInt(serverId)) {
nomad = app;
break;
}
}
} catch (ConnectionTimeout e) {
System.err.println("Timeout while connecting nomad server");
}
}
else {
// Find the real instance.
String[] nomadAppNames = {"ns", "nsd", "nsv"};
try {
for (String appName : nomadAppNames) {
nomad = server.connect(appName);
if (nomad.exists()) {
break;
}
}
} catch (ConnectionTimeout e) {
System.err.println("Timeout while connecting nomad server");
}
}
if (nomad == null) {
System.err.println("Problem to connect to the nomad server " + serverId);
}
System.out.println("Trying to connect log subscriber to " + logPublisherName);
......
package fr.ill.ics.nscclient.serverconnection;
import java.util.List;
import fr.ill.ics.bridge.command.CommandZoneWrapper;
import fr.ill.ics.cameo.Application;
import fr.ill.ics.cameo.ConnectionTimeout;
import fr.ill.ics.cameo.Server;
import fr.ill.ics.util.ConfigManager;
/**
* Class providing access to the running Nomad server instances using Cameo.
*/
public class ServerInstance {
private static ServerInstance instance;
private Server server;
public static ServerInstance getInstance() {
if (instance == null) {
instance = new ServerInstance();
}
return instance;
}
public void init() {
// Get the server endpoint.
String nomadServerEndpoint = ConfigManager.getInstance().getNomadServerEndpoint();
// Connect to the server.
server = new Server(nomadServerEndpoint);
}
public Application.Instance getApplicationInstance(String serverId) {
// Connect nomad server.
Application.Instance nomad = null;
// Do not connect to the same application if it is simulated.
if (!serverId.equals(CommandZoneWrapper.SERVER_ID)) {
try {
// Find the simulated instance.
List<Application.Instance> nomadApps = server.connectAll("nssim");
for (Application.Instance app : nomadApps) {
if (app.exists() && app.getId() == Integer.parseInt(serverId)) {
nomad = app;
break;
}
}
} catch (ConnectionTimeout e) {
System.err.println("Timeout while connecting nomad server");
}
} else {
// Find the real instance.
String[] nomadAppNames = { "ns", "nsd", "nsv" };
try {
for (String appName : nomadAppNames) {
nomad = server.connect(appName);
if (nomad.exists()) {
break;
}
}
} catch (ConnectionTimeout e) {
System.err.println("Timeout while connecting nomad server");
}
}
return nomad;
}
}
......@@ -35,11 +35,12 @@ import fr.ill.ics.nscclient.command.ScanCommandBoxAccessorProxy;
import fr.ill.ics.nscclient.commandline.CommandLineProxyImpl;
import fr.ill.ics.nscclient.condition.ConditionManagerProxy;
import fr.ill.ics.nscclient.dataprovider.DataAccessor;
import fr.ill.ics.nscclient.log.LogSubcriberImpl;
import fr.ill.ics.nscclient.log.LogSubscriber;
import fr.ill.ics.nscclient.notification.commandzone.ServerCommandZoneEventManager;
import fr.ill.ics.nscclient.servant.ConfigurationManager;
import fr.ill.ics.nscclient.servant.ConfigurationManager.LoadFailure;
import fr.ill.ics.nscclient.servant.CorbaServantManager;
import fr.ill.ics.nscclient.serverconnection.ServerInstance;
import fr.ill.ics.nscclient.survey.SurveySubcriberImpl;
public class ServerSessionManager {
......@@ -57,6 +58,8 @@ public class ServerSessionManager {
private void init() throws ConnectionFailure {
ServerInstance.getInstance().init();
CorbaSessionManager.getInstance(serverId).init();
// See if the Server is up and running
......@@ -80,7 +83,7 @@ public class ServerSessionManager {
ConditionManagerProxy.getInstance(serverId).init();
CommandLineProxyImpl.getInstance(serverId).init();
LogSubcriberImpl.getInstance(serverId).init();
LogSubscriber.getInstance(serverId).init();
SurveySubcriberImpl.getInstance(serverId).init();
}
......@@ -148,7 +151,7 @@ public class ServerSessionManager {
public void logout() {
ServerCommandZoneEventManager.getInstance(serverId).reset();
LogSubcriberImpl.getInstance(serverId).unsubscribe();
LogSubscriber.getInstance(serverId).unsubscribe();
SurveySubcriberImpl.getInstance(serverId).unsubscribe();
currentSession.logout();
......@@ -159,7 +162,7 @@ public class ServerSessionManager {
ServerCommandZoneEventManager.getInstance(serverId).reset();
try {
LogSubcriberImpl.getInstance(serverId).unsubscribe();
LogSubscriber.getInstance(serverId).unsubscribe();
} catch (Exception e) {
System.out.println("Cannot unsubscribe Log");
}
......
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