2 * This file is part of the Vamsas Client version 0.2.
\r
3 * Copyright 2010 by Jim Procter, Iain Milne, Pierre Marguerite,
\r
4 * Andrew Waterhouse and Dominik Lindner.
\r
6 * Earlier versions have also been incorporated into Jalview version 2.4
\r
7 * since 2008, and TOPALi version 2 since 2007.
\r
9 * The Vamsas Client is free software: you can redistribute it and/or modify
\r
10 * it under the terms of the GNU Lesser General Public License as published by
\r
11 * the Free Software Foundation, either version 3 of the License, or
\r
12 * (at your option) any later version.
\r
14 * The Vamsas Client is distributed in the hope that it will be useful,
\r
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
17 * GNU Lesser General Public License for more details.
\r
19 * You should have received a copy of the GNU Lesser General Public License
\r
20 * along with the Vamsas Client. If not, see <http://www.gnu.org/licenses/>.
\r
22 package uk.ac.vamsas.client.picking;
\r
26 import java.util.logging.*;
\r
28 import org.apache.commons.logging.Log;
\r
31 * Server class that listens for incoming connections on a predefined port.
\r
33 class PickServer extends Thread {
\r
34 private Log logger = org.apache.commons.logging.LogFactory
\r
35 .getLog(uk.ac.vamsas.client.picking.PickServer.class);
\r
37 // The port number we'll listen on
\r
38 static final int PORT = 53782;
\r
40 private ServerSocket serverSocket;
\r
42 // Set to true once the server is established
\r
43 private boolean isServer = false;
\r
45 // Set to false when server is shutting down
\r
46 private boolean isAlive = true;
\r
48 private SocketManager manager;
\r
51 * Constructs a new instance of the server (but doesn't start it).
\r
54 * a reference to the pick manager that owns this server
\r
56 PickServer(SocketManager manager) {
\r
57 this.manager = manager;
\r
61 * Returns true if this server instance is running. Return true if this server
\r
62 * instance is running
\r
64 boolean isServer() {
\r
69 * Attempts to create the server by opening a server socket on the port.
\r
71 * @return true if the server was created; false otherwise
\r
73 boolean createServer() {
\r
75 serverSocket = new ServerSocket(PORT);
\r
78 return isServer = true;
\r
79 } catch (IOException e) {
\r
80 logger.debug("SERVER: " + e);
\r
86 * Thread listening method - loops indefinitely listening for connections.
\r
87 * When one is received, the socket object is passed to the manager so it can
\r
88 * make a full client connection for further comms.
\r
91 logger.debug("SERVER: listening on " + PORT + " - SERVER");
\r
93 // Loop forever, accepting connectons from other clients
\r
94 // TODO: add in the ability to terminate the server if a VAMSAS session
\r
98 Socket socket = serverSocket.accept();
\r
99 logger.info("SERVER: connection detected");
\r
101 manager.addEndPoint(socket);
\r
102 } catch (IOException e) {
\r
108 logger.debug("Server shutting down...");
\r
111 serverSocket.close();
\r
112 } catch (Exception e) {
\r
115 logger.debug("Server shut down complete.");
\r