Commit e54c731e authored by legoc's avatar legoc

Started with command zone event sync messages

parent e098ba6e
......@@ -25,6 +25,7 @@ import java.util.Map.Entry;
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.notification.commandzone.sync.CommandZoneSyncEventClient;
import fr.ill.ics.nscclient.survey.SurveySubscriberImpl;
......@@ -44,10 +45,13 @@ public class ChangeManager {
}
public void readAndDispatch() {
// Start the event dispatchers.
DataNotificationClient.getInstance().readAndDispatch();
CommandZoneEventClient.getInstance().readAndDispatch();
CommandZoneSyncEventClient.getInstance().readAndDispatch();
// Iterating the map of log subscribers
// Iterate the map of log subscribers.
Map<String, LogSubscriber> logSubscriberMap = LogSubscriber.getInstances();
Iterator<Entry<String, LogSubscriber>> it = logSubscriberMap.entrySet().iterator();
while (it.hasNext()) {
......@@ -55,7 +59,7 @@ public class ChangeManager {
logSubscriber.readAndDispatch();
}
// Iterating the map of survey subscribers
// Iterate the map of survey subscribers.
Map<String, SurveySubscriberImpl> surveySubscriberMap = SurveySubscriberImpl.getInstances();
Iterator<Entry<String, SurveySubscriberImpl>> it2 = surveySubscriberMap.entrySet().iterator();
while (it2.hasNext()) {
......
......@@ -124,4 +124,5 @@ public class AtomicCommandWrapper extends CommandWrapper implements Controller {
@Override
public void updateDynamicProperties() {
}
}
\ No newline at end of file
......@@ -21,6 +21,7 @@ package fr.ill.ics.bridge.command;
import fr.ill.ics.bridge.events.ServerCommandStateChangeEvent;
import fr.ill.ics.bridge.events.ServerCommandStateChangeEvent.CommandState;
import fr.ill.ics.bridge.events.ServerResetCommandEvent;
import fr.ill.ics.bridge.listeners.ServerCommandBoxSyncListener;
import fr.ill.ics.bridge.listeners.ServerCommandStateChangeListener;
import fr.ill.ics.bridge.listeners.ServerExpressionChangeListener;
import fr.ill.ics.bridge.listeners.ServerProgressChangeListener;
......@@ -28,8 +29,9 @@ import fr.ill.ics.bridge.listeners.ServerPropertyChangeListener;
import fr.ill.ics.bridge.listeners.ServerResetCommandListener;
import fr.ill.ics.nscclient.command.ServerCommandBox;
import fr.ill.ics.nscclient.notification.commandzone.CommandZoneEventClient;
import fr.ill.ics.nscclient.notification.commandzone.sync.CommandZoneSyncEventClient;
public abstract class CommandWrapper implements IServerCommand, ICommandBoxEventListener {
public abstract class CommandWrapper implements IServerCommand, ICommandBoxEventListener, ICommandBoxSyncEventListener {
private ServerCommandBox serverCommandBox;
......@@ -38,6 +40,7 @@ public abstract class CommandWrapper implements IServerCommand, ICommandBoxEvent
private ServerPropertyChangeListener propertyListener;
private ServerResetCommandListener resetCommandListener;
private ServerExpressionChangeListener expressionChangeCommandListener;
private ServerCommandBoxSyncListener commandBoxSyncListener;
public CommandWrapper(ServerCommandBox serverCommandBox) {
this.serverCommandBox = serverCommandBox;
......@@ -59,7 +62,12 @@ public abstract class CommandWrapper implements IServerCommand, ICommandBoxEvent
public void addServerResetCommandListener(ServerResetCommandListener listener) {
this.resetCommandListener = listener;
}
public void addServerCommandBoxSyncListener(ServerCommandBoxSyncListener listener) {
this.commandBoxSyncListener = listener;
CommandZoneSyncEventClient.getInstance().addCommandBoxListener(this);
}
public void removeServerCommandStateChangeListener(ServerCommandStateChangeListener listener) {
this.commandStateListener = null;
CommandZoneEventClient.getInstance().removeCommandBoxListener(this);
......@@ -73,6 +81,9 @@ public abstract class CommandWrapper implements IServerCommand, ICommandBoxEvent
this.resetCommandListener = null;
}
public void removeServerCommandBoxSyncListener() {
this.commandBoxSyncListener = null;
}
public void addServerExpressionChangeListener(ServerExpressionChangeListener listener) {
this.expressionChangeCommandListener = listener;
......@@ -186,5 +197,10 @@ public abstract class CommandWrapper implements IServerCommand, ICommandBoxEvent
public int getProgression() {
return serverCommandBox.getProgression();
}
@Override
public void onToggleParallel() {
commandBoxSyncListener.onToggleParallel();
}
}
\ 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.command;
public interface ICommandBoxSyncEventListener {
int getCommandBoxID();
void onToggleParallel();
}
\ 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;
public interface ServerCommandBoxSyncListener {
void onToggleParallel();
}
\ No newline at end of file
......@@ -428,8 +428,6 @@ public class CommandZoneEventClient {
public void eventOccurred(ICommandZoneEvent event) {
//System.out.println("CommandZoneEventClient receives " + event);
synchronized (pendingChanges) {
pendingChanges.add(event);
}
......
......@@ -34,6 +34,10 @@ import fr.ill.ics.nomadserver.notification.NotificationMessage.CommandZoneChange
import fr.ill.ics.nomadserver.notification.NotificationMessage.CommandZoneErrorOccurred;
import fr.ill.ics.nomadserver.notification.NotificationMessage.CommandZoneProgressionChanged;
import fr.ill.ics.nomadserver.notification.NotificationMessage.ForLoopCurrentValueChanged;
import fr.ill.ics.nscclient.notification.commandzone.sync.CommandBoxToggleParallel;
import fr.ill.ics.nscclient.notification.commandzone.sync.CommandZoneSyncEventClient;
import fr.ill.ics.nscclient.notification.commandzone.sync.CommandZoneSyncEventNotifier;
import fr.ill.ics.nscclient.notification.commandzone.sync.ICommandZoneSyncEvent;
import fr.ill.ics.nscclient.serverconnection.ServerInstance;
public class ServerCommandZoneEventManager {
......@@ -45,9 +49,11 @@ public class ServerCommandZoneEventManager {
private String serverId;
private static Map<String, ServerCommandZoneEventManager> instances = new HashMap<String, ServerCommandZoneEventManager>();
private CommandZoneSyncEventNotifier syncEventNotifier;
private ServerCommandZoneEventManager(String serverId) {
this.serverId = serverId;
syncEventNotifier = new CommandZoneSyncEventNotifier(serverId);
}
public static ServerCommandZoneEventManager getInstance(String serverId) {
......@@ -137,7 +143,7 @@ public class ServerCommandZoneEventManager {
notifyForLoopCurrentValueChanged(NotificationMessage.ForLoopCurrentValueChanged.parseFrom(new ByteArrayInputStream(data[1])));
}
else if (NotificationMessage.Message.parseFrom(data[0]).getType() == fr.ill.ics.nomadserver.notification.NotificationMessage.Message.Type.CommandZoneSync) {
System.out.println("Received command zone sync event");
notifyCommandZoneSync(syncEventNotifier.processMessage(NotificationMessage.CommandZoneSync.parseFrom(new ByteArrayInputStream(data[1]))));
}
else {
System.out.println("Unable to process " + NotificationMessage.Message.parseFrom(data[0]).getType());
......@@ -238,4 +244,13 @@ public class ServerCommandZoneEventManager {
private void notifyForLoopCurrentValueChanged(ForLoopCurrentValueChanged message) {
CommandZoneEventClient.getInstance().eventOccurred(new ForLoopCurrentValueChangeEvent(message.getDatabaseID(), message.getCommandBoxID(), message.getIndex(), message.getCurrentValue()));
}
private void notifyCommandZoneSync(ICommandZoneSyncEvent 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
/*
* 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.commandzone.sync;
public class CommandBoxSyncEvent implements ICommandZoneSyncEvent {
private int commandBoxId;
public CommandBoxSyncEvent(int commandBoxId) {
this.commandBoxId = commandBoxId;
}
public int getCommandBoxId() {
return commandBoxId;
}
}
\ 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.nscclient.notification.commandzone.sync;
public class CommandBoxToggleParallel extends CommandBoxSyncEvent {
public CommandBoxToggleParallel(int commandBoxId) {
super(commandBoxId);
}
}
\ 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.nscclient.notification.commandzone.sync;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import fr.ill.ics.bridge.command.ICommandBoxSyncEventListener;
/**
* Class dispatching the command zone sync events to the registered listeners.
*/
public class CommandZoneSyncEventClient {
private static CommandZoneSyncEventClient instance = null;
private List<ICommandZoneSyncEvent> pendingChanges = new LinkedList<ICommandZoneSyncEvent>();
// Command box id.
private Map<Integer, HashSet<ICommandBoxSyncEventListener>> commandBoxListeners = new HashMap<Integer, HashSet<ICommandBoxSyncEventListener>>();
private CommandZoneSyncEventClient() {
}
public static CommandZoneSyncEventClient getInstance() {
if (instance == null) {
instance = new CommandZoneSyncEventClient();
}
return instance;
}
public void addCommandBoxListener(ICommandBoxSyncEventListener listener) {
synchronized (commandBoxListeners) {
if (!commandBoxListeners.containsKey(listener.getCommandBoxID())) {
commandBoxListeners.put(listener.getCommandBoxID(), new HashSet<ICommandBoxSyncEventListener>());
}
commandBoxListeners.get(listener.getCommandBoxID()).add(listener);
}
}
public void removeCommandZoneListener(ICommandBoxSyncEventListener listener) {
synchronized (commandBoxListeners) {
if (commandBoxListeners.containsKey(listener.getCommandBoxID())) {
HashSet<ICommandBoxSyncEventListener> listeners = commandBoxListeners.get(listener.getCommandBoxID());
if (listeners != null) {
listeners.remove(listener);
if (listeners.isEmpty()) {
commandBoxListeners.remove(listener.getCommandBoxID());
}
}
}
}
}
private void notifyCommandBoxToggleParallel(CommandBoxToggleParallel event) {
synchronized (commandBoxListeners) {
if (commandBoxListeners.containsKey(event.getCommandBoxId())) {
HashSet<ICommandBoxSyncEventListener> listeners = commandBoxListeners.get(event.getCommandBoxId());
Iterator<ICommandBoxSyncEventListener> it = listeners.iterator();
while (it.hasNext()) {
it.next().onToggleParallel();
}
}
}
}
public void readAndDispatch() {
// copy pending property changes to unblock any server calls
List<ICommandZoneSyncEvent> changes;
synchronized (pendingChanges) {
changes = new LinkedList<ICommandZoneSyncEvent>(pendingChanges);
pendingChanges.clear();
}
// notify property listeners
Iterator<ICommandZoneSyncEvent> c = changes.iterator();
while (c.hasNext()) {
ICommandZoneSyncEvent event = c.next();
if (event instanceof CommandBoxToggleParallel) {
notifyCommandBoxToggleParallel((CommandBoxToggleParallel)event);
}
}
}
public void clear() {
synchronized (commandBoxListeners) {
commandBoxListeners.clear();
}
}
public void eventOccurred(ICommandZoneSyncEvent event) {
synchronized (pendingChanges) {
pendingChanges.add(event);
}
}
}
\ No newline at end of file
package fr.ill.ics.nscclient.notification.commandzone.sync;
import java.io.IOException;
import fr.ill.ics.nomadserver.commandzone.CommandZoneRequests;
import fr.ill.ics.nomadserver.notification.NotificationMessage;
import fr.ill.ics.nscclient.sessionmanagement.SessionManager;
public class CommandZoneSyncEventNotifier {
private String serverId;
private Integer clientId;
public CommandZoneSyncEventNotifier(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 ICommandZoneSyncEvent processMessage(NotificationMessage.CommandZoneSync message) {
try {
CommandZoneRequests.Message.Type type = CommandZoneRequests.Message.parseFrom(message.getSerializedType()).getType();
if (type == CommandZoneRequests.Message.Type.ToggleParallel) {
CommandZoneRequests.CommandBoxRequest request = CommandZoneRequests.CommandBoxRequest.parseFrom(message.getSerializedContent());
System.out.println("Received a toggle parallel event with client id " + request.getClientID() + ", command box id " + request.getCommandBoxID());
// Notify only if it is the same client that was the originator of the request.
if (request.getClientID() != getClientID()) {
return new CommandBoxToggleParallel(request.getCommandBoxID());
}
}
else {
System.out.println("Unable to process command zone sync message of type " + type);
}
}
catch (IOException e) {
System.err.println("Cannot parse notification server command zone 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.commandzone.sync;
public interface ICommandZoneSyncEvent {
}
\ 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