Update license to GPLv3, fix header comments
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / Peer.java
1 /*
2  * jFCPlib - Peer.java - Copyright © 2008–2016 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 3 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, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.fcp;
19
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Map.Entry;
24
25 /**
26  * The “Peer” reply by the node contains information about a peer.
27  *
28  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
29  */
30 public class Peer extends BaseMessage implements Identifiable {
31
32         /**
33          * Creates a new “Peer” reply from the received message.
34          *
35          * @param receivedMessage
36          *            The received message
37          */
38         public Peer(FcpMessage receivedMessage) {
39                 super(receivedMessage);
40         }
41
42         /**
43          * Returns a collection of fields as a node reference.
44          *
45          * @return The node reference contained within this message
46          */
47         public NodeRef getNodeRef() {
48                 NodeRef nodeRef = new NodeRef();
49                 nodeRef.setARK(getARK());
50                 nodeRef.setDSAGroup(getDSAGroup());
51                 nodeRef.setDSAPublicKey(getDSAPublicKey());
52                 nodeRef.setIdentity(getIdentity());
53                 nodeRef.setLastGoodVersion(getLastGoodVersion());
54                 nodeRef.setLocation(getLocation());
55                 nodeRef.setName(getMyName());
56                 nodeRef.setNegotiationTypes(getNegotiationTypes());
57                 nodeRef.setOpennet(isOpennet());
58                 nodeRef.setPhysicalUDP(getPhysicalUDP());
59                 nodeRef.setVersion(getVersion());
60                 return nodeRef;
61         }
62
63         /**
64          * Returns the identifier of the request.
65          *
66          * @return The identifier of the request
67          */
68         @Override
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
216          * name is prepended with “volatile.” so if you want to get the value of
217          * the field with the name “volatile.status” you only need to specify
218          * “status”.
219          *
220          * @param field
221          *            The name of the field
222          * @return The value of the field, or <code>null</code> if there is no such
223          *         field
224          */
225         public String getVolatile(String field) {
226                 return getField("volatile." + field);
227         }
228
229         /**
230          * Returns all metadata fields from the message.
231          *
232          * @return All volatile files
233          */
234         public Map<String, String> getMetadataFields() {
235                 Map<String, String> metadataFields = new HashMap<String, String>();
236                 for (Entry<String, String> field : getFields().entrySet()) {
237                         if (field.getKey().startsWith("metadata.")) {
238                                 metadataFields.put(field.getKey(), field.getValue());
239                         }
240                 }
241                 return Collections.unmodifiableMap(metadataFields);
242         }
243
244         /**
245          * Returns one of the metadata fields from the message. The given field
246          * name is prepended with “metadata.” so if you want to get the value of
247          * the field with the name “metadata.timeLastRoutable” you only need to
248          * specify “timeLastRoutable”.
249          *
250          * @param field
251          *            The name of the field
252          * @return The value of the field, or <code>null</code> if there is no such
253          *         field
254          */
255         public String getMetadata(String field) {
256                 return getField("metadata." + field);
257         }
258
259 }