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;
27 * A Client executes {@link Command}s over a {@link Connection} to a
28 * {@link Node} and delivers resulting {@link Message}s.
30 * @author David Roden <droden@gmail.com>
33 public class Client implements ConnectionListener {
35 /** The connection this client operates on. */
36 private final Connection connection;
38 /** The identifiers the client filters messages for. */
39 private List<String> identifiers = new ArrayList<String>();
41 /** The queued messages. */
42 private final List<Message> messageQueue = new ArrayList<Message>();
44 /** Whether the client was disconnected. */
45 private boolean disconnected = false;
47 /** Whether to catch all messages from the connection. */
48 private boolean catchAll = false;
51 * Creates a new client that operates on the specified connection.
54 * The connection to operate on
56 public Client(Connection connection) {
57 this.connection = connection;
58 connection.addConnectionListener(this);
62 * Creates a new client that operates on the specified connection and
63 * immediately executes the specified command.
66 * The connection to operate on
68 * The command to execute
70 * if an I/O error occurs
71 * @see #execute(Command)
73 public Client(Connection connection, Command command) throws IOException {
79 * Returns whether this client catches all messages going over the
82 * @return <code>true</code> if the client catches all messages,
83 * <code>false</code> otherwise
85 public boolean isCatchAll() {
90 * Sets whether this client catches all messages going over the connection.
93 * <code>true</code> if the client should catch all messages,
94 * <code>false</code> otherwise
96 public void setCatchAll(boolean catchAll) {
97 this.catchAll = catchAll;
101 * Executes the specified command. This will also clear the queue of
102 * messages, discarding all messages that resulted from the previous command
103 * and have not yet been read.
106 * The command to execute
107 * @throws IOException
108 * if an I/O error occurs
109 * @see #execute(Command, boolean)
111 public void execute(Command command) throws IOException {
112 execute(command, true);
116 * Executes the specified command and optionally clears the list of
117 * identifiers this clients listens to before starting the command.
120 * The command to execute
121 * @param removeExistingIdentifiers
122 * If <code>true</code>, the list of identifiers that this
123 * clients listens to is cleared
124 * @throws IOException
125 * if an I/O error occurs
127 public void execute(Command command, boolean removeExistingIdentifiers) throws IOException {
128 synchronized (messageQueue) {
129 messageQueue.clear();
130 if (removeExistingIdentifiers) {
133 identifiers.add(command.getIdentifier());
135 connection.execute(command);
139 * Returns the next message, waiting endlessly for it, if need be. If you
140 * are not sure whether a message will arrive, better use
141 * {@link #readMessage(long)} to only wait for a specific time.
143 * @return The next message that resulted from the execution of the last
145 * @see #readMessage(long)
146 * @see #execute(Command)
148 public Message readMessage() {
149 return readMessage(0);
153 * Returns the next message. If the message queue is currently empty, at
154 * least <code>maxWaitTime</code> milliseconds will be waited for a
158 * The minimum time to wait for a message, in milliseconds
159 * @return The message, or <code>null</code> if no message arrived in time
160 * or the client is currently disconnected
161 * @see #isDisconnected()
162 * @see Object#wait(long)
164 public Message readMessage(long maxWaitTime) {
165 synchronized (messageQueue) {
169 if (messageQueue.size() == 0) {
171 messageQueue.wait(maxWaitTime);
172 } catch (InterruptedException ie1) {
175 if (messageQueue.size() > 0) {
176 return messageQueue.remove(0);
183 * Returns whether the client is currently disconnected.
185 * @return <code>true</code> if the client is disconnected,
186 * <code>false</code> otherwise
188 public boolean isDisconnected() {
189 synchronized (messageQueue) {
197 public void messageReceived(Connection connection, Message message) {
198 synchronized (messageQueue) {
199 if (catchAll || (message.getIdentifier().length() == 0) || identifiers.contains(message.getIdentifier())) {
200 messageQueue.add(message);
201 messageQueue.notify();
209 public void connectionTerminated(Connection connection) {
210 synchronized (messageQueue) {
212 messageQueue.notify();