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