Author: bombe
Date: 2006-07-23 15:20:47 +0000 (Sun, 23 Jul 2006)
New Revision: 9732

Modified:
   trunk/freenet/src/freenet/clients/http/SimpleToadletServer.java
   trunk/freenet/src/freenet/io/NetworkInterface.java
   trunk/freenet/src/freenet/node/TextModeClientInterfaceServer.java
   trunk/freenet/src/freenet/node/fcp/FCPServer.java
Log:
make bindTo address changeable on the fly

Modified: trunk/freenet/src/freenet/clients/http/SimpleToadletServer.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/SimpleToadletServer.java     
2006-07-23 14:16:56 UTC (rev 9731)
+++ trunk/freenet/src/freenet/clients/http/SimpleToadletServer.java     
2006-07-23 15:20:47 UTC (rev 9732)
@@ -45,7 +45,7 @@
        }

        final int port;
-       final String bindTo;
+       String bindTo;
        String allowedHosts;
        final BucketFactory bf;
        final NetworkInterface networkInterface;
@@ -76,8 +76,14 @@
                }

                public void set(String bindTo) throws 
InvalidConfigValueException {
-                       if(!bindTo.equals(get()))
-                               throw new InvalidConfigValueException("Cannot 
change FProxy bind address on the fly");
+                       if(!bindTo.equals(get())) {
+                               try {
+                                       networkInterface.setBindTo(bindTo);
+                                       SimpleToadletServer.this.bindTo = 
bindTo;
+                               } catch (IOException e) {
+                                       throw new 
InvalidConfigValueException("could not change bind to! " + e.getMessage()); 
+                               }
+                       }
                }
        }


Modified: trunk/freenet/src/freenet/io/NetworkInterface.java
===================================================================
--- trunk/freenet/src/freenet/io/NetworkInterface.java  2006-07-23 14:16:56 UTC 
(rev 9731)
+++ trunk/freenet/src/freenet/io/NetworkInterface.java  2006-07-23 15:20:47 UTC 
(rev 9732)
@@ -24,13 +24,11 @@
 import java.net.SocketException;
 import java.net.SocketTimeoutException;
 import java.util.ArrayList;
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.StringTokenizer;
-import java.util.Vector;

 import freenet.io.AddressIdentifier.AddressType;
 import freenet.support.Logger;
@@ -59,15 +57,15 @@
        /** Queue of accepted client connections. */
        protected final List/* <Socket> */acceptedSockets = new ArrayList();

-       /**
-        * Whether the acceptors have already been started. Necessary for
-        * {@link #setSoTimeout(int)} to work.
-        */
-       private boolean started = false;
-
        /** The timeout set by {@link #setSoTimeout(int)}. */
        private int timeout = 0;

+       /** The port to bind to. */
+       private final int port;
+
+       /** The number of running acceptors. */
+       private int runningAcceptors = 0;
+
        /**
         * Creates a new network interface that can bind to several addresses 
and
         * allows connection filtering on IP address level.
@@ -78,18 +76,61 @@
         *            A comma-separated list of allowed addresses
         */
        public NetworkInterface(int port, String bindTo, String allowedHosts) 
throws IOException {
+               this.port = port;
+               setBindTo(bindTo);
+               setAllowedHosts(allowedHosts);
+       }
+
+       /**
+        * Sets the list of IP address this network interface binds to.
+        * 
+        * @param bindTo
+        *            A comma-separated list of IP address to bind to
+        */
+       public void setBindTo(String bindTo) throws IOException {
                StringTokenizer bindToTokens = new StringTokenizer(bindTo, ",");
                List bindToTokenList = new ArrayList();
                while (bindToTokens.hasMoreTokens()) {
                        bindToTokenList.add(bindToTokens.nextToken().trim());
                }
+               /* stop the old acceptors. */
+               for (int acceptorIndex = 0, acceptorCount = acceptors.size(); 
acceptorIndex < acceptorCount; acceptorIndex++) {
+                       Acceptor acceptor = (Acceptor) 
acceptors.get(acceptorIndex);
+                       try {
+                               acceptor.close();
+                       } catch (IOException e) {
+                               /* swallow exception. */
+                       }
+               }
+               while (runningAcceptors > 0) {
+                       synchronized (syncObject) {
+                               try {
+                                       syncObject.wait();
+                               } catch (InterruptedException e) {
+                               }
+                       }
+               }
+               acceptors.clear();
                for (int serverSocketIndex = 0; serverSocketIndex < 
bindToTokenList.size(); serverSocketIndex++) {
                        ServerSocket serverSocket = new ServerSocket();
                        serverSocket.bind(new InetSocketAddress((String) 
bindToTokenList.get(serverSocketIndex), port));
                        Acceptor acceptor = new Acceptor(serverSocket);
                        acceptors.add(acceptor);
                }
-               setAllowedHosts(allowedHosts);
+               setSoTimeout(timeout);
+               List tempThreads = new ArrayList();
+               synchronized (syncObject) {
+                       Iterator acceptors = this.acceptors.iterator();
+                       while (acceptors.hasNext()) {
+                               Thread t = new Thread((Acceptor) 
acceptors.next(), "Network Interface Acceptor");
+                               t.setDaemon(true);
+                               tempThreads.add(t);
+                       }
+               }
+               for (Iterator i = tempThreads.iterator(); i.hasNext();) {
+                       ((Thread) i.next()).start();
+                       runningAcceptors++;
+               }
        }

        /**
@@ -158,22 +199,7 @@
         *             if the timeout has expired waiting for a connection
         */
        public Socket accept() throws SocketTimeoutException {
-               Vector tempThreads = new Vector();
                synchronized (syncObject) {
-                       if (!started) {
-                               started = true;
-                               Iterator acceptors = this.acceptors.iterator();
-                               while (acceptors.hasNext()) {
-                                       Thread t = new Thread((Acceptor) 
acceptors.next(), "Network Interface Acceptor");
-                                       t.setDaemon(true);
-                                       tempThreads.add(t);
-                               }
-                       }
-               }
-               for(Enumeration e = tempThreads.elements(); 
e.hasMoreElements(); ) {
-                       ((Thread) e.nextElement()).start();
-               }
-               synchronized (syncObject) {
                        while (acceptedSockets.size() == 0) {
                                try {
                                        syncObject.wait(timeout);
@@ -211,6 +237,16 @@
        }

        /**
+        * Gets called by an acceptor if it has stopped.
+        */
+       private void acceptorStopped() {
+               synchronized (syncObject) {
+                       runningAcceptors--;
+                       syncObject.notifyAll();
+               }
+       }
+
+       /**
         * Wrapper around a {@link ServerSocket} that checks whether the 
incoming
         * connection is allowed.
         * 
@@ -304,9 +340,10 @@
                                } catch (SocketTimeoutException ste1) {
                                        Logger.minor(this, "Timeout");
                                } catch (IOException ioe1) {
-                                       Logger.minor(this, "Caught "+ioe1);
+                                       Logger.minor(this, "Caught " + ioe1);
                                }
                        }
+                       NetworkInterface.this.acceptorStopped();
                }

        }

Modified: trunk/freenet/src/freenet/node/TextModeClientInterfaceServer.java
===================================================================
--- trunk/freenet/src/freenet/node/TextModeClientInterfaceServer.java   
2006-07-23 14:16:56 UTC (rev 9731)
+++ trunk/freenet/src/freenet/node/TextModeClientInterfaceServer.java   
2006-07-23 15:20:47 UTC (rev 9732)
@@ -28,7 +28,7 @@
     final Hashtable streams;
     final File downloadsDir;
     int port;
-    final String bindTo;
+    String bindTo;
     String allowedHosts;
     boolean isEnabled;
     NetworkInterface networkInterface;
@@ -150,7 +150,12 @@

        public void set(String val) throws InvalidConfigValueException {
                if(val.equals(get())) return;
-               throw new InvalidConfigValueException("Cannot be updated on the 
fly");
+               try {
+                               
node.getTextModeClientInterface().networkInterface.setBindTo(val);
+                               node.getTextModeClientInterface().bindTo = val;
+                       } catch (IOException e) {
+                               throw new InvalidConfigValueException("could 
not change bind to!");
+                       }
        }
     }


Modified: trunk/freenet/src/freenet/node/fcp/FCPServer.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/FCPServer.java   2006-07-23 14:16:56 UTC 
(rev 9731)
+++ trunk/freenet/src/freenet/node/fcp/FCPServer.java   2006-07-23 15:20:47 UTC 
(rev 9732)
@@ -47,7 +47,7 @@
        final Node node;
        final int port;
        public final boolean enabled;
-       final String bindTo;
+       String bindTo;
        String allowedHosts;
        final WeakHashMap clientsByName;
        final FCPClient globalClient;
@@ -205,10 +205,14 @@
                        return node.getFCPServer().bindTo;
                }

-//TODO: Allow it
                public void set(String val) throws InvalidConfigValueException {
                        if(!val.equals(get())) {
-                               throw new InvalidConfigValueException("Cannot 
change the ip address the server is binded to on the fly");
+                               try {
+                                       
node.getFCPServer().networkInterface.setBindTo(val);
+                                       node.getFCPServer().bindTo = val;
+                               } catch (IOException e) {
+                                       throw new 
InvalidConfigValueException("could not change bind to!");
+                               }
                        }
                }
        }


Reply via email to