add ProtocolError message
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 10 Apr 2008 08:32:58 +0000 (08:32 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 10 Apr 2008 08:32:58 +0000 (08:32 +0000)
git-svn-id: http://trooper/svn/projects/jSite/trunk@687 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/util/fcp/FcpAdapter.java
src/net/pterodactylus/util/fcp/FcpConnection.java
src/net/pterodactylus/util/fcp/FcpListener.java
src/net/pterodactylus/util/fcp/ProtocolError.java [new file with mode: 0644]

index cdfbe03..6ec19e3 100644 (file)
@@ -104,6 +104,14 @@ public class FcpAdapter implements FcpListener {
        }
 
        /**
+        * @see net.pterodactylus.util.fcp.FcpListener#receivedProtocolError(net.pterodactylus.util.fcp.FcpConnection,
+        *      net.pterodactylus.util.fcp.ProtocolError)
+        */
+       public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
+               /* empty. */
+       }
+
+       /**
         * @see net.pterodactylus.util.fcp.FcpListener#receivedMessage(net.pterodactylus.util.fcp.FcpConnection,
         *      net.pterodactylus.util.fcp.FcpMessage)
         */
index 07a0916..dabb542 100644 (file)
@@ -283,6 +283,18 @@ public class FcpConnection {
        }
 
        /**
+        * Notifies all listeners that a “ProtocolError” message was received.
+        * 
+        * @param protocolError
+        *            The “ProtocolError” message
+        */
+       private void fireReceivedProtocolError(ProtocolError protocolError) {
+               for (FcpListener fcpListener: fcpListeners) {
+                       fcpListener.receivedProtocolError(this, protocolError);
+               }
+       }
+
+       /**
         * Notifies all registered listeners that a message has been received.
         * 
         * @see FcpListener#receivedMessage(FcpConnection, FcpMessage)
@@ -356,7 +368,9 @@ public class FcpConnection {
         */
        void handleMessage(FcpMessage fcpMessage) {
                String messageName = fcpMessage.getName();
-               if ("Peer".equals(messageName)) {
+               if ("ProtocolError".equals(messageName)) {
+                       fireReceivedProtocolError(new ProtocolError(fcpMessage));
+               } else if ("Peer".equals(messageName)) {
                        fireReceivedPeer(new Peer(fcpMessage));
                } else if ("PeerNote".equals(messageName)) {
                        fireReceivedPeerNote(new PeerNote(fcpMessage));
index 50156ab..f17cdd3 100644 (file)
@@ -141,6 +141,16 @@ public interface FcpListener extends EventListener {
        public void receivedTestDDAComplete(FcpConnection fcpConnection, TestDDAComplete testDDAComplete);
 
        /**
+        * Notifies a listener that a “ProtocolError” was received.
+        * 
+        * @param fcpConnection
+        *            The connection that received the message
+        * @param protocolError
+        *            The “ProtocolError” message
+        */
+       public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError 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!
diff --git a/src/net/pterodactylus/util/fcp/ProtocolError.java b/src/net/pterodactylus/util/fcp/ProtocolError.java
new file mode 100644 (file)
index 0000000..c812d42
--- /dev/null
@@ -0,0 +1,86 @@
+/**
+ * © 2008 INA Service GmbH
+ */
+package net.pterodactylus.util.fcp;
+
+/**
+ * The “ProtocolError” message signals that something has gone really wrong.
+ * 
+ * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
+ * @version $Id$
+ */
+public class ProtocolError extends BaseMessage {
+
+       /**
+        * Creates a new “ProtocolError” message that wraps the received message.
+        * 
+        * @param receivedMessage
+        *            The received message
+        */
+       public ProtocolError(FcpMessage receivedMessage) {
+               super(receivedMessage);
+       }
+
+       /**
+        * Returns whether the causing message had the “Global” flag set.
+        * 
+        * @return <code>true</code> if the causing message had the “Global” flag
+        *         set
+        */
+       public boolean isGlobal() {
+               return Boolean.valueOf(getField("Global"));
+       }
+
+       /**
+        * Returns the error code.
+        * 
+        * @return The error code, or <code>-1</code> if the error code could not
+        *         be parsed
+        */
+       public int getCode() {
+               try {
+                       return Integer.valueOf(getField("Code"));
+               } catch (NumberFormatException nfe1) {
+                       return -1;
+               }
+       }
+
+       /**
+        * Returns the description of the error.
+        * 
+        * @return The description of the error
+        */
+       public String getCodeDescription() {
+               return getField("CodeDescription");
+       }
+
+       /**
+        * Returns some extra description of the error.
+        * 
+        * @return Extra description of the error, or <code>null</code> if there
+        *         is none
+        */
+       public String getExtraDescription() {
+               return getField("ExtraDescription");
+       }
+
+       /**
+        * Returns whether the connection to the node can stay open.
+        * 
+        * @return <code>true</code> when the connection has to be closed,
+        *         <code>false</code> otherwise
+        */
+       public boolean isFatal() {
+               return Boolean.valueOf(getField("Fatal"));
+       }
+
+       /**
+        * The identifier of the causing request, if any.
+        * 
+        * @return The identifier of the causing request
+        */
+       public String getIdentifier() {
+               return getField("Identifier");
+       }
+
+}