Author: xor
Date: 2008-11-17 00:12:46 +0000 (Mon, 17 Nov 2008)
New Revision: 23668

Modified:
   trunk/plugins/WoT/WoT.java
   trunk/plugins/WoT/introduction/IntroductionClient.java
   trunk/plugins/WoT/introduction/IntroductionServer.java
Log:
General cleanup of the code structure, no logical modifications.

Modified: trunk/plugins/WoT/WoT.java
===================================================================
--- trunk/plugins/WoT/WoT.java  2008-11-16 23:01:34 UTC (rev 23667)
+++ trunk/plugins/WoT/WoT.java  2008-11-17 00:12:46 UTC (rev 23668)
@@ -69,53 +69,56 @@
 import freenet.support.SimpleFieldSet;
 import freenet.support.api.Bucket;
 import freenet.support.api.HTTPRequest;
+import freenet.support.io.TempBucketFactory;
 
 /**
  * @author Julien Cornuwel ([EMAIL PROTECTED])
  */
 public class WoT implements FredPlugin, FredPluginHTTP, FredPluginThreadless, 
FredPluginFCP, FredPluginVersioned, FredPluginL10n {
        
+       public static final String SELF_URI = "/plugins/plugins.WoT.WoT";
+       public static final String WOT_CONTEXT = "WoT";
+       private static final String seedURI = "[EMAIL 
PROTECTED],fQeN-RMQZsUrDha2LCJWOMFk1-EiXZxfTnBT8NEgY00,AQACAAE/WoT/1";
+       
        /* References from the node */
        
        private PluginRespirator pr;
        private HighLevelSimpleClient client;
+       private TempBucketFactory tbf;
        private PageMaker pm;
+       private Random random;
        
        /* References from the plugin itself */
        
+       /* Database & configuration of the plugin */
        private ObjectContainer db;
-       private WebInterface web;
+       private Config config;
+
+       /* Worker objects which actually run the plugin */
        private IdentityInserter inserter;
        private IdentityFetcher fetcher;
        private IntroductionServer introductionServer;
        private IntroductionClient introductionClient;
-       
-       private String seedURI = "[EMAIL 
PROTECTED],fQeN-RMQZsUrDha2LCJWOMFk1-EiXZxfTnBT8NEgY00,AQACAAE/WoT/1";
        private Identity seed = null;
-       private Config config;
-
-       public static final String SELF_URI = "/plugins/plugins.WoT.WoT";
        
-       public static final String WOT_CONTEXT = "WoT";
-       
-       public Random random;
+       private WebInterface web;
 
-       public void runPlugin(PluginRespirator pr) {
-
+       public void runPlugin(PluginRespirator myPR) {
                Logger.debug(this, "Start");
                
                /* Catpcha generation needs headless mode on linux */
                System.setProperty("java.awt.headless", "true"); 
 
-               this.db = initDB();
-               this.pr = pr;
+               pr = myPR;
                client = pr.getHLSimpleClient();
+               tbf = pr.getNode().clientCore.tempBucketFactory;
+               pm = pr.getPageMaker();
+               random = pr.getNode().fastWeakRandom;
+               
+               db = initDB();
                config = initConfig();
                seed = getSeedIdentity();
 
-               pm = pr.getPageMaker();
-               random = pr.getNode().fastWeakRandom;
-
                /* FIXME: i cannot get this to work, it does not print any 
objects although there are definitely IntroductionPuzzle objects in my db.
                
                HashSet<Class> WoTclasses = new 
HashSet<Class>(Arrays.asList(new Class[]{ Identity.class, OwnIdentity.class, 
Trust.class, Score.class }));
@@ -128,9 +131,6 @@
                        Logger.debug(this, o.toString());
                }
                */
-               
-               // Should disappear soon.
-               web = new WebInterface(pr, db, config, client, SELF_URI);
 
                // Create a default OwnIdentity if none exists. Should speed up 
plugin usability for newbies
                if(OwnIdentity.getNbOwnIdentities(db) == 0) {
@@ -157,10 +157,10 @@
                }
                catch(InvalidParameterException e) {}
                
-               introductionServer = new IntroductionServer(this, 
pr.getNode().clientCore.tempBucketFactory, fetcher);
+               introductionServer = new IntroductionServer(this, fetcher);
                pr.getNode().executor.execute(introductionServer, "WoT 
introduction server");
                
-               introductionClient = new IntroductionClient(this, 
pr.getNode().clientCore.tempBucketFactory);
+               introductionClient = new IntroductionClient(this);
                pr.getNode().executor.execute(introductionClient, "WoT 
introduction client");
                
                // Try to fetch all known identities
@@ -168,6 +168,8 @@
                while (identities.hasNext()) {
                        fetcher.fetch(identities.next(), true);
                }
+               
+               web = new WebInterface(pr, db, config, client, SELF_URI);
        }
        
        public void terminate() {
@@ -880,19 +882,27 @@
                return seed;
        }
        
+       public PluginRespirator getPR() {
+               return pr;
+       }
+       
+       public HighLevelSimpleClient getClient() {
+               return client;
+       }
+
+       public TempBucketFactory getTBF() {
+               return tbf;
+       }
+       
        public PageMaker getPageMaker() {
                return pm;
        }
        
+       public Random getRandom() {
+               return random;
+       }
+       
        public ObjectContainer getDB() {
                return db;
        }
-       
-       public PluginRespirator getPR() {
-               return pr;
-       }
-       
-       public HighLevelSimpleClient getClient() {
-               return client;
-       }
 }

Modified: trunk/plugins/WoT/introduction/IntroductionClient.java
===================================================================
--- trunk/plugins/WoT/introduction/IntroductionClient.java      2008-11-16 
23:01:34 UTC (rev 23667)
+++ trunk/plugins/WoT/introduction/IntroductionClient.java      2008-11-17 
00:12:46 UTC (rev 23668)
@@ -12,6 +12,7 @@
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Random;
 import java.util.concurrent.ArrayBlockingQueue;
 
 import javax.xml.parsers.ParserConfigurationException;
@@ -65,32 +66,41 @@
         * insert one puzzle per day. But it might be a good idea to allow many 
puzzles per identity: Our seed identity could be configured to
         * insert a very large amount of puzzles and therefore help the WoT 
while it is small */
        public static final int MAX_PUZZLES_PER_IDENTITY = 
IntroductionServer.PUZZLE_COUNT;
+       private static final int MINIMUM_SCORE_FOR_PUZZLE_DOWNLOAD = 30; /* 
FIXME: tweak before release */
+       private static final int MINIMUM_SCORE_FOR_PUZZLE_DISPLAY = 30; /* 
FIXME: tweak before release */
 
-       private static final int MINIMUM_SCORE_FOR_PUZZLE_DOWNLOAD = 10; /* 
FIXME: tweak before release */
        
-       private static final int MINIMUM_SCORE_FOR_PUZZLE_DISPLAY = 50; /* 
FIXME: tweak before release */
+       /* Objects from the node */
 
-       private Thread mThread;
+       /** A reference the HighLevelSimpleClient used to perform inserts */
+       private HighLevelSimpleClient mClient;
        
-       /** Used to tell the introduction server thread if it should stop */
-       private volatile boolean isRunning;
+       /** The TempBucketFactory used to create buckets from puzzles before 
insert */
+       private final TempBucketFactory mTBF;
        
+       
+       /* Objects from WoT */
+       
        private WoT mWoT;
        
        /** A reference to the database */
        private ObjectContainer db;
-
-       /** A reference the HighLevelSimpleClient used to perform inserts */
-       private HighLevelSimpleClient mClient;
        
-       /** The TempBucketFactory used to create buckets from puzzles before 
insert */
-       private final TempBucketFactory mTBF;
+       /** Random number generator */
+       private Random mRandom;
        
-       /** All current requests */
+       
+       /* Private objects */
+       
+       private Thread mThread;
+       
+       /** Used to tell the introduction server thread if it should stop */
+       private volatile boolean isRunning;
+       
        /* FIXME FIXME FIXME: Use LRUQueue instead. ArrayBlockingQueue does not 
use a Hashset for contains()! */
        private final ArrayBlockingQueue<Identity> mIdentities = new 
ArrayBlockingQueue<Identity>(PUZZLE_POOL_SIZE); /* FIXME: figure out whether my 
assumption that this is just the right size is correct */
+       
        private final HashSet<ClientGetter> mRequests = new 
HashSet<ClientGetter>(PUZZLE_REQUEST_COUNT * 2); /* TODO: profile & tweak */
-       
        private final HashSet<BaseClientPutter> mInserts = new 
HashSet<BaseClientPutter>(PUZZLE_REQUEST_COUNT * 2); 
 
        /**
@@ -104,12 +114,13 @@
         * @param tbf
         *            Needed to create buckets from Identities before insert
         */
-       public IntroductionClient(WoT myWoT, TempBucketFactory myTBF) {
-               isRunning = true;
+       public IntroductionClient(WoT myWoT) {
                mWoT = myWoT;
                db = mWoT.getDB();
                mClient = mWoT.getClient();
-               mTBF = myTBF;
+               mTBF = mWoT.getTBF();
+               mRandom = mWoT.getRandom();
+               isRunning = true;
        }
 
        public void run() {
@@ -117,7 +128,7 @@
                
                mThread = Thread.currentThread();
                try {
-                       Thread.sleep(STARTUP_DELAY/2 + 
mWoT.random.nextInt(STARTUP_DELAY)); // Let the node start up
+                       Thread.sleep(STARTUP_DELAY/2 + 
mRandom.nextInt(STARTUP_DELAY)); // Let the node start up
                }
                catch (InterruptedException e)
                {
@@ -128,13 +139,15 @@
                        Thread.interrupted();
                        Logger.debug(this, "Introduction client loop 
running...");
                        
-                       IntroductionPuzzle.deleteExpiredPuzzles(db);
+                       synchronized(this) {
+                               IntroductionPuzzle.deleteExpiredPuzzles(db);
+                       }
                        downloadPuzzles();
                        
                        Logger.debug(this, "Introduction client loop 
finished.");
                        
                        try {
-                               Thread.sleep(THREAD_PERIOD/2 + 
mWoT.random.nextInt(THREAD_PERIOD));
+                               Thread.sleep(THREAD_PERIOD/2 + 
mRandom.nextInt(THREAD_PERIOD));
                        }
                        catch (InterruptedException e)
                        {
@@ -224,6 +237,8 @@
                }
        }
        
+       /* Private functions */
+       
        private void cancelRequests() {
                Logger.debug(this, "Trying to stop all requests & inserts");
                
@@ -242,6 +257,24 @@
                }
        }
        
+       private void removeRequest(ClientGetter g) {
+               Logger.debug(this, "Trying to remove request " + g.getURI());
+               synchronized(mRequests) {
+                       g.cancel();
+                       mRequests.remove(g);
+               }
+               Logger.debug(this, "Removed request.");
+       }
+       
+       private void removeInsert(BaseClientPutter p) {
+               Logger.debug(this, "Trying to remove insert " + p.getURI());
+               synchronized(mInserts) {
+                       p.cancel();
+                       mInserts.remove(p);
+               }
+               Logger.debug(this, "Removed request.");
+       }
+       
        private synchronized void downloadPuzzles() {
                Query q = db.query();
                q.constrain(Identity.class);
@@ -288,7 +321,7 @@
                
                for(Identity i : ids) {
                        try {
-                               downloadPuzzle(i, 
mWoT.random.nextInt(IntroductionServer.PUZZLE_COUNT)); /* FIXME: store the 
puzzle count as an identity property (i.e. identities will publish in 
identity.xml how many puzzles they upload */
+                               downloadPuzzle(i, 
mRandom.nextInt(IntroductionServer.PUZZLE_COUNT)); /* FIXME: store the puzzle 
count as an identity property (i.e. identities will publish in identity.xml how 
many puzzles they upload */
                        } catch (Exception e) {
                                Logger.error(this, "Starting puzzle download 
failed.", e);
                        }
@@ -318,34 +351,7 @@
                Logger.debug(this, "Trying to fetch puzzle from " + 
uri.toString());
        }
 
-       private void removeRequest(ClientGetter g) {
-               Logger.debug(this, "Trying to remove request " + g.getURI());
-               synchronized(mRequests) {
-                       g.cancel();
-                       mRequests.remove(g);
-               }
-               Logger.debug(this, "Removed request.");
-       }
-       
-       private void removeInsert(BaseClientPutter p) {
-               Logger.debug(this, "Trying to remove insert " + p.getURI());
-               synchronized(mInserts) {
-                       p.cancel();
-                       mInserts.remove(p);
-               }
-               Logger.debug(this, "Removed request.");
-       }
-       
        /**
-        * Called when the node can't fetch a file OR when there is a newer 
edition.
-        * In our case, called when there is no puzzle available.
-        */
-       public void onFailure(FetchException e, ClientGetter state) {
-               Logger.normal(this, "Downloading puzzle " + state.getURI() + " 
failed.", e);
-               removeRequest(state);
-       }
-
-       /**
         * Called when a puzzle is successfully fetched.
         */
        public void onSuccess(FetchResult result, ClientGetter state) {
@@ -356,21 +362,34 @@
                        IntroductionPuzzle.deleteOldestPuzzles(db, 
PUZZLE_POOL_SIZE);
                        removeRequest(state);
                        if(p.getIndex() < MAX_PUZZLES_PER_IDENTITY) {
-                               downloadPuzzle(p.getInserter(), p.getIndex() + 
1);
+                               downloadPuzzle(p.getInserter(), p.getIndex() + 
1); /* FIXME: Also download a random index here */
                        }
                } catch (Exception e) { 
                        Logger.error(this, "Parsing failed for "+ 
state.getURI(), e);
                }
        }
+       
+       /**
+        * Called when the node can't fetch a file OR when there is a newer 
edition.
+        * In our case, called when there is no puzzle available.
+        */
+       public void onFailure(FetchException e, ClientGetter state) {
+               Logger.normal(this, "Downloading puzzle " + state.getURI() + " 
failed.", e);
+               removeRequest(state);
+       }
 
-       // Only called by inserts
+       /**
+        * Called when a puzzle solution is successfully inserted.
+        */
        public void onSuccess(BaseClientPutter state)
        {
                Logger.debug(this, "Successful insert of puzzle solution at " + 
state.getURI());
                removeInsert(state);
        }
        
-       // Only called by inserts
+       /**
+        * Calling when inserting a puzzle solution failed.
+        */
        public void onFailure(InsertException e, BaseClientPutter state)
        {
                Logger.debug(this, "Insert of puzzle solution failed for " + 
state.getURI(), e);
@@ -379,10 +398,10 @@
        
        /* Not needed functions from the ClientCallback interface */
 
-       // Only called by inserts
+       /** Only called by inserts */
        public void onFetchable(BaseClientPutter state) {}
 
-       // Only called by inserts
+       /** Only called by inserts */
        public void onGeneratedURI(FreenetURI uri, BaseClientPutter state) {}
 
        /** Called when freenet.async thinks that the request should be 
serialized to

Modified: trunk/plugins/WoT/introduction/IntroductionServer.java
===================================================================
--- trunk/plugins/WoT/introduction/IntroductionServer.java      2008-11-16 
23:01:34 UTC (rev 23667)
+++ trunk/plugins/WoT/introduction/IntroductionServer.java      2008-11-17 
00:12:46 UTC (rev 23668)
@@ -10,6 +10,7 @@
 import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.Random;
 
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.TransformerException;
@@ -54,14 +55,17 @@
 
        public static final byte PUZZLE_COUNT = 10; 
        public static final byte PUZZLE_INVALID_AFTER_DAYS = 3;
+       
+       
+       /* Objects from WoT */
 
-       private Thread mThread;
+       private WoT mWoT;
        
-       /** Used to tell the introduction server thread if it should stop */
-       private volatile boolean isRunning;
+       private final IdentityFetcher mIdentityFetcher;
        
-       private WoT mWoT; 
        
+       /* Objects from the node */
+       
        /** A reference to the database */
        private ObjectContainer db;
 
@@ -71,33 +75,36 @@
        /** The TempBucketFactory used to create buckets from puzzles before 
insert */
        private final TempBucketFactory mTBF;
        
-       private final IdentityFetcher mIdentityFetcher;
-
+       /** Random number generator */
+       private final Random mRandom;
+       
+       
+       /* Private objects */
+       
+       private Thread mThread;
+       
+       /** Used to tell the introduction server thread if it should stop */
+       private volatile boolean isRunning;
+       
        private final IntroductionPuzzleFactory[] mPuzzleFactories = new 
IntroductionPuzzleFactory[] { new CaptchaFactory1() };
        
        private final ArrayList<ClientGetter> mRequests = new 
ArrayList<ClientGetter>(PUZZLE_COUNT * 5); /* Just assume that there are 5 
identities */
-       
        private final ArrayList<BaseClientPutter> mInserts = new 
ArrayList<BaseClientPutter>(PUZZLE_COUNT * 5); /* Just assume that there are 5 
identities */
        
 
        /**
         * Creates an IntroductionServer
-        * 
-        * @param db
-        *            A reference to the database
-        * @param client
-        *            A reference to an [EMAIL PROTECTED] 
HighLevelSimpleClient} to perform
-        *            inserts
-        * @param tbf
-        *            Needed to create buckets from Identities before insert
         */
-       public IntroductionServer(WoT myWoT, TempBucketFactory myTBF, 
IdentityFetcher myFetcher) {
-               isRunning = true;
+       public IntroductionServer(WoT myWoT, IdentityFetcher myFetcher) {
                mWoT = myWoT;
+               mIdentityFetcher = myFetcher;
+               
                db = mWoT.getDB();
                mClient = mWoT.getClient();
-               mTBF = myTBF;
-               mIdentityFetcher = myFetcher;
+               mTBF = mWoT.getTBF();
+               mRandom = mWoT.getRandom();
+               
+               isRunning = true;
        }
 
        public void run() {
@@ -105,7 +112,7 @@
                
                mThread = Thread.currentThread();
                try {
-                       Thread.sleep(STARTUP_DELAY/2 + 
mWoT.random.nextInt(STARTUP_DELAY)); // Let the node start up
+                       Thread.sleep(STARTUP_DELAY/2 + 
mRandom.nextInt(STARTUP_DELAY)); // Let the node start up
                }
                catch (InterruptedException e)
                {
@@ -115,10 +122,10 @@
                while(isRunning) {
                        Thread.interrupted();
                        Logger.debug(this, "Introduction server loop 
running...");
-                       ObjectSet<OwnIdentity> identities = 
OwnIdentity.getAllOwnIdentities(db);
                        
                        IntroductionPuzzle.deleteExpiredPuzzles(db);
-                       
+
+                       ObjectSet<OwnIdentity> identities = 
OwnIdentity.getAllOwnIdentities(db);
                        while(identities.hasNext()) {
                                OwnIdentity identity = identities.next();
                                
if(identity.hasContext(IntroductionPuzzle.INTRODUCTION_CONTEXT)) {
@@ -136,7 +143,7 @@
                        Logger.debug(this, "Introduction server loop 
finished.");
                        
                        try {
-                               Thread.sleep(THREAD_PERIOD/2 + 
mWoT.random.nextInt(THREAD_PERIOD));
+                               Thread.sleep(THREAD_PERIOD/2 + 
mRandom.nextInt(THREAD_PERIOD));
                        }
                        catch (InterruptedException e)
                        {
@@ -163,6 +170,8 @@
                Logger.debug(this, "Stopped the introduction server.");
        }
        
+       /* Private functions */
+       
        private void cancelRequests() {
                Logger.debug(this, "Trying to stop all requests & inserts");
                
@@ -180,6 +189,24 @@
                        Logger.debug(this, "Stopped " + icounter + " current 
inserts");
                }
        }
+       
+       private void removeRequest(ClientGetter g) {
+               Logger.debug(this, "Trying to remove request " + g.getURI());
+               synchronized(mRequests) {
+                       g.cancel(); /* FIXME: is this necessary ? */
+                       mRequests.remove(g);
+               }
+               Logger.debug(this, "Removed request.");
+       }
+       
+       private void removeInsert(BaseClientPutter p) {
+               Logger.debug(this, "Trying to remove insert " + p.getURI());
+               synchronized(mInserts) {
+                       p.cancel(); /* FIXME: is this necessary ? */
+                       mInserts.remove(p);
+               }
+               Logger.debug(this, "Removed request.");
+       }
                
        private synchronized void downloadSolutions(OwnIdentity identity) 
throws FetchException {
                ObjectSet<IntroductionPuzzle> puzzles = 
IntroductionPuzzle.getByInserter(db, identity);
@@ -237,7 +264,7 @@
                        IntroductionPuzzle p = null;
                        do {
                                try {
-                                       p = 
mPuzzleFactories[mWoT.random.nextInt(mPuzzleFactories.length)].generatePuzzle(db,
 identity);
+                                       p = 
mPuzzleFactories[mRandom.nextInt(mPuzzleFactories.length)].generatePuzzle(db, 
identity);
                                        p.exportToXML(os);
                                        os.close(); os = null;
                                        tempB.setReadOnly();
@@ -277,24 +304,6 @@
                }
        }
        
-       private void removeRequest(ClientGetter g) {
-               Logger.debug(this, "Trying to remove request " + g.getURI());
-               synchronized(mRequests) {
-                       g.cancel(); /* FIXME: is this necessary ? */
-                       mRequests.remove(g);
-               }
-               Logger.debug(this, "Removed request.");
-       }
-       
-       private void removeInsert(BaseClientPutter p) {
-               Logger.debug(this, "Trying to remove insert " + p.getURI());
-               synchronized(mInserts) {
-                       p.cancel(); /* FIXME: is this necessary ? */
-                       mInserts.remove(p);
-               }
-               Logger.debug(this, "Removed request.");
-       }
-       
        /**
         * Called when the node can't fetch a file OR when there is a newer 
edition.
         * In our case, called when there is no solution to a puzzle in the 
network.
@@ -305,8 +314,7 @@
        }
 
        /**
-        * Called when a file is successfully fetched. We then add the identity 
which
-        * solved the puzzle.
+        * Called when a puzzle solution is successfully fetched. We then add 
the identity which solved the puzzle.
         */
        public void onSuccess(FetchResult result, ClientGetter state) {
                Logger.debug(this, "Fetched puzzle solution: " + 
state.getURI());
@@ -330,7 +338,9 @@
                }
        }
        
-       // Only called by inserts
+       /** 
+        * Called when a puzzle was successfully inserted.
+        */
        public void onSuccess(BaseClientPutter state)
        {
                try {
@@ -341,7 +351,9 @@
                removeInsert(state);
        }
        
-       // Only called by inserts
+       /**
+        * Called when the insertion of a puzzle failed.
+        */
        public void onFailure(InsertException e, BaseClientPutter state) 
        {
                try {
@@ -354,13 +366,12 @@
 
        /* Not needed functions from the ClientCallback interface */
        
-       // Only called by inserts
+       /** Only called by inserts */
        public void onFetchable(BaseClientPutter state) {}
 
-       // Only called by inserts
+       /** Only called by inserts */
        public void onGeneratedURI(FreenetURI uri, BaseClientPutter state) {}
 
-       /** Called when freenet.async thinks that the request should be 
serialized to
-        * disk, if it is a persistent request. */
+       /** Called when freenet.async thinks that the request should be 
serialized to disk, if it is a persistent request. */
        public void onMajorProgress() {}
 }

_______________________________________________
cvs mailing list
[email protected]
http://emu.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to