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 implements Identifiable {
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
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 whether this peer is a seed.
97 * @return <code>true</code> if the peer is a seed, <code>false</code>
100 public boolean isSeed() {
101 return Boolean.valueOf(getField("seed"));
105 * Returns the “y” part of the peer’s public DSA key.
107 * @return The public DSA key
109 public String getDSAPublicKey() {
110 return getField("dsaPubKey.y");
114 * Returns the DSA group of the peer.
116 * @return The DSA group of the peer
118 public DSAGroup getDSAGroup() {
119 return new DSAGroup(getField("dsaGroup.g"), getField("dsaGroup.p"), getField("dsaGroup.q"));
123 * Returns the last good version of the peer, i.e. the oldest version the
124 * peer will connect to.
126 * @return The last good version of the peer
128 public Version getLastGoodVersion() {
129 return new Version(getField("lastGoodVersion"));
133 * Returns the ARK of the peer.
135 * @return The ARK of the peer
137 public ARK getARK() {
138 return new ARK(getField("ark.pubURI"), getField("ark.number"));
142 * Returns the identity of the peer.
144 * @return The identity of the peer
146 public String getIdentity() {
147 return getField("identity");
151 * Returns the name of the peer. If the peer is not a darknet peer it will
154 * @return The name of the peer, or <code>null</code> if the peer is an
157 public String getMyName() {
158 return getField("myName");
162 * Returns the location of the peer.
164 * @return The location of the peer
165 * @throws NumberFormatException
166 * if the field can not be parsed
168 public double getLocation() throws NumberFormatException {
169 return Double.valueOf(getField("location"));
173 * Returns whether the peer is a testnet node.
175 * @return <code>true</code> if the peer is a testnet node,
176 * <code>false</code> otherwise
178 public boolean isTestnet() {
179 return Boolean.valueOf("testnet");
183 * Returns the version of the peer.
185 * @return The version of the peer
187 public Version getVersion() {
188 return new Version(getField("version"));
192 * Returns the negotiation types the peer supports.
194 * @return The supported negotiation types
196 public int[] getNegotiationTypes() {
197 return FcpUtils.decodeMultiIntegerField(getField("auth.negTypes"));
201 * Returns all volatile fields from the message.
203 * @return All volatile files
205 public Map<String, String> getVolatileFields() {
206 Map<String, String> volatileFields = new HashMap<String, String>();
207 for (Entry<String, String> field : getFields().entrySet()) {
208 if (field.getKey().startsWith("volatile.")) {
209 volatileFields.put(field.getKey(), field.getValue());
212 return Collections.unmodifiableMap(volatileFields);
216 * Returns one of the volatile fields from the message. The given field
217 * name is prepended with “volatile.” so if you want to get the value of
218 * the field with the name “volatile.status” you only need to specify
222 * The name of the field
223 * @return The value of the field, or <code>null</code> if there is no such
226 public String getVolatile(String field) {
227 return getField("volatile." + field);
231 * Returns all metadata fields from the message.
233 * @return All volatile files
235 public Map<String, String> getMetadataFields() {
236 Map<String, String> metadataFields = new HashMap<String, String>();
237 for (Entry<String, String> field : getFields().entrySet()) {
238 if (field.getKey().startsWith("metadata.")) {
239 metadataFields.put(field.getKey(), field.getValue());
242 return Collections.unmodifiableMap(metadataFields);
246 * Returns one of the metadata fields from the message. The given field
247 * name is prepended with “metadata.” so if you want to get the value of
248 * the field with the name “metadata.timeLastRoutable” you only need to
249 * specify “timeLastRoutable”.
252 * The name of the field
253 * @return The value of the field, or <code>null</code> if there is no such
256 public String getMetadata(String field) {
257 return getField("metadata." + field);