implement node addition and removal events
[jSite2.git] / src / net / pterodactylus / util / fcp / Peer.java
1 /**
2  * © 2008 INA Service GmbH
3  */
4 package net.pterodactylus.util.fcp;
5
6 /**
7  * The “Peer” reply by the node contains information about a peer.
8  * 
9  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
10  * @version $Id$
11  */
12 public class Peer extends BaseMessage {
13
14         /**
15          * Creates a new “Peer” reply from the received message.
16          * 
17          * @param receivedMessage
18          *            The received message
19          */
20         Peer(FcpMessage receivedMessage) {
21                 super(receivedMessage);
22         }
23
24         /**
25          * Returns a collection of fields as a node reference.
26          * 
27          * @return The node reference contained within this message
28          */
29         public NodeRef getNodeRef() {
30                 NodeRef nodeRef = new NodeRef();
31                 nodeRef.setARK(getARK());
32                 nodeRef.setDSAGroup(getDSAGroup());
33                 nodeRef.setDSAPublicKey(getDSAPublicKey());
34                 nodeRef.setIdentity(getIdentity());
35                 nodeRef.setLastGoodVersion(getLastGoodVersion());
36                 nodeRef.setLocation(getLocation());
37                 nodeRef.setName(getMyName());
38                 nodeRef.setNegotiationTypes(getNegotiationTypes());
39                 nodeRef.setOpennet(isOpennet());
40                 nodeRef.setPhysicalUDP(getPhysicalUDP());
41                 nodeRef.setVersion(getVersion());
42                 return nodeRef;
43         }
44
45         /**
46          * Returns the “physical.udp” line from the message. It contains all IP
47          * addresses and port numbers of the peer.
48          * 
49          * @return The IP addresses and port numbers of the peer
50          */
51         public String getPhysicalUDP() {
52                 return getField("physical.udp");
53         }
54
55         /**
56          * Returns whether the listed peer is an opennet peer.
57          * 
58          * @return <code>true</code> if the peer is an opennet peer,
59          *         <code>false</code> if the peer is a darknet peer
60          */
61         public boolean isOpennet() {
62                 return Boolean.valueOf(getField("opennet"));
63         }
64
65         /**
66          * Returns the “y” part of the peer’s public DSA key.
67          * 
68          * @return The public DSA key
69          */
70         public String getDSAPublicKey() {
71                 return getField("dsaPubKey.y");
72         }
73
74         /**
75          * Returns the DSA group of the peer.
76          * 
77          * @return The DSA group of the peer
78          */
79         public DSAGroup getDSAGroup() {
80                 return new DSAGroup(getField("dsaGroup.g"), getField("dsaGroup.p"), getField("dsaGroup.q"));
81         }
82
83         /**
84          * Returns the last good version of the peer, i.e. the oldest version the
85          * peer will connect to.
86          * 
87          * @return The last good version of the peer
88          */
89         public Version getLastGoodVersion() {
90                 return new Version(getField("lastGoodVersion"));
91         }
92
93         /**
94          * Returns the ARK of the peer.
95          * 
96          * @return The ARK of the peer
97          */
98         public ARK getARK() {
99                 return new ARK(getField("ark.pubURI"), getField("ark.number"));
100         }
101
102         /**
103          * Returns the identity of the peer.
104          * 
105          * @return The identity of the peer
106          */
107         public String getIdentity() {
108                 return getField("identity");
109         }
110
111         /**
112          * Returns the name of the peer. If the peer is not a darknet peer it will
113          * have no name.
114          * 
115          * @return The name of the peer, or <code>null</code> if the peer is an
116          *         opennet peer
117          */
118         public String getMyName() {
119                 return getField("myName");
120         }
121
122         /**
123          * Returns the location of the peer.
124          * 
125          * @return The location of the peer
126          * @throws NumberFormatException
127          *             if the field can not be parsed
128          */
129         public double getLocation() throws NumberFormatException {
130                 return Double.valueOf(getField("location"));
131         }
132
133         /**
134          * Returns whether the peer is a testnet node.
135          * 
136          * @return <code>true</code> if the peer is a testnet node,
137          *         <code>false</code> otherwise
138          */
139         public boolean isTestnet() {
140                 return Boolean.valueOf("testnet");
141         }
142
143         /**
144          * Returns the version of the peer.
145          * 
146          * @return The version of the peer
147          */
148         public Version getVersion() {
149                 return new Version(getField("version"));
150         }
151
152         /**
153          * Returns the negotiation types the peer supports.
154          * 
155          * @return The supported negotiation types
156          */
157         public int[] getNegotiationTypes() {
158                 return FcpUtils.decodeMultiIntegerField(getField("auth.negTypes"));
159         }
160
161 }