[freenet-cvs] r17480 - in trunk/freenet/src/freenet: client/async node

toad at freenetproject.org toad at freenetproject.org
Sat Feb 2 20:45:34 UTC 2008


Author: toad
Date: 2008-02-02 20:45:33 +0000 (Sat, 02 Feb 2008)
New Revision: 17480

Modified:
   trunk/freenet/src/freenet/client/async/BaseSingleFileFetcher.java
   trunk/freenet/src/freenet/client/async/ClientRequestScheduler.java
   trunk/freenet/src/freenet/client/async/SimpleSingleFileFetcher.java
   trunk/freenet/src/freenet/client/async/SingleBlockInserter.java
   trunk/freenet/src/freenet/client/async/SplitFileFetcherSubSegment.java
   trunk/freenet/src/freenet/client/async/USKChecker.java
   trunk/freenet/src/freenet/node/RequestStarter.java
   trunk/freenet/src/freenet/node/SendableGet.java
   trunk/freenet/src/freenet/node/SendableInsert.java
   trunk/freenet/src/freenet/node/SendableRequest.java
   trunk/freenet/src/freenet/node/SimpleSendableInsert.java
Log:
Refactor: use Object's not int's as tokens in the SendableRequest API.

Modified: trunk/freenet/src/freenet/client/async/BaseSingleFileFetcher.java
===================================================================
--- trunk/freenet/src/freenet/client/async/BaseSingleFileFetcher.java	2008-02-02 20:05:30 UTC (rev 17479)
+++ trunk/freenet/src/freenet/client/async/BaseSingleFileFetcher.java	2008-02-02 20:45:33 UTC (rev 17480)
@@ -19,7 +19,7 @@
 	final int maxRetries;
 	private int retryCount;
 	final FetchContext ctx;
-	static final int[] keys = new int[] { 0 };
+	static final Object[] keys = new Object[] { new Integer(0) };
 
 	protected BaseSingleFileFetcher(ClientKey key, int maxRetries, FetchContext ctx, ClientRequester parent) {
 		super(parent);
@@ -29,15 +29,15 @@
 		this.ctx = ctx;
 	}
 
-	public int[] allKeys() {
+	public Object[] allKeys() {
 		return keys;
 	}
 	
-	public int chooseKey() {
-		return 0;
+	public Object chooseKey() {
+		return keys[0];
 	}
 	
-	public ClientKey getKey(int token) {
+	public ClientKey getKey(Object token) {
 		return key;
 	}
 	
@@ -111,7 +111,7 @@
 			}
 		}
 		try {
-			onSuccess(Key.createKeyBlock(this.key, block), false, 0);
+			onSuccess(Key.createKeyBlock(this.key, block), false, null);
 		} catch (KeyVerifyException e) {
 			Logger.error(this, "onGotKey("+key+","+block+") got "+e+" for "+this, e);
 			// FIXME if we get rid of the direct route this must call onFailure()

Modified: trunk/freenet/src/freenet/client/async/ClientRequestScheduler.java
===================================================================
--- trunk/freenet/src/freenet/client/async/ClientRequestScheduler.java	2008-02-02 20:05:30 UTC (rev 17479)
+++ trunk/freenet/src/freenet/client/async/ClientRequestScheduler.java	2008-02-02 20:45:33 UTC (rev 17480)
@@ -198,9 +198,9 @@
 			SendableGet getter = (SendableGet)req;
 			if(!getter.ignoreStore()) {
 				boolean anyValid = false;
-				int[] keyTokens = getter.allKeys();
+				Object[] keyTokens = getter.allKeys();
 				for(int i=0;i<keyTokens.length;i++) {
-					int tok = keyTokens[i];
+					Object tok = keyTokens[i];
 					ClientKeyBlock block = null;
 					try {
 						ClientKey key = getter.getKey(tok);
@@ -513,9 +513,9 @@
 	 * @param complain
 	 */
 	public void removePendingKeys(SendableGet getter, boolean complain) {
-		int[] keyTokens = getter.allKeys();
+		Object[] keyTokens = getter.allKeys();
 		for(int i=0;i<keyTokens.length;i++) {
-			int tok = keyTokens[i];
+			Object tok = keyTokens[i];
 			ClientKey ckey = getter.getKey(tok);
 			if(ckey == null) {
 				if(complain)

Modified: trunk/freenet/src/freenet/client/async/SimpleSingleFileFetcher.java
===================================================================
--- trunk/freenet/src/freenet/client/async/SimpleSingleFileFetcher.java	2008-02-02 20:05:30 UTC (rev 17479)
+++ trunk/freenet/src/freenet/client/async/SimpleSingleFileFetcher.java	2008-02-02 20:45:33 UTC (rev 17480)
@@ -39,7 +39,7 @@
 	final long token;
 	
 	// Translate it, then call the real onFailure
-	public void onFailure(LowLevelGetException e, int reqTokenIgnored) {
+	public void onFailure(LowLevelGetException e, Object reqTokenIgnored) {
 		switch(e.code) {
 		case LowLevelGetException.DATA_NOT_FOUND:
 			onFailure(new FetchException(FetchException.DATA_NOT_FOUND));
@@ -117,7 +117,7 @@
 		rcb.onSuccess(data, this);
 	}
 
-	public void onSuccess(ClientKeyBlock block, boolean fromStore, int reqTokenIgnored) {
+	public void onSuccess(ClientKeyBlock block, boolean fromStore, Object reqTokenIgnored) {
 		if(parent instanceof ClientGetter)
 			((ClientGetter)parent).addKeyToBinaryBlob(block);
 		Bucket data = extract(block);

Modified: trunk/freenet/src/freenet/client/async/SingleBlockInserter.java
===================================================================
--- trunk/freenet/src/freenet/client/async/SingleBlockInserter.java	2008-02-02 20:05:30 UTC (rev 17479)
+++ trunk/freenet/src/freenet/client/async/SingleBlockInserter.java	2008-02-02 20:45:33 UTC (rev 17480)
@@ -136,7 +136,7 @@
 		return retries;
 	}
 
-	public void onFailure(LowLevelPutException e, int keyNum) {
+	public void onFailure(LowLevelPutException e, Object keyNum) {
 		if(parent.isCancelled()) {
 			fail(new InsertException(InsertException.CANCELLED));
 			return;
@@ -253,7 +253,7 @@
 		return resultingURI;
 	}
 
-	public void onSuccess(int keyNum) {
+	public void onSuccess(Object keyNum) {
 		if(logMINOR) Logger.minor(this, "Succeeded ("+this+"): "+token);
 		if(parent.isCancelled()) {
 			fail(new InsertException(InsertException.CANCELLED));
@@ -283,7 +283,7 @@
 		return finished;
 	}
 
-	public boolean send(NodeClientCore core, RequestScheduler sched, int keyNum) {
+	public boolean send(NodeClientCore core, RequestScheduler sched, Object keyNum) {
 		// Ignore keyNum, key, since we're only sending one block.
 		try {
 			if(logMINOR) Logger.minor(this, "Starting request: "+this);
@@ -346,16 +346,16 @@
 		return true;
 	}
 
-	public synchronized int[] allKeys() {
+	public synchronized Object[] allKeys() {
 		if(finished)
-			return new int[] {};
+			return new Object[] {};
 		else
-			return new int[] { 0 };
+			return new Object[] { new Integer(0) };
 	}
 
-	public synchronized int chooseKey() {
-		if(finished) return -1;
-		else return 0;
+	public synchronized Object chooseKey() {
+		if(finished) return null;
+		else return new Integer(0);
 	}
 
 }

Modified: trunk/freenet/src/freenet/client/async/SplitFileFetcherSubSegment.java
===================================================================
--- trunk/freenet/src/freenet/client/async/SplitFileFetcherSubSegment.java	2008-02-02 20:05:30 UTC (rev 17479)
+++ trunk/freenet/src/freenet/client/async/SplitFileFetcherSubSegment.java	2008-02-02 20:45:33 UTC (rev 17480)
@@ -49,36 +49,33 @@
 		return ctx;
 	}
 
-	public int chooseKey() {
-		if(cancelled) return -1;
+	public Object chooseKey() {
+		if(cancelled) return null;
 		return removeRandomBlockNum();
 	}
 	
-	public ClientKey getKey(int token) {
+	public ClientKey getKey(Object token) {
 		if(cancelled) {
 			if(logMINOR)
 				Logger.minor(this, "Segment is finishing when getting key "+token+" on "+this);
 			return null;
 		}
-		return segment.getBlockKey(token);
+		return segment.getBlockKey(((Integer)token).intValue());
 	}
 	
-	public synchronized int[] allKeys() {
-		int[] nums = new int[blockNums.size()];
-		for(int i=0;i<nums.length;i++)
-			nums[i] = ((Integer) blockNums.get(i)).intValue();
-		return nums;
+	public synchronized Object[] allKeys() {
+		return blockNums.toArray();
 	}
 	
-	private synchronized int removeRandomBlockNum() {
+	private synchronized Object removeRandomBlockNum() {
 		logMINOR = Logger.shouldLog(Logger.MINOR, this);
 		if(blockNums.isEmpty()) {
 			if(logMINOR)
 				Logger.minor(this, "No blocks to remove");
-			return -1;
+			return null;
 		}
 		int x = ctx.random.nextInt(blockNums.size());
-		int ret = ((Integer) blockNums.remove(x)).intValue();
+		Object ret = (Integer) blockNums.remove(x);
 		if(logMINOR)
 			Logger.minor(this, "Removing block "+x+" of "+(blockNums.size()+1)+ " : "+ret+ " on "+this);
 		return ret;
@@ -90,7 +87,7 @@
 
 	// Translate it, then call the real onFailure
 	// FIXME refactor this out to a common method; see SimpleSingleFileFetcher
-	public void onFailure(LowLevelGetException e, int token) {
+	public void onFailure(LowLevelGetException e, Object token) {
 		if(logMINOR)
 			Logger.minor(this, "onFailure("+e+" , "+token);
 		switch(e.code) {
@@ -132,7 +129,7 @@
 	}
 
 	// Real onFailure
-	protected void onFailure(FetchException e, int token) {
+	protected void onFailure(FetchException e, Object token) {
 		boolean forceFatal = false;
 		if(parent.isCancelled()) {
 			if(Logger.shouldLog(Logger.MINOR, this)) 
@@ -142,13 +139,13 @@
 		}
 		segment.errors.inc(e.getMode());
 		if(e.isFatal() || forceFatal) {
-			segment.onFatalFailure(e, token, this);
+			segment.onFatalFailure(e, ((Integer)token).intValue(), this);
 		} else {
-			segment.onNonFatalFailure(e, token, this);
+			segment.onNonFatalFailure(e, ((Integer)token).intValue(), this);
 		}
 	}
 	
-	public void onSuccess(ClientKeyBlock block, boolean fromStore, int token) {
+	public void onSuccess(ClientKeyBlock block, boolean fromStore, Object token) {
 		Bucket data = extract(block, token);
 		if(fromStore) {
 			// Normally when this method is called the block number has already
@@ -157,7 +154,7 @@
 			synchronized(this) {
 				for(int i=0;i<blockNums.size();i++) {
 					Integer x = (Integer) blockNums.get(i);
-					if(x.intValue() == token) {
+					if(x == token) {
 						blockNums.remove(i);
 						i--;
 					}
@@ -165,16 +162,16 @@
 			}
 		}
 		if(!block.isMetadata()) {
-			onSuccess(data, fromStore, token, block);
+			onSuccess(data, fromStore, (Integer)token, ((Integer)token).intValue(), block);
 		} else {
 			onFailure(new FetchException(FetchException.INVALID_METADATA, "Metadata where expected data"), token);
 		}
 	}
 	
-	protected void onSuccess(Bucket data, boolean fromStore, int blockNo, ClientKeyBlock block) {
+	protected void onSuccess(Bucket data, boolean fromStore, Integer token, int blockNo, ClientKeyBlock block) {
 		if(parent.isCancelled()) {
 			data.free();
-			onFailure(new FetchException(FetchException.CANCELLED), blockNo);
+			onFailure(new FetchException(FetchException.CANCELLED), token);
 			return;
 		}
 		segment.onSuccess(data, blockNo, fromStore, this, block);
@@ -183,7 +180,7 @@
 	/** Convert a ClientKeyBlock to a Bucket. If an error occurs, report it via onFailure
 	 * and return null.
 	 */
-	protected Bucket extract(ClientKeyBlock block, int token) {
+	protected Bucket extract(ClientKeyBlock block, Object token) {
 		Bucket data;
 		try {
 			data = block.decode(ctx.bucketFactory, (int)(Math.min(ctx.maxOutputLength, Integer.MAX_VALUE)), false);
@@ -290,10 +287,12 @@
 
 	public void onGotKey(Key key, KeyBlock block) {
 		int blockNum = -1;
+		Object token = null;
 		ClientKey ckey = null;
 		synchronized(this) {
 			for(int i=0;i<blockNums.size();i++) {
-				int num = ((Integer)blockNums.get(i)).intValue();
+				token = blockNums.get(i);
+				int num = ((Integer)token).intValue();
 				ckey = segment.getBlockKey(num);
 				if(ckey == null) return; // Already got this key
 				Key k = ckey.getNodeKey();
@@ -306,7 +305,7 @@
 		}
 		if(blockNum == -1) return;
 		try {
-			onSuccess(Key.createKeyBlock(ckey, block), false, blockNum);
+			onSuccess(Key.createKeyBlock(ckey, block), false, token);
 		} catch (KeyVerifyException e) {
 			// FIXME if we ever abolish the direct route, this must be turned into an onFailure().
 			Logger.error(this, "Failed to parse in onGotKey("+key+","+block+") - believed to be "+ckey+" (block #"+blockNum+")");

Modified: trunk/freenet/src/freenet/client/async/USKChecker.java
===================================================================
--- trunk/freenet/src/freenet/client/async/USKChecker.java	2008-02-02 20:05:30 UTC (rev 17479)
+++ trunk/freenet/src/freenet/client/async/USKChecker.java	2008-02-02 20:45:33 UTC (rev 17480)
@@ -25,12 +25,12 @@
 		this.cb = cb;
 	}
 	
-	public void onSuccess(ClientKeyBlock block, boolean fromStore, int token) {
+	public void onSuccess(ClientKeyBlock block, boolean fromStore, Object token) {
 		unregister();
 		cb.onSuccess((ClientSSKBlock)block);
 	}
 
-	public void onFailure(LowLevelGetException e, int token) {
+	public void onFailure(LowLevelGetException e, Object token) {
         if(Logger.shouldLog(Logger.MINOR, this))
         	Logger.minor(this, "onFailure: "+e+" for "+this);
 		// Firstly, can we retry?

Modified: trunk/freenet/src/freenet/node/RequestStarter.java
===================================================================
--- trunk/freenet/src/freenet/node/RequestStarter.java	2008-02-02 20:05:30 UTC (rev 17479)
+++ trunk/freenet/src/freenet/node/RequestStarter.java	2008-02-02 20:45:33 UTC (rev 17480)
@@ -153,11 +153,11 @@
 	
 	private boolean startRequest(SendableRequest req, boolean logMINOR) {
 		// Create a thread to handle starting the request, and the resulting feedback
-		int keyNum = -1;
+		Object keyNum = null;
 		while(true) {
 			try {
 				keyNum = req.chooseKey();
-				if(keyNum == -1) return false;
+				if(keyNum == null) return false;
 				core.getExecutor().execute(new SenderThread(req, keyNum), "RequestStarter$SenderThread for "+req);
 				if(logMINOR) Logger.minor(this, "Started "+req);
 				return true;
@@ -171,7 +171,7 @@
 					// Ignore
 				}
 			} catch (Throwable t) {
-				if(keyNum != -1) {
+				if(keyNum != null) {
 					// Re-queue
 					req.internalError(keyNum, t);
 					Logger.error(this, "Caught "+t+" while trying to start request");
@@ -197,9 +197,9 @@
 	private class SenderThread implements Runnable {
 
 		private final SendableRequest req;
-		private final int keyNum;
+		private final Object keyNum;
 		
-		public SenderThread(SendableRequest req, int keyNum) {
+		public SenderThread(SendableRequest req, Object keyNum) {
 			this.req = req;
 			this.keyNum = keyNum;
 		}

Modified: trunk/freenet/src/freenet/node/SendableGet.java
===================================================================
--- trunk/freenet/src/freenet/node/SendableGet.java	2008-02-02 20:05:30 UTC (rev 17479)
+++ trunk/freenet/src/freenet/node/SendableGet.java	2008-02-02 20:45:33 UTC (rev 17480)
@@ -24,16 +24,16 @@
 	public final ClientRequester parent;
 	
 	/** Get a numbered key to fetch. */
-	public abstract ClientKey getKey(int token);
+	public abstract ClientKey getKey(Object token);
 	
 	/** Get the fetch context (settings) object. */
 	public abstract FetchContext getContext();
 	
 	/** Called when/if the low-level request succeeds. */
-	public abstract void onSuccess(ClientKeyBlock block, boolean fromStore, int token);
+	public abstract void onSuccess(ClientKeyBlock block, boolean fromStore, Object token);
 	
 	/** Called when/if the low-level request fails. */
-	public abstract void onFailure(LowLevelGetException e, int token);
+	public abstract void onFailure(LowLevelGetException e, Object token);
 	
 	/** Should the request ignore the datastore? */
 	public abstract boolean ignoreStore();
@@ -50,7 +50,7 @@
 	/** Do the request, blocking. Called by RequestStarter. 
 	 * @return True if a request was executed. False if caller should try to find another request, and remove
 	 * this one from the queue. */
-	public boolean send(NodeClientCore core, RequestScheduler sched, int keyNum) {
+	public boolean send(NodeClientCore core, RequestScheduler sched, Object keyNum) {
 		ClientKey key = getKey(keyNum);
 		if(Logger.shouldLog(Logger.MINOR, this))
 			Logger.minor(this, "Sending get for key "+keyNum+" : "+key);
@@ -59,7 +59,7 @@
 			synchronized (this) {
 				if(isCancelled()) {
 					if(logMINOR) Logger.minor(this, "Cancelled: "+this);
-					onFailure(new LowLevelGetException(LowLevelGetException.CANCELLED), -1);
+					onFailure(new LowLevelGetException(LowLevelGetException.CANCELLED), null);
 					return false;
 				}	
 			}
@@ -115,7 +115,7 @@
 		getScheduler().removePendingKey(this, false, key);
 	}
 
-	public void internalError(int keyNum, Throwable t) {
+	public void internalError(Object keyNum, Throwable t) {
 		onFailure(new LowLevelGetException(LowLevelGetException.INTERNAL_ERROR, t.getMessage(), t), keyNum);
 	}
 	

Modified: trunk/freenet/src/freenet/node/SendableInsert.java
===================================================================
--- trunk/freenet/src/freenet/node/SendableInsert.java	2008-02-02 20:05:30 UTC (rev 17479)
+++ trunk/freenet/src/freenet/node/SendableInsert.java	2008-02-02 20:45:33 UTC (rev 17480)
@@ -12,12 +12,12 @@
 public abstract class SendableInsert extends SendableRequest {
 
 	/** Called when we successfully insert the data */
-	public abstract void onSuccess(int keyNum);
+	public abstract void onSuccess(Object keyNum);
 	
 	/** Called when we don't! */
-	public abstract void onFailure(LowLevelPutException e, int keyNum);
+	public abstract void onFailure(LowLevelPutException e, Object keyNum);
 
-	public void internalError(int keyNum, Throwable t) {
+	public void internalError(Object keyNum, Throwable t) {
 		onFailure(new LowLevelPutException(LowLevelPutException.INTERNAL_ERROR, t.getMessage(), t), keyNum);
 	}
 

Modified: trunk/freenet/src/freenet/node/SendableRequest.java
===================================================================
--- trunk/freenet/src/freenet/node/SendableRequest.java	2008-02-02 20:05:30 UTC (rev 17479)
+++ trunk/freenet/src/freenet/node/SendableRequest.java	2008-02-02 20:45:33 UTC (rev 17480)
@@ -21,11 +21,11 @@
 	/** Choose a key to fetch. Removes the block number from any internal queues 
 	 * (but not the key itself, implementors must have a separate queue of block 
 	 * numbers and mapping of block numbers to keys).
-	 * @return An integer identifying a specific key. -1 indicates no keys available. */
-	public abstract int chooseKey();
+	 * @return An object identifying a specific key. -1 indicates no keys available. */
+	public abstract Object chooseKey();
 	
 	/** All key identifiers */
-	public abstract int[] allKeys();
+	public abstract Object[] allKeys();
 	
 	/** ONLY called by RequestStarter. Start the actual request using the NodeClientCore
 	 * provided, and the key and key number earlier got from chooseKey(). 
@@ -35,7 +35,7 @@
 	 * @param key The key returned from grabKey().
 	 * @return True if a request was sent, false otherwise (in which case the request will
 	 * be removed if it hasn't already been). */
-	public abstract boolean send(NodeClientCore node, RequestScheduler sched, int keyNum);
+	public abstract boolean send(NodeClientCore node, RequestScheduler sched, Object keyNum);
 	
 	/** Get client context object */
 	public abstract Object getClient();
@@ -67,6 +67,6 @@
 	}
 
 	/** Requeue after an internal error */
-	public abstract void internalError(int keyNum, Throwable t);
+	public abstract void internalError(Object keyNum, Throwable t);
 
 }

Modified: trunk/freenet/src/freenet/node/SimpleSendableInsert.java
===================================================================
--- trunk/freenet/src/freenet/node/SimpleSendableInsert.java	2008-02-02 20:05:30 UTC (rev 17479)
+++ trunk/freenet/src/freenet/node/SimpleSendableInsert.java	2008-02-02 20:45:33 UTC (rev 17480)
@@ -42,13 +42,13 @@
 		this.scheduler = scheduler;
 	}
 	
-	public void onSuccess(int keyNum) {
+	public void onSuccess(Object keyNum) {
 		// Yay!
 		if(Logger.shouldLog(Logger.MINOR, this))
 			Logger.minor(this, "Finished insert of "+block);
 	}
 
-	public void onFailure(LowLevelPutException e, int keyNum) {
+	public void onFailure(LowLevelPutException e, Object keyNum) {
 		if(Logger.shouldLog(Logger.MINOR, this))
 			Logger.minor(this, "Failed insert of "+block+": "+e);
 	}
@@ -62,7 +62,7 @@
 		return 0;
 	}
 
-	public boolean send(NodeClientCore core, RequestScheduler sched, int keyNum) {
+	public boolean send(NodeClientCore core, RequestScheduler sched, Object keyNum) {
 		// Ignore keyNum, key, since this is a single block
 		boolean logMINOR = Logger.shouldLog(Logger.MINOR, this);
 		try {
@@ -114,13 +114,13 @@
 		return false;
 	}
 
-	public synchronized int[] allKeys() {
-		if(finished) return new int[] {};
-		return new int[] { 0 };
+	public synchronized Object[] allKeys() {
+		if(finished) return new Object[] {};
+		return new Object[] { new Integer(0) };
 	}
 
-	public synchronized int chooseKey() {
-		if(finished) return -1;
-		else return 0;
+	public synchronized Object chooseKey() {
+		if(finished) return null;
+		else return new Integer(0);
 	}
 }




More information about the cvs mailing list