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