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