/**
* Interface for the core.
- *
+ *
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
* @version $Id$
*/
/**
* Adds the given listener to the list of registered listeners.
- *
+ *
* @param coreListener
* The listener to add
*/
/**
* Removes the given listener from the list of registered listeners.
- *
+ *
* @param coreListener
* The listener to remove
*/
public void removeCoreListener(CoreListener coreListener);
/**
+ * Adds the given node to the core.
+ *
+ * @param node
+ * The node to add
+ */
+ public void addNode(Node node);
+
+ /**
+ * Removes the given node from the core.
+ *
+ * @param node
+ * The node to remove
+ */
+ public void removeNode(Node node);
+
+ /**
* Returns the list of all configured nodes.
- *
+ *
* @return All configured nodes
*/
public List<Node> getNodes();
/**
* Returns whether the core is currently connected to the given node.
- *
+ *
* @param node
* The node to check
* @return <code>true</code> if the core is currently connected to the
/**
* Connects to the given node.
- *
+ *
* @param node
* The node to connect to
*/
/**
* Disconnects from the given node.
- *
+ *
* @param node
* The node to disconnect from
*/
}
/**
+ * Notifies all listeners that a node was added to the core.
+ *
+ * @param node
+ * The node that was added
+ */
+ private void fireNodeAdded(Node node) {
+ for (CoreListener coreListener: coreListeners) {
+ coreListener.nodeAdded(node);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a node was removed from the core.
+ *
+ * @param node
+ * The node that was removed
+ */
+ private void fireNodeRemoved(Node node) {
+ for (CoreListener coreListener: coreListeners) {
+ coreListener.nodeRemoved(node);
+ }
+ }
+
+ /**
* Notifies all listeners that a connection to the given node is now being
* established.
*
/**
* {@inheritDoc}
*/
+ public void addNode(Node node) {
+ nodeManager.addNode(node);
+ fireNodeAdded(node);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void removeNode(Node node) {
+ nodeManager.removeNode(node);
+ fireNodeRemoved(node);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public void connectToNode(Node node) {
fireNodeConnecting(node);
nodeManager.addNode(node);
/**
* {@inheritDoc}
*/
+ public void nodeAdded(Node node) {
+ fireNodeAdded(node);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void nodeRemoved(Node node) {
+ fireNodeRemoved(node);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public void nodeConnected(Node node) {
fireNodeConnected(node);
}
//
/**
+ * Notifies a listener that a node was added to the core.
+ *
+ * @param node
+ * The node that was added.
+ */
+ public void nodeAdded(Node node);
+
+ /**
+ * Notifies a listener that a node was removed from the core. Before a node
+ * is removed, it will be disconnected (and
+ * {@link #nodeDisconnected(Node, Throwable)} will be called).
+ *
+ * @param node
+ * The node that was removed
+ */
+ public void nodeRemoved(Node node);
+
+ /**
* Notifies all listeners that the core started connecting to the given
- * node.
+ * node. Before a node is connected, it will be added (and
+ * {@link #nodeAdded(Node)} will be called).
*
* @param node
* The node that is being connected
*/
public void requestAdded(Node node, 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
+ * <code>true</code> if the number of total blocks is
+ * finalized, <code>false</code> if it is not
+ */
+ public void requestProgressed(Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal);
+
}
public interface NodeListener extends EventListener {
/**
+ * Notifies a listener that a node was added.
+ *
+ * @param node
+ * The node that was added
+ */
+ public void nodeAdded(Node node);
+
+ /**
+ * Notifies a listener that a node was removed.
+ *
+ * @param node
+ * The node that was removed
+ */
+ public void nodeRemoved(Node node);
+
+ /**
* Notifies a listener that a connection to the given node was established.
*
* @param node
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
* @version $Id$
*/
-public class NodeManager implements HighLevelClientListener {
+public class NodeManager implements Iterable<Node>, HighLevelClientListener {
/** Logger. */
private static final Logger logger = Logging.getLogger(NodeManager.class.getName());
}
/**
+ * Notifies all listeners that a node was added.
+ *
+ * @param node
+ * The node that was added.
+ */
+ private void fireNodeAdded(Node node) {
+ for (NodeListener nodeListener: nodeListeners) {
+ nodeListener.nodeAdded(node);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a node was removed.
+ *
+ * @param node
+ * The node that was removed
+ */
+ private void fireNodeRemoved(Node node) {
+ for (NodeListener nodeListener: nodeListeners) {
+ nodeListener.nodeRemoved(node);
+ }
+ }
+
+ /**
* Notifies all listeners that the given node was connected.
*
* @param node
return nodes.contains(node);
}
+ /**
+ * {@inheritDoc}
+ */
+ public Iterator<Node> iterator() {
+ return nodes.iterator();
+ }
+
//
// ACTIONS
//
newNode.setPort(nodePort);
loadedNodes.add(newNode);
}
+ logger.fine("loaded " + loadedNodes.size() + " nodes from config");
synchronized (syncObject) {
nodes.clear();
nodes.addAll(loadedNodes);
}
+ for (Node node: nodes) {
+ fireNodeAdded(node);
+ }
}
/**
synchronized (syncObject) {
if (!nodes.contains(node)) {
nodes.add(node);
+ fireNodeAdded(node);
}
}
}
if (nodeClients.containsKey(node)) {
disconnect(node);
}
+ fireNodeRemoved(node);
}
}
import java.io.IOException;
import java.util.ArrayList;
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;
/** The node manager. */
private NodeManager nodeManager;
+ /** Request lists for all nodes. */
+ @SuppressWarnings("unused")
+ private Map<Node, Set<Request>> nodeRequests = Collections.synchronizedMap(new HashMap<Node, Set<Request>>());
+
//
// EVENT MANAGEMENT
//
// ACTIONS
//
+ //
+ // PRIVATE ACTIONS
+ //
+
/**
* Requests a list of all running requests from a node. This method will
* block until the request has been sent!
* @throws IOException
* if an I/O error occurs while communicating with the node
*/
- public void getRequests(final Node node) throws IOException {
+ private void getRequests(final Node node) throws IOException {
HighLevelClient highLevelClient = nodeManager.borrowHighLevelClient(node);
if (highLevelClient == null) {
logger.log(Level.WARNING, "no client for node: " + node);
/**
* {@inheritDoc}
*/
+ public void nodeAdded(Node node) {
+ /* ignore. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void nodeRemoved(Node node) {
+ /* ignore. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public void nodeConnected(Node node) {
- /* TODO - get all requests. */
+ HighLevelClient highLevelClient = nodeManager.borrowHighLevelClient(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. */
+ }
}
/**
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
+import java.awt.event.WindowListener;
import javax.swing.Action;
import javax.swing.Box;
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
* @version $Id$
*/
-public class MainWindow extends JFrame implements I18nable {
+public class MainWindow extends JFrame implements WindowListener, I18nable {
/** The swing interface that receives all actions. */
private final SwingInterface swingInterface;
pack();
SwingUtils.center(this);
I18n.registerI18nable(this);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ addWindowListener(this);
}
//
SwingUtils.repackCentered(this);
}
+ //
+ // INTERFACE WindowListener
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ public void windowActivated(WindowEvent e) {
+ /* do nothing. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void windowClosed(WindowEvent e) {
+ /* do nothing. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void windowClosing(WindowEvent e) {
+ swingInterface.getQuitAction().actionPerformed(null);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void windowDeactivated(WindowEvent e) {
+ /* do nothing. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void windowDeiconified(WindowEvent e) {
+ /* do nothing. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void windowIconified(WindowEvent e) {
+ /* do nothing. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void windowOpened(WindowEvent e) {
+ /* do nothing. */
+ }
+
}
private ConfigurationDialog configurationDialog;
/** The list of all defined nodes. */
- private List<Node> nodeList;
+ private List<Node> nodeList = Collections.synchronizedList(new ArrayList<Node>());
//
// CONFIGURATION
* Quits jSite.
*/
private void quit() {
+ /* TODO - ask */
+ core.stop();
saveConfig();
System.exit(0);
}
* The list of nodes
*/
private void rebuildNodeActions(List<Node> nodes) {
+ logger.fine("rebuilding node actions…");
nodeConnectActions.clear();
nodeNodeConnectActions.clear();
nodeConnectActionNodes.clear();
nodeNodeDisconnectActions.clear();
nodeDisconnectActionNodes.clear();
for (Node node: nodes) {
+ logger.finer("adding node “" + node + "” to menu");
Action nodeConnectAction = new AbstractAction(node.getName()) {
/**
* {@inheritDoc}
*/
public void coreLoaded() {
- this.nodeList = core.getNodes();
- manageNodesDialog.setNodeList(nodeList);
mainWindow.setVisible(true);
mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.coreLoaded"));
}
/**
* {@inheritDoc}
*/
+ public void nodeAdded(Node node) {
+ logger.log(Level.INFO, "node added: " + node);
+ nodeList.add(node);
+ manageNodesDialog.setNodeList(nodeList);
+ rebuildNodeActions(nodeList);
+ mainWindow.refreshNodeMenuItems();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void nodeRemoved(Node node) {
+ logger.log(Level.INFO, "node removed: " + node);
+ nodeList.remove(node);
+ rebuildNodeActions(nodeList);
+ mainWindow.refreshNodeMenuItems();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public void nodeConnecting(Node node) {
Action nodeConnectAction = nodeNodeConnectActions.get(node);
nodeConnectActions.remove(nodeConnectAction);
logger.log(Level.INFO, "request added to node: " + request + ", " + node);
/* TODO - implement */
}
-
+
+ /**
+ * {@inheritDoc}
+ */
+ public void requestProgressed(Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
+ /* TODO - update table model */
+ }
+
//
// INTERFACE LoggingListener
//