package net.pterodactylus.fcp;
+import java.io.Closeable;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.logging.Logger;
/**
* An FCP connection to a Freenet node.
- *
+ *
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
* @version $Id$
*/
-public class FcpConnection {
+public class FcpConnection implements Closeable {
+
+ /** Logger. */
+ private static final Logger logger = Logger.getLogger(FcpConnection.class.getName());
/** The default port for FCP v2. */
public static final int DEFAULT_PORT = 9481;
/**
* Creates a new FCP connection to the freenet node running on localhost,
* using the default port.
- *
+ *
* @throws UnknownHostException
* if the hostname can not be resolved
*/
/**
* Creates a new FCP connection to the Freenet node running on the given
* host, listening on the default port.
- *
+ *
* @param host
* The hostname of the Freenet node
* @throws UnknownHostException
/**
* Creates a new FCP connection to the Freenet node running on the given
* host, listening on the given port.
- *
+ *
* @param host
* The hostname of the Freenet node
* @param port
/**
* Creates a new FCP connection to the Freenet node running at the given
* address, listening on the default port.
- *
+ *
* @param address
* The address of the Freenet node
*/
/**
* Creates a new FCP connection to the Freenet node running at the given
* address, listening on the given port.
- *
+ *
* @param address
* The address of the Freenet node
* @param port
/**
* Adds the given listener to the list of listeners.
- *
+ *
* @param fcpListener
* The listener to add
*/
/**
* Removes the given listener from the list of listeners.
- *
+ *
* @param fcpListener
* The listener to remove
*/
/**
* Notifies listeners that a “NodeHello” message was received.
- *
+ *
* @see FcpListener#receivedNodeHello(FcpConnection, NodeHello)
* @param nodeHello
* The “NodeHello” message
/**
* Notifies listeners that a “CloseConnectionDuplicateClientName” message
* was received.
- *
+ *
* @see FcpListener#receivedCloseConnectionDuplicateClientName(FcpConnection,
* CloseConnectionDuplicateClientName)
* @param closeConnectionDuplicateClientName
/**
* Notifies listeners that a “SSKKeypair” message was received.
- *
+ *
* @see FcpListener#receivedSSKKeypair(FcpConnection, SSKKeypair)
* @param sskKeypair
* The “SSKKeypair” message
/**
* Notifies listeners that a “Peer” message was received.
- *
+ *
* @see FcpListener#receivedPeer(FcpConnection, Peer)
* @param peer
* The “Peer” message
/**
* Notifies all listeners that an “EndListPeers” message was received.
- *
+ *
* @see FcpListener#receivedEndListPeers(FcpConnection, EndListPeers)
* @param endListPeers
* The “EndListPeers” message
/**
* Notifies all listeners that a “PeerNote” message was received.
- *
+ *
* @see FcpListener#receivedPeerNote(FcpConnection, PeerNote)
* @param peerNote
*/
/**
* Notifies all listeners that an “EndListPeerNotes” message was received.
- *
+ *
* @see FcpListener#receivedEndListPeerNotes(FcpConnection,
* EndListPeerNotes)
* @param endListPeerNotes
/**
* Notifies all listeners that a “PeerRemoved” message was received.
- *
+ *
* @see FcpListener#receivedPeerRemoved(FcpConnection, PeerRemoved)
* @param peerRemoved
* The “PeerRemoved” message
/**
* Notifies all listeners that a “NodeData” message was received.
- *
+ *
* @see FcpListener#receivedNodeData(FcpConnection, NodeData)
* @param nodeData
* The “NodeData” message
/**
* Notifies all listeners that a “TestDDAReply” message was received.
- *
+ *
* @see FcpListener#receivedTestDDAReply(FcpConnection, TestDDAReply)
* @param testDDAReply
* The “TestDDAReply” message
/**
* Notifies all listeners that a “TestDDAComplete” message was received.
- *
+ *
* @see FcpListener#receivedTestDDAComplete(FcpConnection, TestDDAComplete)
* @param testDDAComplete
* The “TestDDAComplete” message
/**
* Notifies all listeners that a “PersistentGet” message was received.
- *
+ *
* @see FcpListener#receivedPersistentGet(FcpConnection, PersistentGet)
* @param persistentGet
* The “PersistentGet” message
/**
* Notifies all listeners that a “PersistentPut” message was received.
- *
+ *
* @see FcpListener#receivedPersistentPut(FcpConnection, PersistentPut)
* @param persistentPut
* The “PersistentPut” message
/**
* Notifies all listeners that a “EndListPersistentRequests” message was
* received.
- *
+ *
* @see FcpListener#receivedEndListPersistentRequests(FcpConnection,
* EndListPersistentRequests)
* @param endListPersistentRequests
/**
* Notifies all listeners that a “URIGenerated” message was received.
- *
+ *
* @see FcpListener#receivedURIGenerated(FcpConnection, URIGenerated)
* @param uriGenerated
* The “URIGenerated” message
/**
* Notifies all listeners that a “DataFound” message was received.
- *
+ *
* @see FcpListener#receivedDataFound(FcpConnection, DataFound)
* @param dataFound
* The “DataFound” message
/**
* Notifies all listeners that an “AllData” message was received.
- *
+ *
* @see FcpListener#receivedAllData(FcpConnection, AllData)
* @param allData
* The “AllData” message
/**
* Notifies all listeners that a “SimpleProgress” message was received.
- *
+ *
* @see FcpListener#receivedSimpleProgress(FcpConnection, SimpleProgress)
* @param simpleProgress
* The “SimpleProgress” message
/**
* Notifies all listeners that a “StartedCompression” message was received.
- *
+ *
* @see FcpListener#receivedStartedCompression(FcpConnection,
* StartedCompression)
* @param startedCompression
/**
* Notifies all listeners that a “FinishedCompression” message was received.
- *
+ *
* @see FcpListener#receviedFinishedCompression(FcpConnection,
* FinishedCompression)
* @param finishedCompression
/**
* Notifies all listeners that an “UnknownPeerNoteType” message was
* received.
- *
+ *
* @see FcpListener#receivedUnknownPeerNoteType(FcpConnection,
* UnknownPeerNoteType)
* @param unknownPeerNoteType
/**
* Notifies all listeners that an “UnknownNodeIdentifier” message was
* received.
- *
+ *
* @see FcpListener#receivedUnknownNodeIdentifier(FcpConnection,
* UnknownNodeIdentifier)
* @param unknownNodeIdentifier
/**
* Notifies all listeners that a “ConfigData” message was received.
- *
+ *
* @see FcpListener#receivedConfigData(FcpConnection, ConfigData)
* @param configData
* The “ConfigData” message
/**
* Notifies all listeners that a “GetFailed” message was received.
- *
+ *
* @see FcpListener#receivedGetFailed(FcpConnection, GetFailed)
* @param getFailed
* The “GetFailed” message
/**
* Notifies all listeners that a “PutFailed” message was received.
- *
+ *
* @see FcpListener#receivedPutFailed(FcpConnection, PutFailed)
* @param putFailed
* The “PutFailed” message
/**
* Notifies all listeners that an “IdentifierCollision” message was
* received.
- *
+ *
* @see FcpListener#receivedIdentifierCollision(FcpConnection,
* IdentifierCollision)
* @param identifierCollision
/**
* Notifies all listeners that an “PersistentPutDir” message was received.
- *
+ *
* @see FcpListener#receivedPersistentPutDir(FcpConnection,
* PersistentPutDir)
* @param persistentPutDir
/**
* Notifies all listeners that a “PersistentRequestRemoved” message was
* received.
- *
+ *
* @see FcpListener#receivedPersistentRequestRemoved(FcpConnection,
* PersistentRequestRemoved)
* @param persistentRequestRemoved
/**
* Notifies all listeners that a “SubscribedUSKUpdate” message was received.
- *
+ *
* @see FcpListener#receivedSubscribedUSKUpdate(FcpConnection,
* SubscribedUSKUpdate)
* @param subscribedUSKUpdate
/**
* Notifies all listeners that a “PluginInfo” message was received.
- *
+ *
* @see FcpListener#receivedPluginInfo(FcpConnection, PluginInfo)
* @param pluginInfo
* The “PluginInfo” message
/**
* Notifies all listeners that an “FCPPluginReply” message was received.
- *
+ *
* @see FcpListener#receivedFCPPluginReply(FcpConnection, FCPPluginReply)
* @param fcpPluginReply
* The “FCPPluginReply” message
/**
* Notifies all listeners that a “PersistentRequestModified” message was
* received.
- *
+ *
* @see FcpListener#receivedPersistentRequestModified(FcpConnection,
* PersistentRequestModified)
* @param persistentRequestModified
/**
* Notifies all listeners that a “PutSuccessful” message was received.
- *
+ *
* @see FcpListener#receivedPutSuccessful(FcpConnection, PutSuccessful)
* @param putSuccessful
* The “PutSuccessful” message
/**
* Notifies all listeners that a “PutFetchable” message was received.
- *
+ *
* @see FcpListener#receivedPutFetchable(FcpConnection, PutFetchable)
* @param putFetchable
* The “PutFetchable” message
/**
* Notifies all listeners that a “ProtocolError” message was received.
- *
+ *
* @see FcpListener#receivedProtocolError(FcpConnection, ProtocolError)
* @param protocolError
* The “ProtocolError” message
/**
* Notifies all registered listeners that a message has been received.
- *
+ *
* @see FcpListener#receivedMessage(FcpConnection, FcpMessage)
* @param fcpMessage
* The message that was received
/**
* Notifies all listeners that the connection to the node was closed.
- *
- * @see FcpListener#connectionClosed(FcpConnection)
+ *
+ * @param throwable
+ * The exception that caused the disconnect, or <code>null</code>
+ * if there was no exception
+ * @see FcpListener#connectionClosed(FcpConnection, Throwable)
*/
- private void fireConnectionClosed() {
+ private void fireConnectionClosed(Throwable throwable) {
for (FcpListener fcpListener: fcpListeners) {
- fcpListener.connectionClosed(this);
+ fcpListener.connectionClosed(this, throwable);
}
}
/**
* Connects to the node.
- *
+ *
* @throws IOException
* if an I/O error occurs
* @throws IllegalStateException
if (connectionHandler != null) {
throw new IllegalStateException("already connected, disconnect first");
}
+ logger.info("connecting to " + address + ":" + port + "…");
remoteSocket = new Socket(address, port);
remoteInputStream = remoteSocket.getInputStream();
remoteOutputStream = remoteSocket.getOutputStream();
/**
* Disconnects from the node. If there is no connection to the node, this
* method does nothing.
+ *
+ * @deprecated Use {@link #close()} instead
*/
+ @Deprecated
public synchronized void disconnect() {
- if (connectionHandler == null) {
- return;
- }
- FcpUtils.close(remoteSocket);
- connectionHandler.stop();
- connectionHandler = null;
- fireConnectionClosed();
+ close();
+ }
+
+ /**
+ * Closes the connection. If there is no connection to the node, this method
+ * does nothing.
+ */
+ public void close() {
+ handleDisconnect(null);
}
/**
* Sends the given FCP message.
- *
+ *
* @param fcpMessage
* The FCP message to send
* @throws IOException
* if an I/O error occurs
*/
public synchronized void sendMessage(FcpMessage fcpMessage) throws IOException {
- System.out.println("sending message: " + fcpMessage.getName());
+ logger.fine("sending message: " + fcpMessage.getName());
fcpMessage.write(remoteOutputStream);
}
/**
* Handles the given message, notifying listeners. This message should only
* be called by {@link FcpConnectionHandler}.
- *
+ *
* @param fcpMessage
* The received message
*/
void handleMessage(FcpMessage fcpMessage) {
+ logger.fine("received message: " + fcpMessage.getName());
String messageName = fcpMessage.getName();
countMessage(messageName);
if ("SimpleProgress".equals(messageName)) {
/**
* Handles a disconnect from the node.
+ *
+ * @param throwable
+ * The exception that caused the disconnect, or <code>null</code>
+ * if there was no exception
*/
- synchronized void handleDisconnect() {
+ synchronized void handleDisconnect(Throwable throwable) {
FcpUtils.close(remoteInputStream);
FcpUtils.close(remoteOutputStream);
FcpUtils.close(remoteSocket);
- connectionHandler = null;
- fireConnectionClosed();
+ if (connectionHandler != null) {
+ connectionHandler.stop();
+ connectionHandler = null;
+ fireConnectionClosed(throwable);
+ }
}
//
/**
* Incremets the counter in {@link #incomingMessageStatistics} by <cod>1</code>
* for the given message name.
- *
+ *
* @param name
* The name of the message to count
*/
oldValue = incomingMessageStatistics.get(name);
}
incomingMessageStatistics.put(name, oldValue + 1);
+ logger.finest("count for " + name + ": " + (oldValue + 1));
}
/**
* Returns a limited input stream from the node’s input stream.
- *
+ *
* @param dataLength
* The length of the stream
* @return The limited input stream
/**
* A wrapper around an {@link InputStream} that only supplies a limit number
* of bytes from the underlying input stream.
- *
+ *
* @author <a href="mailto:dr@ina-germany.de">David Roden</a>
* @version $Id$
*/
/**
* Creates a new LimitedInputStream that supplies at most
* <code>length</code> bytes from the given input stream.
- *
+ *
* @param inputStream
* The input stream
* @param length
/**
* {@inheritDoc} This method does nothing, as {@link #mark(int)} and
* {@link #reset()} are not supported.
- *
+ *
* @see java.io.FilterInputStream#mark(int)
*/
@Override
/**
* {@inheritDoc}
- *
+ *
* @see java.io.FilterInputStream#markSupported()
* @return <code>false</code>
*/
/**
* {@inheritDoc} This method does nothing, as {@link #mark(int)} and
* {@link #reset()} are not supported.
- *
+ *
* @see java.io.FilterInputStream#reset()
*/
@Override
/**
* Consumes the input stream, i.e. read all bytes until the limit is
* reached.
- *
+ *
* @throws IOException
* if an I/O error occurs
*/
/**
* Interface for objects that want to be notified on certain FCP events.
- *
+ *
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
* @version $Id$
*/
/**
* Notifies a listener that a “NodeHello” message was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param nodeHello
/**
* Notifies a listener that a “CloseConnectionDuplicateClientName” message
* was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param closeConnectionDuplicateClientName
/**
* Notifies a listener that a “SSKKeypair” message was received.
- *
+ *
* @param fcpConnection
* The connection that received themessage
* @param sskKeypair
/**
* Notifies a listener that a “Peer” message was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param peer
/**
* Notifies a listener that an “EndListPeers” message was received.
- *
+ *
* @param fcpConnection
* The connection that recevied the message
* @param endListPeers
/**
* Notifies a listener that a “PeerNote” message was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param peerNote
/**
* Notifies a listener that an “EndListPeerNotes” message was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param endListPeerNotes
/**
* Notifies a listener that a “PeerRemoved” message was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param peerRemoved
/**
* Notifies a listener that a “NodeData” message was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param nodeData
/**
* Notifies a listener that a “TestDDAReply” message was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param testDDAReply
/**
* Notifies a listener that a “TestDDAComplete” was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param testDDAComplete
/**
* Notifies a listener that a “PersistentGet” was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param persistentGet
/**
* Notifies a listener that a “PersistentPut” was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param persistentPut
/**
* Notifies a listener that a “EndListPersistentRequests” was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param endListPersistentRequests
/**
* Notifies a listener that a “URIGenerated” was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param uriGenerated
/**
* Notifies a listener that a “DataFound” was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param dataFound
/**
* Notifies a listener that an “AllData” was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param allData
/**
* Notifies a listener that a “SimpleProgress” was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param simpleProgress
/**
* Notifies a listener that a “StartedCompression” was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param startedCompression
/**
* Notifies a listener that a “FinishedCompression” was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param finishedCompression
/**
* Notifies a listener that an “UnknownPeerNoteType” was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param unknownPeerNoteType
/**
* Notifies a listener that a “UnknownNodeIdentifier” message was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param unknownNodeIdentifier
/**
* Notifies a listener that a “ConfigData” message was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param configData
/**
* Notifies a listener that a “GetFailed” message was recevied.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param getFailed
/**
* Notifies a listener that a “PutFailed” message was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param putFailed
/**
* Notifies a listener that an “IdentifierCollision” message was receied.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param identifierCollision
/**
* Notifies a listener that a “PersistentPutDir” message was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param persistentPutDir
/**
* Notifies a listener that a “PersistentRequestRemoved” message was
* received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param persistentRequestRemoved
/**
* Notifies a listener that a “SubscribedUSKUpdate” message was received.
- *
+ *
* @param fcpConnection
* The connection that recevied the message
* @param subscribedUSKUpdate
/**
* Notifies a listener that a “PluginInfo” message was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param pluginInfo
/**
* Notifies a listener that an “FCPPluginReply“ message was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param fcpPluginReply
/**
* Notifies a listener that a “PersistentRequestModified” message was
* received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param persistentRequestModified
/**
* Notifies a listener that a “PutSuccessful” message was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param putSuccessful
/**
* Notifies a listener that a “PutFetchable” message was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param putFetchable
/**
* Notifies a listener that a “ProtocolError” was received.
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param protocolError
* Notifies a listener that a message has been received. This method is only
* called if {@link FcpConnection#handleMessage(FcpMessage)} does not
* recognize the message. Should that ever happen, please file a bug report!
- *
+ *
* @param fcpConnection
* The connection that received the message
* @param fcpMessage
* Notifies a listener that a connection was closed. A closed connection can
* be reestablished by calling {@link FcpConnection#connect()} on the same
* object again.
- *
+ *
* @param fcpConnection
* The connection that was closed.
+ * @param throwable
+ * The exception that caused the disconnect, or <code>null</code>
+ * if there was no exception
*/
- public void connectionClosed(FcpConnection fcpConnection);
+ public void connectionClosed(FcpConnection fcpConnection, Throwable throwable);
}