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

Last change on this file since 3965 was 3707, checked in by bastiK, 13 years ago

Added remotecontrol plugin to josm core. This plugin was initially written by Frederik Ramm (frederik) and incorporates code taken from YWMS plugin by frsantos. Major contributions by Bodo Meissner (bomm) and stephankn. Note: The code has been added, but is not "active" yet (RemoteControl.on == false). This is because wmsplugin and imagery plugin has not been adapted to this change. They are about to be integrated as well, so this adaption is not necessary.

  • Property svn:eol-style set to native
File size: 2.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.remotecontrol;
3
4import java.io.IOException;
5import java.net.ServerSocket;
6import java.net.Socket;
7import java.net.SocketException;
8import 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
17public 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}
Note: See TracBrowser for help on using the repository browser.