3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.fcp;
23 * The “Peer” reply by the node contains information about a peer.
25 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
28 public class Peer extends BaseMessage {
31 * Creates a new “Peer” reply from the received message.
33 * @param receivedMessage
34 * The received message
36 Peer(FcpMessage receivedMessage) {
37 super(receivedMessage);
41 * Returns a collection of fields as a node reference.
43 * @return The node reference contained within this message
45 public NodeRef getNodeRef() {
46 NodeRef nodeRef = new NodeRef();
47 nodeRef.setARK(getARK());
48 nodeRef.setDSAGroup(getDSAGroup());
49 nodeRef.setDSAPublicKey(getDSAPublicKey());
50 nodeRef.setIdentity(getIdentity());
51 nodeRef.setLastGoodVersion(getLastGoodVersion());
52 nodeRef.setLocation(getLocation());
53 nodeRef.setName(getMyName());
54 nodeRef.setNegotiationTypes(getNegotiationTypes());
55 nodeRef.setOpennet(isOpennet());
56 nodeRef.setPhysicalUDP(getPhysicalUDP());
57 nodeRef.setVersion(getVersion());
62 * Returns the identifier of the request.
64 * @return The identifier of the request
66 public String getIdentifier() {
67 return getField("Identifier");
71 * Returns the “physical.udp” line from the message. It contains all IP
72 * addresses and port numbers of the peer.
74 * @return The IP addresses and port numbers of the peer
76 public String getPhysicalUDP() {
77 return getField("physical.udp");
81 * Returns whether the listed peer is an opennet peer.
83 * @return <code>true</code> if the peer is an opennet peer,
84 * <code>false</code> if the peer is a darknet peer
86 public boolean isOpennet() {
87 return Boolean.valueOf(getField("opennet"));
91 * Returns the “y” part of the peer’s public DSA key.
93 * @return The public DSA key
95 public String getDSAPublicKey() {
96 return getField("dsaPubKey.y");
100 * Returns the DSA group of the peer.
102 * @return The DSA group of the peer
104 public DSAGroup getDSAGroup() {
105 return new DSAGroup(getField("dsaGroup.g"), getField("dsaGroup.p"), getField("dsaGroup.q"));
109 * Returns the last good version of the peer, i.e. the oldest version the
110 * peer will connect to.
112 * @return The last good version of the peer
114 public Version getLastGoodVersion() {
115 return new Version(getField("lastGoodVersion"));
119 * Returns the ARK of the peer.
121 * @return The ARK of the peer
123 public ARK getARK() {
124 return new ARK(getField("ark.pubURI"), getField("ark.number"));
128 * Returns the identity of the peer.
130 * @return The identity of the peer
132 public String getIdentity() {
133 return getField("identity");
137 * Returns the name of the peer. If the peer is not a darknet peer it will
140 * @return The name of the peer, or <code>null</code> if the peer is an
143 public String getMyName() {
144 return getField("myName");
148 * Returns the location of the peer.
150 * @return The location of the peer
151 * @throws NumberFormatException
152 * if the field can not be parsed
154 public double getLocation() throws NumberFormatException {
155 return Double.valueOf(getField("location"));
159 * Returns whether the peer is a testnet node.
161 * @return <code>true</code> if the peer is a testnet node,
162 * <code>false</code> otherwise
164 public boolean isTestnet() {
165 return Boolean.valueOf("testnet");
169 * Returns the version of the peer.
171 * @return The version of the peer
173 public Version getVersion() {
174 return new Version(getField("version"));
178 * Returns the negotiation types the peer supports.
180 * @return The supported negotiation types
182 public int[] getNegotiationTypes() {
183 return FcpUtils.decodeMultiIntegerField(getField("auth.negTypes"));
187 * Returns one of the volatile fields from the message. The given field name
188 * is prepended with “volatile.” so if you want to get the value of the
189 * field with the name “volatile.status” you only need to specify “status”.
192 * The name of the field
193 * @return The value of the field, or <code>null</code> if there is no
196 public String getVolatile(String field) {
197 return getField("volatile." + field);
201 * Returns one of the metadata fields from the message. The given field name
202 * is prepended with “metadata.” so if you want to get the value of the
203 * field with the name “metadata.timeLastRoutable” you only need to specify
204 * “timeLastRoutable”.
207 * The name of the field
208 * @return The value of the field, or <code>null</code> if there is no
211 public String getMetadata(String field) {
212 return getField("metadata." + field);