01: /*
02: * Copyright (c) xsocket.org, 2006 - 2008. All rights reserved.
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
19: * The latest copy of this software may be found on http://www.xsocket.org/
20: */
21: package org.xsocket.connection;
22:
23: import java.io.IOException;
24: import java.nio.BufferUnderflowException;
25:
26: import org.xsocket.MaxReadSizeExceededException;
27:
28: /**
29: * Reads and processes the incoming data. This method will be called
30: * each time when data is available. Because this depends on the
31: * underlying tcp protocol, it is not predictable how often and
32: * when this method will be call. Please note, that on network level
33: * data can be fragmented on several TCP packets as well as data can
34: * be bundled into one TCP packet. <br><br>
35: *
36: * Performing a write operation like <code>connection.write(“hello it’s me. What I have to say is….”)</code>
37: * on the client side doesn’t mean that exact one onData call occurs on
38: * the server side. A common pattern to solve this is to identify logical
39: * parts by a delimiter or a leading length field.
40: * xSocket provides methods to support this pattern. E.g. the {@link INonBlockingConnection#readStringByDelimiter(String)}
41: * method only returns a record if the whole part (identified by the delimiter) has
42: * been received, or if not, a BufferUnderflowException will be thrown. In
43: * contrast to {@link IBlockingConnection}, a {@link INonBlockingConnection} read
44: * method always returns immediately and could thrown a BufferUnderflowException.
45: * The {@link BufferUnderflowException} will be swallowed by the framework, if
46: * the DataHandler doesn’t catch this exception. It is a common pattern
47: * not to handle such an exception by the DataHandler.
48: *
49: * <pre>
50: * public final class MyHandler implements IDataHandler, IConnectionScoped {
51: * ...
52: * public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException, MaxReadSizeExceededException {
53: * ...
54: * // BufferUnderflowException will been thrown if delimiter has not been found.
55: * // A MaxReadSizeExceededException will be thrown if the max read size has been exceeded. Not handling this exception causes
56: * // that the server closes the underlying connection
57: * String command = connection.readStringByDelimiter("\r\n", "US-ASCII", 5000);
58: * ...
59: * connection.write(response, "US-ASCII");
60: * return true;
61: * }
62: * }
63: * </pre>
64: *
65: * @author grro@xsocket.org
66: */
67: public interface IDataHandler extends IHandler {
68:
69: /**
70: * processes the incoming data based on the given connection. <br><br>
71: *
72: * Please note, that the <code>onData</code> callback method could also be called
73: * for an already closed connection. This occurs when data has been received
74: * (and buffered internally) and the connection has been closed by the peer,
75: * immediately. In this case the <code>isOpen</code> call within the <code>onData</code>
76: * Method will return false. Reading of already received data would not fail.
77: * To detect if a connection has been closed the callback method <code>onDisconnect</code>
78: * should be implemented. The correct callback order will be managed by the xSocket.
79: *
80: * @param connection the underlying connection
81: * @return true for positive result of handling, false for negative result of handling.
82: * The return value will be used by the {@link HandlerChain} to interrupted
83: * the chaining (if result is true)
84: * @throws IOException If some other I/O error occurs. Throwing this exception causes that the underlying connection will be closed.
85: * @throws BufferUnderflowException if more incoming data is required to process (e.g. delimiter hasn't yet received -> readByDelimiter methods or size of the available, received data is smaller than the required size -> readByLength). The BufferUnderflowException will be swallowed by the framework
86: * @throws MaxReadSizeExceededException if the max read size has been reached (e.g. by calling method {@link INonBlockingConnection#readStringByDelimiter(String, int)}).
87: * Throwing this exception causes that the underlying connection will be closed.
88: */
89: public boolean onData(INonBlockingConnection connection)
90: throws IOException, BufferUnderflowException,
91: MaxReadSizeExceededException;
92: }
|