d85c3d44c75cdb99a4d9b73d3a08f1f357141e6a
[jFCPlib.git] / src / net / pterodactylus / fcp / NodeRef.java
1 /*
2  * jSite2 - NodeRef.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  * A reference for a node. The noderef contains all data that is necessary to
24  * establish a trusted and secure connection to the node.
25  * 
26  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
27  * @version $Id$
28  */
29 public class NodeRef {
30
31         /** The identity of the node. */
32         private String identity;
33
34         /** Whether the node is an opennet peer. */
35         private boolean opennet;
36
37         /** The name of the node. */
38         private String name;
39
40         /** The location of the node. */
41         private double location;
42
43         /** The IP addresses and ports of the node. */
44         private String physicalUDP;
45
46         /** The ARK of the node. */
47         private ARK ark;
48
49         /** The public DSA key of the node. */
50         private String dsaPublicKey;
51
52         /** The DSA group of the node. */
53         private DSAGroup dsaGroup;
54
55         /** The node’s supported negotiation types. */
56         private int[] negotiationTypes;
57
58         /** The version of the node. */
59         private Version version;
60
61         /** The oldest version the node will connect to. */
62         private Version lastGoodVersion;
63
64         /** Whether the node is a testnet node. */
65         private boolean testnet;
66
67         /** The signature of the reference. */
68         private String signature;
69
70         /**
71          * Creates a new, empty noderef.
72          */
73         public NodeRef() {
74                 /* intentionally left blank. */
75         }
76
77         /**
78          * Creates a new noderef that is initialized with fields from the given
79          * message.
80          * 
81          * @param fromMessage
82          *            The message to get initial values for the noderef from
83          */
84         public NodeRef(FcpMessage fromMessage) {
85                 identity = fromMessage.getField("identity");
86                 opennet = Boolean.valueOf(fromMessage.getField("opennet"));
87                 name = fromMessage.getField("myName");
88                 if (fromMessage.hasField("location")) {
89                         location = Double.valueOf(fromMessage.getField("location"));
90                 }
91                 physicalUDP = fromMessage.getField("physical.udp");
92                 ark = new ARK(fromMessage.getField("ark.pubURI"), fromMessage.getField("ark.privURI"), fromMessage.getField("ark.number"));
93                 dsaPublicKey = fromMessage.getField("dsaPubKey.y");
94                 dsaGroup = new DSAGroup(fromMessage.getField("dsaGroup.b"), fromMessage.getField("dsaGroup.p"), fromMessage.getField("dsaGroup.q"));
95                 negotiationTypes = FcpUtils.decodeMultiIntegerField(fromMessage.getField("auth.negTypes"));
96                 version = new Version(fromMessage.getField("version"));
97                 lastGoodVersion = new Version(fromMessage.getField("lastGoodVersion"));
98                 testnet = Boolean.valueOf(fromMessage.getField("testnet"));
99                 signature = fromMessage.getField("sig");
100         }
101
102         /**
103          * Returns the identity of the node.
104          * 
105          * @return The identity of the node
106          */
107         public String getIdentity() {
108                 return identity;
109         }
110
111         /**
112          * Sets the identity of the node.
113          * 
114          * @param identity
115          *            The identity of the node
116          */
117         public void setIdentity(String identity) {
118                 this.identity = identity;
119         }
120
121         /**
122          * Returns whether the node is an opennet peer.
123          * 
124          * @return <code>true</code> if the node is an opennet peer,
125          *         <code>false</code> otherwise
126          */
127         public boolean isOpennet() {
128                 return opennet;
129         }
130
131         /**
132          * Sets whether the node is an opennet peer.
133          * 
134          * @param opennet
135          *            <code>true</code> if the node is an opennet peer,
136          *            <code>false</code> otherwise
137          */
138         public void setOpennet(boolean opennet) {
139                 this.opennet = opennet;
140         }
141
142         /**
143          * Returns the name of the node. If the node is an opennet peer, it will not
144          * have a name!
145          * 
146          * @return The name of the node, or <code>null</code> if the node is an
147          *         opennet peer
148          */
149         public String getMyName() {
150                 return name;
151         }
152
153         /**
154          * Sets the name of the peer.
155          * 
156          * @param name
157          *            The name of the peer
158          */
159         public void setName(String name) {
160                 this.name = name;
161         }
162
163         /**
164          * Returns the location of the node.
165          * 
166          * @return The location of the node
167          */
168         public double getLocation() {
169                 return location;
170         }
171
172         /**
173          * Sets the location of the node
174          * 
175          * @param location
176          *            The location of the node
177          */
178         public void setLocation(double location) {
179                 this.location = location;
180         }
181
182         /**
183          * Returns the IP addresses and port numbers of the node.
184          * 
185          * @return The IP addresses and port numbers of the node
186          */
187         public String getPhysicalUDP() {
188                 return physicalUDP;
189         }
190
191         /**
192          * Sets the IP addresses and port numbers of the node.
193          * 
194          * @param physicalUDP
195          *            The IP addresses and port numbers of the node
196          */
197         public void setPhysicalUDP(String physicalUDP) {
198                 this.physicalUDP = physicalUDP;
199         }
200
201         /**
202          * Returns the ARK of the node.
203          * 
204          * @return The ARK of the node
205          */
206         public ARK getARK() {
207                 return ark;
208         }
209
210         /**
211          * Sets the ARK of the node.
212          * 
213          * @param ark
214          *            The ARK of the node
215          */
216         public void setARK(ARK ark) {
217                 this.ark = ark;
218         }
219
220         /**
221          * Returns the public DSA key of the node.
222          * 
223          * @return The public DSA key of the node
224          */
225         public String getDSAPublicKey() {
226                 return dsaPublicKey;
227         }
228
229         /**
230          * Sets the public DSA key of the node.
231          * 
232          * @param dsaPublicKey
233          *            The public DSA key of the node
234          */
235         public void setDSAPublicKey(String dsaPublicKey) {
236                 this.dsaPublicKey = dsaPublicKey;
237         }
238
239         /**
240          * Returns the DSA group of the node.
241          * 
242          * @return The DSA group of the node
243          */
244         public DSAGroup getDSAGroup() {
245                 return dsaGroup;
246         }
247
248         /**
249          * Sets the DSA group of the node.
250          * 
251          * @param dsaGroup
252          *            The DSA group of the node
253          */
254         public void setDSAGroup(DSAGroup dsaGroup) {
255                 this.dsaGroup = dsaGroup;
256         }
257
258         /**
259          * Returns the negotiation types supported by the node.
260          * 
261          * @return The node’s supported negotiation types
262          */
263         public int[] getNegotiationTypes() {
264                 return negotiationTypes;
265         }
266
267         /**
268          * Sets the negotiation types supported by the node.
269          * 
270          * @param negotiationTypes
271          *            The node’s supported negotiation types
272          */
273         public void setNegotiationTypes(int[] negotiationTypes) {
274                 this.negotiationTypes = negotiationTypes;
275         }
276
277         /**
278          * Returns the version of the node.
279          * 
280          * @return The version of the node
281          */
282         public Version getVersion() {
283                 return version;
284         }
285
286         /**
287          * Sets the version of the node.
288          * 
289          * @param version
290          *            The version of the node
291          */
292         public void setVersion(Version version) {
293                 this.version = version;
294         }
295
296         /**
297          * Returns the last good version of the node.
298          * 
299          * @return The oldest version the node will connect to
300          */
301         public Version getLastGoodVersion() {
302                 return lastGoodVersion;
303         }
304
305         /**
306          * Sets the last good version of the node.
307          * 
308          * @param lastGoodVersion
309          *            The oldest version the node will connect to
310          */
311         public void setLastGoodVersion(Version lastGoodVersion) {
312                 this.lastGoodVersion = lastGoodVersion;
313         }
314
315         /**
316          * Returns whether the node is a testnet node.
317          * 
318          * @return <code>true</code> if the node is a testnet node,
319          *         <code>false</code> otherwise
320          */
321         public boolean isTestnet() {
322                 return testnet;
323         }
324
325         /**
326          * Sets whether this node is a testnet node.
327          * 
328          * @param testnet
329          *            <code>true</code> if the node is a testnet node,
330          *            <code>false</code> otherwise
331          */
332         public void setTestnet(boolean testnet) {
333                 this.testnet = testnet;
334         }
335
336         /**
337          * Returns the signature of the noderef.
338          * 
339          * @return The signature of the noderef
340          */
341         public String getSignature() {
342                 return signature;
343         }
344
345         /**
346          * Sets the signature of the noderef.
347          * 
348          * @param signature
349          *            The signature of the noderef
350          */
351         public void setSignature(String signature) {
352                 this.signature = signature;
353         }
354
355 }