rename fcplib to jFCPlib
[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 “physical.udp” line from the message. It contains all IP
63          * addresses and port numbers of the peer.
64          * 
65          * @return The IP addresses and port numbers of the peer
66          */
67         public String getPhysicalUDP() {
68                 return getField("physical.udp");
69         }
70
71         /**
72          * Returns whether the listed peer is an opennet peer.
73          * 
74          * @return <code>true</code> if the peer is an opennet peer,
75          *         <code>false</code> if the peer is a darknet peer
76          */
77         public boolean isOpennet() {
78                 return Boolean.valueOf(getField("opennet"));
79         }
80
81         /**
82          * Returns the “y” part of the peer’s public DSA key.
83          * 
84          * @return The public DSA key
85          */
86         public String getDSAPublicKey() {
87                 return getField("dsaPubKey.y");
88         }
89
90         /**
91          * Returns the DSA group of the peer.
92          * 
93          * @return The DSA group of the peer
94          */
95         public DSAGroup getDSAGroup() {
96                 return new DSAGroup(getField("dsaGroup.g"), getField("dsaGroup.p"), getField("dsaGroup.q"));
97         }
98
99         /**
100          * Returns the last good version of the peer, i.e. the oldest version the
101          * peer will connect to.
102          * 
103          * @return The last good version of the peer
104          */
105         public Version getLastGoodVersion() {
106                 return new Version(getField("lastGoodVersion"));
107         }
108
109         /**
110          * Returns the ARK of the peer.
111          * 
112          * @return The ARK of the peer
113          */
114         public ARK getARK() {
115                 return new ARK(getField("ark.pubURI"), getField("ark.number"));
116         }
117
118         /**
119          * Returns the identity of the peer.
120          * 
121          * @return The identity of the peer
122          */
123         public String getIdentity() {
124                 return getField("identity");
125         }
126
127         /**
128          * Returns the name of the peer. If the peer is not a darknet peer it will
129          * have no name.
130          * 
131          * @return The name of the peer, or <code>null</code> if the peer is an
132          *         opennet peer
133          */
134         public String getMyName() {
135                 return getField("myName");
136         }
137
138         /**
139          * Returns the location of the peer.
140          * 
141          * @return The location of the peer
142          * @throws NumberFormatException
143          *             if the field can not be parsed
144          */
145         public double getLocation() throws NumberFormatException {
146                 return Double.valueOf(getField("location"));
147         }
148
149         /**
150          * Returns whether the peer is a testnet node.
151          * 
152          * @return <code>true</code> if the peer is a testnet node,
153          *         <code>false</code> otherwise
154          */
155         public boolean isTestnet() {
156                 return Boolean.valueOf("testnet");
157         }
158
159         /**
160          * Returns the version of the peer.
161          * 
162          * @return The version of the peer
163          */
164         public Version getVersion() {
165                 return new Version(getField("version"));
166         }
167
168         /**
169          * Returns the negotiation types the peer supports.
170          * 
171          * @return The supported negotiation types
172          */
173         public int[] getNegotiationTypes() {
174                 return FcpUtils.decodeMultiIntegerField(getField("auth.negTypes"));
175         }
176
177 }