3 * Copyright (C) 2006 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 de.todesbaum.util.freenet.fcp2;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.io.OutputStreamWriter;
28 import java.io.Writer;
29 import java.net.Socket;
30 import java.nio.charset.Charset;
31 import java.util.ArrayList;
32 import java.util.List;
34 import de.todesbaum.util.io.Closer;
35 import de.todesbaum.util.io.LineInputStream;
36 import de.todesbaum.util.io.StreamCopier;
37 import de.todesbaum.util.io.StreamCopier.ProgressListener;
38 import de.todesbaum.util.io.TempFileInputStream;
41 * A physical connection to a Freenet node.
43 * @author David Roden <droden@gmail.com>
46 public class Connection {
48 /** The listeners that receive events from this connection. */
49 private List<ConnectionListener> connectionListeners = new ArrayList<ConnectionListener>();
51 /** The node this connection is connected to. */
52 private final Node node;
54 /** The name of this connection. */
55 private final String name;
57 /** The network socket of this connection. */
58 private Socket nodeSocket;
60 /** The input stream that reads from the socket. */
61 private InputStream nodeInputStream;
63 /** The output stream that writes to the socket. */
64 private OutputStream nodeOutputStream;
66 /** The thread that reads from the socket. */
67 private NodeReader nodeReader;
69 /** A writer for the output stream. */
70 private Writer nodeWriter;
72 /** The NodeHello message sent by the node on connect. */
73 protected Message nodeHello;
75 /** The temp directory to use. */
76 private String tempDirectory;
79 * Creates a new connection to the specified node with the specified name.
82 * The node to connect to
84 * The name of this connection
86 public Connection(Node node, String name) {
92 * Adds a listener that gets notified on connection events.
94 * @param connectionListener
97 public void addConnectionListener(ConnectionListener connectionListener) {
98 connectionListeners.add(connectionListener);
102 * Removes a listener from the list of registered listeners. Only the first
103 * matching listener is removed.
105 * @param connectionListener
106 * The listener to remove
107 * @see List#remove(java.lang.Object)
109 public void removeConnectionListener(ConnectionListener connectionListener) {
110 connectionListeners.remove(connectionListener);
114 * Notifies listeners about a received message.
117 * The received message
119 protected void fireMessageReceived(Message message) {
120 for (ConnectionListener connectionListener : connectionListeners) {
121 connectionListener.messageReceived(this, message);
126 * Notifies listeners about the loss of the connection.
128 protected void fireConnectionTerminated() {
129 for (ConnectionListener connectionListener : connectionListeners) {
130 connectionListener.connectionTerminated(this);
135 * Returns the name of the connection.
137 * @return The name of the connection
139 public String getName() {
144 * Sets the temp directory to use for creation of temporary files.
146 * @param tempDirectory
147 * The temp directory to use, or {@code null} to use the default
150 public void setTempDirectory(String tempDirectory) {
151 this.tempDirectory = tempDirectory;
155 * Connects to the node.
157 * @return <code>true</code> if the connection succeeded and the node
158 * returned a NodeHello message
159 * @throws IOException
160 * if an I/O error occurs
161 * @see #getNodeHello()
163 public synchronized boolean connect() throws IOException {
165 nodeInputStream = null;
166 nodeOutputStream = null;
170 nodeSocket = new Socket(node.getHostname(), node.getPort());
171 nodeSocket.setReceiveBufferSize(65535);
172 nodeInputStream = nodeSocket.getInputStream();
173 nodeOutputStream = nodeSocket.getOutputStream();
174 nodeWriter = new OutputStreamWriter(nodeOutputStream, Charset.forName("UTF-8"));
175 nodeReader = new NodeReader(nodeInputStream);
176 Thread nodeReaderThread = new Thread(nodeReader);
177 nodeReaderThread.setDaemon(true);
178 nodeReaderThread.start();
179 ClientHello clientHello = new ClientHello();
180 clientHello.setName(name);
181 clientHello.setExpectedVersion("2.0");
182 execute(clientHello);
183 synchronized (this) {
186 } catch (InterruptedException e) {
189 return nodeHello != null;
190 } catch (IOException ioe1) {
197 * Returns whether this connection is still connected to the node.
199 * @return <code>true</code> if this connection is still valid,
200 * <code>false</code> otherwise
202 public boolean isConnected() {
203 return (nodeHello != null) && (nodeSocket != null) && (nodeSocket.isConnected());
207 * Returns the NodeHello message the node sent on connection.
209 * @return The NodeHello message of the node
211 public Message getNodeHello() {
216 * Disconnects from the node.
218 public void disconnect() {
219 if (nodeWriter != null) {
222 } catch (IOException ioe1) {
226 if (nodeOutputStream != null) {
228 nodeOutputStream.close();
229 } catch (IOException ioe1) {
231 nodeOutputStream = null;
233 if (nodeInputStream != null) {
235 nodeInputStream.close();
236 } catch (IOException ioe1) {
238 nodeInputStream = null;
240 if (nodeSocket != null) {
243 } catch (IOException ioe1) {
247 synchronized (this) {
250 fireConnectionTerminated();
254 * Executes the specified command.
257 * The command to execute
258 * @throws IllegalStateException
259 * if the connection is not connected
260 * @throws IOException
261 * if an I/O error occurs
263 public synchronized void execute(Command command) throws IllegalStateException, IOException {
264 execute(command, null);
268 * Executes the specified command.
271 * The command to execute
272 * @param progressListener
273 * A progress listener for a payload transfer
274 * @throws IllegalStateException
275 * if the connection is not connected
276 * @throws IOException
277 * if an I/O error occurs
279 public synchronized void execute(Command command, ProgressListener progressListener) throws IllegalStateException, IOException {
280 if (nodeSocket == null) {
281 throw new IllegalStateException("connection is not connected");
283 nodeWriter.write(command.getCommandName() + Command.LINEFEED);
284 command.write(nodeWriter);
285 nodeWriter.write("EndMessage" + Command.LINEFEED);
287 if (command.hasPayload()) {
288 InputStream payloadInputStream = null;
290 payloadInputStream = command.getPayload();
291 StreamCopier.copy(payloadInputStream, nodeOutputStream, command.getPayloadLength(), progressListener);
293 Closer.close(payloadInputStream);
295 nodeOutputStream.flush();
300 * The reader thread for this connection. This is essentially a thread that
301 * reads lines from the node, creates messages from them and notifies
302 * listeners about the messages.
304 * @author David Roden <droden@gmail.com>
307 private class NodeReader implements Runnable {
309 /** The input stream to read from. */
310 @SuppressWarnings("hiding")
311 private InputStream nodeInputStream;
314 * Creates a new reader that reads from the specified input stream.
316 * @param nodeInputStream
317 * The input stream to read from
319 public NodeReader(InputStream nodeInputStream) {
320 this.nodeInputStream = nodeInputStream;
324 * Main loop of the reader. Lines are read and converted into
325 * {@link Message} objects.
328 LineInputStream nodeReader = null;
330 nodeReader = new LineInputStream(nodeInputStream);
332 Message message = null;
333 while (line != null) {
334 line = nodeReader.readLine();
335 // System.err.println("> " + line);
339 if (message == null) {
340 message = new Message(line);
343 if ("Data".equals(line)) {
344 /* need to read message from stream now */
345 File tempFile = null;
347 tempFile = File.createTempFile("fcpv2", "data", (tempDirectory != null) ? new File(tempDirectory) : null);
348 tempFile.deleteOnExit();
349 FileOutputStream tempFileOutputStream = new FileOutputStream(tempFile);
350 long dataLength = Long.parseLong(message.get("DataLength"));
351 StreamCopier.copy(nodeInputStream, tempFileOutputStream, dataLength);
352 tempFileOutputStream.close();
353 message.setPayloadInputStream(new TempFileInputStream(tempFile));
354 } catch (IOException ioe1) {
355 ioe1.printStackTrace();
358 if ("Data".equals(line) || "EndMessage".equals(line)) {
359 if (message.getName().equals("NodeHello")) {
361 synchronized (Connection.this) {
362 Connection.this.notify();
365 fireMessageReceived(message);
370 int equalsPosition = line.indexOf('=');
371 if (equalsPosition > -1) {
372 String key = line.substring(0, equalsPosition).trim();
373 String value = line.substring(equalsPosition + 1).trim();
374 if (key.equals("Identifier")) {
375 message.setIdentifier(value);
377 message.put(key, value);
381 /* skip lines consisting of whitespace only */
382 if (line.trim().length() == 0) {
385 /* if we got here, some error occured! */
386 throw new IOException("Unexpected line: " + line);
388 } catch (IOException ioe1) {
389 // ioe1.printStackTrace();
391 if (nodeReader != null) {
394 } catch (IOException ioe1) {
397 if (nodeInputStream != null) {
399 nodeInputStream.close();
400 } catch (IOException ioe1) {
404 Connection.this.disconnect();