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;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.Writer;
27 * Abstract base class for all commands.
29 * In addition to the replies listed at the type comment of each specific
30 * command the node can <strong>always</strong> send the following messages:
31 * <code>ProtocolError</code> (if this library screws up),
32 * <code>CloseConnectionDuplicateClientName</code> (if a client with the same
33 * name of the {@link de.todesbaum.util.freenet.fcp2.Connection} connects). So
34 * when receiving messages from the node you should always be prepared for
35 * something you did not expect.
37 * @author David Roden <droden@gmail.com>
40 public abstract class Command {
42 /** The line feed sequence used by the library. */
43 protected static final String LINEFEED = "\r\n";
46 * The name of the command. The name is sent to the node so it can not be
49 private final String commandName;
52 * The identifier of the command. This identifier is used to identify
53 * replies that are caused by a command.
55 private final String identifier;
58 * Creates a new command with the specified name and identifier.
61 * The name of the command
63 * The identifier of the command
65 public Command(String name, String identifier) {
66 this.commandName = name;
67 this.identifier = identifier;
71 * Returns the name of this command.
73 * @return The name of this command
75 public String getCommandName() {
80 * Return the identifier of this command.
82 * @return The identifier of this command
84 public String getIdentifier() {
89 * Writes all parameters to the specified writer.
91 * <strong>NOTE:</strong> Subclasses of Command <strong>must</strong> call
92 * <code>super.write(writer)</code> before or after writing their own
96 * The stream to write the parameters to
98 * if an I/O error occurs
100 protected void write(Writer writer) throws IOException {
101 if (identifier != null)
102 writer.write("Identifier=" + identifier + LINEFEED);
106 * Returns whether this command has payload to send after the message.
107 * Subclasses need to return <code>true</code> here if they need to send
108 * payload after the message.
110 * @return <code>true</code> if this command has payload to send,
111 * <code>false</code> otherwise
113 protected boolean hasPayload() {
118 * Returns the payload of this command as an {@link InputStream}. This
119 * method is never called if {@link #hasPayload()} returns
120 * <code>false</code>.
122 * @return The payload of this command
124 protected InputStream getPayload() {
129 * Returns the length of the payload. This method is never called if
130 * {@link #hasPayload()} returns <code>false</code>.
132 * @return The length of the payload
134 protected long getPayloadLength() {