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: f@0: import java.net.InetSocketAddress; f@0: import java.nio.channels.SocketChannel; f@0: f@0: import uk.ac.qmul.eecs.ccmi.gui.DiagramEventSource; f@0: import uk.ac.qmul.eecs.ccmi.gui.SpeechOptionPane; f@0: import uk.ac.qmul.eecs.ccmi.utils.PreferencesService; f@0: f@0: /** f@0: * A {@code SwingWorker} that takes on the communication with the server in the very first phase of the f@0: * connection for a new diagram. It handles the download of the list of diagrams available for sharing on the server f@0: * and, once the user has chosen one, it downloads the diagram into the local editor. f@0: * Since this tasks can take a long time due to network delay and the interaction has not yet started f@0: * a {@code SwingWorker} is used so that the user interface won't get stuck in the process, the user f@0: * being able to cancel the job and to go back to the diagram editor. f@0: * f@0: */ f@0: public class DiagramDownloader extends SpeechOptionPane.ProgressDialogWorker { f@0: f@0: public DiagramDownloader(SocketChannel channel, String target, int task){ f@0: this.channel = channel; f@0: this.task = task; f@0: if(task == CONNECT_AND_DOWNLOAD_LIST_TASK) f@0: this.address = target; f@0: else f@0: this.diagramName = target; f@0: } f@0: f@0: @Override f@0: protected String doInBackground() throws Exception { f@0: if(task == CONNECT_AND_DOWNLOAD_LIST_TASK){ f@0: int port = Integer.parseInt(PreferencesService.getInstance().get("server.remote_port", Server.DEFAULT_REMOTE_PORT)); f@0: channel.connect(new InetSocketAddress(address,port)); f@0: } f@0: f@0: Protocol protocol = ProtocolFactory.newInstance(); f@0: switch(task){ f@0: case CONNECT_AND_DOWNLOAD_LIST_TASK : f@0: protocol.send(channel, new Command(Command.Name.LIST,"",DiagramEventSource.NONE)); f@0: break; f@0: case DOWNLOAD_DIAGRAM_TASK : f@0: protocol.send(channel, new Command(Command.Name.GET ,diagramName,DiagramEventSource.NONE)); f@0: } f@0: Reply reply = protocol.receiveReply(channel); f@0: switch(reply.getName()){ f@0: case ERROR_R : f@0: throw new DiagramShareException(reply.getMessage()); f@0: case LIST_R : f@0: String result = new String(reply.getMessage()); f@0: if("".equals(result)) f@0: return null; f@0: return result; f@0: case GET_R : f@0: return reply.getMessage(); f@0: default : throw new RuntimeException(); f@0: } f@0: } f@0: f@0: public static final int CONNECT_AND_DOWNLOAD_LIST_TASK = 0; f@0: public static final int DOWNLOAD_DIAGRAM_TASK = 1; f@0: f@0: private SocketChannel channel; f@0: private String diagramName; f@0: private String address; f@0: private int task; f@0: }