7d74f0abd2d5a0f8a0fde8d540287d427eb12776
[jFCPlib.git] / src / net / pterodactylus / fcp / AddPeer.java
1 /*
2  * jSite2 - AddPeer.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 import java.net.URL;
23
24 /**
25  * The “AddPeer” request adds a peer to the node.
26  * 
27  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
28  */
29 public class AddPeer extends FcpMessage {
30
31         /**
32          * Creates a new “AddPeer” request.
33          */
34         private AddPeer() {
35                 super("AddPeer");
36         }
37
38         /**
39          * Creates a new “AddPeer” request that reads the noderef of the peer from
40          * the given file.
41          * 
42          * @param file
43          *            The file to read the noderef from
44          */
45         public AddPeer(String file) {
46                 this();
47                 setField("File", file);
48         }
49
50         /**
51          * Creates a new “AddPeer” request that reads the noderef of the peer from
52          * the given URL.
53          * 
54          * @param url
55          *            The URL to read the noderef from
56          */
57         public AddPeer(URL url) {
58                 this();
59                 setField("URL", String.valueOf(url));
60         }
61
62         /**
63          * Creates a new “AddPeer” request that adds the peer given by the noderef.
64          * 
65          * @param nodeRef
66          *            The noderef of the peer
67          */
68         public AddPeer(NodeRef nodeRef) {
69                 this();
70                 setNodeRef(nodeRef);
71         }
72
73         //
74         // PRIVATE METHODS
75         //
76
77         /**
78          * Sets the noderef of the peer to add.
79          * 
80          * @param nodeRef
81          *            The noderef of the peer
82          */
83         private void setNodeRef(NodeRef nodeRef) {
84                 setField("lastGoodVersion", nodeRef.getLastGoodVersion().toString());
85                 setField("opennet", String.valueOf(nodeRef.isOpennet()));
86                 setField("identity", nodeRef.getIdentity());
87                 setField("myName", nodeRef.getMyName());
88                 setField("location", String.valueOf(nodeRef.getLocation()));
89                 setField("testnet", String.valueOf(nodeRef.isTestnet()));
90                 setField("version", String.valueOf(nodeRef.getVersion()));
91                 setField("physical.udp", nodeRef.getPhysicalUDP());
92                 setField("ark.pubURI", nodeRef.getARK().getPublicURI());
93                 setField("ark.number", String.valueOf(nodeRef.getARK().getNumber()));
94                 setField("dsaPubKey.y", nodeRef.getDSAPublicKey());
95                 setField("dsaGroup.g", nodeRef.getDSAGroup().getBase());
96                 setField("dsaGroup.p", nodeRef.getDSAGroup().getPrime());
97                 setField("dsaGroup.q", nodeRef.getDSAGroup().getSubprime());
98                 setField("auth.negTypes", FcpUtils.encodeMultiIntegerField(nodeRef.getNegotiationTypes()));
99                 setField("sig", nodeRef.getSignature());
100         }
101
102 }