1fa13e3fcb809be817b7c43e55aab8e376c53286
[jFCPlib.git] / src / net / pterodactylus / fcp / Peer.java
1 /*
2  * jSite2 - Peer.java -
3  * Copyright © 2008 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.fcp;
21
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Map.Entry;
26
27 /**
28  * The “Peer” reply by the node contains information about a peer.
29  *
30  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
31  */
32 public class Peer extends BaseMessage {
33
34         /**
35          * Creates a new “Peer” reply from the received message.
36          *
37          * @param receivedMessage
38          *            The received message
39          */
40         Peer(FcpMessage receivedMessage) {
41                 super(receivedMessage);
42         }
43
44         /**
45          * Returns a collection of fields as a node reference.
46          *
47          * @return The node reference contained within this message
48          */
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());
62                 return nodeRef;
63         }
64
65         /**
66          * Returns the identifier of the request.
67          *
68          * @return The identifier of the request
69          */
70         public String getIdentifier() {
71                 return getField("Identifier");
72         }
73
74         /**
75          * Returns the “physical.udp” line from the message. It contains all IP
76          * addresses and port numbers of the peer.
77          *
78          * @return The IP addresses and port numbers of the peer
79          */
80         public String getPhysicalUDP() {
81                 return getField("physical.udp");
82         }
83
84         /**
85          * Returns whether the listed peer is an opennet peer.
86          *
87          * @return <code>true</code> if the peer is an opennet peer,
88          *         <code>false</code> if the peer is a darknet peer
89          */
90         public boolean isOpennet() {
91                 return Boolean.valueOf(getField("opennet"));
92         }
93
94         /**
95          * Returns the “y” part of the peer’s public DSA key.
96          *
97          * @return The public DSA key
98          */
99         public String getDSAPublicKey() {
100                 return getField("dsaPubKey.y");
101         }
102
103         /**
104          * Returns the DSA group of the peer.
105          *
106          * @return The DSA group of the peer
107          */
108         public DSAGroup getDSAGroup() {
109                 return new DSAGroup(getField("dsaGroup.g"), getField("dsaGroup.p"), getField("dsaGroup.q"));
110         }
111
112         /**
113          * Returns the last good version of the peer, i.e. the oldest version the
114          * peer will connect to.
115          *
116          * @return The last good version of the peer
117          */
118         public Version getLastGoodVersion() {
119                 return new Version(getField("lastGoodVersion"));
120         }
121
122         /**
123          * Returns the ARK of the peer.
124          *
125          * @return The ARK of the peer
126          */
127         public ARK getARK() {
128                 return new ARK(getField("ark.pubURI"), getField("ark.number"));
129         }
130
131         /**
132          * Returns the identity of the peer.
133          *
134          * @return The identity of the peer
135          */
136         public String getIdentity() {
137                 return getField("identity");
138         }
139
140         /**
141          * Returns the name of the peer. If the peer is not a darknet peer it will
142          * have no name.
143          *
144          * @return The name of the peer, or <code>null</code> if the peer is an
145          *         opennet peer
146          */
147         public String getMyName() {
148                 return getField("myName");
149         }
150
151         /**
152          * Returns the location of the peer.
153          *
154          * @return The location of the peer
155          * @throws NumberFormatException
156          *             if the field can not be parsed
157          */
158         public double getLocation() throws NumberFormatException {
159                 return Double.valueOf(getField("location"));
160         }
161
162         /**
163          * Returns whether the peer is a testnet node.
164          *
165          * @return <code>true</code> if the peer is a testnet node,
166          *         <code>false</code> otherwise
167          */
168         public boolean isTestnet() {
169                 return Boolean.valueOf("testnet");
170         }
171
172         /**
173          * Returns the version of the peer.
174          *
175          * @return The version of the peer
176          */
177         public Version getVersion() {
178                 return new Version(getField("version"));
179         }
180
181         /**
182          * Returns the negotiation types the peer supports.
183          *
184          * @return The supported negotiation types
185          */
186         public int[] getNegotiationTypes() {
187                 return FcpUtils.decodeMultiIntegerField(getField("auth.negTypes"));
188         }
189
190         /**
191          * Returns all volatile fields from the message.
192          *
193          * @return All volatile files
194          */
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());
200                         }
201                 }
202                 return Collections.unmodifiableMap(volatileFields);
203         }
204
205         /**
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”.
209          *
210          * @param field
211          *            The name of the field
212          * @return The value of the field, or <code>null</code> if there is no such
213          *         field
214          */
215         public String getVolatile(String field) {
216                 return getField("volatile." + field);
217         }
218
219         /**
220          * Returns all metadata fields from the message.
221          *
222          * @return All volatile files
223          */
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());
229                         }
230                 }
231                 return Collections.unmodifiableMap(metadataFields);
232         }
233
234         /**
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”.
239          *
240          * @param field
241          *            The name of the field
242          * @return The value of the field, or <code>null</code> if there is no such
243          *         field
244          */
245         public String getMetadata(String field) {
246                 return getField("metadata." + field);
247         }
248
249 }