1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.net.ServerSocket;
|
---|
6 | import java.net.Socket;
|
---|
7 | import java.net.SocketException;
|
---|
8 | import java.net.InetAddress;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Simple HTTP server that spawns a {@link RequestProcessor} for every
|
---|
12 | * connection.
|
---|
13 | *
|
---|
14 | * Taken from YWMS plugin by frsantos.
|
---|
15 | */
|
---|
16 |
|
---|
17 | public class RemoteControlHttpServer extends Thread {
|
---|
18 |
|
---|
19 | /** Default port for the HTTP server */
|
---|
20 | public static final int DEFAULT_PORT = 8111;
|
---|
21 |
|
---|
22 | /** The server socket */
|
---|
23 | private ServerSocket server;
|
---|
24 |
|
---|
25 | private static RemoteControlHttpServer instance;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Starts or restarts the HTTP server
|
---|
29 | */
|
---|
30 | public static void restartRemoteControlHttpServer()
|
---|
31 | {
|
---|
32 | try
|
---|
33 | {
|
---|
34 | if (instance != null)
|
---|
35 | instance.stopServer();
|
---|
36 |
|
---|
37 | int port = DEFAULT_PORT;
|
---|
38 | instance = new RemoteControlHttpServer(port);
|
---|
39 | instance.start();
|
---|
40 | }
|
---|
41 | catch(IOException ioe)
|
---|
42 | {
|
---|
43 | ioe.printStackTrace();
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Constructor
|
---|
49 | * @param port The port this server will listen on
|
---|
50 | * @throws IOException when connection errors
|
---|
51 | */
|
---|
52 | public RemoteControlHttpServer(int port)
|
---|
53 | throws IOException
|
---|
54 | {
|
---|
55 | super("RemoteControl HTTP Server");
|
---|
56 | this.setDaemon(true);
|
---|
57 | // Start the server socket with only 1 connection.
|
---|
58 | // Also make sure we only listen
|
---|
59 | // on the local interface so nobody from the outside can connect!
|
---|
60 | this.server = new ServerSocket(port, 1,
|
---|
61 | InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }));
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * The main loop, spawns a {@link RequestProcessor} for each connection
|
---|
66 | */
|
---|
67 | public void run()
|
---|
68 | {
|
---|
69 | System.out.println("RemoteControl::Accepting connections on port " + server.getLocalPort());
|
---|
70 | while (true)
|
---|
71 | {
|
---|
72 | try
|
---|
73 | {
|
---|
74 | Socket request = server.accept();
|
---|
75 | RequestProcessor.processRequest(request);
|
---|
76 | }
|
---|
77 | catch( SocketException se)
|
---|
78 | {
|
---|
79 | if( !server.isClosed() )
|
---|
80 | se.printStackTrace();
|
---|
81 | }
|
---|
82 | catch (IOException ioe)
|
---|
83 | {
|
---|
84 | ioe.printStackTrace();
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Stops the HTTP server
|
---|
91 | *
|
---|
92 | * @throws IOException
|
---|
93 | */
|
---|
94 | public void stopServer() throws IOException
|
---|
95 | {
|
---|
96 | server.close();
|
---|
97 | }
|
---|
98 | }
|
---|