Update license to GPLv3, fix header comments
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / AddPeer.java
1 /*
2  * jFCPlib - AddPeer.java - Copyright © 2008–2016 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.fcp;
19
20 import java.net.URL;
21
22 /**
23  * The “AddPeer” request adds a peer to the node.
24  *
25  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
26  */
27 public class AddPeer extends FcpMessage {
28
29         /**
30          * Creates a new “AddPeer” request.
31          */
32         private AddPeer() {
33                 super("AddPeer");
34         }
35
36         /**
37          * Creates a new “AddPeer” request that reads the noderef of the peer from
38          * the given file.
39          *
40          * @param file
41          *            The file to read the noderef from
42          */
43         public AddPeer(String file) {
44                 this();
45                 setField("File", file);
46         }
47
48         public AddPeer(String identifier, String file) {
49                 this(file);
50                 setField("Identifier", identifier);
51         }
52
53         /**
54          * Creates a new “AddPeer” request that reads the noderef of the peer from
55          * the given URL.
56          *
57          * @param url
58          *            The URL to read the noderef from
59          */
60         public AddPeer(URL url) {
61                 this();
62                 setField("URL", String.valueOf(url));
63         }
64
65         public AddPeer(String identifier, URL url) {
66                 this(url);
67                 setField("Identifier", identifier);
68         }
69
70         /**
71          * Creates a new “AddPeer” request that adds the peer given by the noderef.
72          *
73          * @param nodeRef
74          *            The noderef of the peer
75          */
76         public AddPeer(NodeRef nodeRef) {
77                 this();
78                 setNodeRef(nodeRef);
79         }
80
81         public AddPeer(String identifier, NodeRef nodeRef) {
82                 this(nodeRef);
83                 setField("Identifier", identifier);
84         }
85
86         //
87         // PRIVATE METHODS
88         //
89
90         /**
91          * Sets the noderef of the peer to add.
92          *
93          * @param nodeRef
94          *            The noderef of the peer
95          */
96         private void setNodeRef(NodeRef nodeRef) {
97                 setField("lastGoodVersion", String.valueOf(nodeRef.getLastGoodVersion()));
98                 setField("opennet", String.valueOf(nodeRef.isOpennet()));
99                 setField("identity", nodeRef.getIdentity());
100                 setField("myName", nodeRef.getMyName());
101                 setField("location", String.valueOf(nodeRef.getLocation()));
102                 setField("testnet", String.valueOf(nodeRef.isTestnet()));
103                 setField("version", String.valueOf(nodeRef.getVersion()));
104                 setField("physical.udp", nodeRef.getPhysicalUDP());
105                 setField("ark.pubURI", nodeRef.getARK().getPublicURI());
106                 setField("ark.number", String.valueOf(nodeRef.getARK().getNumber()));
107                 setField("dsaPubKey.y", nodeRef.getDSAPublicKey());
108                 setField("dsaGroup.g", nodeRef.getDSAGroup().getBase());
109                 setField("dsaGroup.p", nodeRef.getDSAGroup().getPrime());
110                 setField("dsaGroup.q", nodeRef.getDSAGroup().getSubprime());
111                 setField("auth.negTypes", FcpUtils.encodeMultiIntegerField(nodeRef.getNegotiationTypes()));
112                 setField("sig", nodeRef.getSignature());
113         }
114
115 }