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;
22 import java.util.Collections;
23 import java.util.HashMap;
25 import java.util.Map.Entry;
28 * The “Peer” reply by the node contains information about a peer.
30 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
32 public class Peer extends BaseMessage {
35 * Creates a new “Peer” reply from the received message.
37 * @param receivedMessage
38 * The received message
40 Peer(FcpMessage receivedMessage) {
41 super(receivedMessage);
45 * Returns a collection of fields as a node reference.
47 * @return The node reference contained within this message
49 public NodeRef getNodeRef() {
50 NodeRef nodeRef = new NodeRef();
51 nodeRef.setARK(getARK());
52 nodeRef.setDSAGroup(getDSAGroup());
53 nodeRef.setDSAPublicKey(getDSAPublicKey());
54 nodeRef.setIdentity(getIdentity());
55 nodeRef.setLastGoodVersion(getLastGoodVersion());
56 nodeRef.setLocation(getLocation());
57 nodeRef.setName(getMyName());
58 nodeRef.setNegotiationTypes(getNegotiationTypes());
59 nodeRef.setOpennet(isOpennet());
60 nodeRef.setPhysicalUDP(getPhysicalUDP());
61 nodeRef.setVersion(getVersion());
66 * Returns the identifier of the request.
68 * @return The identifier of the request
70 public String getIdentifier() {
71 return getField("Identifier");
75 * Returns the “physical.udp” line from the message. It contains all IP
76 * addresses and port numbers of the peer.
78 * @return The IP addresses and port numbers of the peer
80 public String getPhysicalUDP() {
81 return getField("physical.udp");
85 * Returns whether the listed peer is an opennet peer.
87 * @return <code>true</code> if the peer is an opennet peer,
88 * <code>false</code> if the peer is a darknet peer
90 public boolean isOpennet() {
91 return Boolean.valueOf(getField("opennet"));
95 * Returns the “y” part of the peer’s public DSA key.
97 * @return The public DSA key
99 public String getDSAPublicKey() {
100 return getField("dsaPubKey.y");
104 * Returns the DSA group of the peer.
106 * @return The DSA group of the peer
108 public DSAGroup getDSAGroup() {
109 return new DSAGroup(getField("dsaGroup.g"), getField("dsaGroup.p"), getField("dsaGroup.q"));
113 * Returns the last good version of the peer, i.e. the oldest version the
114 * peer will connect to.
116 * @return The last good version of the peer
118 public Version getLastGoodVersion() {
119 return new Version(getField("lastGoodVersion"));
123 * Returns the ARK of the peer.
125 * @return The ARK of the peer
127 public ARK getARK() {
128 return new ARK(getField("ark.pubURI"), getField("ark.number"));
132 * Returns the identity of the peer.
134 * @return The identity of the peer
136 public String getIdentity() {
137 return getField("identity");
141 * Returns the name of the peer. If the peer is not a darknet peer it will
144 * @return The name of the peer, or <code>null</code> if the peer is an
147 public String getMyName() {
148 return getField("myName");
152 * Returns the location of the peer.
154 * @return The location of the peer
155 * @throws NumberFormatException
156 * if the field can not be parsed
158 public double getLocation() throws NumberFormatException {
159 return Double.valueOf(getField("location"));
163 * Returns whether the peer is a testnet node.
165 * @return <code>true</code> if the peer is a testnet node,
166 * <code>false</code> otherwise
168 public boolean isTestnet() {
169 return Boolean.valueOf("testnet");
173 * Returns the version of the peer.
175 * @return The version of the peer
177 public Version getVersion() {
178 return new Version(getField("version"));
182 * Returns the negotiation types the peer supports.
184 * @return The supported negotiation types
186 public int[] getNegotiationTypes() {
187 return FcpUtils.decodeMultiIntegerField(getField("auth.negTypes"));
191 * Returns all volatile fields from the message.
193 * @return All volatile files
195 public Map<String, String> getVolatileFields() {
196 Map<String, String> volatileFields = new HashMap<String, String>();
197 for (Entry<String, String> field : getFields().entrySet()) {
198 if (field.getKey().startsWith("volatile.")) {
199 volatileFields.put(field.getKey(), field.getValue());
202 return Collections.unmodifiableMap(volatileFields);
206 * Returns one of the volatile fields from the message. The given field name
207 * is prepended with “volatile.” so if you want to get the value of the
208 * field with the name “volatile.status” you only need to specify “status”.
211 * The name of the field
212 * @return The value of the field, or <code>null</code> if there is no such
215 public String getVolatile(String field) {
216 return getField("volatile." + field);
220 * Returns all metadata fields from the message.
222 * @return All volatile files
224 public Map<String, String> getMetadataFields() {
225 Map<String, String> metadataFields = new HashMap<String, String>();
226 for (Entry<String, String> field : getFields().entrySet()) {
227 if (field.getKey().startsWith("metadata.")) {
228 metadataFields.put(field.getKey(), field.getValue());
231 return Collections.unmodifiableMap(metadataFields);
235 * Returns one of the metadata fields from the message. The given field name
236 * is prepended with “metadata.” so if you want to get the value of the
237 * field with the name “metadata.timeLastRoutable” you only need to specify
238 * “timeLastRoutable”.
241 * The name of the field
242 * @return The value of the field, or <code>null</code> if there is no such
245 public String getMetadata(String field) {
246 return getField("metadata." + field);