Add Maven project description.
[jFCPlib.git] / src / net / pterodactylus / fcp / Peer.java
index e45d563..3cfa856 100644 (file)
@@ -1,6 +1,5 @@
 /*
- * jSite2 - Peer.java -
- * Copyright © 2008 David Roden
+ * jFCPlib - Peer.java - Copyright © 2008 David Roden
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.fcp;
 
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
 /**
  * The “Peer” reply by the node contains information about a peer.
- * 
+ *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
- * @version $Id$
  */
 public class Peer extends BaseMessage {
 
        /**
         * Creates a new “Peer” reply from the received message.
-        * 
+        *
         * @param receivedMessage
         *            The received message
         */
@@ -39,7 +42,7 @@ public class Peer extends BaseMessage {
 
        /**
         * Returns a collection of fields as a node reference.
-        * 
+        *
         * @return The node reference contained within this message
         */
        public NodeRef getNodeRef() {
@@ -59,9 +62,18 @@ public class Peer extends BaseMessage {
        }
 
        /**
+        * Returns the identifier of the request.
+        *
+        * @return The identifier of the request
+        */
+       public String getIdentifier() {
+               return getField("Identifier");
+       }
+
+       /**
         * Returns the “physical.udp” line from the message. It contains all IP
         * addresses and port numbers of the peer.
-        * 
+        *
         * @return The IP addresses and port numbers of the peer
         */
        public String getPhysicalUDP() {
@@ -70,7 +82,7 @@ public class Peer extends BaseMessage {
 
        /**
         * Returns whether the listed peer is an opennet peer.
-        * 
+        *
         * @return <code>true</code> if the peer is an opennet peer,
         *         <code>false</code> if the peer is a darknet peer
         */
@@ -79,8 +91,18 @@ public class Peer extends BaseMessage {
        }
 
        /**
+        * Returns whether this peer is a seed.
+        *
+        * @return <code>true</code> if the peer is a seed, <code>false</code>
+        *         otherwise
+        */
+       public boolean isSeed() {
+               return Boolean.valueOf(getField("seed"));
+       }
+
+       /**
         * Returns the “y” part of the peer’s public DSA key.
-        * 
+        *
         * @return The public DSA key
         */
        public String getDSAPublicKey() {
@@ -89,7 +111,7 @@ public class Peer extends BaseMessage {
 
        /**
         * Returns the DSA group of the peer.
-        * 
+        *
         * @return The DSA group of the peer
         */
        public DSAGroup getDSAGroup() {
@@ -99,7 +121,7 @@ public class Peer extends BaseMessage {
        /**
         * Returns the last good version of the peer, i.e. the oldest version the
         * peer will connect to.
-        * 
+        *
         * @return The last good version of the peer
         */
        public Version getLastGoodVersion() {
@@ -108,7 +130,7 @@ public class Peer extends BaseMessage {
 
        /**
         * Returns the ARK of the peer.
-        * 
+        *
         * @return The ARK of the peer
         */
        public ARK getARK() {
@@ -117,7 +139,7 @@ public class Peer extends BaseMessage {
 
        /**
         * Returns the identity of the peer.
-        * 
+        *
         * @return The identity of the peer
         */
        public String getIdentity() {
@@ -127,7 +149,7 @@ public class Peer extends BaseMessage {
        /**
         * Returns the name of the peer. If the peer is not a darknet peer it will
         * have no name.
-        * 
+        *
         * @return The name of the peer, or <code>null</code> if the peer is an
         *         opennet peer
         */
@@ -137,7 +159,7 @@ public class Peer extends BaseMessage {
 
        /**
         * Returns the location of the peer.
-        * 
+        *
         * @return The location of the peer
         * @throws NumberFormatException
         *             if the field can not be parsed
@@ -148,7 +170,7 @@ public class Peer extends BaseMessage {
 
        /**
         * Returns whether the peer is a testnet node.
-        * 
+        *
         * @return <code>true</code> if the peer is a testnet node,
         *         <code>false</code> otherwise
         */
@@ -158,7 +180,7 @@ public class Peer extends BaseMessage {
 
        /**
         * Returns the version of the peer.
-        * 
+        *
         * @return The version of the peer
         */
        public Version getVersion() {
@@ -167,11 +189,70 @@ public class Peer extends BaseMessage {
 
        /**
         * Returns the negotiation types the peer supports.
-        * 
+        *
         * @return The supported negotiation types
         */
        public int[] getNegotiationTypes() {
                return FcpUtils.decodeMultiIntegerField(getField("auth.negTypes"));
        }
 
+       /**
+        * Returns all volatile fields from the message.
+        *
+        * @return All volatile files
+        */
+       public Map<String, String> getVolatileFields() {
+               Map<String, String> volatileFields = new HashMap<String, String>();
+               for (Entry<String, String> field : getFields().entrySet()) {
+                       if (field.getKey().startsWith("volatile.")) {
+                               volatileFields.put(field.getKey(), field.getValue());
+                       }
+               }
+               return Collections.unmodifiableMap(volatileFields);
+       }
+
+       /**
+        * Returns one of the volatile fields from the message. The given field name
+        * is prepended with “volatile.” so if you want to get the value of the
+        * field with the name “volatile.status” you only need to specify “status”.
+        *
+        * @param field
+        *            The name of the field
+        * @return The value of the field, or <code>null</code> if there is no such
+        *         field
+        */
+       public String getVolatile(String field) {
+               return getField("volatile." + field);
+       }
+
+       /**
+        * Returns all metadata fields from the message.
+        *
+        * @return All volatile files
+        */
+       public Map<String, String> getMetadataFields() {
+               Map<String, String> metadataFields = new HashMap<String, String>();
+               for (Entry<String, String> field : getFields().entrySet()) {
+                       if (field.getKey().startsWith("metadata.")) {
+                               metadataFields.put(field.getKey(), field.getValue());
+                       }
+               }
+               return Collections.unmodifiableMap(metadataFields);
+       }
+
+       /**
+        * Returns one of the metadata fields from the message. The given field name
+        * is prepended with “metadata.” so if you want to get the value of the
+        * field with the name “metadata.timeLastRoutable” you only need to specify
+        * “timeLastRoutable”.
+        *
+        * @param field
+        *            The name of the field
+        * @return The value of the field, or <code>null</code> if there is no such
+        *         field
+        */
+       public String getMetadata(String field) {
+               return getField("metadata." + field);
+       }
+
 }