Commit 6eb9880d authored by legoc's avatar legoc
Browse files

Receive sync messages from command line for remote clients and filter some...

Receive sync messages from command line for remote clients and filter some messages from command zone
parent b07d3747
/*
* 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.nscclient.notification.commandline;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import fr.ill.ics.cameo.Application;
import fr.ill.ics.nomadserver.notification.NotificationMessage;
import fr.ill.ics.nscclient.notification.commandline.sync.CommandLineSyncEventNotifier;
import fr.ill.ics.nscclient.notification.commandline.sync.ICommandLineSyncEvent;
import fr.ill.ics.nscclient.notification.commandzone.sync.CommandZoneSyncEventClient;
import fr.ill.ics.nscclient.serverconnection.ServerInstance;
public class ServerCommandLineEventManager {
private static final Logger LOGGER = Logger.getLogger(ServerCommandLineEventManager.class.getName());
private Application.Subscriber subscriber;
private Thread subscriberThread;
private String serverId;
private static Map<String, ServerCommandLineEventManager> instances = new HashMap<String, ServerCommandLineEventManager>();
private CommandLineSyncEventNotifier syncEventNotifier;
private ServerCommandLineEventManager(String serverId) {
this.serverId = serverId;
syncEventNotifier = new CommandLineSyncEventNotifier(serverId);
}
public static ServerCommandLineEventManager getInstance(String serverId) {
if (!instances.containsKey(serverId)) {
ServerCommandLineEventManager instance = new ServerCommandLineEventManager(serverId);
instances.put(serverId, instance);
}
return instances.get(serverId);
}
public void init() {
// Connect nomad server.
Application.Instance nomad = ServerInstance.getInstance().getApplicationInstance(serverId);
if (nomad == null) {
System.err.println("Problem to connect to the nomad server " + serverId);
return;
}
// Define the command line publisher.
String commandLinePublisherName = "command_line_publisher";
System.out.println("Trying to connect command line subscriber to " + commandLinePublisherName);
// Create the subscriber.
subscriber = Application.Subscriber.create(nomad, commandLinePublisherName);
System.out.println("Connected command line subscriber " + subscriber);
// Start the thread.
subscriberThread = new Thread(new Runnable() {
@Override
public void run() {
// Receive data
while (true) {
// Blocking call.
byte[][] data = subscriber.receiveTwoParts();
if (data != null) {
try {
if (NotificationMessage.Message.parseFrom(data[0]).getType() == fr.ill.ics.nomadserver.notification.NotificationMessage.Message.Type.CommandLineSync) {
notifyCommandLineSync(syncEventNotifier.processMessage(NotificationMessage.CommandLineSync.parseFrom(new ByteArrayInputStream(data[1]))));
}
else {
System.out.println("Unable to process " + NotificationMessage.Message.parseFrom(data[0]).getType());
}
}
catch (IOException e) {
System.err.println("Cannot parse notification server command zone message");
}
}
else {
break;
}
}
}
});
subscriberThread.start();
}
public void reset() {
if (subscriber != null) {
System.out.println("Unsubscribing command line...");
// Stop the subscriber.
subscriber.cancel();
// Join the thread.
try {
subscriberThread.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
// Terminate the subscriber.
subscriber.terminate();
System.out.println("Unsubscribed from the command line");
}
}
private void notifyCommandLineSync(ICommandLineSyncEvent event) {
// The event can be null if the incoming message if from client itself.
if (event != null) {
//CommandZoneSyncEventClient.getInstance().eventOccurred(event);
}
}
}
\ No newline at end of file
package fr.ill.ics.nscclient.notification.commandline.sync;
import java.io.IOException;
import fr.ill.ics.nomadserver.commandline.CommandLineRequest;
import fr.ill.ics.nomadserver.notification.NotificationMessage;
import fr.ill.ics.nscclient.notification.commandzone.sync.ICommandZoneSyncEvent;
import fr.ill.ics.nscclient.sessionmanagement.SessionManager;
public class CommandLineSyncEventNotifier {
private String serverId;
private Integer clientId;
public CommandLineSyncEventNotifier(String serverId) {
this.serverId = serverId;
}
private int getClientID() {
if (clientId == null) {
// Get the client id from the session manager.
clientId = SessionManager.getInstance(serverId).getClientId();
}
return clientId;
}
public ICommandLineSyncEvent processMessage(NotificationMessage.CommandLineSync message) {
try {
CommandLineRequest.Message.Type type = CommandLineRequest.Message.parseFrom(message.getSerializedType()).getType();
if (type == CommandLineRequest.Message.Type.SetCommandLineState) {
// CommandLineRequest.CommandBoxRequest request = CommandZoneRequests.CommandBoxRequest.parseFrom(message.getSerializedContent());
//
// // Notify if it is not the same client that sent the request.
// if (request.getClientID() != getClientID()) {
// return new CommandBoxToggleBackground(request.getCommandBoxID());
// }
System.out.println("Process command line sync message of type " + type);
}
else {
System.out.println("Unable to process command line sync message of type " + type);
}
}
catch (IOException e) {
System.err.println("Cannot parse notification server command line sync message");
}
return null;
}
}
/*
* 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.nscclient.notification.commandline.sync;
public interface ICommandLineSyncEvent {
}
\ No newline at end of file
......@@ -378,6 +378,22 @@ public class CommandZoneSyncEventNotifier {
return new DeleteFile(request.getFileName());
}
}
else if (type == CommandZoneRequests.Message.Type.SetModifiable
|| type == CommandZoneRequests.Message.Type.StopCommandBox
|| type == CommandZoneRequests.Message.Type.SetInt32Value
|| type == CommandZoneRequests.Message.Type.SetFloat64Value
|| type == CommandZoneRequests.Message.Type.SetStringValue
|| type == CommandZoneRequests.Message.Type.SetBooleanValue
|| type == CommandZoneRequests.Message.Type.Create
|| type == CommandZoneRequests.Message.Type.Reset
|| type == CommandZoneRequests.Message.Type.Execute
|| type == CommandZoneRequests.Message.Type.Stop
|| type == CommandZoneRequests.Message.Type.StopAtEnd
|| type == CommandZoneRequests.Message.Type.Pause
|| type == CommandZoneRequests.Message.Type.Restart
|| type == CommandZoneRequests.Message.Type.SetPropertyExpression) {
// Do nothing for those notifications.
}
else {
System.out.println("Unable to process command zone sync message of type " + type);
}
......
......@@ -32,6 +32,7 @@ import fr.ill.ics.nscclient.dataprovider.DataAccessor;
import fr.ill.ics.nscclient.dataprovider.ServantManagerAccessor;
import fr.ill.ics.nscclient.log.LogSubscriber;
import fr.ill.ics.nscclient.notification.DataChangeSubscriber;
import fr.ill.ics.nscclient.notification.commandline.ServerCommandLineEventManager;
import fr.ill.ics.nscclient.notification.commandzone.ServerCommandZoneEventManager;
import fr.ill.ics.nscclient.ploty.PlotyInstance;
import fr.ill.ics.nscclient.servant.ConfigurationManager;
......@@ -69,6 +70,7 @@ public class ServerSessionManager {
CommandZoneAccessor.getInstance(serverId).init();
ServerCommandZoneEventManager.getInstance(serverId).init();
ServerCommandLineEventManager.getInstance(serverId).init();
VariableManagerAccessor.getInstance(serverId).init();
ConditionManagerAccessor.getInstance(serverId).init();
......@@ -147,6 +149,7 @@ public class ServerSessionManager {
public void logout() {
ServerCommandZoneEventManager.getInstance(serverId).reset();
ServerCommandLineEventManager.getInstance(serverId).reset();
LogSubscriber.getInstance(serverId).unsubscribe();
SurveySubscriberImpl.getInstance(serverId).unsubscribe();
......@@ -171,6 +174,7 @@ public class ServerSessionManager {
public void logoutAll(boolean stop) {
ServerCommandZoneEventManager.getInstance(serverId).reset();
ServerCommandLineEventManager.getInstance(serverId).reset();
try {
LogSubscriber.getInstance(serverId).unsubscribe();
......
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