2 * jSite2 - FpcConnection.java -
3 * Copyright © 2008 David Roden
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.
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.
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.
20 package net.pterodactylus.util.fcp;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.net.InetAddress;
26 import java.net.Socket;
27 import java.net.UnknownHostException;
28 import java.util.ArrayList;
29 import java.util.List;
31 import net.pterodactylus.util.fcp.message.CloseConnectionDuplicateClientName;
32 import net.pterodactylus.util.fcp.message.EndListPeerNotes;
33 import net.pterodactylus.util.fcp.message.EndListPeers;
34 import net.pterodactylus.util.fcp.message.NodeHello;
35 import net.pterodactylus.util.fcp.message.Peer;
36 import net.pterodactylus.util.fcp.message.PeerNote;
37 import net.pterodactylus.util.fcp.message.SSKKeypair;
38 import net.pterodactylus.util.io.Closer;
41 * An FCP connection to a Freenet node.
43 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
46 public class FcpConnection {
48 /** The default port for FCP v2. */
49 public static final int DEFAULT_PORT = 9481;
51 /** The list of FCP listeners. */
52 private final List<FcpListener> fcpListeners = new ArrayList<FcpListener>();
54 /** The address of the node. */
55 private final InetAddress address;
57 /** The port number of the node’s FCP port. */
58 private final int port;
60 /** The remote socket. */
61 private Socket remoteSocket;
63 /** The input stream from the node. */
64 private InputStream remoteInputStream;
66 /** The output stream to the node. */
67 private OutputStream remoteOutputStream;
69 /** The connection handler. */
70 private FcpConnectionHandler connectionHandler;
73 * Creates a new FCP connection to the Freenet node running on the given
74 * host, listening on the default port.
77 * The hostname of the Freenet node
78 * @throws UnknownHostException
79 * if <code>host</code> can not be resolved
81 public FcpConnection(String host) throws UnknownHostException {
82 this(host, DEFAULT_PORT);
86 * Creates a new FCP connection to the Freenet node running on the given
87 * host, listening on the given port.
90 * The hostname of the Freenet node
92 * The port number of the node’s FCP port
93 * @throws UnknownHostException
94 * if <code>host</code> can not be resolved
96 public FcpConnection(String host, int port) throws UnknownHostException {
97 this(InetAddress.getByName(host), port);
101 * Creates a new FCP connection to the Freenet node running at the given
102 * address, listening on the default port.
105 * The address of the Freenet node
107 public FcpConnection(InetAddress address) {
108 this(address, DEFAULT_PORT);
112 * Creates a new FCP connection to the Freenet node running at the given
113 * address, listening on the given port.
116 * The address of the Freenet node
118 * The port number of the node’s FCP port
120 public FcpConnection(InetAddress address, int port) {
121 this.address = address;
126 // LISTENER MANAGEMENT
130 * Adds the given listener to the list of listeners.
133 * The listener to add
135 public void addFcpListener(FcpListener fcpListener) {
136 fcpListeners.add(fcpListener);
140 * Removes the given listener from the list of listeners.
143 * The listener to remove
145 public void removeFcpListener(FcpListener fcpListener) {
146 fcpListeners.remove(fcpListener);
150 * Notifies listeners that a “NodeHello” message was received.
152 * @see FcpListener#receivedNodeHello(FcpConnection, NodeHello)
154 * The “NodeHello” message
156 private void fireReceivedNodeHello(NodeHello nodeHello) {
157 for (FcpListener fcpListener: fcpListeners) {
158 fcpListener.receivedNodeHello(this, nodeHello);
163 * Notifies listeners that a “CloseConnectionDuplicateClientName” message
166 * @see FcpListener#receivedCloseConnectionDuplicateClientName(FcpConnection,
167 * CloseConnectionDuplicateClientName)
168 * @param closeConnectionDuplicateClientName
169 * The “CloseConnectionDuplicateClientName” message
171 private void fireReceivedCloseConnectionDuplicateClientName(CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
172 for (FcpListener fcpListener: fcpListeners) {
173 fcpListener.receivedCloseConnectionDuplicateClientName(this, closeConnectionDuplicateClientName);
178 * Notifies listeners that a “SSKKeypair” message was received.
180 * @see FcpListener#receivedSSKKeypair(FcpConnection, SSKKeypair)
182 * The “SSKKeypair” message
184 private void fireReceivedSSKKeypair(SSKKeypair sskKeypair) {
185 for (FcpListener fcpListener: fcpListeners) {
186 fcpListener.receivedSSKKeypair(this, sskKeypair);
191 * Notifies listeners that a “Peer” message was received.
193 * @see FcpListener#receivedPeer(FcpConnection, Peer)
197 private void fireReceivedPeer(Peer peer) {
198 for (FcpListener fcpListener: fcpListeners) {
199 fcpListener.receivedPeer(this, peer);
204 * Notifies all listeners that an “EndListPeers” message was received.
206 * @see FcpListener#receivedEndListPeers(FcpConnection, EndListPeers)
207 * @param endListPeers
208 * The “EndListPeers” message
210 private void fireReceivedEndListPeers(EndListPeers endListPeers) {
211 for (FcpListener fcpListener: fcpListeners) {
212 fcpListener.receivedEndListPeers(this, endListPeers);
217 * Notifies all listeners that a “PeerNote” message was received.
219 * @see FcpListener#receviedPeerNote(FcpConnection, PeerNote)
222 private void fireReceivedPeerNote(PeerNote peerNote) {
223 for (FcpListener fcpListener: fcpListeners) {
224 fcpListener.receviedPeerNote(this, peerNote);
229 * Notifies all listeners that an “EndListPeerNotes” message was received.
231 * @see FcpListener#receivedEndListPeerNotes(FcpConnection,
233 * @param endListPeerNotes
234 * The “EndListPeerNotes” message
236 private void fireReceivedEndListPeerNotes(EndListPeerNotes endListPeerNotes) {
237 for (FcpListener fcpListener: fcpListeners) {
238 fcpListener.receivedEndListPeerNotes(this, endListPeerNotes);
243 * Notifies all registered listeners that a message has been received.
245 * @see FcpListener#receivedMessage(FcpConnection, FcpMessage)
247 * The message that was received
249 private void fireMessageReceived(FcpMessage fcpMessage) {
250 for (FcpListener fcpListener: fcpListeners) {
251 fcpListener.receivedMessage(this, fcpMessage);
260 * Connects to the node.
262 * @throws IOException
263 * if an I/O error occurs
264 * @throws IllegalStateException
265 * if there is already a connection to the node
267 public synchronized void connect() throws IOException, IllegalStateException {
268 if (connectionHandler != null) {
269 throw new IllegalStateException("already connected, disconnect first");
271 remoteSocket = new Socket(address, port);
272 remoteInputStream = remoteSocket.getInputStream();
273 remoteOutputStream = remoteSocket.getOutputStream();
274 new Thread(connectionHandler = new FcpConnectionHandler(this, remoteInputStream)).start();
278 * Disconnects from the node. If there is no connection to the node, this
279 * method does nothing.
281 public synchronized void disconnect() {
282 if (connectionHandler == null) {
285 Closer.close(remoteSocket);
286 connectionHandler.stop();
287 connectionHandler = null;
291 * Sends the given FCP message.
294 * The FCP message to send
295 * @throws IOException
296 * if an I/O error occurs
298 public synchronized void sendMessage(FcpMessage fcpMessage) throws IOException {
299 System.out.println("sending message: " + fcpMessage.getName());
300 fcpMessage.write(remoteOutputStream);
304 // PACKAGE-PRIVATE METHODS
308 * Handles the given message, notifying listeners. This message should only
309 * be called by {@link FcpConnectionHandler}.
312 * The received message
314 void handleMessage(FcpMessage fcpMessage) {
315 String messageName = fcpMessage.getName();
316 if ("Peer".equals(messageName)) {
317 fireReceivedPeer(new Peer(fcpMessage));
318 } else if ("PeerNote".equals(messageName)) {
319 fireReceivedPeerNote(new PeerNote(fcpMessage));
320 } else if ("EndListPeerNotes".equals(messageName)) {
321 fireReceivedEndListPeerNotes(new EndListPeerNotes(fcpMessage));
322 } else if ("EndListPeers".equals(messageName)) {
323 fireReceivedEndListPeers(new EndListPeers(fcpMessage));
324 } else if ("SSKKeypair".equals(messageName)) {
325 fireReceivedSSKKeypair(new SSKKeypair(fcpMessage));
326 } else if ("NodeHello".equals(messageName)) {
327 fireReceivedNodeHello(new NodeHello(fcpMessage));
328 } else if ("CloseConnectionDuplicateClientName".equals(messageName)) {
329 fireReceivedCloseConnectionDuplicateClientName(new CloseConnectionDuplicateClientName(fcpMessage));
331 fireMessageReceived(fcpMessage);