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

Last change on this file since 11620 was 11620, checked in by Don-vip, 7 years ago

checkstyle - enable CatchParameterName rule

  • Property svn:eol-style set to native
File size: 4.0 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.ServerSocket;
8import java.net.Socket;
9import java.net.SocketException;
10
11import 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 */
19public class RemoteControlHttpServer extends Thread {
20
21 /** The server socket */
22 private final ServerSocket server;
23
24 /** The server instance for IPv4 */
25 private static volatile RemoteControlHttpServer instance4;
26 /** The server instance for IPv6 */
27 private static volatile RemoteControlHttpServer instance6;
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 (IOException ex) {
39 Main.debug(ex);
40 Main.warn(marktr("Cannot start IPv4 remotecontrol server on port {0}: {1}"),
41 Integer.toString(port), ex.getLocalizedMessage());
42 }
43 try {
44 instance6 = new RemoteControlHttpServer(port, true);
45 instance6.start();
46 } catch (IOException ex) {
47 /* only show error when we also have no IPv4 */
48 if (instance4 == null) {
49 Main.debug(ex);
50 Main.warn(marktr("Cannot start IPv6 remotecontrol server on port {0}: {1}"),
51 Integer.toString(port), ex.getLocalizedMessage());
52 }
53 }
54 }
55
56 /**
57 * Stops the HTTP server
58 * @since 5861
59 */
60 public static void stopRemoteControlHttpServer() {
61 if (instance4 != null) {
62 try {
63 instance4.stopServer();
64 } catch (IOException ioe) {
65 Main.error(ioe);
66 }
67 instance4 = null;
68 }
69 if (instance6 != null) {
70 try {
71 instance6.stopServer();
72 } catch (IOException ioe) {
73 Main.error(ioe);
74 }
75 instance6 = null;
76 }
77 }
78
79 /**
80 * Constructor
81 * @param port The port this server will listen on
82 * @param ipv6 Whether IPv6 or IPv4 server should be started
83 * @throws IOException when connection errors
84 * @since 8339
85 */
86 public RemoteControlHttpServer(int port, boolean ipv6) throws IOException {
87 super("RemoteControl HTTP Server");
88 this.setDaemon(true);
89 this.server = new ServerSocket(port, 1, ipv6 ?
90 RemoteControl.getInet6Address() : RemoteControl.getInet4Address());
91 }
92
93 /**
94 * The main loop, spawns a {@link RequestProcessor} for each connection
95 */
96 @Override
97 public void run() {
98 Main.info(marktr("RemoteControl::Accepting remote connections on {0}:{1}"),
99 server.getInetAddress(), Integer.toString(server.getLocalPort()));
100 while (true) {
101 try {
102 @SuppressWarnings("resource")
103 Socket request = server.accept();
104 RequestProcessor.processRequest(request);
105 } catch (SocketException e) {
106 if (!server.isClosed()) {
107 Main.error(e);
108 } else {
109 // stop the thread automatically if server is stopped
110 return;
111 }
112 } catch (IOException ioe) {
113 Main.error(ioe);
114 }
115 }
116 }
117
118 /**
119 * Stops the HTTP server
120 *
121 * @throws IOException if any I/O error occurs
122 */
123 public void stopServer() throws IOException {
124 Main.info(marktr("RemoteControl::Server {0}:{1} stopped."),
125 server.getInetAddress(), Integer.toString(server.getLocalPort()));
126 server.close();
127 }
128}
Note: See TracBrowser for help on using the repository browser.