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

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.remotecontrol;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5
6import java.io.IOException;
7import java.net.BindException;
8import java.net.ServerSocket;
9import java.net.Socket;
10import java.net.SocketException;
11import java.net.InetAddress;
12
13import 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 */
21public 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("Warning: Cannot start remotecontrol server on port {0}: {1}"),
40 Integer.toString(port), ex.getLocalizedMessage());
41 } catch (IOException ioe) {
42 ioe.printStackTrace();
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 ioe.printStackTrace();
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)
67 throws IOException
68 {
69 super("RemoteControl HTTP Server");
70 this.setDaemon(true);
71 // Start the server socket with only 1 connection.
72 // Also make sure we only listen
73 // on the local interface so nobody from the outside can connect!
74 // NOTE: On a dual stack machine with old Windows OS this may not listen on both interfaces!
75 this.server = new ServerSocket(port, 1,
76 InetAddress.getByName(Main.pref.get("remote.control.host", "localhost")));
77 }
78
79 /**
80 * The main loop, spawns a {@link RequestProcessor} for each connection
81 */
82 @Override
83 public void run()
84 {
85 Main.info(marktr("RemoteControl::Accepting connections on port {0}"),
86 Integer.toString(server.getLocalPort()));
87 while (true)
88 {
89 try
90 {
91 Socket request = server.accept();
92 RequestProcessor.processRequest(request);
93 }
94 catch( SocketException se)
95 {
96 if( !server.isClosed() )
97 se.printStackTrace();
98 }
99 catch (IOException ioe)
100 {
101 ioe.printStackTrace();
102 }
103 }
104 }
105
106 /**
107 * Stops the HTTP server
108 *
109 * @throws IOException
110 */
111 public void stopServer() throws IOException
112 {
113 server.close();
114 Main.info(marktr("RemoteControl::Server stopped."));
115 }
116}
Note: See TracBrowser for help on using the repository browser.