Author: toad
Date: 2006-02-21 19:02:58 +0000 (Tue, 21 Feb 2006)
New Revision: 8091

Added:
   
trunk/freenet/src/freenet/node/fcp/CloseConnectionDuplicateClientNameMessage.java
   trunk/freenet/src/freenet/node/fcp/FCPClient.java
Modified:
   trunk/freenet/src/freenet/node/Version.java
   trunk/freenet/src/freenet/node/fcp/FCPConnectionHandler.java
   trunk/freenet/src/freenet/node/fcp/FCPServer.java
Log:
461:
Track FCP clients by Name.
Only one FCP connection is allowed at any given time with the same Name.
This may break clients but is necessary for persistent requests etc. Sorry 
folks.

Modified: trunk/freenet/src/freenet/node/Version.java
===================================================================
--- trunk/freenet/src/freenet/node/Version.java 2006-02-21 18:34:11 UTC (rev 
8090)
+++ trunk/freenet/src/freenet/node/Version.java 2006-02-21 19:02:58 UTC (rev 
8091)
@@ -20,7 +20,7 @@
        public static final String protocolVersion = "1.0";

        /** The build number of the current revision */
-       private static final int buildNumber = 460;
+       private static final int buildNumber = 461;

        /** Oldest build of Fred we will talk to */
        private static final int lastGoodBuild = 403;

Added: 
trunk/freenet/src/freenet/node/fcp/CloseConnectionDuplicateClientNameMessage.java
===================================================================
--- 
trunk/freenet/src/freenet/node/fcp/CloseConnectionDuplicateClientNameMessage.java
   2006-02-21 18:34:11 UTC (rev 8090)
+++ 
trunk/freenet/src/freenet/node/fcp/CloseConnectionDuplicateClientNameMessage.java
   2006-02-21 19:02:58 UTC (rev 8091)
@@ -0,0 +1,25 @@
+package freenet.node.fcp;
+
+import freenet.node.Node;
+import freenet.support.SimpleFieldSet;
+
+/** Error sent when the connection is closed because another connection with 
the same
+ * client Name has been opened. Usually the client will not see this, because 
it is being
+ * sent to a dead connection.
+ */
+public class CloseConnectionDuplicateClientNameMessage extends FCPMessage {
+
+       public SimpleFieldSet getFieldSet() {
+               return new SimpleFieldSet(false);
+       }
+
+       public String getName() {
+               return "CloseConnectionDuplicateClientName";
+       }
+
+       public void run(FCPConnectionHandler handler, Node node)
+                       throws MessageInvalidException {
+               throw new 
MessageInvalidException(ProtocolErrorMessage.INVALID_MESSAGE, 
"CloseConnectionDuplicateClientName goes from server to client not the other 
way around", null);
+       }
+
+}

Added: trunk/freenet/src/freenet/node/fcp/FCPClient.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/FCPClient.java   2006-02-21 18:34:11 UTC 
(rev 8090)
+++ trunk/freenet/src/freenet/node/fcp/FCPClient.java   2006-02-21 19:02:58 UTC 
(rev 8091)
@@ -0,0 +1,27 @@
+package freenet.node.fcp;
+
+/**
+ * An FCP client.
+ * Identified by its Name which is sent on connection.
+ */
+public class FCPClient {
+
+       public FCPClient(String name2, FCPConnectionHandler handler) {
+               this.name = name2;
+               this.currentConnection = handler;
+       }
+       
+       /** The client's Name sent in the ClientHello message */
+       final String name;
+       /** The current connection handler, if any. */
+       private FCPConnectionHandler currentConnection;
+       
+       public FCPConnectionHandler getConnection() {
+               return currentConnection;
+       }
+
+       public void setConnection(FCPConnectionHandler handler) {
+               this.currentConnection = handler;
+       }
+       
+}

Modified: trunk/freenet/src/freenet/node/fcp/FCPConnectionHandler.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/FCPConnectionHandler.java        
2006-02-21 18:34:11 UTC (rev 8090)
+++ trunk/freenet/src/freenet/node/fcp/FCPConnectionHandler.java        
2006-02-21 19:02:58 UTC (rev 8091)
@@ -13,6 +13,7 @@

 public class FCPConnectionHandler {

+       final FCPServer server;
        final Socket sock;
        final FCPConnectionInputHandler inputHandler;
        final FCPConnectionOutputHandler outputHandler;
@@ -21,14 +22,16 @@
        private boolean inputClosed;
        private boolean outputClosed;
        private String clientName;
+       private FCPClient client;
        final BucketFactory bf;
        final HashMap requestsByIdentifier;
        final FetcherContext defaultFetchContext;
        public InserterContext defaultInsertContext;

-       public FCPConnectionHandler(Socket s, Node node) {
+       public FCPConnectionHandler(Socket s, Node node, FCPServer server) {
                this.sock = s;
                this.node = node;
+               this.server = server;
                isClosed = false;
                this.bf = node.tempBucketFactory;
                requestsByIdentifier = new HashMap();
@@ -91,6 +94,7 @@

        public void setClientName(String name) {
                this.clientName = name;
+               client = server.registerClient(name, this);
        }

        public String getClientName() {

Modified: trunk/freenet/src/freenet/node/fcp/FCPServer.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/FCPServer.java   2006-02-21 18:34:11 UTC 
(rev 8090)
+++ trunk/freenet/src/freenet/node/fcp/FCPServer.java   2006-02-21 19:02:58 UTC 
(rev 8091)
@@ -4,6 +4,7 @@
 import java.net.InetAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.util.WeakHashMap;

 import freenet.config.Config;
 import freenet.config.IntCallback;
@@ -20,11 +21,13 @@
        final ServerSocket sock;
        final Node node;
        final int port;
+       final WeakHashMap clientsByName;

        public FCPServer(int port, Node node) throws IOException {
                this.port = port;
                this.sock = new ServerSocket(port, 0, 
InetAddress.getByName("127.0.0.1"));
                this.node = node;
+               clientsByName = new WeakHashMap();
                Thread t = new Thread(this, "FCP server");
                t.setDaemon(true);
                t.start();
@@ -45,7 +48,7 @@
        private void realRun() throws IOException {
                // Accept a connection
                Socket s = sock.accept();
-               FCPConnectionHandler handler = new FCPConnectionHandler(s, 
node);
+               FCPConnectionHandler handler = new FCPConnectionHandler(s, 
node, this);
        }

        static class FCPPortNumberCallback implements IntCallback {
@@ -79,4 +82,27 @@
                return fcp;
        }

+       public synchronized FCPClient registerClient(String name, 
FCPConnectionHandler handler) {
+               FCPClient oldClient = (FCPClient) clientsByName.get(name);
+               if(oldClient == null) {
+                       // Create new client
+                       FCPClient client = new FCPClient(name, handler);
+                       clientsByName.put(name, client);
+                       return client;
+               } else {
+                       FCPConnectionHandler oldConn = 
oldClient.getConnection();
+                       // Have existing client
+                       if(oldConn == null) {
+                               // Easy
+                               oldClient.setConnection(handler);
+                       } else {
+                               // Kill old connection
+                               oldConn.outputHandler.queue(new 
CloseConnectionDuplicateClientNameMessage());
+                               oldConn.close();
+                               oldClient.setConnection(handler);
+                       }
+                       return oldClient;
+               }
+       }
+
 }


Reply via email to