add AddPeer command
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 9 Apr 2008 14:04:02 +0000 (14:04 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 9 Apr 2008 14:04:02 +0000 (14:04 +0000)
git-svn-id: http://trooper/svn/projects/jSite/trunk@666 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/util/fcp/AddPeer.java [new file with mode: 0644]
src/net/pterodactylus/util/fcp/FcpUtils.java
src/net/pterodactylus/util/fcp/NodeRef.java [new file with mode: 0644]
src/net/pterodactylus/util/fcp/Peer.java
src/net/pterodactylus/util/fcp/Version.java

diff --git a/src/net/pterodactylus/util/fcp/AddPeer.java b/src/net/pterodactylus/util/fcp/AddPeer.java
new file mode 100644 (file)
index 0000000..0665587
--- /dev/null
@@ -0,0 +1,87 @@
+/**
+ * © 2008 INA Service GmbH
+ */
+package net.pterodactylus.util.fcp;
+
+import java.net.URL;
+
+/**
+ * The “AddPeer” request adds a peer to the node.
+ * 
+ * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
+ * @version $Id$
+ */
+public class AddPeer extends FcpMessage {
+
+       /**
+        * Creates a new “AddPeer” request.
+        */
+       private AddPeer() {
+               super("AddPeer");
+       }
+
+       /**
+        * Creates a new “AddPeer” request that reads the noderef of the peer from
+        * the given file.
+        * 
+        * @param file
+        *            The file to read the noderef from
+        */
+       public AddPeer(String file) {
+               this();
+               setField("File", file);
+       }
+
+       /**
+        * Creates a new “AddPeer” request that reads the noderef of the peer from
+        * the given URL.
+        * 
+        * @param url
+        *            The URL to read the noderef from
+        */
+       public AddPeer(URL url) {
+               this();
+               setField("URL", String.valueOf(url));
+       }
+
+       /**
+        * Creates a new “AddPeer” request that adds the peer given by the noderef.
+        * 
+        * @param nodeRef
+        *            The noderef of the peer
+        */
+       public AddPeer(NodeRef nodeRef) {
+               this();
+               setNodeRef(nodeRef);
+       }
+
+       //
+       // PRIVATE METHODS
+       //
+
+       /**
+        * Sets the noderef of the peer to add.
+        * 
+        * @param nodeRef
+        *            The noderef of the peer
+        */
+       private void setNodeRef(NodeRef nodeRef) {
+               setField("lastGoodVersion", nodeRef.getLastGoodVersion().toString());
+               setField("opennet", String.valueOf(nodeRef.isOpennet()));
+               setField("identity", nodeRef.getIdentity());
+               setField("myName", nodeRef.getName());
+               setField("location", String.valueOf(nodeRef.getLocation()));
+               setField("testnet", String.valueOf(nodeRef.isTestnet()));
+               setField("version", String.valueOf(nodeRef.getVersion()));
+               setField("physical.udp", nodeRef.getPhysicalUDP());
+               setField("ark.pubURI", nodeRef.getArk().getPublicURI());
+               setField("ark.number", String.valueOf(nodeRef.getArk().getNumber()));
+               setField("dsaPubKey.y", nodeRef.getDSAPublicKey());
+               setField("dsaGroup.g", nodeRef.getDSAGroup().getBase());
+               setField("dsaGroup.p", nodeRef.getDSAGroup().getPrime());
+               setField("dsaGroup.q", nodeRef.getDSAGroup().getSubprime());
+               setField("auth.negTypes", FcpUtils.encodeMultiIntegerField(nodeRef.getNegotiationTypes()));
+               setField("signature", nodeRef.getSignature());
+       }
+
+}
index a492f2e..3a5d5ba 100644 (file)
@@ -35,7 +35,7 @@ public class FcpUtils {
         * @throws NumberFormatException
         *             if a value can not be converted to a number
         */
-       public static int[] parseMultiIntegerField(String field) throws NumberFormatException {
+       public static int[] decodeMultiIntegerField(String field) throws NumberFormatException {
                StringTokenizer fieldTokens = new StringTokenizer(field, ";");
                int[] result = new int[fieldTokens.countTokens()];
                int counter = 0;
@@ -46,4 +46,23 @@ public class FcpUtils {
                return result;
        }
 
+       /**
+        * Encodes the given integer array into a string, separating the values by
+        * ‘;’.
+        * 
+        * @param values
+        *            The values to encode
+        * @return The encoded values
+        */
+       public static String encodeMultiIntegerField(int[] values) {
+               StringBuilder encodedField = new StringBuilder();
+               for (int value: values) {
+                       if (encodedField.length() > 0) {
+                               encodedField.append(';');
+                       }
+                       encodedField.append(value);
+               }
+               return encodedField.toString();
+       }
+
 }
diff --git a/src/net/pterodactylus/util/fcp/NodeRef.java b/src/net/pterodactylus/util/fcp/NodeRef.java
new file mode 100644 (file)
index 0000000..4eb80eb
--- /dev/null
@@ -0,0 +1,307 @@
+/**
+ * © 2008 INA Service GmbH
+ */
+package net.pterodactylus.util.fcp;
+
+/**
+ * A reference for a node. The noderef contains all data that is necessary to
+ * establish a trusted and secure connection to the node.
+ * 
+ * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
+ * @version $Id$
+ */
+public class NodeRef {
+
+       /** The identity of the node. */
+       private String identity;
+
+       /** Whether the node is an opennet peer. */
+       private boolean opennet;
+
+       /** The name of the node. */
+       private String name;
+
+       /** The location of the node. */
+       private double location;
+
+       /** The IP addresses and ports of the node. */
+       private String physicalUDP;
+
+       /** The ARK of the node. */
+       private ARK ark;
+
+       /** The public DSA key of the node. */
+       private String dsaPublicKey;
+
+       /** The DSA group of the node. */
+       private DSAGroup dsaGroup;
+
+       /** The node’s supported negotiation types. */
+       private int[] negotiationTypes;
+
+       /** The version of the node. */
+       private Version version;
+
+       /** The oldest version the node will connect to. */
+       private Version lastGoodVersion;
+
+       /** Whether the node is a testnet node. */
+       private boolean testnet;
+
+       /** The signature of the reference. */
+       private String signature;
+
+       /**
+        * Returns the identity of the node.
+        * 
+        * @return The identity of the node
+        */
+       public String getIdentity() {
+               return identity;
+       }
+
+       /**
+        * Sets the identity of the node.
+        * 
+        * @param identity
+        *            The identity of the node
+        */
+       public void setIdentity(String identity) {
+               this.identity = identity;
+       }
+
+       /**
+        * Returns whether the node is an opennet peer.
+        * 
+        * @return <code>true</code> if the node is an opennet peer,
+        *         <code>false</code> otherwise
+        */
+       public boolean isOpennet() {
+               return opennet;
+       }
+
+       /**
+        * Sets whether the node is an opennet peer.
+        * 
+        * @param opennet
+        *            <code>true</code> if the node is an opennet peer,
+        *            <code>false</code> otherwise
+        */
+       public void setOpennet(boolean opennet) {
+               this.opennet = opennet;
+       }
+
+       /**
+        * Returns the name of the node. If the node is an opennet peer, it will not
+        * have a name!
+        * 
+        * @return The name of the node, or <code>null</code> if the node is an
+        *         opennet peer
+        */
+       public String getName() {
+               return name;
+       }
+
+       /**
+        * Sets the name of the peer.
+        * 
+        * @param name
+        *            The name of the peer
+        */
+       public void setName(String name) {
+               this.name = name;
+       }
+
+       /**
+        * Returns the location of the node.
+        * 
+        * @return The location of the node
+        */
+       public double getLocation() {
+               return location;
+       }
+
+       /**
+        * Sets the location of the node
+        * 
+        * @param location
+        *            The location of the node
+        */
+       public void setLocation(double location) {
+               this.location = location;
+       }
+
+       /**
+        * Returns the IP addresses and port numbers of the node.
+        * 
+        * @return The IP addresses and port numbers of the node
+        */
+       public String getPhysicalUDP() {
+               return physicalUDP;
+       }
+
+       /**
+        * Sets the IP addresses and port numbers of the node.
+        * 
+        * @param physicalUDP
+        *            The IP addresses and port numbers of the node
+        */
+       public void setPhysicalUDP(String physicalUDP) {
+               this.physicalUDP = physicalUDP;
+       }
+
+       /**
+        * Returns the ARK of the node.
+        * 
+        * @return The ARK of the node
+        */
+       public ARK getArk() {
+               return ark;
+       }
+
+       /**
+        * Sets the ARK of the node.
+        * 
+        * @param ark
+        *            The ARK of the node
+        */
+       public void setArk(ARK ark) {
+               this.ark = ark;
+       }
+
+       /**
+        * Returns the public DSA key of the node.
+        * 
+        * @return The public DSA key of the node
+        */
+       public String getDSAPublicKey() {
+               return dsaPublicKey;
+       }
+
+       /**
+        * Sets the public DSA key of the node.
+        * 
+        * @param dsaPublicKey
+        *            The public DSA key of the node
+        */
+       public void setDSAPublicKey(String dsaPublicKey) {
+               this.dsaPublicKey = dsaPublicKey;
+       }
+
+       /**
+        * Returns the DSA group of the node.
+        * 
+        * @return The DSA group of the node
+        */
+       public DSAGroup getDSAGroup() {
+               return dsaGroup;
+       }
+
+       /**
+        * Sets the DSA group of the node.
+        * 
+        * @param dsaGroup
+        *            The DSA group of the node
+        */
+       public void setDSAGroup(DSAGroup dsaGroup) {
+               this.dsaGroup = dsaGroup;
+       }
+
+       /**
+        * Returns the negotiation types supported by the node.
+        * 
+        * @return The node’s supported negotiation types
+        */
+       public int[] getNegotiationTypes() {
+               return negotiationTypes;
+       }
+
+       /**
+        * Sets the negotiation types supported by the node.
+        * 
+        * @param negotiationTypes
+        *            The node’s supported negotiation types
+        */
+       public void setNegotiationTypes(int[] negotiationTypes) {
+               this.negotiationTypes = negotiationTypes;
+       }
+
+       /**
+        * Returns the version of the node.
+        * 
+        * @return The version of the node
+        */
+       public Version getVersion() {
+               return version;
+       }
+
+       /**
+        * Sets the version of the node.
+        * 
+        * @param version
+        *            The version of the node
+        */
+       public void setVersion(Version version) {
+               this.version = version;
+       }
+
+       /**
+        * Returns the last good version of the node.
+        * 
+        * @return The oldest version the node will connect to
+        */
+       public Version getLastGoodVersion() {
+               return lastGoodVersion;
+       }
+
+       /**
+        * Sets the last good version of the node.
+        * 
+        * @param lastGoodVersion
+        *            The oldest version the node will connect to
+        */
+       public void setLastGoodVersion(Version lastGoodVersion) {
+               this.lastGoodVersion = lastGoodVersion;
+       }
+
+       /**
+        * Returns whether the node is a testnet node.
+        * 
+        * @return <code>true</code> if the node is a testnet node,
+        *         <code>false</code> otherwise
+        */
+       public boolean isTestnet() {
+               return testnet;
+       }
+
+       /**
+        * Sets whether this node is a testnet node.
+        * 
+        * @param testnet
+        *            <code>true</code> if the node is a testnet node,
+        *            <code>false</code> otherwise
+        */
+       public void setTestnet(boolean testnet) {
+               this.testnet = testnet;
+       }
+
+       /**
+        * Returns the signature of the noderef.
+        * 
+        * @return The signature of the noderef
+        */
+       public String getSignature() {
+               return signature;
+       }
+
+       /**
+        * Sets the signature of the noderef.
+        * 
+        * @param signature
+        *            The signature of the noderef
+        */
+       public void setSignature(String signature) {
+               this.signature = signature;
+       }
+
+}
index c3dac0d..ca09ef3 100644 (file)
@@ -3,8 +3,6 @@
  */
 package net.pterodactylus.util.fcp;
 
-
-
 /**
  * The “Peer” reply by the node contains information about a peer.
  * 
@@ -24,6 +22,27 @@ 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() {
+               NodeRef nodeRef = new NodeRef();
+               nodeRef.setArk(getARK());
+               nodeRef.setDSAGroup(getDSAGroup());
+               nodeRef.setDSAPublicKey(getDSAPublicKey());
+               nodeRef.setIdentity(getIdentity());
+               nodeRef.setLastGoodVersion(getLastGoodVersion());
+               nodeRef.setLocation(getLocation());
+               nodeRef.setName(getName());
+               nodeRef.setNegotiationTypes(getNegotiationTypes());
+               nodeRef.setOpennet(isOpennet());
+               nodeRef.setPhysicalUDP(getPhysicalUDP());
+               nodeRef.setVersion(getVersion());
+               return nodeRef;
+       }
+
+       /**
         * Returns the “physical.udp” line from the message. It contains all IP
         * addresses and port numbers of the peer.
         * 
@@ -136,7 +155,7 @@ public class Peer extends BaseMessage {
         * @return The supported negotiation types
         */
        public int[] getNegotiationTypes() {
-               return FcpUtils.parseMultiIntegerField(getField("auth.negTypes"));
+               return FcpUtils.decodeMultiIntegerField(getField("auth.negTypes"));
        }
 
 }
index 0bcb28b..416d843 100644 (file)
@@ -23,9 +23,9 @@ public class Version {
        private final int buildNumber;
 
        /**
-        * Creates a new Version from the given string. The string consists of
-        * the four required fields node name, tree version, protocol version,
-        * and build number, separated by a comma.
+        * Creates a new Version from the given string. The string consists of the
+        * four required fields node name, tree version, protocol version, and build
+        * number, separated by a comma.
         * 
         * @param version
         *            The version string
@@ -107,4 +107,12 @@ public class Version {
                return buildNumber;
        }
 
+       /**
+        * @see java.lang.Object#toString()
+        */
+       @Override
+       public String toString() {
+               return nodeName + "," + treeVersion + "," + protocolVersion + "," + buildNumber;
+       }
+
 }
\ No newline at end of file