f@0: /*
f@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0:
f@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0:
f@0: This program is free software: you can redistribute it and/or modify
f@0: it under the terms of the GNU General Public License as published by
f@0: the Free Software Foundation, either version 3 of the License, or
f@0: (at your option) any later version.
f@0:
f@0: This program is distributed in the hope that it will be useful,
f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0: GNU General Public License for more details.
f@0:
f@0: You should have received a copy of the GNU General Public License
f@0: along with this program. If not, see .
f@0: */
f@0:
f@0: package uk.ac.qmul.eecs.ccmi.network;
f@0:
f@0: import java.awt.geom.Point2D;
f@0: import java.awt.geom.Rectangle2D;
f@0: import java.io.IOException;
f@0: import java.nio.channels.SelectionKey;
f@0: import java.nio.channels.Selector;
f@0: import java.nio.channels.SocketChannel;
f@0: import java.text.MessageFormat;
f@0: import java.util.HashMap;
f@0: import java.util.HashSet;
f@0: import java.util.Iterator;
f@0: import java.util.LinkedList;
f@0: import java.util.Map;
f@0: import java.util.ResourceBundle;
f@0: import java.util.Set;
f@0: import java.util.concurrent.BlockingQueue;
f@0: import java.util.concurrent.ConcurrentLinkedQueue;
f@0: import java.util.concurrent.LinkedBlockingQueue;
f@0:
f@0: import javax.swing.SwingUtilities;
f@0:
f@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramElement;
f@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramTreeNode;
f@0: import uk.ac.qmul.eecs.ccmi.gui.Diagram;
f@0: import uk.ac.qmul.eecs.ccmi.gui.DiagramEventSource;
f@0: import uk.ac.qmul.eecs.ccmi.gui.DiagramPanel;
f@0: import uk.ac.qmul.eecs.ccmi.gui.Edge;
f@0: import uk.ac.qmul.eecs.ccmi.gui.EditorTabbedPane;
f@0: import uk.ac.qmul.eecs.ccmi.gui.Finder;
f@0: import uk.ac.qmul.eecs.ccmi.gui.Node;
f@0: import uk.ac.qmul.eecs.ccmi.gui.SpeechOptionPane;
f@0: import uk.ac.qmul.eecs.ccmi.gui.awareness.DisplayFilter;
f@0: import uk.ac.qmul.eecs.ccmi.speech.Narrator;
f@0: import uk.ac.qmul.eecs.ccmi.speech.NarratorFactory;
f@0: import uk.ac.qmul.eecs.ccmi.utils.InteractionLog;
f@0:
f@0: /**
f@0: * This is the class that manages the connection with the server. When a diagram is shared
f@0: * this class becomes responsible for actually operating the model (trough the EVT tough, by calling SwingUtilities.invokeLater ).
f@0: * If the operation is issued by the local user, than it performs the local action with local data only after
f@0: * being acknowledged by the server, else it creates the data on demand. For example upon an insert
f@0: * issued by the server, the element is created from scratch, according to the message of the server.
f@0: *
f@0: */
f@0: public class ClientConnectionManager extends NetworkThread {
f@0:
f@0: public ClientConnectionManager(EditorTabbedPane tabbedPane) throws IOException{
f@0: super("Network Client Thread");
f@0: channels = new HashMap();
f@0: requests = new ConcurrentLinkedQueue();
f@0: answers = new LinkedBlockingQueue();
f@0: pendingCommands = new LinkedList();
f@0: selector = Selector.open();
f@0: this.tabbedPane = tabbedPane;
f@0: protocol = ProtocolFactory.newInstance();
f@0: mustSayGoodbye = false;
f@0: waitingAnswer = false;
f@0: }
f@0:
f@0: /**
f@0: * The Event Dispatching Thread communicates with this thread through a concurrent queue.
f@0: * This is the method to add requests to the queue.
f@0: * @param r the request for this thread
f@0: */
f@0: public void addRequest(Request r){
f@0: requests.add(r);
f@0: if(r instanceof SendLockRequest){
f@0: SendLockRequest slr = (SendLockRequest) r;
f@0: if(slr.lock.getName().toString().startsWith(LockMessage.GET_LOCK_PREFIX))
f@0: waitingAnswer = true;
f@0: }
f@0: selector.wakeup();
f@0: }
f@0:
f@0: public Answer getAnswer(){
f@0: try {
f@0: Answer answer = answers.take();
f@0: waitingAnswer = false;
f@0: return answer;
f@0: } catch (InterruptedException e) {
f@0: throw new RuntimeException(e);// must never happen
f@0: }
f@0: }
f@0:
f@0: @Override
f@0: public void run(){
f@0: while(!mustSayGoodbye){
f@0: try {
f@0: selector.select();
f@0: } catch (IOException e) {
f@0: revertAllDiagrams();
f@0: }
f@0:
f@0: if(mustSayGoodbye)
f@0: break;
f@0:
f@0: /* handle the requests for the remote server from the local users */
f@0: handleRequests();
f@0:
f@0: for (Iterator itr = selector.selectedKeys().iterator(); itr.hasNext();){
f@0: SelectionKey key = itr.next();
f@0: itr.remove();
f@0:
f@0: if(!key.isValid())
f@0: continue;
f@0:
f@0: if(key.isReadable()){
f@0: SocketChannel channel = (SocketChannel)key.channel();
f@0: Message msg = null;
f@0: try {
f@0: msg = protocol.receiveMessage(channel);
f@0: } catch (IOException e) {
f@0: revertDiagram(channel,false);
f@0: /* signal the event dispatching thread, otherwise blocked */
f@0: try {
f@0: /* RevertedDiagramAnswer is to prevent the Event dispatching Thread from blocking if the *
f@0: * server goes down and the client is still waiting for an answer. If the thread *
f@0: * was not waiting for an answers the RevertedDiagramAnswer will not be put in the queue */
f@0: if(waitingAnswer)
f@0: answers.put(new RevertedDiagramAnswer());
f@0: } catch (InterruptedException ie) {
f@0: throw new RuntimeException(ie);
f@0: }
f@0: continue;
f@0: }
f@0: //System.out.println("ClientConnaectionManager: read message " + msg.getName());
f@0: /* retrieve the diagram */
f@0: String diagramName = msg.getDiagram();
f@0: final Diagram diagram = channels.get(channel);
f@0: node = null;
f@0: edge = null;
f@0: if(msg instanceof Command){
f@0: final Command cmd = (Command)msg;
f@0: cmd.getSource().setDiagramName(diagram.getName());
f@0: /* log the command through the interaction log, if any */
f@0: Command.log(cmd, "remote command received");
f@0: switch(cmd.getName()){
f@0: case INSERT_NODE :
f@0: double dx = (Double)cmd.getArgAt(1);
f@0: double dy = (Double)cmd.getArgAt(2);
f@0: node = Finder.findNode((String)cmd.getArgAt(0), diagram.getNodePrototypes());
f@0: node = (Node)node.clone();
f@0: /* Place the top left corner of the bounds at the origin. It might be different from *
f@0: * the origin, as it depends on how the clonation is implemented internally. These calls *
f@0: * to translate are not notified to any listener as the node is not n the model yet */
f@0: Rectangle2D bounds = node.getBounds();
f@0: node.translate(new Point2D.Double(),-bounds.getX(),-bounds.getY(), DiagramEventSource.NONE);
f@0: /* perform the actual translation from the origin */
f@0: node.translate(new Point2D.Double(),dx,dy,DiagramEventSource.NONE);
f@0: SwingUtilities.invokeLater(new CommandExecutor.Insert(diagram.getCollectionModel(), node, null, cmd.getSource()));
f@0: break;
f@0: case INSERT_EDGE :
f@0: diagram.getCollectionModel().getMonitor().lock();
f@0: edge = Finder.findEdge((String)cmd.getArgAt(0), diagram.getEdgePrototypes());
f@0: edge = (Edge)edge.clone();
f@0: long[] nodesToConnect = new long[cmd.getArgNum()-1];
f@0: for(int i=0;i(cmd.getArgNum()-3);
f@0: for(int i=3;i(sendCmdRequest.cmd.getArgNum()-3);
f@0: for(int i=3;i> entryset = channels.entrySet();
f@0: SocketChannel channel = null;
f@0: for(Map.Entry entry : entryset){
f@0: if(entry.getValue().getName().equals(rdr.diagramName)){
f@0: channel = entry.getKey();
f@0: }
f@0: }
f@0: if(channel != null)
f@0: revertDiagram(channel,true);
f@0: }else if(request instanceof SendCmdRequest||request instanceof SendTreeCmdRequest){
f@0: SendCmdRequest scr = (SendCmdRequest)request;
f@0: //System.out.println("ClientConnectionManager:handling request "+scr.cmd.getName());
f@0: if(!channels.containsKey(scr.channel))
f@0: continue; // commands issued after reverting a diagram are dropped
f@0: pendingCommands.add(scr);
f@0: try{
f@0: protocol.send(scr.channel, scr.cmd);
f@0: }catch(IOException e){
f@0: /* the pending commands is normally removed upon reply receive */
f@0: pendingCommands.remove(scr);
f@0: revertDiagram(scr.channel,false);
f@0: }
f@0: }else if(request instanceof SendLockRequest){
f@0: SendLockRequest slr = (SendLockRequest)request;
f@0: try {
f@0: protocol.send(slr.channel,slr.lock);
f@0: } catch (IOException e) {
f@0: revertDiagram(slr.channel,false);
f@0: try {
f@0: /* RevertedDiagramAnswer is to prevent the Event dispatching Thread from blocking if the *
f@0: * server goes down and the client is still waiting for an answer. If the thread *
f@0: * was not waiting for an answers the RevertedDiagramAnswer will not be put in the queue */
f@0: if(waitingAnswer)
f@0: answers.put(new RevertedDiagramAnswer());
f@0: } catch (InterruptedException ie) {
f@0: throw new RuntimeException(ie); //must never happen
f@0: }
f@0: }
f@0: }else if(request instanceof SendAwarenessRequest){
f@0: SendAwarenessRequest awr = (SendAwarenessRequest)request;
f@0: try{
f@0: protocol.send(awr.channel, awr.awMsg);
f@0: }catch (IOException e) {
f@0: revertDiagram(awr.channel,false);
f@0: }
f@0: }
f@0: }
f@0: }
f@0:
f@0: private void revertDiagram(SocketChannel c,final boolean userRequest){
f@0: /* from now on all the commands using this channel will be dropped */
f@0: final Diagram diagram = channels.remove(c);
f@0: if(diagram == null)
f@0: return;
f@0: try{c.close();}catch(IOException ioe){ioe.printStackTrace();}
f@0: SwingUtilities.invokeLater(new Runnable(){
f@0: @Override
f@0: public void run() {
f@0: for(int i=0; i< tabbedPane.getTabCount();i++){
f@0: DiagramPanel dPanel = (DiagramPanel)tabbedPane.getComponentAt(i);
f@0: if(dPanel.getDiagram() instanceof NetDiagram){
f@0: NetDiagram netDiagram = (NetDiagram)dPanel.getDiagram();
f@0: if( netDiagram.getDelegate().equals(diagram)){
f@0: /* set the old (unwrapped) diagram as the current one */
f@0: dPanel.setAwarenessPanelEnabled(false);
f@0: dPanel.setDiagram(diagram);
f@0: break;
f@0: }
f@0: }
f@0: }
f@0: if(!userRequest)// show the message only if the revert is due to an error
f@0: SpeechOptionPane.showMessageDialog(tabbedPane, MessageFormat.format(
f@0: ResourceBundle.getBundle(Server.class.getName()).getString("dialog.error.connection"),
f@0: diagram.getName())
f@0: );
f@0: }
f@0: });
f@0:
f@0: }
f@0:
f@0: private void revertAllDiagrams(){
f@0: for(SocketChannel c : channels.keySet())
f@0: try{c.close();}catch(IOException ioe){ioe.printStackTrace();}
f@0: channels.clear();
f@0:
f@0: SwingUtilities.invokeLater(new Runnable(){
f@0: @Override
f@0: public void run() {
f@0: for(int i=0; i< tabbedPane.getTabCount();i++){
f@0: DiagramPanel dPanel = (DiagramPanel)tabbedPane.getComponentAt(i);
f@0: if(dPanel.getDiagram() instanceof NetDiagram){
f@0: NetDiagram netDiagram = (NetDiagram)dPanel.getDiagram();
f@0: /* set the old (unwrapped) diagram as the current one */
f@0: dPanel.setAwarenessPanelEnabled(false);
f@0: dPanel.setDiagram(netDiagram.getDelegate());
f@0: }
f@0: }
f@0: SpeechOptionPane.showMessageDialog(
f@0: tabbedPane,
f@0: ResourceBundle.getBundle(Server.class.getName()).getString("dialog.error.connections"));
f@0: }
f@0: });
f@0: }
f@0:
f@0: public interface Request {};
f@0:
f@0: public interface DiagramRequest extends Request {};
f@0:
f@0: public static class AddDiagramRequest implements DiagramRequest {
f@0: public AddDiagramRequest(SocketChannel channel, Diagram diagram){
f@0: this.channel = channel; this.diagram = diagram;
f@0: }
f@0: public SocketChannel channel;
f@0: public Diagram diagram;
f@0: }
f@0:
f@0: public static class RmDiagramRequest implements DiagramRequest {
f@0: public RmDiagramRequest(String diagramName){
f@0: this.diagramName = diagramName;
f@0: }
f@0: public String diagramName;
f@0: }
f@0:
f@0: public static class SendCmdRequest implements Request {
f@0: public SendCmdRequest(Command cmd, SocketChannel channel, DiagramElement element ){
f@0: this.cmd = cmd; this.element = element;this.channel = channel;
f@0: }
f@0:
f@0: public boolean matches(SocketChannel c,String diagramName){
f@0: return(diagramName.equals(cmd.getDiagram())&&c.socket().getInetAddress().equals(channel.socket().getInetAddress()));
f@0: }
f@0: public DiagramElement element;
f@0: public SocketChannel channel;
f@0: public Command cmd;
f@0: }
f@0:
f@0: public static class SendTreeCmdRequest extends SendCmdRequest{
f@0: public SendTreeCmdRequest( Command cmd,SocketChannel channel,DiagramTreeNode treeNode) {
f@0: super(cmd,channel,null);
f@0: this.treeNode = treeNode;
f@0: }
f@0: public DiagramTreeNode treeNode;
f@0: public SocketChannel channel;
f@0: public Command cmd;
f@0: }
f@0:
f@0: public static class SendLockRequest implements Request {
f@0: public SendLockRequest (SocketChannel channel, LockMessage lock){
f@0: this.channel = channel;
f@0: this.lock = lock;
f@0: }
f@0: public SocketChannel channel;
f@0: public LockMessage lock;
f@0: }
f@0:
f@0: public static class SendAwarenessRequest implements Request {
f@0: public SendAwarenessRequest(SocketChannel channel, AwarenessMessage awMsg){
f@0: this.awMsg = awMsg;
f@0: this.channel = channel;
f@0: }
f@0: public SocketChannel channel;
f@0: public AwarenessMessage awMsg;
f@0: }
f@0:
f@0: public interface Answer {};
f@0: public static class LockAnswer implements Answer {
f@0: public LockAnswer(LockMessage answer){
f@0: this.message = answer;
f@0: }
f@0: public LockMessage message;
f@0: }
f@0:
f@0: public static class RevertedDiagramAnswer implements Answer{}
f@0:
f@0: private Node node;
f@0: private Edge edge;
f@0: private DiagramTreeNode treeNode;
f@0: private Set indexes;
f@0: private SendCmdRequest sendCmdRequest;
f@0: /* for each server hold the diagram it shares with it */
f@0: private Map channels;
f@0: private ConcurrentLinkedQueue requests;
f@0: private BlockingQueue answers;
f@0: private LinkedList pendingCommands;
f@0: private Selector selector;
f@0: private EditorTabbedPane tabbedPane;
f@0: private Protocol protocol;
f@0: private volatile boolean waitingAnswer;
f@0: private volatile boolean mustSayGoodbye;
f@0: }