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;
/** The connection handler. */
private FcpConnectionHandler connectionHandler;
+ /** Incoming message statistics. */
+ private Map<String, Integer> incomingMessageStatistics = Collections.synchronizedMap(new HashMap<String, Integer>());
+
/**
* Creates a new FCP connection to the Freenet node running on the given
* host, listening on the default port.
}
/**
+ * 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)
}
/**
+ * 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
*/
void handleMessage(FcpMessage fcpMessage) {
String messageName = fcpMessage.getName();
+ 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)) {
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 {
}
}
+ /**
+ * 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 <cod>1</code>
+ * 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);
+ }
+
}