X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Futil%2Ffcp%2FFcpConnection.java;h=d585022f3e17ab472abcabe082b2445514cc531a;hb=2b77426f2c37342d979fb44b8bb89991d61df6d0;hp=96f2ea0e903208212335e9a53c1cbe5b2dbde98d;hpb=b56d9c55ebfc3651b8b3a0b2e43240a9054d680c;p=jSite2.git diff --git a/src/net/pterodactylus/util/fcp/FcpConnection.java b/src/net/pterodactylus/util/fcp/FcpConnection.java index 96f2ea0..d585022 100644 --- a/src/net/pterodactylus/util/fcp/FcpConnection.java +++ b/src/net/pterodactylus/util/fcp/FcpConnection.java @@ -26,7 +26,10 @@ import java.net.InetAddress; import java.net.Socket; 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 net.pterodactylus.util.io.Closer; import net.pterodactylus.util.io.LimitedInputStream; @@ -63,6 +66,20 @@ public class FcpConnection { /** The connection handler. */ private FcpConnectionHandler connectionHandler; + /** Incoming message statistics. */ + private Map incomingMessageStatistics = Collections.synchronizedMap(new HashMap()); + + /** + * 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 + */ + public FcpConnection() throws UnknownHostException { + this(InetAddress.getLocalHost()); + } + /** * Creates a new FCP connection to the Freenet node running on the given * host, listening on the default port. @@ -286,6 +303,18 @@ public class FcpConnection { } /** + * Notifies all listeners that a “PersistentGet” message was received. + * + * @param persistentGet + * The “PersistentGet” message + */ + private void fireReceivedPersistentGet(PersistentGet persistentGet) { + for (FcpListener fcpListener: fcpListeners) { + fcpListener.receivedPersistentGet(this, persistentGet); + } + } + + /** * Notifies all listeners that a “PersistentPut” message was received. * * @see FcpListener#receivedPersistentPut(FcpConnection, PersistentPut) @@ -324,6 +353,18 @@ public class FcpConnection { } /** + * Notifies all listeners that a “DataFound” message was received. + * + * @param dataFound + * The “DataFound” message + */ + private void fireReceivedDataFound(DataFound dataFound) { + for (FcpListener fcpListener: fcpListeners) { + fcpListener.receivedDataFound(this, dataFound); + } + } + + /** * Notifies all listeners that an “AllData” message was received. * * @param allData @@ -336,6 +377,55 @@ public class FcpConnection { } /** + * Notifies all listeners that a “SimpleProgress” message was received. + * + * @param simpleProgress + * The “SimpleProgress” message + */ + private void fireReceivedSimpleProgress(SimpleProgress simpleProgress) { + for (FcpListener fcpListener: fcpListeners) { + fcpListener.receivedSimpleProgress(this, simpleProgress); + } + } + + /** + * Notifies all listeners that a “StartedCompression” message was received. + * + * @param startedCompression + * The “StartedCompression” message + */ + private void fireReceivedStartedCompression(StartedCompression startedCompression) { + for (FcpListener fcpListener: fcpListeners) { + fcpListener.receivedStartedCompression(this, startedCompression); + } + } + + /** + * Notifies all listeners that a “FinishedCompression” message was received. + * + * @param finishedCompression + * The “FinishedCompression” message + */ + private void fireReceivedFinishedCompression(FinishedCompression finishedCompression) { + for (FcpListener fcpListener: fcpListeners) { + fcpListener.receviedFinishedCompression(this, finishedCompression); + } + } + + /** + * Notifies all listeners that an “UnknownPeerNoteType” message was + * received. + * + * @param unknownPeerNoteType + * The “UnknownPeerNoteType” message + */ + private void fireReceivedUnknownPeerNoteType(UnknownPeerNoteType unknownPeerNoteType) { + for (FcpListener fcpListener: fcpListeners) { + fcpListener.receivedUnknownPeerNoteType(this, unknownPeerNoteType); + } + } + + /** * Notifies all listeners that a “ProtocolError” message was received. * * @param protocolError @@ -421,8 +511,13 @@ public class FcpConnection { */ void handleMessage(FcpMessage fcpMessage) { String messageName = fcpMessage.getName(); - if ("ProtocolError".equals(messageName)) { + countMessage(messageName); + if ("SimpleProgress".equals(messageName)) { + fireReceivedSimpleProgress(new SimpleProgress(fcpMessage)); + } else if ("ProtocolError".equals(messageName)) { fireReceivedProtocolError(new ProtocolError(fcpMessage)); + } else if ("PersistentGet".equals(messageName)) { + fireReceivedPersistentGet(new PersistentGet(fcpMessage)); } else if ("PersistentPut".equals(messageName)) { fireReceivedPersistentPut(new PersistentPut(fcpMessage)); } else if ("URIGenerated".equals(messageName)) { @@ -433,6 +528,12 @@ public class FcpConnection { fireReceivedPeer(new Peer(fcpMessage)); } else if ("PeerNote".equals(messageName)) { fireReceivedPeerNote(new PeerNote(fcpMessage)); + } else if ("StartedCompression".equals(messageName)) { + fireReceivedStartedCompression(new StartedCompression(fcpMessage)); + } else if ("FinishedCompression".equals(messageName)) { + fireReceivedFinishedCompression(new FinishedCompression(fcpMessage)); + } else if ("DataFound".equals(messageName)) { + fireReceivedDataFound(new DataFound(fcpMessage)); } else if ("AllData".equals(messageName)) { long dataLength; try { @@ -456,6 +557,8 @@ public class FcpConnection { fireReceivedSSKKeypair(new SSKKeypair(fcpMessage)); } else if ("PeerRemoved".equals(messageName)) { fireReceivedPeerRemoved(new PeerRemoved(fcpMessage)); + } else if ("UnknownPeerNoteType".equals(messageName)) { + fireReceivedUnknownPeerNoteType(new UnknownPeerNoteType(fcpMessage)); } else if ("NodeData".equals(messageName)) { fireReceivedNodeData(new NodeData(fcpMessage)); } else if ("TestDDAReply".equals(messageName)) { @@ -471,4 +574,33 @@ public class FcpConnection { } } + /** + * Handles a disconnect from the node. + */ + synchronized void handleDisconnect() { + Closer.close(remoteInputStream); + Closer.close(remoteOutputStream); + Closer.close(remoteSocket); + connectionHandler = null; + } + + // + // PRIVATE METHODS + // + + /** + * Incremets the counter in {@link #incomingMessageStatistics} by 1 + * for the given message name. + * + * @param name + * The name of the message to count + */ + private void countMessage(String name) { + int oldValue = 0; + if (incomingMessageStatistics.containsKey(name)) { + oldValue = incomingMessageStatistics.get(name); + } + incomingMessageStatistics.put(name, oldValue + 1); + } + }