X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fcore%2FRequestManager.java;h=002cd17748ecbf4f5ac60d56054148786daf59d8;hb=d9babeba262240417325c00535e4c91c24520f2c;hp=925573d9328c1c3927eb0b61ed3f205d38b51526;hpb=ebd531bc775036dd66a7e20abebbcb480af35491;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/core/RequestManager.java b/src/net/pterodactylus/jsite/core/RequestManager.java index 925573d..002cd17 100644 --- a/src/net/pterodactylus/jsite/core/RequestManager.java +++ b/src/net/pterodactylus/jsite/core/RequestManager.java @@ -32,6 +32,8 @@ import java.util.logging.Logger; import net.pterodactylus.fcp.highlevel.HighLevelCallback; import net.pterodactylus.fcp.highlevel.HighLevelCallbackListener; import net.pterodactylus.fcp.highlevel.HighLevelClient; +import net.pterodactylus.fcp.highlevel.HighLevelProgress; +import net.pterodactylus.fcp.highlevel.HighLevelProgressListener; import net.pterodactylus.fcp.highlevel.RequestListResult; import net.pterodactylus.fcp.highlevel.RequestResult; import net.pterodactylus.util.logging.Logging; @@ -45,7 +47,7 @@ import net.pterodactylus.util.logging.Logging; * @author David ‘Bombe’ Roden <bombe@freenetproject.org> * @version $Id$ */ -public class RequestManager implements NodeListener { +public class RequestManager implements NodeListener, HighLevelProgressListener { /** Logger. */ private static final Logger logger = Logging.getLogger(RequestManager.class.getName()); @@ -98,6 +100,33 @@ public class RequestManager implements NodeListener { } } + /** + * Notifies all listeners that a request progressed. + * + * @param node + * The node that runs the request + * @param request + * The request + * @param totalBlocks + * The total number of blocks + * @param requiredBlocks + * The number of required blocks + * @param successfulBlocks + * The number of successful blocks + * @param failedBlocks + * The number of failed blocks + * @param fatallyFailedBlocks + * The number of fatally failed blocks + * @param finalizedTotal + * true if the total number of blocks in final, + * false otherwise + */ + private void fireRequestProgressed(Node node, Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) { + for (RequestListener requestListener: requestListeners) { + requestListener.requestProgressed(node, request, totalBlocks, requiredBlocks, successfulBlocks, failedBlocks, fatallyFailedBlocks, finalizedTotal); + } + } + // // ACCESSORS // @@ -130,34 +159,30 @@ public class RequestManager implements NodeListener { * if an I/O error occurs while communicating with the node */ private void getRequests(final Node node) throws IOException { - HighLevelClient highLevelClient = nodeManager.borrowHighLevelClient(node); + HighLevelClient highLevelClient = nodeManager.getHighLevelClient(node); if (highLevelClient == null) { logger.log(Level.WARNING, "no client for node: " + node); return; } - try { - HighLevelCallback requestListCallback = highLevelClient.getRequests(); - requestListCallback.addHighLevelCallbackListener(new HighLevelCallbackListener() { - - @SuppressWarnings("synthetic-access") - public void gotResult(HighLevelCallback highLevelCallback) { - RequestListResult requestListResult; - try { - requestListResult = highLevelCallback.getResult(); - } catch (InterruptedException e) { - logger.log(Level.SEVERE, "getResult() blocked and was interrupted"); - return; - } - for (RequestResult requestResult: requestListResult) { - Request request = new Request(requestResult.getIdentifier()); - /* TODO - fill request */ - fireRequestAdded(node, request); - } + HighLevelCallback requestListCallback = highLevelClient.getRequests(); + requestListCallback.addHighLevelCallbackListener(new HighLevelCallbackListener() { + + @SuppressWarnings("synthetic-access") + public void gotResult(HighLevelCallback highLevelCallback) { + RequestListResult requestListResult; + try { + requestListResult = highLevelCallback.getResult(); + } catch (InterruptedException e) { + logger.log(Level.SEVERE, "getResult() blocked and was interrupted"); + return; } - }); - } finally { - nodeManager.returnHighLevelClient(highLevelClient); - } + for (RequestResult requestResult: requestListResult) { + Request request = new Request(requestResult.getIdentifier()); + /* TODO - fill request */ + fireRequestAdded(node, request); + } + } + }); } // @@ -168,33 +193,37 @@ public class RequestManager implements NodeListener { * {@inheritDoc} */ public void nodeAdded(Node node) { - /* ignore. */ + HighLevelClient highLevelClient = nodeManager.getHighLevelClient(node); + if (highLevelClient == null) { + logger.warning("got nodeAdded but no high-level client: " + node); + return; + } + highLevelClient.addHighLevelProgressListener(this); } /** * {@inheritDoc} */ public void nodeRemoved(Node node) { - /* ignore. */ + HighLevelClient highLevelClient = nodeManager.getHighLevelClient(node); + if (highLevelClient == null) { + logger.warning("got nodeRemoved but no high-level client: " + node); + return; + } + highLevelClient.removeHighLevelProgressListener(this); } /** * {@inheritDoc} */ public void nodeConnected(Node node) { - HighLevelClient highLevelClient = nodeManager.borrowHighLevelClient(node); + HighLevelClient highLevelClient = nodeManager.getHighLevelClient(node); if (highLevelClient == null) { logger.log(Level.WARNING, "got no high-level client for node " + node); return; } try { highLevelClient.setWatchGlobal(true); - } catch (IOException ioe1) { - /* ignore exception, disconnects are handled elsewhere. */ - } finally { - nodeManager.returnHighLevelClient(highLevelClient); - } - try { getRequests(node); } catch (IOException e) { /* ignore exception, disconnects are handled elsewhere. */ @@ -204,8 +233,31 @@ public class RequestManager implements NodeListener { /** * {@inheritDoc} */ + public void nodeConnectionFailed(Node node, Throwable cause) { + /* we don't care about this. */ + } + + /** + * {@inheritDoc} + */ public void nodeDisconnected(Node node, Throwable throwable) { - /* TODO - remove all requests. */ + HighLevelClient highLevelClient = nodeManager.getHighLevelClient(node); + if (highLevelClient == null) { + logger.warning("got nodeDisconnected from node without high-level client: " + node); + return; + } + } + + // + // INTERFACE HighLevelProgressListener + // + + /** + * @see net.pterodactylus.fcp.highlevel.HighLevelProgressListener#progressReceived(java.lang.String, + * net.pterodactylus.fcp.highlevel.HighLevelProgress) + */ + public void progressReceived(String identifier, HighLevelProgress highLevelProgress) { + fireRequestProgressed(null, new Request(identifier), highLevelProgress.getTotalBlocks(), highLevelProgress.getRequiredBlocks(), highLevelProgress.getSuccessfulBlocks(), highLevelProgress.getFailedBlocks(), highLevelProgress.getFatallyFailedBlocks(), highLevelProgress.isTotalFinalized()); } }