f7d33787d0c5639d7d15fcd78b49fdc33a53c0fe
[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 /**
23  * The “Peer” reply by the node contains information about a peer.
24  * 
25  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
26  * @version $Id$
27  */
28 public class Peer extends BaseMessage {
29
30         /**
31          * Creates a new “Peer” reply from the received message.
32          * 
33          * @param receivedMessage
34          *            The received message
35          */
36         Peer(FcpMessage receivedMessage) {
37                 super(receivedMessage);
38         }
39
40         /**
41          * Returns a collection of fields as a node reference.
42          * 
43          * @return The node reference contained within this message
44          */
45         public NodeRef getNodeRef() {
46                 NodeRef nodeRef = new NodeRef();
47                 nodeRef.setARK(getARK());
48                 nodeRef.setDSAGroup(getDSAGroup());
49                 nodeRef.setDSAPublicKey(getDSAPublicKey());
50                 nodeRef.setIdentity(getIdentity());
51                 nodeRef.setLastGoodVersion(getLastGoodVersion());
52                 nodeRef.setLocation(getLocation());
53                 nodeRef.setName(getMyName());
54                 nodeRef.setNegotiationTypes(getNegotiationTypes());
55                 nodeRef.setOpennet(isOpennet());
56                 nodeRef.setPhysicalUDP(getPhysicalUDP());
57                 nodeRef.setVersion(getVersion());
58                 return nodeRef;
59         }
60
61         /**
62          * Returns the identifier of the request.
63          * 
64          * @return The identifier of the request
65          */
66         public String getIdentifier() {
67                 return getField("Identifier");
68         }
69
70         /**
71          * Returns the “physical.udp” line from the message. It contains all IP
72          * addresses and port numbers of the peer.
73          * 
74          * @return The IP addresses and port numbers of the peer
75          */
76         public String getPhysicalUDP() {
77                 return getField("physical.udp");
78         }
79
80         /**
81          * Returns whether the listed peer is an opennet peer.
82          * 
83          * @return <code>true</code> if the peer is an opennet peer,
84          *         <code>false</code> if the peer is a darknet peer
85          */
86         public boolean isOpennet() {
87                 return Boolean.valueOf(getField("opennet"));
88         }
89
90         /**
91          * Returns the “y” part of the peer’s public DSA key.
92          * 
93          * @return The public DSA key
94          */
95         public String getDSAPublicKey() {
96                 return getField("dsaPubKey.y");
97         }
98
99         /**
100          * Returns the DSA group of the peer.
101          * 
102          * @return The DSA group of the peer
103          */
104         public DSAGroup getDSAGroup() {
105                 return new DSAGroup(getField("dsaGroup.g"), getField("dsaGroup.p"), getField("dsaGroup.q"));
106         }
107
108         /**
109          * Returns the last good version of the peer, i.e. the oldest version the
110          * peer will connect to.
111          * 
112          * @return The last good version of the peer
113          */
114         public Version getLastGoodVersion() {
115                 return new Version(getField("lastGoodVersion"));
116         }
117
118         /**
119          * Returns the ARK of the peer.
120          * 
121          * @return The ARK of the peer
122          */
123         public ARK getARK() {
124                 return new ARK(getField("ark.pubURI"), getField("ark.number"));
125         }
126
127         /**
128          * Returns the identity of the peer.
129          * 
130          * @return The identity of the peer
131          */
132         public String getIdentity() {
133                 return getField("identity");
134         }
135
136         /**
137          * Returns the name of the peer. If the peer is not a darknet peer it will
138          * have no name.
139          * 
140          * @return The name of the peer, or <code>null</code> if the peer is an
141          *         opennet peer
142          */
143         public String getMyName() {
144                 return getField("myName");
145         }
146
147         /**
148          * Returns the location of the peer.
149          * 
150          * @return The location of the peer
151          * @throws NumberFormatException
152          *             if the field can not be parsed
153          */
154         public double getLocation() throws NumberFormatException {
155                 return Double.valueOf(getField("location"));
156         }
157
158         /**
159          * Returns whether the peer is a testnet node.
160          * 
161          * @return <code>true</code> if the peer is a testnet node,
162          *         <code>false</code> otherwise
163          */
164         public boolean isTestnet() {
165                 return Boolean.valueOf("testnet");
166         }
167
168         /**
169          * Returns the version of the peer.
170          * 
171          * @return The version of the peer
172          */
173         public Version getVersion() {
174                 return new Version(getField("version"));
175         }
176
177         /**
178          * Returns the negotiation types the peer supports.
179          * 
180          * @return The supported negotiation types
181          */
182         public int[] getNegotiationTypes() {
183                 return FcpUtils.decodeMultiIntegerField(getField("auth.negTypes"));
184         }
185
186         /**
187          * Returns one of the volatile fields from the message. The given field name
188          * is prepended with “volatile.” so if you want to get the value of the
189          * field with the name “volatile.status” you only need to specify “status”.
190          * 
191          * @param field
192          *            The name of the field
193          * @return The value of the field, or <code>null</code> if there is no
194          *         such field
195          */
196         public String getVolatile(String field) {
197                 return getField("volatile." + field);
198         }
199
200         /**
201          * Returns one of the metadata fields from the message. The given field name
202          * is prepended with “metadata.” so if you want to get the value of the
203          * field with the name “metadata.timeLastRoutable” you only need to specify
204          * “timeLastRoutable”.
205          * 
206          * @param field
207          *            The name of the field
208          * @return The value of the field, or <code>null</code> if there is no
209          *         such field
210          */
211         public String getMetadata(String field) {
212                 return getField("metadata." + field);
213         }
214
215 }