applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / picking / PickServer.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \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
13  *  \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
18  * \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
21  */\r
22 package uk.ac.vamsas.client.picking;\r
23 \r
24 import java.io.*;\r
25 import java.net.*;\r
26 import java.util.logging.*;\r
27 \r
28 import org.apache.commons.logging.Log;\r
29 \r
30 /**\r
31  * Server class that listens for incoming connections on a predefined port.\r
32  */\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
36 \r
37   // The port number we'll listen on\r
38   static final int PORT = 53782;\r
39 \r
40   private ServerSocket serverSocket;\r
41 \r
42   // Set to true once the server is established\r
43   private boolean isServer = false;\r
44 \r
45   // Set to false when server is shutting down\r
46   private boolean isAlive = true;\r
47 \r
48   private SocketManager manager;\r
49 \r
50   /**\r
51    * Constructs a new instance of the server (but doesn't start it).\r
52    * \r
53    * @param manager\r
54    *          a reference to the pick manager that owns this server\r
55    */\r
56   PickServer(SocketManager manager) {\r
57     this.manager = manager;\r
58   }\r
59 \r
60   /**\r
61    * Returns true if this server instance is running. Return true if this server\r
62    * instance is running\r
63    */\r
64   boolean isServer() {\r
65     return isServer;\r
66   }\r
67 \r
68   /**\r
69    * Attempts to create the server by opening a server socket on the port.\r
70    * \r
71    * @return true if the server was created; false otherwise\r
72    */\r
73   boolean createServer() {\r
74     try {\r
75       serverSocket = new ServerSocket(PORT);\r
76       start();\r
77 \r
78       return isServer = true;\r
79     } catch (IOException e) {\r
80       logger.debug("SERVER: " + e);\r
81       return false;\r
82     }\r
83   }\r
84 \r
85   /**\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
89    */\r
90   public void run() {\r
91     logger.debug("SERVER: listening on " + PORT + " - SERVER");\r
92 \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
95     // is ended\r
96     while (isAlive) {\r
97       try {\r
98         Socket socket = serverSocket.accept();\r
99         logger.info("SERVER: connection detected");\r
100         if (isAlive)\r
101           manager.addEndPoint(socket);\r
102       } catch (IOException e) {\r
103       }\r
104     }\r
105   }\r
106 \r
107   void terminate() {\r
108     logger.debug("Server shutting down...");\r
109     isAlive = false;\r
110     try {\r
111       serverSocket.close();\r
112     } catch (Exception e) {\r
113       logger.error(e);\r
114     }\r
115     logger.debug("Server shut down complete.");\r
116   }\r
117 }\r