2008-08-08 02:37:18 +02:00
|
|
|
/*
|
2008-08-08 03:27:24 +02:00
|
|
|
* JBoss, Home of Professional Open Source
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2008-08-08 03:27:24 +02:00
|
|
|
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
|
|
|
|
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
|
|
|
|
* full listing of individual contributors.
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2008-08-08 03:27:24 +02:00
|
|
|
* This is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful,
|
2008-08-08 02:37:18 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-08 03:27:24 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2008-08-08 02:37:18 +02:00
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2008-08-08 03:27:24 +02:00
|
|
|
* License along with this software; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
2008-08-08 02:37:18 +02:00
|
|
|
*/
|
2008-08-08 03:40:10 +02:00
|
|
|
package org.jboss.netty.channel;
|
2008-08-08 02:37:18 +02:00
|
|
|
|
2008-09-02 12:39:57 +02:00
|
|
|
import java.net.InetSocketAddress;
|
2008-08-08 02:37:18 +02:00
|
|
|
import java.net.SocketAddress;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-09-02 09:22:16 +02:00
|
|
|
* A nexus to a network socket or a component which is capable of I/O
|
|
|
|
* operations such as read, write, connect, and bind.
|
2008-09-02 09:13:20 +02:00
|
|
|
* <p>
|
|
|
|
* A channel provides a user:
|
|
|
|
* <ul>
|
2008-09-02 15:45:14 +02:00
|
|
|
* <li>the current state of the channel (e.g. is it open? is it connected?),</li>
|
2008-09-03 03:09:40 +02:00
|
|
|
* <li>the {@linkplain ChannelConfig configuration parameters} of the channel (e.g. receive buffer size),</li>
|
2008-09-02 09:22:16 +02:00
|
|
|
* <li>the I/O operations that the channel supports (e.g. read, write, connect, and bind), and</li>
|
2008-09-03 12:57:17 +02:00
|
|
|
* <li>the {@link ChannelPipeline} which handles all {@linkplain ChannelEvent I/O events and requests}
|
2008-09-02 15:45:14 +02:00
|
|
|
* associated with the channel.</li>
|
2008-09-02 09:13:20 +02:00
|
|
|
* </ul>
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2008-09-24 12:49:39 +02:00
|
|
|
* <h3>All I/O operations are asynchronous.</h3>
|
|
|
|
*
|
|
|
|
* All I/O operations in Netty are asynchronous. It means any I/O calls will
|
|
|
|
* return immediately with no guarantee that the requested I/O operation has
|
|
|
|
* been completed at the end of the call. Instead, you will be returned with
|
|
|
|
* a {@link ChannelFuture} instance which tells you when the requested I/O
|
|
|
|
* operation has succeeded, failed, or canceled.
|
|
|
|
*
|
2008-08-08 03:27:24 +02:00
|
|
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
|
|
|
* @author Trustin Lee (tlee@redhat.com)
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
|
|
|
* @version $Rev$, $Date$
|
|
|
|
*
|
|
|
|
* @apiviz.landmark
|
2008-08-08 03:40:10 +02:00
|
|
|
* @apiviz.composedOf org.jboss.netty.channel.ChannelConfig
|
|
|
|
* @apiviz.composedOf org.jboss.netty.channel.ChannelPipeline
|
2008-08-08 02:37:18 +02:00
|
|
|
*/
|
2008-12-01 07:08:52 +01:00
|
|
|
public interface Channel extends Comparable<Channel>{
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The {@link #getInterestOps() interestOps} value which tells that the
|
|
|
|
* I/O thread will not read a message from the channel but will perform
|
|
|
|
* the requested write operation immediately.
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
static int OP_NONE = 0;
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The {@link #getInterestOps() interestOps} value which tells that the
|
|
|
|
* I/O thread will read a message from the channel and will perform the
|
|
|
|
* requested write operation immediately.
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
static int OP_READ = 1;
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The {@link #getInterestOps() interestOps} value which tells that the
|
|
|
|
* I/O thread will not read a message from the channel and will not perform
|
|
|
|
* the requested write operation immediately. Any write requests made when
|
|
|
|
* {@link #OP_WRITE} flag is set are queued until the I/O thread is ready
|
|
|
|
* to process the queued write requests.
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
static int OP_WRITE = 4;
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The {@link #getInterestOps() interestOps} value which tells that the
|
|
|
|
* I/O thread will read a message from the channel but will not perform
|
|
|
|
* the requested write operation immediately. Any write requests made when
|
|
|
|
* {@link #OP_WRITE} flag is set are queued until the I/O thread is ready
|
|
|
|
* to process the queued write requests.
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
static int OP_READ_WRITE = OP_READ | OP_WRITE;
|
|
|
|
|
2008-09-02 09:13:20 +02:00
|
|
|
/**
|
|
|
|
* Returns the {@link UUID} of this channel.
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
UUID getId();
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the {@link ChannelFactory} which created this channel.
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
ChannelFactory getFactory();
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the parent of this channel.
|
|
|
|
*
|
|
|
|
* @return the parent channel.
|
2008-11-14 08:45:53 +01:00
|
|
|
* {@code null} if this channel does not have a parent channel.
|
2008-09-02 09:13:20 +02:00
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
Channel getParent();
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the configuration of this channel.
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
ChannelConfig getConfig();
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the {@link ChannelPipeline} which handles {@link ChannelEvent}s
|
|
|
|
* associated with this channel.
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
ChannelPipeline getPipeline();
|
|
|
|
|
2008-09-02 09:13:20 +02:00
|
|
|
/**
|
2008-09-02 12:39:57 +02:00
|
|
|
* Returns {@code true} if and only if this channel is open.
|
2008-09-02 09:13:20 +02:00
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
boolean isOpen();
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
2008-09-02 12:39:57 +02:00
|
|
|
* Returns {@code true} if and only if this channel is bound to a
|
2008-09-02 09:13:20 +02:00
|
|
|
* {@linkplain #getLocalAddress() local address}.
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
boolean isBound();
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
2008-09-02 12:39:57 +02:00
|
|
|
* Returns {@code true} if and only if this channel is connected to a
|
2008-09-02 09:13:20 +02:00
|
|
|
* {@linkplain #getRemoteAddress() remote address}.
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
boolean isConnected();
|
|
|
|
|
2008-09-02 09:13:20 +02:00
|
|
|
/**
|
2008-09-02 12:39:57 +02:00
|
|
|
* Returns the local address where this channel is bound to. The returned
|
|
|
|
* {@link SocketAddress} is supposed to be down-cast into more concrete
|
|
|
|
* type such as {@link InetSocketAddress} to retrieve the detailed
|
|
|
|
* information.
|
2008-09-02 09:13:20 +02:00
|
|
|
*
|
|
|
|
* @return the local address of this channel.
|
|
|
|
* {@code null} if this channel is not bound.
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
SocketAddress getLocalAddress();
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
2008-09-02 12:39:57 +02:00
|
|
|
* Returns the remote address where this channel is connected to. The
|
|
|
|
* returned {@link SocketAddress} is supposed to be down-cast into more
|
|
|
|
* concrete type such as {@link InetSocketAddress} to retrieve the detailed
|
|
|
|
* information.
|
2008-09-02 09:13:20 +02:00
|
|
|
*
|
|
|
|
* @return the remote address of this channel.
|
|
|
|
* {@code null} if this channel is not connected.
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
SocketAddress getRemoteAddress();
|
|
|
|
|
2008-09-02 09:13:20 +02:00
|
|
|
/**
|
|
|
|
* Sends a message to this channel asynchronously.
|
|
|
|
*
|
|
|
|
* @param message the message to write
|
|
|
|
*
|
|
|
|
* @return the {@link ChannelFuture} which will be notified when the
|
|
|
|
* write request succeeds or fails
|
2008-09-02 12:39:57 +02:00
|
|
|
*
|
|
|
|
* @throws NullPointerException if the specified message is {@code null}
|
2008-09-02 09:13:20 +02:00
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
ChannelFuture write(Object message);
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a message to this channel asynchronously. It has an additional
|
|
|
|
* parameter that allows a user to specify where to send the specified
|
|
|
|
* message instead of this channel's current remote address.
|
|
|
|
*
|
|
|
|
* @param message the message to write
|
2008-09-02 12:39:57 +02:00
|
|
|
* @param remoteAddress where to send the specified message.
|
|
|
|
* This method is identical to {@link #write(Object)}
|
|
|
|
* if {@code null} is specified here.
|
2008-09-02 09:13:20 +02:00
|
|
|
*
|
|
|
|
* @return the {@link ChannelFuture} which will be notified when the
|
|
|
|
* write request succeeds or fails
|
2008-09-02 12:39:57 +02:00
|
|
|
*
|
|
|
|
* @throws NullPointerException if the specified message is {@code null}
|
2008-09-02 09:13:20 +02:00
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
ChannelFuture write(Object message, SocketAddress remoteAddress);
|
|
|
|
|
2008-09-02 09:13:20 +02:00
|
|
|
/**
|
|
|
|
* Binds this channel to the specified local address asynchronously.
|
|
|
|
*
|
|
|
|
* @param localAddress where to bind
|
|
|
|
*
|
|
|
|
* @return the {@link ChannelFuture} which will be notified when the
|
|
|
|
* bind request succeeds or fails
|
2008-09-02 12:39:57 +02:00
|
|
|
*
|
|
|
|
* @throws NullPointerException if the specified address is {@code null}
|
2008-09-02 09:13:20 +02:00
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
ChannelFuture bind(SocketAddress localAddress);
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Connects this channel to the specified remote address asynchronously.
|
|
|
|
*
|
|
|
|
* @param remoteAddress where to connect
|
|
|
|
*
|
|
|
|
* @return the {@link ChannelFuture} which will be notified when the
|
|
|
|
* connection request succeeds or fails
|
2008-09-02 12:39:57 +02:00
|
|
|
*
|
|
|
|
* @throws NullPointerException if the specified address is {@code null}
|
2008-09-02 09:13:20 +02:00
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
ChannelFuture connect(SocketAddress remoteAddress);
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnects this channel from the current remote address asynchronously.
|
|
|
|
*
|
|
|
|
* @return the {@link ChannelFuture} which will be notified when the
|
|
|
|
* disconnection request succeeds or fails
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
ChannelFuture disconnect();
|
2008-09-02 09:13:20 +02:00
|
|
|
|
2008-10-02 06:40:46 +02:00
|
|
|
/**
|
|
|
|
* Unbinds this channel from the current local address asynchronously.
|
|
|
|
*
|
|
|
|
* @return the {@link ChannelFuture} which will be notified when the
|
|
|
|
* unbind request succeeds or fails
|
|
|
|
*/
|
|
|
|
ChannelFuture unbind();
|
|
|
|
|
2008-09-02 09:13:20 +02:00
|
|
|
/**
|
|
|
|
* Closes this channel asynchronously. If this channel is bound or
|
2008-11-26 10:21:00 +01:00
|
|
|
* connected, it will be disconnected and unbound first. Once a channel
|
|
|
|
* is closed, it can not be open again. Calling this method on a closed
|
|
|
|
* channel has no effect. Please note that this method always returns the
|
|
|
|
* same future instance.
|
2008-09-02 09:13:20 +02:00
|
|
|
*
|
|
|
|
* @return the {@link ChannelFuture} which will be notified when the
|
|
|
|
* close request succeeds or fails
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
ChannelFuture close();
|
|
|
|
|
2008-11-26 10:21:00 +01:00
|
|
|
/**
|
|
|
|
* Returns the {@link ChannelFuture} which will be notified when this
|
|
|
|
* channel is closed. This method always returns the same future instance.
|
|
|
|
*/
|
|
|
|
ChannelFuture getCloseFuture();
|
|
|
|
|
2008-09-02 09:13:20 +02:00
|
|
|
/**
|
|
|
|
* Returns the current {@code interestOps} of this channel.
|
|
|
|
*
|
|
|
|
* @return {@link #OP_NONE}, {@link #OP_READ}, {@link #OP_WRITE}, or
|
|
|
|
* {@link #OP_READ_WRITE}
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
int getInterestOps();
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns {@code true} if and only if the I/O thread will read a message
|
|
|
|
* from this channel. This method is a shortcut to the following code:
|
|
|
|
* <pre>
|
|
|
|
* return (getInterestOps() & OP_READ) != 0;
|
|
|
|
* </pre>
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
boolean isReadable();
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns {@code true} if and only if the I/O thread will perform the
|
|
|
|
* requested write operation immediately. Any write requests made when
|
|
|
|
* this method returns {@code false} are queued until the I/O thread is
|
|
|
|
* ready to process the queued write requests. This method is a shortcut
|
|
|
|
* to the following code:
|
|
|
|
* <pre>
|
|
|
|
* return (getInterestOps() & OP_WRITE) != 0;
|
|
|
|
* </pre>
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
boolean isWritable();
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the {@code interestOps} of this channel asynchronously.
|
|
|
|
*
|
|
|
|
* @param interestOps the new {@code interestOps}
|
|
|
|
*
|
|
|
|
* @return the {@link ChannelFuture} which will be notified when the
|
|
|
|
* {@code interestOps} change request succeeds or fails
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
ChannelFuture setInterestOps(int interestOps);
|
2008-09-02 09:13:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Suspends or resumes the read operation of the I/O thread asynchronously.
|
|
|
|
* This method is a shortcut to the following code:
|
|
|
|
* <pre>
|
|
|
|
* int interestOps = getInterestOps();
|
|
|
|
* if (readable) {
|
|
|
|
* setInterestOps(interestOps | OP_READ);
|
|
|
|
* } else {
|
|
|
|
* setInterestOps(interestOps & ~OP_READ);
|
|
|
|
* }
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* @param readable {@code true} to resume the read operation and
|
|
|
|
* {@code false} to suspend the read operation
|
|
|
|
*
|
|
|
|
* @return the {@link ChannelFuture} which will be notified when the
|
|
|
|
* {@code interestOps} change request succeeds or fails
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
ChannelFuture setReadable(boolean readable);
|
|
|
|
}
|