source: josm/trunk/src/org/openstreetmap/josm/io/remotecontrol/RemoteControlHttpServer.java@ 12620

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 4.1 KB
RevLine 
[3707]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.remotecontrol;
3
[5893]4import static org.openstreetmap.josm.tools.I18n.marktr;
[4797]5
[3707]6import java.io.IOException;
7import java.net.ServerSocket;
8import java.net.Socket;
9import java.net.SocketException;
10
[5893]11import org.openstreetmap.josm.Main;
[12620]12import org.openstreetmap.josm.tools.Logging;
[5893]13
[3707]14/**
15 * Simple HTTP server that spawns a {@link RequestProcessor} for every
16 * connection.
17 *
18 * Taken from YWMS plugin by frsantos.
19 */
20public class RemoteControlHttpServer extends Thread {
21
[8339]22 /** The server socket */
[9078]23 private final ServerSocket server;
[3707]24
[8339]25 /** The server instance for IPv4 */
[8840]26 private static volatile RemoteControlHttpServer instance4;
[8339]27 /** The server instance for IPv6 */
[8840]28 private static volatile RemoteControlHttpServer instance6;
[3707]29
30 /**
31 * Starts or restarts the HTTP server
32 */
[4797]33 public static void restartRemoteControlHttpServer() {
[8339]34 stopRemoteControlHttpServer();
[5894]35 int port = Main.pref.getInteger("remote.control.port", 8111);
[4797]36 try {
[8339]37 instance4 = new RemoteControlHttpServer(port, false);
38 instance4.start();
[10212]39 } catch (IOException ex) {
[12620]40 Logging.debug(ex);
41 Logging.warn(marktr("Cannot start IPv4 remotecontrol server on port {0}: {1}"),
[5894]42 Integer.toString(port), ex.getLocalizedMessage());
[3707]43 }
[8339]44 try {
45 instance6 = new RemoteControlHttpServer(port, true);
46 instance6.start();
[10212]47 } catch (IOException ex) {
[8339]48 /* only show error when we also have no IPv4 */
[8510]49 if (instance4 == null) {
[12620]50 Logging.debug(ex);
51 Logging.warn(marktr("Cannot start IPv6 remotecontrol server on port {0}: {1}"),
[8339]52 Integer.toString(port), ex.getLocalizedMessage());
53 }
54 }
[3707]55 }
56
57 /**
[5861]58 * Stops the HTTP server
59 * @since 5861
60 */
61 public static void stopRemoteControlHttpServer() {
[8339]62 if (instance4 != null) {
[5861]63 try {
[8339]64 instance4.stopServer();
[5861]65 } catch (IOException ioe) {
[12620]66 Logging.error(ioe);
[5861]67 }
[8340]68 instance4 = null;
[5861]69 }
[8339]70 if (instance6 != null) {
71 try {
72 instance6.stopServer();
73 } catch (IOException ioe) {
[12620]74 Logging.error(ioe);
[8339]75 }
[8340]76 instance6 = null;
[8339]77 }
[5861]78 }
79
80 /**
[3707]81 * Constructor
82 * @param port The port this server will listen on
[8339]83 * @param ipv6 Whether IPv6 or IPv4 server should be started
[3707]84 * @throws IOException when connection errors
[8339]85 * @since 8339
[3707]86 */
[8339]87 public RemoteControlHttpServer(int port, boolean ipv6) throws IOException {
[3707]88 super("RemoteControl HTTP Server");
89 this.setDaemon(true);
[8339]90 this.server = new ServerSocket(port, 1, ipv6 ?
91 RemoteControl.getInet6Address() : RemoteControl.getInet4Address());
[3707]92 }
93
94 /**
95 * The main loop, spawns a {@link RequestProcessor} for each connection
96 */
[6084]97 @Override
[6643]98 public void run() {
[12620]99 Logging.info(marktr("RemoteControl::Accepting remote connections on {0}:{1}"),
[8339]100 server.getInetAddress(), Integer.toString(server.getLocalPort()));
[6643]101 while (true) {
[8339]102 try {
103 @SuppressWarnings("resource")
104 Socket request = server.accept();
105 RequestProcessor.processRequest(request);
[11620]106 } catch (SocketException e) {
[8966]107 if (!server.isClosed()) {
[12620]108 Logging.error(e);
[8966]109 } else {
110 // stop the thread automatically if server is stopped
111 return;
112 }
[8339]113 } catch (IOException ioe) {
[12620]114 Logging.error(ioe);
[3707]115 }
116 }
117 }
118
119 /**
120 * Stops the HTTP server
121 *
[8470]122 * @throws IOException if any I/O error occurs
[3707]123 */
[6643]124 public void stopServer() throws IOException {
[12620]125 Logging.info(marktr("RemoteControl::Server {0}:{1} stopped."),
[8339]126 server.getInetAddress(), Integer.toString(server.getLocalPort()));
127 server.close();
[3707]128 }
129}
Note: See TracBrowser for help on using the repository browser.