import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Logger;
/** The FCP connection to the node. */
private FcpConnection fcpConnection;
+ /** Listeners for high-level client events. */
+ private List<HighLevelClientListener> highLevelClientListeners = Collections.synchronizedList(new ArrayList<HighLevelClientListener>());
+
/** The listener for the connection. */
private HighLevelClientFcpListener highLevelClientFcpListener = new HighLevelClientFcpListener();
}
//
+ // EVENT MANAGEMENT
+ //
+
+ /**
+ * Adds the given high-level client listener to list of listeners.
+ *
+ * @param highLevelClientListener
+ * The listener to add
+ */
+ public void addHighLevelClientListener(HighLevelClientListener highLevelClientListener) {
+ highLevelClientListeners.add(highLevelClientListener);
+ }
+
+ /**
+ * Removes the given high-level client listener from the list of listeners.
+ *
+ * @param highLevelClientListener
+ * The listener to remove
+ */
+ public void removeHighLevelClientListener(HighLevelClientListener highLevelClientListener) {
+ highLevelClientListeners.remove(highLevelClientListener);
+ }
+
+ /**
+ * Notifies all listeners that a client has connected.
+ */
+ private void fireClientConnected() {
+ for (HighLevelClientListener highLevelClientListener: highLevelClientListeners) {
+ highLevelClientListener.clientConnected(this);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a client has disconnected.
+ *
+ * @param throwable
+ * The exception that caused the disconnect, or <code>null</code>
+ * if there was no exception
+ */
+ private void fireClientDisconnected(Throwable throwable) {
+ for (HighLevelClientListener highLevelClientListener: highLevelClientListeners) {
+ highLevelClientListener.clientDisconnected(this, throwable);
+ }
+ }
+
+ //
// ACCESSORS
//
* Disconnects the client from the node.
*/
public void disconnect() {
- fcpConnection.disconnect();
+ disconnect(null);
}
/**
}
/**
+ * Disconnects the client from the node, handing the given Throwable to
+ * {@link #fireClientDisconnected(Throwable)}.
+ *
+ * @param throwable
+ * The exception that caused the disconnect, or <code>null</code>
+ * if there was no exception
+ */
+ private void disconnect(Throwable throwable) {
+ fcpConnection.close();
+ fireClientDisconnected(throwable);
+ }
+
+ /**
* FCP listener for {@link HighLevelClient}.
*
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
//
/**
- * @see net.pterodactylus.fcp.FcpListener#connectionClosed(net.pterodactylus.fcp.FcpConnection)
+ * @see net.pterodactylus.fcp.FcpListener#connectionClosed(net.pterodactylus.fcp.FcpConnection,
+ * Throwable)
*/
@SuppressWarnings("synthetic-access")
- public void connectionClosed(FcpConnection fcpConnection) {
+ public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
if (fcpConnection != HighLevelClient.this.fcpConnection) {
return;
}
cancelIdentifier(null);
+ disconnect(throwable);
}
/**
connectCallback.setDone();
connectCallback = null;
}
+ fireClientConnected();
}
/**