Author: batosai
Date: 2008-08-16 19:58:50 +0000 (Sat, 16 Aug 2008)
New Revision: 21940
Modified:
trunk/apps/WoT/src/plugins/WoT/IdentityFetcher.java
trunk/apps/WoT/src/plugins/WoT/IdentityParser.java
trunk/apps/WoT/src/plugins/WoT/WoT.java
trunk/apps/WoT/src/plugins/WoT/WoTplugin.java
Log:
Proper handling of requests, needs further testing (tomorrow).
Modified: trunk/apps/WoT/src/plugins/WoT/IdentityFetcher.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/IdentityFetcher.java 2008-08-16 19:34:12 UTC
(rev 21939)
+++ trunk/apps/WoT/src/plugins/WoT/IdentityFetcher.java 2008-08-16 19:58:50 UTC
(rev 21940)
@@ -6,6 +6,14 @@
package plugins.WoT;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.xml.sax.SAXException;
+
import com.db4o.ObjectContainer;
import freenet.client.FetchContext;
@@ -28,17 +36,23 @@
private ObjectContainer db;
private WoT wot;
private HighLevelSimpleClient client;
+ private ArrayList<ClientGetter> requests;
public IdentityFetcher(ObjectContainer db, WoT wot,
HighLevelSimpleClient client) {
this.db = db;
this.wot = wot;
this.client = client;
+ requests = new ArrayList<ClientGetter>();
}
- public void fetch(Identity identity) throws FetchException {
+ public void fetch(Identity identity) {
- fetch(identity.getRequestURI());
+ try {
+ fetch(identity.getRequestURI());
+ } catch (FetchException e) {
+ Logger.error(this, "Request restart failed: "+e, e);
+ }
}
public void fetch(FreenetURI uri) throws FetchException {
@@ -46,10 +60,14 @@
FetchContext fetchContext = client.getFetchContext();
fetchContext.maxSplitfileBlockRetries = -1; // retry forever
fetchContext.maxNonSplitfileRetries = -1; // retry forever
- ClientGetter request = client.fetch(uri, -1, this, this,
fetchContext);
- // addRequestToList(request);
+ requests.add(client.fetch(uri, -1, this, this, fetchContext));
}
+ public void stop() {
+ Iterator<ClientGetter> i = requests.iterator();
+ while (i.hasNext()) i.next().cancel();
+ }
+
@Override
public void onFailure(FetchException e, ClientGetter state) {
@@ -65,7 +83,7 @@
}
// Errors we can't/want deal with
Logger.error(this, "Fetch failed for "+ state.getURI(), e);
- // removeRequestFromList(state);
+ requests.remove(state);
}
@Override
@@ -92,14 +110,12 @@
public void onSuccess(FetchResult result, ClientGetter state) {
try {
- new IdentityParser(db, wot,
client).parse(result.asBucket().getInputStream(), state.getURI());
+ new IdentityParser(db, wot, client,
this).parse(result.asBucket().getInputStream(), state.getURI());
db.commit();
-
fetch(state.getURI().setSuggestedEdition(state.getURI().getSuggestedEdition() +
1));
+
state.restart(state.getURI().setSuggestedEdition(state.getURI().getSuggestedEdition()
+ 1));
} catch (Exception e) {
- // TODO Log this properly
- e.printStackTrace();
+ Logger.error(this, "Parsing failed for "+
state.getURI(), e);
}
-
}
@Override
Modified: trunk/apps/WoT/src/plugins/WoT/IdentityParser.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/IdentityParser.java 2008-08-16 19:34:12 UTC
(rev 21939)
+++ trunk/apps/WoT/src/plugins/WoT/IdentityParser.java 2008-08-16 19:58:50 UTC
(rev 21940)
@@ -32,21 +32,23 @@
ObjectContainer db;
WoT wot;
HighLevelSimpleClient client;
+ IdentityFetcher fetcher;
SAXParser saxParser;
Identity identity;
- public IdentityParser(ObjectContainer db, WoT wot,
HighLevelSimpleClient client) throws ParserConfigurationException, SAXException
{
+ public IdentityParser(ObjectContainer db, WoT wot,
HighLevelSimpleClient client, IdentityFetcher fetcher) throws
ParserConfigurationException, SAXException {
this.db = db;
this.wot = wot;
this.client = client;
+ this.fetcher = fetcher;
saxParser = SAXParserFactory.newInstance().newSAXParser();
}
public void parse (InputStream is, FreenetURI uri) throws
InvalidParameterException, SAXException, IOException, UnknownIdentityException {
- identity = wot.getIdentityByURI(uri.toString());
- identity.setLastChange(new Date());
+ identity = wot.getIdentityByURI(uri.toString());
+ if(!(identity instanceof OwnIdentity))
identity.setLastChange(new Date());
saxParser.parse(is, new IdentityHandler() );
db.store(identity);
}
@@ -78,10 +80,16 @@
}
else if (elt_name.equals("trust")) {
- Identity trustee = new
Identity(attrs.getValue("uri"), new Date(0), "Not found yet...", "false");
- db.store(trustee);
- new IdentityFetcher(db, wot,
client).fetch(trustee);
-
+ Identity trustee;
+ try{
+ trustee =
wot.getIdentityByURI(attrs.getValue("uri")) ;
+ }
+ catch (UnknownIdentityException e) {
+ trustee = new
Identity(attrs.getValue("uri"), new Date(0), "Not found yet...", "false");
+ db.store(trustee);
+ fetcher.fetch(trustee);
+ }
+
int value =
Integer.parseInt(attrs.getValue("value"));
String comment =
attrs.getValue("comment");
Modified: trunk/apps/WoT/src/plugins/WoT/WoT.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/WoT.java 2008-08-16 19:34:12 UTC (rev
21939)
+++ trunk/apps/WoT/src/plugins/WoT/WoT.java 2008-08-16 19:58:50 UTC (rev
21940)
@@ -202,6 +202,10 @@
return db.queryByExample(OwnIdentity.class);
}
+ public ObjectSet<Identity> getAllIdentities() {
+ return db.queryByExample(Identity.class);
+ }
+
public OwnIdentity getOwnIdentityByURI(String uri) throws
InvalidParameterException, MalformedURLException, UnknownIdentityException {
Identity identity = getIdentityByURI(uri);
Modified: trunk/apps/WoT/src/plugins/WoT/WoTplugin.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/WoTplugin.java 2008-08-16 19:34:12 UTC
(rev 21939)
+++ trunk/apps/WoT/src/plugins/WoT/WoTplugin.java 2008-08-16 19:58:50 UTC
(rev 21940)
@@ -51,6 +51,7 @@
private WoT wot;
private WebInterface web;
private IdentityInserter inserter;
+ private IdentityFetcher fetcher;
public static String SELF_URI = "/plugins/plugins.WoT.WoTplugin";
@@ -67,10 +68,18 @@
inserter = new IdentityInserter(wot, db, client);
pr.getNode().executor.execute(inserter, "WoTinserter");
+
+ fetcher = new IdentityFetcher(db, wot, client);
+
+ ObjectSet<Identity> identities = wot.getAllIdentities();
+ while (identities.hasNext()) {
+ fetcher.fetch(identities.next());
+ }
}
public void terminate() {
inserter.stop();
+ fetcher.stop();
db.commit();
db.close();
}
@@ -175,7 +184,7 @@
identity = new Identity(requestURI, new
Date(0), "Not found yet...", "false");
db.store(identity);
db.commit();
- new IdentityFetcher(db, wot,
client).fetch(identity);
+ fetcher.fetch(identity);
}
return identity;
}