annotate 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
rev   line source
f@0 1 /*
f@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0 3
f@0 4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0 5
f@0 6 This program is free software: you can redistribute it and/or modify
f@0 7 it under the terms of the GNU General Public License as published by
f@0 8 the Free Software Foundation, either version 3 of the License, or
f@0 9 (at your option) any later version.
f@0 10
f@0 11 This program is distributed in the hope that it will be useful,
f@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0 14 GNU General Public License for more details.
f@0 15
f@0 16 You should have received a copy of the GNU General Public License
f@0 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@0 18 */
f@0 19
f@0 20 package uk.ac.qmul.eecs.ccmi.network;
f@0 21
f@0 22
f@0 23 import java.net.InetSocketAddress;
f@0 24 import java.nio.channels.SocketChannel;
f@0 25
f@0 26 import uk.ac.qmul.eecs.ccmi.gui.DiagramEventSource;
f@0 27 import uk.ac.qmul.eecs.ccmi.gui.SpeechOptionPane;
f@0 28 import uk.ac.qmul.eecs.ccmi.utils.PreferencesService;
f@0 29
f@0 30 /**
f@0 31 * A {@code SwingWorker} that takes on the communication with the server in the very first phase of the
f@0 32 * connection for a new diagram. It handles the download of the list of diagrams available for sharing on the server
f@0 33 * and, once the user has chosen one, it downloads the diagram into the local editor.
f@0 34 * Since this tasks can take a long time due to network delay and the interaction has not yet started
f@0 35 * a {@code SwingWorker} is used so that the user interface won't get stuck in the process, the user
f@0 36 * being able to cancel the job and to go back to the diagram editor.
f@0 37 *
f@0 38 */
f@0 39 public class DiagramDownloader extends SpeechOptionPane.ProgressDialogWorker<String,Void> {
f@0 40
f@0 41 public DiagramDownloader(SocketChannel channel, String target, int task){
f@0 42 this.channel = channel;
f@0 43 this.task = task;
f@0 44 if(task == CONNECT_AND_DOWNLOAD_LIST_TASK)
f@0 45 this.address = target;
f@0 46 else
f@0 47 this.diagramName = target;
f@0 48 }
f@0 49
f@0 50 @Override
f@0 51 protected String doInBackground() throws Exception {
f@0 52 if(task == CONNECT_AND_DOWNLOAD_LIST_TASK){
f@0 53 int port = Integer.parseInt(PreferencesService.getInstance().get("server.remote_port", Server.DEFAULT_REMOTE_PORT));
f@0 54 channel.connect(new InetSocketAddress(address,port));
f@0 55 }
f@0 56
f@0 57 Protocol protocol = ProtocolFactory.newInstance();
f@0 58 switch(task){
f@0 59 case CONNECT_AND_DOWNLOAD_LIST_TASK :
f@0 60 protocol.send(channel, new Command(Command.Name.LIST,"",DiagramEventSource.NONE));
f@0 61 break;
f@0 62 case DOWNLOAD_DIAGRAM_TASK :
f@0 63 protocol.send(channel, new Command(Command.Name.GET ,diagramName,DiagramEventSource.NONE));
f@0 64 }
f@0 65 Reply reply = protocol.receiveReply(channel);
f@0 66 switch(reply.getName()){
f@0 67 case ERROR_R :
f@0 68 throw new DiagramShareException(reply.getMessage());
f@0 69 case LIST_R :
f@0 70 String result = new String(reply.getMessage());
f@0 71 if("".equals(result))
f@0 72 return null;
f@0 73 return result;
f@0 74 case GET_R :
f@0 75 return reply.getMessage();
f@0 76 default : throw new RuntimeException();
f@0 77 }
f@0 78 }
f@0 79
f@0 80 public static final int CONNECT_AND_DOWNLOAD_LIST_TASK = 0;
f@0 81 public static final int DOWNLOAD_DIAGRAM_TASK = 1;
f@0 82
f@0 83 private SocketChannel channel;
f@0 84 private String diagramName;
f@0 85 private String address;
f@0 86 private int task;
f@0 87 }