[freenet-cvs] r20137 - trunk/apps/WoT/src/plugins/WoT

batosai at freenetproject.org batosai at freenetproject.org
Thu May 29 20:14:00 UTC 2008


Author: batosai
Date: 2008-05-29 20:14:00 +0000 (Thu, 29 May 2008)
New Revision: 20137

Modified:
   trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java
   trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java
   trunk/apps/WoT/src/plugins/WoT/WoT.java
Log:
Created a new class to handle every communications with the node.

Modified: trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java	2008-05-29 17:09:26 UTC (rev 20136)
+++ trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java	2008-05-29 20:14:00 UTC (rev 20137)
@@ -10,9 +10,6 @@
 import java.util.Iterator;
 import java.util.List;
 
-import net.pterodactylus.fcp.ClientPutDiskDir;
-import net.pterodactylus.fcp.FcpConnection;
-
 import com.db4o.ObjectContainer;
 import com.db4o.query.Predicate;
 
@@ -24,7 +21,7 @@
 public class IdentityInserter implements Runnable{
 	
 	private ObjectContainer db;
-	private FcpConnection fcp;
+	private FCPHandler fcp;
 	private Thread inserter;
 	
 	/**
@@ -32,7 +29,7 @@
 	 * 
 	 * @param db Connection to the database
 	 */
-	public IdentityInserter(ObjectContainer db, FcpConnection fcp) {
+	public IdentityInserter(ObjectContainer db, FCPHandler fcp) {
 		this.db = db;
 		this.fcp = fcp;
 	}
@@ -41,7 +38,7 @@
 	 * Periodically checks what identities have been updated and inserts them
 	 */
 	public void run() {
-
+		
 		while(Thread.currentThread() == inserter) {
 			
 			try {
@@ -61,12 +58,8 @@
 			
 			while(it.hasNext()) {
 				OwnIdentity identityToInsert = it.next();
-				insert(db, fcp, identityToInsert);
-				identityToInsert.setLastInsert(new Date());
+				insert(db, fcp, identityToInsert);			
 			}
-			
-
-			
 		}
 	}
 	
@@ -87,32 +80,29 @@
 		inserter = null;
 	}
 
-	public void insert(ObjectContainer db, FcpConnection fcp, OwnIdentity identity) {
+	public void insert(ObjectContainer db, FCPHandler fcp, OwnIdentity identity) {
 		
-		ClientPutDiskDir clientPut;
-		
-		/* This is ugly ! There is certainly a way to avoid creating a file on disk but
-		 * I can't find it at the moment.  
+		/* We set the date now, so that if changes are made between now and the actual insert, 
+		 * we are going for another insert.
 		 */
-		File exportFile;
-
+		identity.setLastInsert(new Date());
+		
 		try {
-			exportFile = identity.exportToXML(db, new File("exportWOT"));
-			clientPut = new ClientPutDiskDir(identity.getFullInsertURI(), "WoTinserter", exportFile.getParent());
-			clientPut.setDefaultName("identity.xml");
-			fcp.sendMessage(clientPut);
-			//TODO Cleanly handle errors. ATM, we don't even listen to them !
+			File exportDir = new File("exportWOT");
+			identity.exportToXML(db, exportDir);
 			
-			System.out.println(exportFile.getParent());
-			System.out.println(identity.getFullInsertURI());
-			
-			identity.setEdition(identity.getEdition() + 1);
-			db.store(identity);
+			while(!fcp.insertDir(exportDir.getCanonicalPath(), identity.getFullInsertURI())) {
+				// We just keep trying until the insert succeeds
+			}
 		}
 		catch (Exception e) {
-			System.out.println("Export failed !");
+			// TODO Find a way to warn the user of the problem. As we are implementing Runnable, we can't throw anything from here :/
 			e.printStackTrace();
-			return;
-		}		
+			stop();
+		}
+		
+		identity.setEdition(identity.getEdition() + 1);
+		db.store(identity);
+	
 	}
 }

Modified: trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java	2008-05-29 17:09:26 UTC (rev 20136)
+++ trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java	2008-05-29 20:14:00 UTC (rev 20137)
@@ -7,12 +7,9 @@
 
 import java.io.BufferedOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.StringWriter;
 import java.util.Date;
 
 import javax.xml.parsers.DocumentBuilder;
@@ -26,12 +23,7 @@
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 
-import net.pterodactylus.fcp.ClientPut;
-import net.pterodactylus.fcp.FcpAdapter;
-import net.pterodactylus.fcp.FcpConnection;
-import net.pterodactylus.fcp.GenerateSSK;
 import net.pterodactylus.fcp.SSKKeypair;
-import net.pterodactylus.fcp.UploadFrom;
 
 import org.w3c.dom.DOMImplementation;
 import org.w3c.dom.Document;
@@ -75,28 +67,16 @@
 	 * @throws IOException
 	 * @throws InterruptedException
 	 */
-	public OwnIdentity(FcpConnection fcp) throws IOException,InterruptedException {
+	public OwnIdentity(FCPHandler fcp) throws IOException,InterruptedException {
 		
 		lastInsert = new Date(0);
 		lastChange = new Date();
 		publishTrustList = true;
 		
-		FcpAdapter fcpAdapter = new FcpAdapter() {
-			@Override
-			public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
-				requestURI = sskKeypair.getRequestURI();
-				insertURI = sskKeypair.getInsertURI();
-				synchronized (this) {
-					notify();
-				}
-			}
-		};
-		
-		fcp.addFcpListener(fcpAdapter);
-		synchronized (fcpAdapter) {
-			fcp.sendMessage(new GenerateSSK());
-			fcpAdapter.wait();
-		}
+		SSKKeypair ssk = fcp.getSSKKeypair();
+		requestURI = ssk.getRequestURI();
+		insertURI = ssk.getInsertURI();
+
 	}
 	
 	/**

Modified: trunk/apps/WoT/src/plugins/WoT/WoT.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/WoT.java	2008-05-29 17:09:26 UTC (rev 20136)
+++ trunk/apps/WoT/src/plugins/WoT/WoT.java	2008-05-29 20:14:00 UTC (rev 20137)
@@ -5,11 +5,8 @@
  */
 package plugins.WoT;
 
-import java.io.File;
 import java.io.IOException;
 import java.net.UnknownHostException;
-import java.sql.Timestamp;
-import java.util.Date;
 
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.TransformerException;
@@ -18,11 +15,6 @@
 import com.db4o.ObjectContainer;
 import com.db4o.ObjectSet;
 
-import net.pterodactylus.fcp.ClientHello;
-import net.pterodactylus.fcp.FcpAdapter;
-import net.pterodactylus.fcp.FcpConnection;
-import net.pterodactylus.fcp.NodeHello;
-
 /**
  * The Web of Trust
  * 
@@ -40,11 +32,12 @@
 			2,
 			1	// Every identity above rank 5 can give 1 point
 	};			// Identities with negative score have zero capacity
-	
-	private FcpConnection fcp;
+
 	private IdentityInserter identityInserter;
+	private FCPHandler fcp;
 	private ObjectContainer db;
 	
+	
 	/**
 	 * Creates a new WoT instance
 	 * 
@@ -56,58 +49,24 @@
 	 */
 	public WoT(String host, int port) throws UnknownHostException, IOException, InterruptedException {
 
-		initFCPConnection(host, port);
 		db = Db4o.openFile("WoT.db4o");
+		fcp = new FCPHandler(host,port);
 
 		identityInserter = new IdentityInserter(db, fcp);
 		identityInserter.start();
 		
 	}
-	
-	/**
-	 * Creates an FCP connection with the Freenet node
-	 *  
-	 * @param host Hostname or address of the Freenet node
-	 * @param port FCP port of the Freenet node
-	 * @throws UnknownHostException
-	 * @throws IOException
-	 * @throws InterruptedException
-	 */
-	private void initFCPConnection(String host, int port) throws UnknownHostException, IOException, InterruptedException {
-		
-		this.fcp = new FcpConnection(host,port);
-		fcp.connect();
-		
-		FcpAdapter fcpAdapter = new FcpAdapter() {	
-			@Override
-			public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
-				synchronized (this) {
-					notify();
-				}
-			}
-		};
-		
-		fcp.addFcpListener(fcpAdapter);
-		synchronized (fcpAdapter) {
-			fcp.sendMessage(new ClientHello("WoT"));
-			fcpAdapter.wait();
-		}
-	}
 
 	/**
 	 * Stops every threads and cleanly close all connections (FCP, database)
 	 * 
 	 * @throws InterruptedException
 	 */
-	private void close() throws InterruptedException {
+	private void close() {
 		
-		identityInserter.stop();
-
-		// Wait for threads to stop
-		Thread.sleep(1000);
-		
+		//identityInserter.stop();
+		fcp.close();
 		db.close();
-		fcp.disconnect();
 		
 	}
 	
@@ -119,12 +78,14 @@
 		//Empty the database
 		ObjectSet<Object> result = db.queryByExample(new Object());
 		while (result.hasNext()) { db.delete(result.next()); }
-		
+
 		OwnIdentity root = new OwnIdentity(fcp);
+		db.store(root);
+
+		/*
 		Identity a = new Identity("a", new Date(), true);
 		Identity b = new Identity("b", new Date(), true);
 		
-		db.store(root);
 		db.store(a);
 		db.store(b);
 		
@@ -133,6 +94,7 @@
 		a.addTrust(db, b, 100);
 		
 		root.exportToXML(db, new File("exportWOT"));
+		*/
 	}
 	
 	/**
@@ -141,12 +103,7 @@
 	public static void main(String[] args) {
 		try {
 			WoT wot = new WoT("localhost", 9481);
-			wot.testDrive();
-			
-			Thread.sleep(5000);
-			
-			//wot.close();
-			
+			wot.testDrive();		
 		}
 		catch (Exception e) {
 			// We catch everything here as there is not yet an interface to warn the user of the problems 




More information about the cvs mailing list