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.BindException;
|
---|
8 | import java.net.InetAddress;
|
---|
9 | import java.net.ServerSocket;
|
---|
10 | import java.net.Socket;
|
---|
11 | import java.net.SocketException;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Simple HTTP server that spawns a {@link RequestProcessor} for every
|
---|
17 | * connection.
|
---|
18 | *
|
---|
19 | * Taken from YWMS plugin by frsantos.
|
---|
20 | */
|
---|
21 | public class RemoteControlHttpServer extends Thread {
|
---|
22 |
|
---|
23 | /** The server socket */
|
---|
24 | private ServerSocket server;
|
---|
25 |
|
---|
26 | private static RemoteControlHttpServer instance;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Starts or restarts the HTTP server
|
---|
30 | */
|
---|
31 | public static void restartRemoteControlHttpServer() {
|
---|
32 | int port = Main.pref.getInteger("remote.control.port", 8111);
|
---|
33 | try {
|
---|
34 | stopRemoteControlHttpServer();
|
---|
35 |
|
---|
36 | instance = new RemoteControlHttpServer(port);
|
---|
37 | instance.start();
|
---|
38 | } catch (BindException ex) {
|
---|
39 | Main.warn(marktr("Cannot start remotecontrol server on port {0}: {1}"),
|
---|
40 | Integer.toString(port), ex.getLocalizedMessage());
|
---|
41 | } catch (IOException ioe) {
|
---|
42 | Main.error(ioe);
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Stops the HTTP server
|
---|
48 | * @since 5861
|
---|
49 | */
|
---|
50 | public static void stopRemoteControlHttpServer() {
|
---|
51 | if (instance != null) {
|
---|
52 | try {
|
---|
53 | instance.stopServer();
|
---|
54 | instance = null;
|
---|
55 | } catch (IOException ioe) {
|
---|
56 | Main.error(ioe);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Constructor
|
---|
63 | * @param port The port this server will listen on
|
---|
64 | * @throws IOException when connection errors
|
---|
65 | */
|
---|
66 | public RemoteControlHttpServer(int port) throws IOException {
|
---|
67 | super("RemoteControl HTTP Server");
|
---|
68 | this.setDaemon(true);
|
---|
69 | // Start the server socket with only 1 connection.
|
---|
70 | // Also make sure we only listen
|
---|
71 | // on the local interface so nobody from the outside can connect!
|
---|
72 | // NOTE: On a dual stack machine with old Windows OS this may not listen on both interfaces!
|
---|
73 | this.server = new ServerSocket(port, 1,
|
---|
74 | InetAddress.getByName(Main.pref.get("remote.control.host", "localhost")));
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * The main loop, spawns a {@link RequestProcessor} for each connection
|
---|
79 | */
|
---|
80 | @Override
|
---|
81 | public void run() {
|
---|
82 | Main.info(marktr("RemoteControl::Accepting connections on port {0}"),
|
---|
83 | Integer.toString(server.getLocalPort()));
|
---|
84 | while (true) {
|
---|
85 | try {
|
---|
86 | @SuppressWarnings("resource")
|
---|
87 | Socket request = server.accept();
|
---|
88 | RequestProcessor.processRequest(request);
|
---|
89 | } catch (SocketException se) {
|
---|
90 | if (!server.isClosed())
|
---|
91 | Main.error(se);
|
---|
92 | } catch (IOException ioe) {
|
---|
93 | Main.error(ioe);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Stops the HTTP server
|
---|
100 | *
|
---|
101 | * @throws IOException
|
---|
102 | */
|
---|
103 | public void stopServer() throws IOException {
|
---|
104 | server.close();
|
---|
105 | Main.info(marktr("RemoteControl::Server stopped."));
|
---|
106 | }
|
---|
107 | }
|
---|