Changeset 8339 in josm for trunk/src/org/openstreetmap/josm/io/remotecontrol/RemoteControlHttpServer.java
- Timestamp:
- 2015-05-07T17:30:43+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/remotecontrol/RemoteControlHttpServer.java
r8337 r8339 20 20 public class RemoteControlHttpServer extends Thread { 21 21 22 /** The server socket for IPv4 */ 23 private ServerSocket server4 = null; 24 /** The server socket for IPv6 */ 25 private ServerSocket server6 = null; 22 /** The server socket */ 23 private ServerSocket server = null; 26 24 27 private static volatile RemoteControlHttpServer instance; 25 /** The server instance for IPv4 */ 26 private static volatile RemoteControlHttpServer instance4 = null; 27 /** The server instance for IPv6 */ 28 private static volatile RemoteControlHttpServer instance6 = null; 28 29 29 30 /** … … 31 32 */ 32 33 public static void restartRemoteControlHttpServer() { 34 stopRemoteControlHttpServer(); 33 35 int port = Main.pref.getInteger("remote.control.port", 8111); 34 36 try { 35 stopRemoteControlHttpServer(); 36 37 instance = new RemoteControlHttpServer(port); 38 instance.start(); 39 } catch (BindException ex) { 40 Main.warn(marktr("Cannot start remotecontrol server on port {0}: {1}"), 37 instance4 = new RemoteControlHttpServer(port, false); 38 instance4.start(); 39 } catch (Exception ex) { 40 Main.warn(marktr("Cannot start IPv4 remotecontrol server on port {0}: {1}"), 41 41 Integer.toString(port), ex.getLocalizedMessage()); 42 } catch (IOException ioe) { 43 Main.error(ioe); 42 } 43 try { 44 instance6 = new RemoteControlHttpServer(port, true); 45 instance6.start(); 46 } catch (Exception ex) { 47 /* only show error when we also have no IPv4 */ 48 if(instance4 == null) { 49 Main.warn(marktr("Cannot start IPv6 remotecontrol server on port {0}: {1}"), 50 Integer.toString(port), ex.getLocalizedMessage()); 51 } 44 52 } 45 53 } … … 50 58 */ 51 59 public static void stopRemoteControlHttpServer() { 52 if (instance != null) {60 if (instance4 != null) { 53 61 try { 54 instance.stopServer(); 55 instance = null; 62 instance4.stopServer(); 63 instance4 = null; 64 } catch (IOException ioe) { 65 Main.error(ioe); 66 } 67 } 68 if (instance6 != null) { 69 try { 70 instance6.stopServer(); 71 instance6 = null; 56 72 } catch (IOException ioe) { 57 73 Main.error(ioe); … … 63 79 * Constructor 64 80 * @param port The port this server will listen on 81 * @param ipv6 Whether IPv6 or IPv4 server should be started 65 82 * @throws IOException when connection errors 83 * @since 8339 66 84 */ 67 public RemoteControlHttpServer(int port ) throws IOException {85 public RemoteControlHttpServer(int port, boolean ipv6) throws IOException { 68 86 super("RemoteControl HTTP Server"); 69 87 this.setDaemon(true); 70 try { 71 this.server4 = new ServerSocket(port, 1, RemoteControl.getInet4Address()); 72 } catch (IOException e) { 73 } 74 try { 75 this.server6 = new ServerSocket(port, 1, RemoteControl.getInet6Address()); 76 } catch (IOException e) { 77 if(this.server4 == null) /* both failed */ 78 throw e; 79 } 88 this.server = new ServerSocket(port, 1, ipv6 ? 89 RemoteControl.getInet6Address() : RemoteControl.getInet4Address()); 80 90 } 81 91 … … 85 95 @Override 86 96 public void run() { 87 if(server4 != null) { 88 Main.info(marktr("RemoteControl::Accepting IPv4 connections on {0}:{1}"), 89 server4.getInetAddress(), Integer.toString(server4.getLocalPort())); 90 } 91 if(server6 != null) { 92 Main.info(marktr("RemoteControl::Accepting IPv6 connections on {0}:{1}"), 93 server6.getInetAddress(), Integer.toString(server6.getLocalPort())); 94 } 97 Main.info(marktr("RemoteControl::Accepting remote connections on {0}:{1}"), 98 server.getInetAddress(), Integer.toString(server.getLocalPort())); 95 99 while (true) { 96 if(server4 != null) { 97 try { 98 @SuppressWarnings("resource") 99 Socket request = server4.accept(); 100 RequestProcessor.processRequest(request); 101 } catch (SocketException se) { 102 if (!server4.isClosed()) 103 Main.error(se); 104 } catch (IOException ioe) { 105 Main.error(ioe); 106 } 107 } 108 if(server6 != null) { 109 try { 110 @SuppressWarnings("resource") 111 Socket request = server6.accept(); 112 RequestProcessor.processRequest(request); 113 } catch (SocketException se) { 114 if (!server6.isClosed()) 115 Main.error(se); 116 } catch (IOException ioe) { 117 Main.error(ioe); 118 } 100 try { 101 @SuppressWarnings("resource") 102 Socket request = server.accept(); 103 RequestProcessor.processRequest(request); 104 } catch (SocketException se) { 105 if (!server.isClosed()) 106 Main.error(se); 107 } catch (IOException ioe) { 108 Main.error(ioe); 119 109 } 120 110 } … … 127 117 */ 128 118 public void stopServer() throws IOException { 129 if(server4 != null) 130 server4.close(); 131 if(server6 != null) 132 server6.close(); 133 Main.info(marktr("RemoteControl::Server stopped.")); 119 Main.info(marktr("RemoteControl::Server {0}:{1} stopped."), 120 server.getInetAddress(), Integer.toString(server.getLocalPort())); 121 server.close(); 134 122 } 135 123 }
Note:
See TracChangeset
for help on using the changeset viewer.