Expose lots of constructors and accessors
[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 implements Identifiable {
32
33         /**
34          * Creates a new “Peer” reply from the received message.
35          *
36          * @param receivedMessage
37          *            The received message
38          */
39         public 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         @Override
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 whether this peer is a seed.
96          *
97          * @return <code>true</code> if the peer is a seed, <code>false</code>
98          *         otherwise
99          */
100         public boolean isSeed() {
101                 return Boolean.valueOf(getField("seed"));
102         }
103
104         /**
105          * Returns the “y” part of the peer’s public DSA key.
106          *
107          * @return The public DSA key
108          */
109         public String getDSAPublicKey() {
110                 return getField("dsaPubKey.y");
111         }
112
113         /**
114          * Returns the DSA group of the peer.
115          *
116          * @return The DSA group of the peer
117          */
118         public DSAGroup getDSAGroup() {
119                 return new DSAGroup(getField("dsaGroup.g"), getField("dsaGroup.p"), getField("dsaGroup.q"));
120         }
121
122         /**
123          * Returns the last good version of the peer, i.e. the oldest version the
124          * peer will connect to.
125          *
126          * @return The last good version of the peer
127          */
128         public Version getLastGoodVersion() {
129                 return new Version(getField("lastGoodVersion"));
130         }
131
132         /**
133          * Returns the ARK of the peer.
134          *
135          * @return The ARK of the peer
136          */
137         public ARK getARK() {
138                 return new ARK(getField("ark.pubURI"), getField("ark.number"));
139         }
140
141         /**
142          * Returns the identity of the peer.
143          *
144          * @return The identity of the peer
145          */
146         public String getIdentity() {
147                 return getField("identity");
148         }
149
150         /**
151          * Returns the name of the peer. If the peer is not a darknet peer it will
152          * have no name.
153          *
154          * @return The name of the peer, or <code>null</code> if the peer is an
155          *         opennet peer
156          */
157         public String getMyName() {
158                 return getField("myName");
159         }
160
161         /**
162          * Returns the location of the peer.
163          *
164          * @return The location of the peer
165          * @throws NumberFormatException
166          *             if the field can not be parsed
167          */
168         public double getLocation() throws NumberFormatException {
169                 return Double.valueOf(getField("location"));
170         }
171
172         /**
173          * Returns whether the peer is a testnet node.
174          *
175          * @return <code>true</code> if the peer is a testnet node,
176          *         <code>false</code> otherwise
177          */
178         public boolean isTestnet() {
179                 return Boolean.valueOf("testnet");
180         }
181
182         /**
183          * Returns the version of the peer.
184          *
185          * @return The version of the peer
186          */
187         public Version getVersion() {
188                 return new Version(getField("version"));
189         }
190
191         /**
192          * Returns the negotiation types the peer supports.
193          *
194          * @return The supported negotiation types
195          */
196         public int[] getNegotiationTypes() {
197                 return FcpUtils.decodeMultiIntegerField(getField("auth.negTypes"));
198         }
199
200         /**
201          * Returns all volatile fields from the message.
202          *
203          * @return All volatile files
204          */
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());
210                         }
211                 }
212                 return Collections.unmodifiableMap(volatileFields);
213         }
214
215         /**
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
219          * “status”.
220          *
221          * @param field
222          *            The name of the field
223          * @return The value of the field, or <code>null</code> if there is no such
224          *         field
225          */
226         public String getVolatile(String field) {
227                 return getField("volatile." + field);
228         }
229
230         /**
231          * Returns all metadata fields from the message.
232          *
233          * @return All volatile files
234          */
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());
240                         }
241                 }
242                 return Collections.unmodifiableMap(metadataFields);
243         }
244
245         /**
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”.
250          *
251          * @param field
252          *            The name of the field
253          * @return The value of the field, or <code>null</code> if there is no such
254          *         field
255          */
256         public String getMetadata(String field) {
257                 return getField("metadata." + field);
258         }
259
260 }