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