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.util.ArrayList;
24 import java.util.List;
26 import de.todesbaum.util.io.StreamCopier.ProgressListener;
29 * A Client executes {@link Command}s over a {@link Connection} to a
30 * {@link Node} and delivers resulting {@link Message}s.
32 * @author David Roden <droden@gmail.com>
35 public class Client implements ConnectionListener {
37 /** The connection this client operates on. */
38 private final Connection connection;
40 /** The identifiers the client filters messages for. */
41 private List<String> identifiers = new ArrayList<String>();
43 /** The queued messages. */
44 private final List<Message> messageQueue = new ArrayList<Message>();
46 /** Whether the client was disconnected. */
47 private boolean disconnected = false;
49 /** Whether to catch all messages from the connection. */
50 private boolean catchAll = false;
53 * Creates a new client that operates on the specified connection.
56 * The connection to operate on
58 public Client(Connection connection) {
59 this.connection = connection;
60 connection.addConnectionListener(this);
64 * Creates a new client that operates on the specified connection and
65 * immediately executes the specified command.
68 * The connection to operate on
70 * The command to execute
72 * if an I/O error occurs
73 * @see #execute(Command)
75 public Client(Connection connection, Command command) throws IOException {
81 * Returns whether this client catches all messages going over the
84 * @return <code>true</code> if the client catches all messages,
85 * <code>false</code> otherwise
87 public boolean isCatchAll() {
92 * Sets whether this client catches all messages going over the connection.
95 * <code>true</code> if the client should catch all messages,
96 * <code>false</code> otherwise
98 public void setCatchAll(boolean catchAll) {
99 this.catchAll = catchAll;
103 * Executes the specified command. This will also clear the queue of
104 * messages, discarding all messages that resulted from the previous command
105 * and have not yet been read.
108 * The command to execute
109 * @throws IOException
110 * if an I/O error occurs
111 * @see #execute(Command, boolean)
113 public void execute(Command command) throws IOException {
114 execute(command, true);
118 * Executes the specified command. This will also clear the queue of
119 * messages, discarding all messages that resulted from the previous
120 * command and have not yet been read.
123 * The command to execute
124 * @param progressListener
125 * The progress listener for payload transfers
126 * @throws IOException
127 * if an I/O error occurs
128 * @see #execute(Command, boolean)
130 public void execute(Command command, ProgressListener progressListener) throws IOException {
131 execute(command, true, progressListener);
135 * Executes the specified command and optionally clears the list of
136 * identifiers this clients listens to before starting the command.
139 * The command to execute
140 * @param removeExistingIdentifiers
141 * If <code>true</code>, the list of identifiers that this
142 * clients listens to is cleared
143 * @throws IOException
144 * if an I/O error occurs
146 public void execute(Command command, boolean removeExistingIdentifiers) throws IOException {
147 execute(command, removeExistingIdentifiers, null);
151 * Executes the specified command and optionally clears the list of
152 * identifiers this clients listens to before starting the command.
155 * The command to execute
156 * @param removeExistingIdentifiers
157 * If <code>true</code>, the list of identifiers that this
158 * clients listens to is cleared
159 * @param progressListener
160 * The progress listener for payload transfers
161 * @throws IOException
162 * if an I/O error occurs
164 public void execute(Command command, boolean removeExistingIdentifiers, ProgressListener progressListener) throws IOException {
165 synchronized (messageQueue) {
166 messageQueue.clear();
167 if (removeExistingIdentifiers) {
170 identifiers.add(command.getIdentifier());
172 connection.execute(command, progressListener);
176 * Returns the next message, waiting endlessly for it, if need be. If you
177 * are not sure whether a message will arrive, better use
178 * {@link #readMessage(long)} to only wait for a specific time.
180 * @return The next message that resulted from the execution of the last
182 * @see #readMessage(long)
183 * @see #execute(Command)
185 public Message readMessage() {
186 return readMessage(0);
190 * Returns the next message. If the message queue is currently empty, at
191 * least <code>maxWaitTime</code> milliseconds will be waited for a
195 * The minimum time to wait for a message, in milliseconds
196 * @return The message, or <code>null</code> if no message arrived in time
197 * or the client is currently disconnected
198 * @see #isDisconnected()
199 * @see Object#wait(long)
201 public Message readMessage(long maxWaitTime) {
202 synchronized (messageQueue) {
206 if (messageQueue.size() == 0) {
208 messageQueue.wait(maxWaitTime);
209 } catch (InterruptedException ie1) {
212 if (messageQueue.size() > 0) {
213 return messageQueue.remove(0);
220 * Returns whether the client is currently disconnected.
222 * @return <code>true</code> if the client is disconnected,
223 * <code>false</code> otherwise
225 public boolean isDisconnected() {
226 synchronized (messageQueue) {
234 public void messageReceived(Connection connection, Message message) {
235 synchronized (messageQueue) {
236 if (catchAll || (message.getIdentifier().length() == 0) || identifiers.contains(message.getIdentifier())) {
237 messageQueue.add(message);
238 messageQueue.notify();
246 public void connectionTerminated(Connection connection) {
247 synchronized (messageQueue) {
249 messageQueue.notify();