Ignore:
Timestamp:
2015-05-07T17:30:43+02:00 (9 years ago)
Author:
stoecker
Message:

fix #11409 - Remotecontrol on IPv6 and IPv4

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/RemoteControlHttpServer.java

    r8337 r8339  
    2020public class RemoteControlHttpServer extends Thread {
    2121
    22     /** The server socket for IPv4 */
    23     private ServerSocket server4 = null;
    24     /** The server socket for IPv6 */
    25     private ServerSocket server6 = null;
     22    /** The server socket */
     23    private ServerSocket server = null;
    2624
    27     private static volatile RemoteControlHttpServer instance;
     25    /** The server instance for IPv4 */
     26    private static volatile RemoteControlHttpServer instance4 = null;
     27    /** The server instance for IPv6 */
     28    private static volatile RemoteControlHttpServer instance6 = null;
    2829
    2930    /**
     
    3132     */
    3233    public static void restartRemoteControlHttpServer() {
     34        stopRemoteControlHttpServer();
    3335        int port = Main.pref.getInteger("remote.control.port", 8111);
    3436        try {
    35             stopRemoteControlHttpServer();
    36 
    37             instance = new RemoteControlHttpServer(port);
    38             instance.start();
    39         } catch (BindException ex) {
    40             Main.warn(marktr("Cannot start remotecontrol server on port {0}: {1}"),
     37            instance4 = new RemoteControlHttpServer(port, false);
     38            instance4.start();
     39        } catch (Exception ex) {
     40            Main.warn(marktr("Cannot start IPv4 remotecontrol server on port {0}: {1}"),
    4141                    Integer.toString(port), ex.getLocalizedMessage());
    42         } catch (IOException ioe) {
    43             Main.error(ioe);
     42        }
     43        try {
     44            instance6 = new RemoteControlHttpServer(port, true);
     45            instance6.start();
     46        } catch (Exception ex) {
     47            /* only show error when we also have no IPv4 */
     48            if(instance4 == null) {
     49                Main.warn(marktr("Cannot start IPv6 remotecontrol server on port {0}: {1}"),
     50                    Integer.toString(port), ex.getLocalizedMessage());
     51            }
    4452        }
    4553    }
     
    5058     */
    5159    public static void stopRemoteControlHttpServer() {
    52         if (instance != null) {
     60        if (instance4 != null) {
    5361            try {
    54                 instance.stopServer();
    55                 instance = null;
     62                instance4.stopServer();
     63                instance4 = null;
     64            } catch (IOException ioe) {
     65                Main.error(ioe);
     66            }
     67        }
     68        if (instance6 != null) {
     69            try {
     70                instance6.stopServer();
     71                instance6 = null;
    5672            } catch (IOException ioe) {
    5773                Main.error(ioe);
     
    6379     * Constructor
    6480     * @param port The port this server will listen on
     81     * @param ipv6 Whether IPv6 or IPv4 server should be started
    6582     * @throws IOException when connection errors
     83     * @since 8339
    6684     */
    67     public RemoteControlHttpServer(int port) throws IOException {
     85    public RemoteControlHttpServer(int port, boolean ipv6) throws IOException {
    6886        super("RemoteControl HTTP Server");
    6987        this.setDaemon(true);
    70         try {
    71             this.server4 = new ServerSocket(port, 1, RemoteControl.getInet4Address());
    72         } catch (IOException e) {
    73         }
    74         try {
    75             this.server6 = new ServerSocket(port, 1, RemoteControl.getInet6Address());
    76         } catch (IOException e) {
    77             if(this.server4 == null) /* both failed */
    78                 throw e;
    79         }
     88        this.server = new ServerSocket(port, 1, ipv6 ?
     89            RemoteControl.getInet6Address() : RemoteControl.getInet4Address());
    8090    }
    8191
     
    8595    @Override
    8696    public void run() {
    87         if(server4 != null) {
    88             Main.info(marktr("RemoteControl::Accepting IPv4 connections on {0}:{1}"),
    89                 server4.getInetAddress(), Integer.toString(server4.getLocalPort()));
    90         }
    91         if(server6 != null) {
    92             Main.info(marktr("RemoteControl::Accepting IPv6 connections on {0}:{1}"),
    93                 server6.getInetAddress(), Integer.toString(server6.getLocalPort()));
    94         }
     97        Main.info(marktr("RemoteControl::Accepting remote connections on {0}:{1}"),
     98                server.getInetAddress(), Integer.toString(server.getLocalPort()));
    9599        while (true) {
    96             if(server4 != null) {
    97                 try {
    98                     @SuppressWarnings("resource")
    99                     Socket request = server4.accept();
    100                     RequestProcessor.processRequest(request);
    101                 } catch (SocketException se) {
    102                     if (!server4.isClosed())
    103                         Main.error(se);
    104                 } catch (IOException ioe) {
    105                     Main.error(ioe);
    106                 }
    107             }
    108             if(server6 != null) {
    109                 try {
    110                     @SuppressWarnings("resource")
    111                     Socket request = server6.accept();
    112                     RequestProcessor.processRequest(request);
    113                 } catch (SocketException se) {
    114                     if (!server6.isClosed())
    115                         Main.error(se);
    116                 } catch (IOException ioe) {
    117                     Main.error(ioe);
    118                 }
     100            try {
     101                @SuppressWarnings("resource")
     102                Socket request = server.accept();
     103                RequestProcessor.processRequest(request);
     104            } catch (SocketException se) {
     105                if (!server.isClosed())
     106                    Main.error(se);
     107            } catch (IOException ioe) {
     108                Main.error(ioe);
    119109            }
    120110        }
     
    127117     */
    128118    public void stopServer() throws IOException {
    129         if(server4 != null)
    130             server4.close();
    131         if(server6 != null)
    132             server6.close();
    133         Main.info(marktr("RemoteControl::Server stopped."));
     119        Main.info(marktr("RemoteControl::Server {0}:{1} stopped."),
     120        server.getInetAddress(), Integer.toString(server.getLocalPort()));
     121        server.close();
    134122    }
    135123}
Note: See TracChangeset for help on using the changeset viewer.