Author: toad
Date: 2008-06-14 13:33:23 +0000 (Sat, 14 Jun 2008)
New Revision: 20338

Modified:
   branches/db4o/freenet/src/freenet/node/fcp/FCPClient.java
Log:
Make currentConnection transient.
Make clientsWatching transient, lazily init it, and sync on an sync object.

Modified: branches/db4o/freenet/src/freenet/node/fcp/FCPClient.java
===================================================================
--- branches/db4o/freenet/src/freenet/node/fcp/FCPClient.java   2008-06-14 
13:27:01 UTC (rev 20337)
+++ branches/db4o/freenet/src/freenet/node/fcp/FCPClient.java   2008-06-14 
13:33:23 UTC (rev 20338)
@@ -34,7 +34,6 @@
                assert(persistenceType == ClientRequest.PERSIST_FOREVER || 
persistenceType == ClientRequest.PERSIST_REBOOT);
                defaultFetchContext = client.getFetchContext();
                defaultInsertContext = client.getInsertContext(false);
-               clientsWatching = new LinkedList();
                watchGlobalVerbosityMask = Integer.MAX_VALUE;
                toStart = new LinkedList();
                lowLevelClientPersistent = new RequestClient() {
@@ -55,7 +54,7 @@
        /** The FCPServer */
        final FCPServer server;
        /** The current connection handler, if any. */
-       private FCPConnectionHandler currentConnection;
+       private transient FCPConnectionHandler currentConnection;
        /** Currently running persistent requests */
        private final HashSet runningPersistentRequests;
        /** Completed unacknowledged persistent requests */
@@ -72,10 +71,9 @@
        /** Are we watching the global queue? */
        boolean watchGlobal;
        int watchGlobalVerbosityMask;
-       /** FCPClients watching us */
-       // FIXME how do we lazily init this without synchronization problems?
-       // We obviously can't synchronize on it when it hasn't been constructed 
yet...
-       final LinkedList clientsWatching;
+       /** FCPClients watching us. Lazy init, sync on clientsWatchingLock */
+       private transient LinkedList clientsWatching;
+       private final Object clientsWatchingLock = new Object();
        private final LinkedList toStart;
        final RequestClient lowLevelClientPersistent;
        final RequestClient lowLevelClientTransient;
@@ -210,13 +208,16 @@
                        return;
                }
                if(watchGlobal && !enabled) {
-                       server.globalClient.unwatch(this);
+                       server.globalRebootClient.unwatch(this);
+                       server.globalForeverClient.unwatch(this);
                        watchGlobal = false;
                } else if(enabled && !watchGlobal) {
-                       server.globalClient.watch(this);
+                       server.globalRebootClient.watch(this);
+                       server.globalForeverClient.watch(this);
                        FCPConnectionHandler connHandler = getConnection();
                        if(connHandler != null) {
-                               
server.globalClient.queuePendingMessagesOnConnectionRestart(connHandler.outputHandler);
+                               
server.globalRebootClient.queuePendingMessagesOnConnectionRestart(connHandler.outputHandler);
+                               
server.globalForeverClient.queuePendingMessagesOnConnectionRestart(connHandler.outputHandler);
                        }
                        watchGlobal = true;
                }
@@ -233,9 +234,13 @@
                }
                FCPClient[] clients;
                if(isGlobalQueue) {
-                       synchronized(clientsWatching) {
+                       synchronized(clientsWatchingLock) {
+                               if(clientsWatching != null)
                                clients = (FCPClient[]) 
clientsWatching.toArray(new FCPClient[clientsWatching.size()]);
+                               else
+                                       clients = null;
                        }
+                       if(clients != null)
                        for(int i=0;i<clients.length;i++)
                                clients[i].queueClientRequestMessage(msg, 
verbosityLevel);
                }
@@ -243,14 +248,17 @@

        private void unwatch(FCPClient client) {
                if(!isGlobalQueue) return;
-               synchronized(clientsWatching) {
+               synchronized(clientsWatchingLock) {
+                       if(clientsWatching != null)
                        clientsWatching.remove(client);
                }
        }

        private void watch(FCPClient client) {
                if(!isGlobalQueue) return;
-               synchronized(clientsWatching) {
+               synchronized(clientsWatchingLock) {
+                       if(clientsWatching == null)
+                               clientsWatching = new LinkedList();
                        clientsWatching.add(client);
                }
        }


Reply via email to