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>
27 public class Peer extends BaseMessage {
30 * Creates a new “Peer” reply from the received message.
32 * @param receivedMessage
33 * The received message
35 Peer(FcpMessage receivedMessage) {
36 super(receivedMessage);
40 * Returns a collection of fields as a node reference.
42 * @return The node reference contained within this message
44 public NodeRef getNodeRef() {
45 NodeRef nodeRef = new NodeRef();
46 nodeRef.setARK(getARK());
47 nodeRef.setDSAGroup(getDSAGroup());
48 nodeRef.setDSAPublicKey(getDSAPublicKey());
49 nodeRef.setIdentity(getIdentity());
50 nodeRef.setLastGoodVersion(getLastGoodVersion());
51 nodeRef.setLocation(getLocation());
52 nodeRef.setName(getMyName());
53 nodeRef.setNegotiationTypes(getNegotiationTypes());
54 nodeRef.setOpennet(isOpennet());
55 nodeRef.setPhysicalUDP(getPhysicalUDP());
56 nodeRef.setVersion(getVersion());
61 * Returns the identifier of the request.
63 * @return The identifier of the request
65 public String getIdentifier() {
66 return getField("Identifier");
70 * Returns the “physical.udp” line from the message. It contains all IP
71 * addresses and port numbers of the peer.
73 * @return The IP addresses and port numbers of the peer
75 public String getPhysicalUDP() {
76 return getField("physical.udp");
80 * Returns whether the listed peer is an opennet peer.
82 * @return <code>true</code> if the peer is an opennet peer,
83 * <code>false</code> if the peer is a darknet peer
85 public boolean isOpennet() {
86 return Boolean.valueOf(getField("opennet"));
90 * Returns the “y” part of the peer’s public DSA key.
92 * @return The public DSA key
94 public String getDSAPublicKey() {
95 return getField("dsaPubKey.y");
99 * Returns the DSA group of the peer.
101 * @return The DSA group of the peer
103 public DSAGroup getDSAGroup() {
104 return new DSAGroup(getField("dsaGroup.g"), getField("dsaGroup.p"), getField("dsaGroup.q"));
108 * Returns the last good version of the peer, i.e. the oldest version the
109 * peer will connect to.
111 * @return The last good version of the peer
113 public Version getLastGoodVersion() {
114 return new Version(getField("lastGoodVersion"));
118 * Returns the ARK of the peer.
120 * @return The ARK of the peer
122 public ARK getARK() {
123 return new ARK(getField("ark.pubURI"), getField("ark.number"));
127 * Returns the identity of the peer.
129 * @return The identity of the peer
131 public String getIdentity() {
132 return getField("identity");
136 * Returns the name of the peer. If the peer is not a darknet peer it will
139 * @return The name of the peer, or <code>null</code> if the peer is an
142 public String getMyName() {
143 return getField("myName");
147 * Returns the location of the peer.
149 * @return The location of the peer
150 * @throws NumberFormatException
151 * if the field can not be parsed
153 public double getLocation() throws NumberFormatException {
154 return Double.valueOf(getField("location"));
158 * Returns whether the peer is a testnet node.
160 * @return <code>true</code> if the peer is a testnet node,
161 * <code>false</code> otherwise
163 public boolean isTestnet() {
164 return Boolean.valueOf("testnet");
168 * Returns the version of the peer.
170 * @return The version of the peer
172 public Version getVersion() {
173 return new Version(getField("version"));
177 * Returns the negotiation types the peer supports.
179 * @return The supported negotiation types
181 public int[] getNegotiationTypes() {
182 return FcpUtils.decodeMultiIntegerField(getField("auth.negTypes"));
186 * Returns one of the volatile fields from the message. The given field name
187 * is prepended with “volatile.” so if you want to get the value of the
188 * field with the name “volatile.status” you only need to specify “status”.
191 * The name of the field
192 * @return The value of the field, or <code>null</code> if there is no
195 public String getVolatile(String field) {
196 return getField("volatile." + field);
200 * Returns one of the metadata fields from the message. The given field name
201 * is prepended with “metadata.” so if you want to get the value of the
202 * field with the name “metadata.timeLastRoutable” you only need to specify
203 * “timeLastRoutable”.
206 * The name of the field
207 * @return The value of the field, or <code>null</code> if there is no
210 public String getMetadata(String field) {
211 return getField("metadata." + field);