Mercurial > hg > accesspd
view java/src/uk/ac/qmul/eecs/ccmi/network/DiagramDownloader.java @ 1:e3935c01cde2 tip
moved license of PdPersistenceManager to the beginning of the file
author | Fiore Martin <f.martin@qmul.ac.uk> |
---|---|
date | Tue, 08 Jul 2014 19:52:03 +0100 |
parents | 78b7fc5391a2 |
children |
line wrap: on
line source
/* CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package uk.ac.qmul.eecs.ccmi.network; import java.net.InetSocketAddress; import java.nio.channels.SocketChannel; import uk.ac.qmul.eecs.ccmi.gui.DiagramEventSource; import uk.ac.qmul.eecs.ccmi.gui.SpeechOptionPane; import uk.ac.qmul.eecs.ccmi.utils.PreferencesService; /** * A {@code SwingWorker} that takes on the communication with the server in the very first phase of the * connection for a new diagram. It handles the download of the list of diagrams available for sharing on the server * and, once the user has chosen one, it downloads the diagram into the local editor. * Since this tasks can take a long time due to network delay and the interaction has not yet started * a {@code SwingWorker} is used so that the user interface won't get stuck in the process, the user * being able to cancel the job and to go back to the diagram editor. * */ public class DiagramDownloader extends SpeechOptionPane.ProgressDialogWorker<String,Void> { public DiagramDownloader(SocketChannel channel, String target, int task){ this.channel = channel; this.task = task; if(task == CONNECT_AND_DOWNLOAD_LIST_TASK) this.address = target; else this.diagramName = target; } @Override protected String doInBackground() throws Exception { if(task == CONNECT_AND_DOWNLOAD_LIST_TASK){ int port = Integer.parseInt(PreferencesService.getInstance().get("server.remote_port", Server.DEFAULT_REMOTE_PORT)); channel.connect(new InetSocketAddress(address,port)); } Protocol protocol = ProtocolFactory.newInstance(); switch(task){ case CONNECT_AND_DOWNLOAD_LIST_TASK : protocol.send(channel, new Command(Command.Name.LIST,"",DiagramEventSource.NONE)); break; case DOWNLOAD_DIAGRAM_TASK : protocol.send(channel, new Command(Command.Name.GET ,diagramName,DiagramEventSource.NONE)); } Reply reply = protocol.receiveReply(channel); switch(reply.getName()){ case ERROR_R : throw new DiagramShareException(reply.getMessage()); case LIST_R : String result = new String(reply.getMessage()); if("".equals(result)) return null; return result; case GET_R : return reply.getMessage(); default : throw new RuntimeException(); } } public static final int CONNECT_AND_DOWNLOAD_LIST_TASK = 0; public static final int DOWNLOAD_DIAGRAM_TASK = 1; private SocketChannel channel; private String diagramName; private String address; private int task; }