2 * jFCPlib - Peer.java - Copyright © 2008 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package net.pterodactylus.fcp;
21 import java.util.Collections;
22 import java.util.HashMap;
24 import java.util.Map.Entry;
27 * The “Peer” reply by the node contains information about a peer.
29 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
31 public class Peer extends BaseMessage {
34 * Creates a new “Peer” reply from the received message.
36 * @param receivedMessage
37 * The received message
39 Peer(FcpMessage receivedMessage) {
40 super(receivedMessage);
44 * Returns a collection of fields as a node reference.
46 * @return The node reference contained within this message
48 public NodeRef getNodeRef() {
49 NodeRef nodeRef = new NodeRef();
50 nodeRef.setARK(getARK());
51 nodeRef.setDSAGroup(getDSAGroup());
52 nodeRef.setDSAPublicKey(getDSAPublicKey());
53 nodeRef.setIdentity(getIdentity());
54 nodeRef.setLastGoodVersion(getLastGoodVersion());
55 nodeRef.setLocation(getLocation());
56 nodeRef.setName(getMyName());
57 nodeRef.setNegotiationTypes(getNegotiationTypes());
58 nodeRef.setOpennet(isOpennet());
59 nodeRef.setPhysicalUDP(getPhysicalUDP());
60 nodeRef.setVersion(getVersion());
65 * Returns the identifier of the request.
67 * @return The identifier of the request
69 public String getIdentifier() {
70 return getField("Identifier");
74 * Returns the “physical.udp” line from the message. It contains all IP
75 * addresses and port numbers of the peer.
77 * @return The IP addresses and port numbers of the peer
79 public String getPhysicalUDP() {
80 return getField("physical.udp");
84 * Returns whether the listed peer is an opennet peer.
86 * @return <code>true</code> if the peer is an opennet peer,
87 * <code>false</code> if the peer is a darknet peer
89 public boolean isOpennet() {
90 return Boolean.valueOf(getField("opennet"));
94 * Returns whether this peer is a seed.
96 * @return <code>true</code> if the peer is a seed, <code>false</code>
99 public boolean isSeed() {
100 return Boolean.valueOf(getField("seed"));
104 * Returns the “y” part of the peer’s public DSA key.
106 * @return The public DSA key
108 public String getDSAPublicKey() {
109 return getField("dsaPubKey.y");
113 * Returns the DSA group of the peer.
115 * @return The DSA group of the peer
117 public DSAGroup getDSAGroup() {
118 return new DSAGroup(getField("dsaGroup.g"), getField("dsaGroup.p"), getField("dsaGroup.q"));
122 * Returns the last good version of the peer, i.e. the oldest version the
123 * peer will connect to.
125 * @return The last good version of the peer
127 public Version getLastGoodVersion() {
128 return new Version(getField("lastGoodVersion"));
132 * Returns the ARK of the peer.
134 * @return The ARK of the peer
136 public ARK getARK() {
137 return new ARK(getField("ark.pubURI"), getField("ark.number"));
141 * Returns the identity of the peer.
143 * @return The identity of the peer
145 public String getIdentity() {
146 return getField("identity");
150 * Returns the name of the peer. If the peer is not a darknet peer it will
153 * @return The name of the peer, or <code>null</code> if the peer is an
156 public String getMyName() {
157 return getField("myName");
161 * Returns the location of the peer.
163 * @return The location of the peer
164 * @throws NumberFormatException
165 * if the field can not be parsed
167 public double getLocation() throws NumberFormatException {
168 return Double.valueOf(getField("location"));
172 * Returns whether the peer is a testnet node.
174 * @return <code>true</code> if the peer is a testnet node,
175 * <code>false</code> otherwise
177 public boolean isTestnet() {
178 return Boolean.valueOf("testnet");
182 * Returns the version of the peer.
184 * @return The version of the peer
186 public Version getVersion() {
187 return new Version(getField("version"));
191 * Returns the negotiation types the peer supports.
193 * @return The supported negotiation types
195 public int[] getNegotiationTypes() {
196 return FcpUtils.decodeMultiIntegerField(getField("auth.negTypes"));
200 * Returns all volatile fields from the message.
202 * @return All volatile files
204 public Map<String, String> getVolatileFields() {
205 Map<String, String> volatileFields = new HashMap<String, String>();
206 for (Entry<String, String> field : getFields().entrySet()) {
207 if (field.getKey().startsWith("volatile.")) {
208 volatileFields.put(field.getKey(), field.getValue());
211 return Collections.unmodifiableMap(volatileFields);
215 * Returns one of the volatile fields from the message. The given field name
216 * is prepended with “volatile.” so if you want to get the value of the
217 * field with the name “volatile.status” you only need to specify “status”.
220 * The name of the field
221 * @return The value of the field, or <code>null</code> if there is no such
224 public String getVolatile(String field) {
225 return getField("volatile." + field);
229 * Returns all metadata fields from the message.
231 * @return All volatile files
233 public Map<String, String> getMetadataFields() {
234 Map<String, String> metadataFields = new HashMap<String, String>();
235 for (Entry<String, String> field : getFields().entrySet()) {
236 if (field.getKey().startsWith("metadata.")) {
237 metadataFields.put(field.getKey(), field.getValue());
240 return Collections.unmodifiableMap(metadataFields);
244 * Returns one of the metadata fields from the message. The given field name
245 * is prepended with “metadata.” so if you want to get the value of the
246 * field with the name “metadata.timeLastRoutable” you only need to specify
247 * “timeLastRoutable”.
250 * The name of the field
251 * @return The value of the field, or <code>null</code> if there is no such
254 public String getMetadata(String field) {
255 return getField("metadata." + field);