From a6d31fa9f170c57ba80b0f5ede4ef9bcbe3c9375 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 14 May 2008 13:27:56 +0000 Subject: [PATCH] first version of request table change update tracking git-svn-id: http://trooper/svn/projects/jSite/trunk@876 c3eda9e8-030b-0410-8277-bc7414b0a119 --- src/net/pterodactylus/jsite/core/CoreImpl.java | 47 ++-- src/net/pterodactylus/jsite/core/CoreListener.java | 21 +- src/net/pterodactylus/jsite/core/NodeManager.java | 12 + src/net/pterodactylus/jsite/core/Request.java | 253 ++++++++++++++++++++- .../pterodactylus/jsite/core/RequestListener.java | 29 +-- .../pterodactylus/jsite/core/RequestManager.java | 66 +++--- src/net/pterodactylus/jsite/gui/MainWindow.java | 8 + .../pterodactylus/jsite/gui/SwingInterface.java | 22 +- 8 files changed, 357 insertions(+), 101 deletions(-) diff --git a/src/net/pterodactylus/jsite/core/CoreImpl.java b/src/net/pterodactylus/jsite/core/CoreImpl.java index 07c6345..73039f4 100644 --- a/src/net/pterodactylus/jsite/core/CoreImpl.java +++ b/src/net/pterodactylus/jsite/core/CoreImpl.java @@ -268,41 +268,24 @@ public class CoreImpl implements Core, NodeListener, RequestListener { /** * Notifies all listeners that a request was added to a node. * - * @param node - * The node the request was added to * @param request * The request that was added */ - private void fireRequestAdded(Node node, Request request) { + private void fireRequestAdded(Request request) { for (CoreListener coreListener: coreListeners) { - coreListener.requestAdded(node, request); + coreListener.requestAdded(request); } } /** * 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) { + */ + private void fireRequestProgressed(Request request) { for (CoreListener coreListener: coreListeners) { - coreListener.requestProgressed(request, totalBlocks, requiredBlocks, successfulBlocks, failedBlocks, fatallyFailedBlocks, finalizedTotal); + coreListener.requestProgressed(request); } } @@ -509,16 +492,22 @@ public class CoreImpl implements Core, NodeListener, RequestListener { /** * {@inheritDoc} */ - public void requestAdded(Node node, Request request) { - fireRequestAdded(node, request); + public void requestAdded(Request request) { + fireRequestAdded(request); } /** - * @see net.pterodactylus.jsite.core.RequestListener#requestProgressed(net.pterodactylus.jsite.core.Node, - * net.pterodactylus.jsite.core.Request, int, int, int, int, int, - * boolean) + * @see net.pterodactylus.jsite.core.RequestListener#requestProgressed(Request) */ - public void requestProgressed(Node node, Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) { - fireRequestProgressed(node, request, totalBlocks, requiredBlocks, successfulBlocks, failedBlocks, fatallyFailedBlocks, finalizedTotal); + public void requestProgressed(Request request) { + fireRequestProgressed(request); } + + /** + * @see net.pterodactylus.jsite.core.RequestListener#requestRemoved(net.pterodactylus.jsite.core.Request) + */ + public void requestRemoved(Request request) { + /* TODO */ + } + } diff --git a/src/net/pterodactylus/jsite/core/CoreListener.java b/src/net/pterodactylus/jsite/core/CoreListener.java index 04b063e..96fcd9d 100644 --- a/src/net/pterodactylus/jsite/core/CoreListener.java +++ b/src/net/pterodactylus/jsite/core/CoreListener.java @@ -190,32 +190,17 @@ public interface CoreListener { /** * Notifies a listener that a request was added to a node. * - * @param node - * The node the request was added to * @param request * The request that was added */ - public void requestAdded(Node node, Request request); + public void requestAdded(Request request); /** * Notifies a listener that a request made some progress. * * @param request * The request that made the progress - * @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 number of total blocks is - * finalized, false if it is not - */ - public void requestProgressed(Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal); + */ + public void requestProgressed(Request request); } diff --git a/src/net/pterodactylus/jsite/core/NodeManager.java b/src/net/pterodactylus/jsite/core/NodeManager.java index c676289..1e61980 100644 --- a/src/net/pterodactylus/jsite/core/NodeManager.java +++ b/src/net/pterodactylus/jsite/core/NodeManager.java @@ -409,6 +409,18 @@ public class NodeManager implements Iterable, PropertyChangeListener, High return nodeClients.get(node); } + /** + * Returns the node for a high-level client. + * + * @param highLevelClient + * The high-level client to get the node for + * @return The node for the high-level client, or null if the + * high-level client is not known + */ + public Node getNode(HighLevelClient highLevelClient) { + return clientNodes.get(highLevelClient); + } + // // PRIVATE METHODS // diff --git a/src/net/pterodactylus/jsite/core/Request.java b/src/net/pterodactylus/jsite/core/Request.java index 47349ca..6f8afa1 100644 --- a/src/net/pterodactylus/jsite/core/Request.java +++ b/src/net/pterodactylus/jsite/core/Request.java @@ -19,6 +19,12 @@ package net.pterodactylus.jsite.core; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + /** * A request is an ongoing download or upload reported by the freenet node. * @@ -27,20 +33,122 @@ package net.pterodactylus.jsite.core; */ public class Request { + /** Name of the “client token” property. */ + public static final String PROPERTY_CLIENT_TOKEN = "clientToken"; + + /** Name of the “total blocks” property. */ + public static final String PROPERTY_TOTAL_BLOCKS = "totalBlocks"; + + /** Name of the “required blocks” property. */ + public static final String PROPERTY_REQUIRED_BLOCKS = "requiredBlocks"; + + /** Name of the “successful blocks” property. */ + public static final String PROPERTY_SUCCESSFUL_BLOCKS = "successfulBlocks"; + + /** Name of the “failed blocks” property. */ + public static final String PROPERTY_FAILED_BLOCKS = "failedBlocks"; + + /** Name of the “fatally failed blocks” property. */ + public static final String PROPERTY_FATALLY_FAILED_BLOCKS = "fatallyFailedBlocks"; + + /** Name of the “total finalized” property. */ + public static final String PROPERTY_TOTAL_FINALIZED = "totalFinalized"; + + /** Property change listeners. */ + private final List propertyChangeListeners = Collections.synchronizedList(new ArrayList()); + + /** The node the request belongs to. */ + private final Node node; + /** The identifier of the request. */ private final String identifier; + /** The client token of the request. */ + private String clientToken; + + /** The total number of blocks. */ + private int totalBlocks; + + /** The required number of blocks. */ + private int requiredBlocks; + + /** The number of successful blocks. */ + private int successfulBlocks; + + /** The number of failedBlocks. */ + private int failedBlocks; + + /** The number of fatally failed blocks. */ + private int fatallyFailedBlocks; + + /** Whether the total number has been finalized. */ + private boolean totalFinalized; + + // + // EVENT MANAGEMENT + // + + /** + * Adds a property change listener. + * + * @param propertyChangeListener + * The property change listener to add + */ + public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) { + propertyChangeListeners.add(propertyChangeListener); + } + + /** + * Removes a property change listener. + * + * @param propertyChangeListener + * The property change listener to remove + */ + public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) { + propertyChangeListeners.remove(propertyChangeListener); + } + + /** + * Notifies all listeners that a property has changed. + * + * @param property + * The name of the property + * @param oldValue + * The old value of the property + * @param newValue + * The new value of the property + */ + private void firePropertyChange(String property, Object oldValue, Object newValue) { + PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this, property, oldValue, newValue); + for (PropertyChangeListener propertyChangeListener: propertyChangeListeners) { + propertyChangeListener.propertyChange(propertyChangeEvent); + } + + } + /** * Creates a new request with the given identifier. * + * @param node + * The node the request belongs to * @param identifier * The identifier of the request */ - Request(String identifier) { + Request(Node node, String identifier) { + this.node = node; this.identifier = identifier; } /** + * Returns the node the request belongs to. + * + * @return The node the request belongs to + */ + public Node getNode() { + return node; + } + + /** * Returns the identifier of the request. It is unique per node. * * @return The identifier of the request @@ -49,4 +157,147 @@ public class Request { return identifier; } + /** + * Returns the client token of the request. + * + * @return The client token of the request + */ + public String getClientToken() { + return clientToken; + } + + /** + * Sets the client token of the request. + * + * @param clientToken + * The client token of the request + */ + public void setClientToken(String clientToken) { + String oldClientToken = this.clientToken; + this.clientToken = clientToken; + if (((oldClientToken == null) && (clientToken != null)) || ((oldClientToken != null) && (clientToken == null)) || ((clientToken != null) && !clientToken.equals(oldClientToken))) { + firePropertyChange(PROPERTY_CLIENT_TOKEN, oldClientToken, clientToken); + } + } + + /** + * Returns the total number of blocks of a request. Until + * {@link #isTotalFinalized()} returns true this value may + * change! + * + * @return The total number of blocks of a request + */ + public int getTotalBlocks() { + return totalBlocks; + } + + /** + * Sets the total number of blocks of a request. + * + * @param totalBlocks + * The total number of blocks + */ + public void setTotalBlocks(int totalBlocks) { + int oldTotalBlocks = this.totalBlocks; + this.totalBlocks = totalBlocks; + if (oldTotalBlocks != totalBlocks) { + firePropertyChange(PROPERTY_TOTAL_BLOCKS, oldTotalBlocks, totalBlocks); + } + } + + /** + * @return the requiredBlocks + */ + public int getRequiredBlocks() { + return requiredBlocks; + } + + /** + * @param requiredBlocks + * the requiredBlocks to set + */ + public void setRequiredBlocks(int requiredBlocks) { + int oldRequiredBlocks = this.requiredBlocks; + this.requiredBlocks = requiredBlocks; + if (oldRequiredBlocks != requiredBlocks) { + firePropertyChange(PROPERTY_REQUIRED_BLOCKS, oldRequiredBlocks, requiredBlocks); + } + } + + /** + * @return the successfulBlocks + */ + public int getSuccessfulBlocks() { + return successfulBlocks; + } + + /** + * @param successfulBlocks + * the successfulBlocks to set + */ + public void setSuccessfulBlocks(int successfulBlocks) { + int oldSuccessfulBlocks = this.successfulBlocks; + this.successfulBlocks = successfulBlocks; + if (oldSuccessfulBlocks != successfulBlocks) { + firePropertyChange(PROPERTY_SUCCESSFUL_BLOCKS, oldSuccessfulBlocks, successfulBlocks); + } + } + + /** + * @return the failedBlocks + */ + public int getFailedBlocks() { + return failedBlocks; + } + + /** + * @param failedBlocks + * the failedBlocks to set + */ + public void setFailedBlocks(int failedBlocks) { + int oldFailedBlocks = this.failedBlocks; + this.failedBlocks = failedBlocks; + if (oldFailedBlocks != failedBlocks) { + firePropertyChange(PROPERTY_FAILED_BLOCKS, oldFailedBlocks, failedBlocks); + } + } + + /** + * @return the fatallyFailedBlocks + */ + public int getFatallyFailedBlocks() { + return fatallyFailedBlocks; + } + + /** + * @param fatallyFailedBlocks + * the fatallyFailedBlocks to set + */ + public void setFatallyFailedBlocks(int fatallyFailedBlocks) { + int oldFatallyFailedBlocks = this.fatallyFailedBlocks; + this.fatallyFailedBlocks = fatallyFailedBlocks; + if (oldFatallyFailedBlocks != fatallyFailedBlocks) { + firePropertyChange(PROPERTY_FATALLY_FAILED_BLOCKS, oldFatallyFailedBlocks, fatallyFailedBlocks); + } + } + + /** + * @return the totalFinalized + */ + public boolean isTotalFinalized() { + return totalFinalized; + } + + /** + * @param totalFinalized + * the totalFinalized to set + */ + public void setTotalFinalized(boolean totalFinalized) { + boolean oldTotalFinalized = this.totalFinalized; + this.totalFinalized = totalFinalized; + if (oldTotalFinalized != totalFinalized) { + firePropertyChange(PROPERTY_TOTAL_FINALIZED, oldTotalFinalized, totalFinalized); + } + } + } diff --git a/src/net/pterodactylus/jsite/core/RequestListener.java b/src/net/pterodactylus/jsite/core/RequestListener.java index 32c55a8..27ce517 100644 --- a/src/net/pterodactylus/jsite/core/RequestListener.java +++ b/src/net/pterodactylus/jsite/core/RequestListener.java @@ -32,34 +32,25 @@ public interface RequestListener extends EventListener { /** * Notifies a listener that a request was added to a node. * - * @param node - * The node the request was added to * @param request * The request that was added */ - public void requestAdded(Node node, Request request); + public void requestAdded(Request request); /** * Notifies a listener that a request made progress. * - * @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 */ - public void requestProgressed(Node node, Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal); + public void requestProgressed(Request request); + + /** + * Notifies a listener that a request was removed. + * + * @param request + * The request that was removed + */ + public void requestRemoved(Request request); } diff --git a/src/net/pterodactylus/jsite/core/RequestManager.java b/src/net/pterodactylus/jsite/core/RequestManager.java index 002cd17..618dbf1 100644 --- a/src/net/pterodactylus/jsite/core/RequestManager.java +++ b/src/net/pterodactylus/jsite/core/RequestManager.java @@ -25,7 +25,6 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; @@ -60,7 +59,7 @@ public class RequestManager implements NodeListener, HighLevelProgressListener { /** Request lists for all nodes. */ @SuppressWarnings("unused") - private Map> nodeRequests = Collections.synchronizedMap(new HashMap>()); + private Map> nodeRequests = Collections.synchronizedMap(new HashMap>()); // // EVENT MANAGEMENT @@ -89,44 +88,29 @@ public class RequestManager implements NodeListener, HighLevelProgressListener { /** * Notifies all listeners that a request was added. * - * @param node - * The node that added the request * @param request * The request that was added */ - private void fireRequestAdded(Node node, Request request) { + private void fireRequestAdded(Request request) { for (RequestListener requestListener: requestListeners) { - requestListener.requestAdded(node, request); + requestListener.requestAdded(request); } } /** * 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) { + private void fireRequestProgressed(Request request) { for (RequestListener requestListener: requestListeners) { - requestListener.requestProgressed(node, request, totalBlocks, requiredBlocks, successfulBlocks, failedBlocks, fatallyFailedBlocks, finalizedTotal); + requestListener.requestProgressed(request); } } + // TODO - fireRequestRemoved + // // ACCESSORS // @@ -164,6 +148,8 @@ public class RequestManager implements NodeListener, HighLevelProgressListener { logger.log(Level.WARNING, "no client for node: " + node); return; } + final Map identifierRequests = new HashMap(); + nodeRequests.put(node, identifierRequests); HighLevelCallback requestListCallback = highLevelClient.getRequests(); requestListCallback.addHighLevelCallbackListener(new HighLevelCallbackListener() { @@ -177,9 +163,10 @@ public class RequestManager implements NodeListener, HighLevelProgressListener { return; } for (RequestResult requestResult: requestListResult) { - Request request = new Request(requestResult.getIdentifier()); + Request request = new Request(node, requestResult.getIdentifier()); + identifierRequests.put(requestResult.getIdentifier(), request); /* TODO - fill request */ - fireRequestAdded(node, request); + fireRequestAdded(request); } } }); @@ -253,11 +240,32 @@ public class RequestManager implements NodeListener, HighLevelProgressListener { // /** - * @see net.pterodactylus.fcp.highlevel.HighLevelProgressListener#progressReceived(java.lang.String, - * net.pterodactylus.fcp.highlevel.HighLevelProgress) + * @see net.pterodactylus.fcp.highlevel.HighLevelProgressListener#progressReceived(HighLevelClient, + * String, 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()); + public void progressReceived(HighLevelClient highLevelClient, String identifier, HighLevelProgress highLevelProgress) { + Node node = nodeManager.getNode(highLevelClient); + if (node == null) { + logger.warning("got high-level client without node: " + highLevelClient); + return; + } + Map identifierRequests = nodeRequests.get(node); + if (identifierRequests == null) { + logger.warning("got node without request map: " + node); + return; + } + Request request = identifierRequests.get(identifier); + if (request == null) { + logger.warning("got progress for unknown request: " + identifier); + return; + } + request.setTotalBlocks(highLevelProgress.getTotalBlocks()); + request.setRequiredBlocks(highLevelProgress.getRequiredBlocks()); + request.setSuccessfulBlocks(highLevelProgress.getSuccessfulBlocks()); + request.setFailedBlocks(highLevelProgress.getFailedBlocks()); + request.setFatallyFailedBlocks(highLevelProgress.getFatallyFailedBlocks()); + request.setTotalFinalized(highLevelProgress.isTotalFinalized()); + fireRequestProgressed(request); } } diff --git a/src/net/pterodactylus/jsite/gui/MainWindow.java b/src/net/pterodactylus/jsite/gui/MainWindow.java index 813df7c..04c51d2 100644 --- a/src/net/pterodactylus/jsite/gui/MainWindow.java +++ b/src/net/pterodactylus/jsite/gui/MainWindow.java @@ -36,7 +36,9 @@ import javax.swing.JFrame; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; +import javax.swing.JScrollPane; import javax.swing.JTabbedPane; +import javax.swing.JTable; import javax.swing.JToolBar; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder; @@ -106,6 +108,9 @@ public class MainWindow extends JFrame implements WindowListener, I18nable { /** The project overview panel. */ private Box projectOverviewPanel; + /** The request table. */ + private JTable requestTable; + /** * Creates a new main window that redirects all actions to the given swing * interface. @@ -335,6 +340,9 @@ public class MainWindow extends JFrame implements WindowListener, I18nable { projectOverviewPanel.add(addProjectButton); projectOverviewPanel.add(Box.createVerticalGlue()); + requestTable = new JTable(swingInterface.getRequestTableModel()); + getContentPane().add(new JScrollPane(requestTable), BorderLayout.CENTER); + // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12)); // getContentPane().add(lowerPanel, BorderLayout.CENTER); } diff --git a/src/net/pterodactylus/jsite/gui/SwingInterface.java b/src/net/pterodactylus/jsite/gui/SwingInterface.java index 7d0a96d..4d83851 100644 --- a/src/net/pterodactylus/jsite/gui/SwingInterface.java +++ b/src/net/pterodactylus/jsite/gui/SwingInterface.java @@ -144,6 +144,9 @@ public class SwingInterface implements CoreListener, LoggingListener { /** The list of all defined nodes. */ private List nodeList = Collections.synchronizedList(new ArrayList()); + /** The request table model. */ + private RequestTableModel requestTableModel = new RequestTableModel(); + // // CONFIGURATION // @@ -370,6 +373,15 @@ public class SwingInterface implements CoreListener, LoggingListener { return deleteProjectAction; } + /** + * Returns the request table model. + * + * @return The request table model + */ + RequestTableModel getRequestTableModel() { + return requestTableModel; + } + // // ACTIONS // @@ -950,7 +962,7 @@ public class SwingInterface implements CoreListener, LoggingListener { JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.nodeConnectionFailed.message", node.getName(), node.getHostname(), node.getPort(), (cause != null) ? cause.getMessage() : "no reason given"), I18n.get("mainWindow.error.nodeConnectionFailed.title"), JOptionPane.ERROR_MESSAGE); mainWindow.refreshNodeMenuItems(); } - + /** * {@inheritDoc} */ @@ -966,17 +978,17 @@ public class SwingInterface implements CoreListener, LoggingListener { /** * {@inheritDoc} */ - public void requestAdded(Node node, Request request) { - logger.log(Level.INFO, "request added to node: " + request + ", " + node); + public void requestAdded(Request request) { + logger.log(Level.INFO, "request added to node: " + request + ", " + request.getNode()); /* TODO - implement */ + requestTableModel.addRequest(request); } /** * {@inheritDoc} */ - public void requestProgressed(Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) { + public void requestProgressed(Request request) { /* TODO - update table model */ - mainWindow.setStatusBarText(request.getIdentifier() + " @ " + ((10000 * successfulBlocks / requiredBlocks) / 100.0) + "%"); } // -- 2.7.4