Package org.jboss.netty.channel
Class AbstractChannel
- java.lang.Object
-
- org.jboss.netty.channel.AbstractChannel
-
- Direct Known Subclasses:
AbstractNioChannel
,AbstractOioChannel
,AbstractServerChannel
,DefaultLocalChannel
,EmbeddedChannel
,HttpTunnelingClientSocketChannel
public abstract class AbstractChannel extends java.lang.Object implements Channel
A skeletalChannel
implementation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
AbstractChannel.ChannelCloseFuture
-
Field Summary
Fields Modifier and Type Field Description (package private) static java.util.concurrent.ConcurrentMap<java.lang.Integer,Channel>
allChannels
private java.lang.Object
attachment
private AbstractChannel.ChannelCloseFuture
closeFuture
private ChannelFactory
factory
private java.lang.Integer
id
private int
interestOps
private Channel
parent
private ChannelPipeline
pipeline
private static java.util.Random
random
private java.lang.String
strVal
private boolean
strValConnected
Cache for the string representation of this channelprivate ChannelFuture
succeededFuture
private int
unwritable
private static java.util.concurrent.atomic.AtomicIntegerFieldUpdater<AbstractChannel>
UNWRITABLE_UPDATER
-
Fields inherited from interface org.jboss.netty.channel.Channel
OP_NONE, OP_READ, OP_READ_WRITE, OP_WRITE
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractChannel(java.lang.Integer id, Channel parent, ChannelFactory factory, ChannelPipeline pipeline, ChannelSink sink)
(Internal use only) Creates a new temporary instance with the specified ID.protected
AbstractChannel(Channel parent, ChannelFactory factory, ChannelPipeline pipeline, ChannelSink sink)
Creates a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static java.lang.Integer
allocateId(Channel channel)
ChannelFuture
bind(java.net.SocketAddress localAddress)
Binds this channel to the specified local address asynchronously.private void
clearUserDefinedWritability(int index)
ChannelFuture
close()
Closes this channel asynchronously.int
compareTo(Channel o)
Compares the ID of the two channels.ChannelFuture
connect(java.net.SocketAddress remoteAddress)
Connects this channel to the specified remote address asynchronously.ChannelFuture
disconnect()
Disconnects this channel from the current remote address asynchronously.boolean
equals(java.lang.Object o)
Returnstrue
if and only if the specified object is identical with this channel (i.e:this == o
).java.lang.Object
getAttachment()
ChannelFuture
getCloseFuture()
Returns theChannelFuture
which will be notified when this channel is closed.ChannelFactory
getFactory()
Returns theChannelFactory
which created this channel.java.lang.Integer
getId()
Returns the unique integer ID of this channel.private java.lang.String
getIdString()
int
getInterestOps()
Returns the currentinterestOps
of this channel.protected int
getInternalInterestOps()
Channel
getParent()
Returns the parent of this channel.ChannelPipeline
getPipeline()
Returns theChannelPipeline
which handlesChannelEvent
s associated with this channel.protected ChannelFuture
getSucceededFuture()
Returns the cachedSucceededChannelFuture
instance.protected ChannelFuture
getUnsupportedOperationFuture()
Returns theFailedChannelFuture
whose cause is anUnsupportedOperationException
.boolean
getUserDefinedWritability(int index)
Returnstrue
if and only if the user-defined writability flag at the specified index is set totrue
.int
hashCode()
Returns the ID of this channel.boolean
isOpen()
Returnstrue
if and only if this channel is open.boolean
isReadable()
Returnstrue
if and only if the I/O thread will read a message from this channel.boolean
isWritable()
Returnstrue
if and only if the I/O thread will perform the requested write operation immediately.void
setAttachment(java.lang.Object attachment)
Attaches an object to thisChannel
to store a stateful informationprotected boolean
setClosed()
Marks this channel as closed.ChannelFuture
setInterestOps(int interestOps)
Changes theinterestOps
of this channel asynchronously.protected void
setInternalInterestOps(int interestOps)
Sets theinterestOps
property of this channel immediately.ChannelFuture
setReadable(boolean readable)
Suspends or resumes the read operation of the I/O thread asynchronously.protected boolean
setUnwritable()
private void
setUserDefinedWritability(int index)
void
setUserDefinedWritability(int index, boolean writable)
Sets a user-defined writability flag at the specified index.protected boolean
setWritable()
java.lang.String
toString()
Returns theString
representation of this channel.ChannelFuture
unbind()
Unbinds this channel from the current local address asynchronously.private static int
writabilityMask(int index)
ChannelFuture
write(java.lang.Object message)
Sends a message to this channel asynchronously.ChannelFuture
write(java.lang.Object message, java.net.SocketAddress remoteAddress)
Sends a message to this channel asynchronously.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.jboss.netty.channel.Channel
getConfig, getLocalAddress, getRemoteAddress, isBound, isConnected
-
-
-
-
Field Detail
-
allChannels
static final java.util.concurrent.ConcurrentMap<java.lang.Integer,Channel> allChannels
-
random
private static final java.util.Random random
-
id
private final java.lang.Integer id
-
parent
private final Channel parent
-
factory
private final ChannelFactory factory
-
pipeline
private final ChannelPipeline pipeline
-
succeededFuture
private final ChannelFuture succeededFuture
-
closeFuture
private final AbstractChannel.ChannelCloseFuture closeFuture
-
interestOps
private volatile int interestOps
-
strValConnected
private boolean strValConnected
Cache for the string representation of this channel
-
strVal
private java.lang.String strVal
-
attachment
private volatile java.lang.Object attachment
-
UNWRITABLE_UPDATER
private static final java.util.concurrent.atomic.AtomicIntegerFieldUpdater<AbstractChannel> UNWRITABLE_UPDATER
-
unwritable
private volatile int unwritable
-
-
Constructor Detail
-
AbstractChannel
protected AbstractChannel(Channel parent, ChannelFactory factory, ChannelPipeline pipeline, ChannelSink sink)
Creates a new instance.- Parameters:
parent
- the parent of this channel.null
if there's no parent.factory
- the factory which created this channelpipeline
- the pipeline which is going to be attached to this channelsink
- the sink which will receive downstream events from the pipeline and send upstream events to the pipeline
-
AbstractChannel
protected AbstractChannel(java.lang.Integer id, Channel parent, ChannelFactory factory, ChannelPipeline pipeline, ChannelSink sink)
(Internal use only) Creates a new temporary instance with the specified ID.- Parameters:
parent
- the parent of this channel.null
if there's no parent.factory
- the factory which created this channelpipeline
- the pipeline which is going to be attached to this channelsink
- the sink which will receive downstream events from the pipeline and send upstream events to the pipeline
-
-
Method Detail
-
allocateId
private static java.lang.Integer allocateId(Channel channel)
-
getId
public final java.lang.Integer getId()
Description copied from interface:Channel
Returns the unique integer ID of this channel.
-
getParent
public Channel getParent()
Description copied from interface:Channel
Returns the parent of this channel.
-
getFactory
public ChannelFactory getFactory()
Description copied from interface:Channel
Returns theChannelFactory
which created this channel.- Specified by:
getFactory
in interfaceChannel
-
getPipeline
public ChannelPipeline getPipeline()
Description copied from interface:Channel
Returns theChannelPipeline
which handlesChannelEvent
s associated with this channel.- Specified by:
getPipeline
in interfaceChannel
-
getSucceededFuture
protected ChannelFuture getSucceededFuture()
Returns the cachedSucceededChannelFuture
instance.
-
getUnsupportedOperationFuture
protected ChannelFuture getUnsupportedOperationFuture()
Returns theFailedChannelFuture
whose cause is anUnsupportedOperationException
.
-
hashCode
public final int hashCode()
Returns the ID of this channel.- Overrides:
hashCode
in classjava.lang.Object
-
equals
public final boolean equals(java.lang.Object o)
Returnstrue
if and only if the specified object is identical with this channel (i.e:this == o
).- Overrides:
equals
in classjava.lang.Object
-
compareTo
public final int compareTo(Channel o)
Compares the ID of the two channels.- Specified by:
compareTo
in interfacejava.lang.Comparable<Channel>
-
isOpen
public boolean isOpen()
Description copied from interface:Channel
Returnstrue
if and only if this channel is open.
-
setClosed
protected boolean setClosed()
Marks this channel as closed. This method is intended to be called by an internal component - please do not call it unless you know what you are doing.- Returns:
true
if and only if this channel was not marked as closed yet
-
bind
public ChannelFuture bind(java.net.SocketAddress localAddress)
Description copied from interface:Channel
Binds this channel to the specified local address asynchronously.- Specified by:
bind
in interfaceChannel
- Parameters:
localAddress
- where to bind- Returns:
- the
ChannelFuture
which will be notified when the bind request succeeds or fails
-
unbind
public ChannelFuture unbind()
Description copied from interface:Channel
Unbinds this channel from the current local address asynchronously.- Specified by:
unbind
in interfaceChannel
- Returns:
- the
ChannelFuture
which will be notified when the unbind request succeeds or fails
-
close
public ChannelFuture close()
Description copied from interface:Channel
Closes this channel asynchronously. If this channel is bound or 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.- Specified by:
close
in interfaceChannel
- Returns:
- the
ChannelFuture
which will be notified when the close request succeeds or fails
-
getCloseFuture
public ChannelFuture getCloseFuture()
Description copied from interface:Channel
Returns theChannelFuture
which will be notified when this channel is closed. This method always returns the same future instance.- Specified by:
getCloseFuture
in interfaceChannel
-
connect
public ChannelFuture connect(java.net.SocketAddress remoteAddress)
Description copied from interface:Channel
Connects this channel to the specified remote address asynchronously.- Specified by:
connect
in interfaceChannel
- Parameters:
remoteAddress
- where to connect- Returns:
- the
ChannelFuture
which will be notified when the connection request succeeds or fails
-
disconnect
public ChannelFuture disconnect()
Description copied from interface:Channel
Disconnects this channel from the current remote address asynchronously.- Specified by:
disconnect
in interfaceChannel
- Returns:
- the
ChannelFuture
which will be notified when the disconnection request succeeds or fails
-
getInterestOps
public int getInterestOps()
Description copied from interface:Channel
Returns the currentinterestOps
of this channel.- Specified by:
getInterestOps
in interfaceChannel
- Returns:
Channel.OP_NONE
,Channel.OP_READ
,Channel.OP_WRITE
, orChannel.OP_READ_WRITE
-
setInterestOps
public ChannelFuture setInterestOps(int interestOps)
Description copied from interface:Channel
Changes theinterestOps
of this channel asynchronously.- Specified by:
setInterestOps
in interfaceChannel
- Parameters:
interestOps
- the newinterestOps
- Returns:
- the
ChannelFuture
which will be notified when theinterestOps
change request succeeds or fails
-
getInternalInterestOps
protected int getInternalInterestOps()
-
setInternalInterestOps
protected void setInternalInterestOps(int interestOps)
Sets theinterestOps
property of this channel immediately. This method is intended to be called by an internal component - please do not call it unless you know what you are doing.
-
isReadable
public boolean isReadable()
Description copied from interface:Channel
Returnstrue
if and only if the I/O thread will read a message from this channel. This method is a shortcut to the following code:return (getInterestOps() & OP_READ) != 0;
- Specified by:
isReadable
in interfaceChannel
-
isWritable
public boolean isWritable()
Description copied from interface:Channel
Returnstrue
if and only if the I/O thread will perform the requested write operation immediately. Any write requests made when this method returnsfalse
are queued until the I/O thread is ready to process the queued write requests. This method is a shortcut to the following code:return (getInterestOps() & OP_WRITE) == 0;
- Specified by:
isWritable
in interfaceChannel
-
getUserDefinedWritability
public final boolean getUserDefinedWritability(int index)
Description copied from interface:Channel
Returnstrue
if and only if the user-defined writability flag at the specified index is set totrue
.- Specified by:
getUserDefinedWritability
in interfaceChannel
-
setUserDefinedWritability
public final void setUserDefinedWritability(int index, boolean writable)
Description copied from interface:Channel
Sets a user-defined writability flag at the specified index.- Specified by:
setUserDefinedWritability
in interfaceChannel
-
setUserDefinedWritability
private void setUserDefinedWritability(int index)
-
clearUserDefinedWritability
private void clearUserDefinedWritability(int index)
-
writabilityMask
private static int writabilityMask(int index)
-
setWritable
protected boolean setWritable()
-
setUnwritable
protected boolean setUnwritable()
-
setReadable
public ChannelFuture setReadable(boolean readable)
Description copied from interface:Channel
Suspends or resumes the read operation of the I/O thread asynchronously. This method is a shortcut to the following code:int interestOps = getInterestOps(); if (readable) { setInterestOps(interestOps | OP_READ); } else { setInterestOps(interestOps & ~OP_READ); }
- Specified by:
setReadable
in interfaceChannel
- Parameters:
readable
-true
to resume the read operation andfalse
to suspend the read operation- Returns:
- the
ChannelFuture
which will be notified when theinterestOps
change request succeeds or fails
-
write
public ChannelFuture write(java.lang.Object message)
Description copied from interface:Channel
Sends a message to this channel asynchronously. If this channel was created by a connectionless transport (e.g.DatagramChannel
) and is not connected yet, you have to callChannel.write(Object, SocketAddress)
instead. Otherwise, the write request will fail withNotYetConnectedException
and an'exceptionCaught'
event will be triggered.- Specified by:
write
in interfaceChannel
- Parameters:
message
- the message to write- Returns:
- the
ChannelFuture
which will be notified when the write request succeeds or fails
-
write
public ChannelFuture write(java.lang.Object message, java.net.SocketAddress remoteAddress)
Description copied from interface:Channel
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. If this channel was created by a connectionless transport (e.g.DatagramChannel
) and is not connected yet, you must specify non-null address. Otherwise, the write request will fail withNotYetConnectedException
and an'exceptionCaught'
event will be triggered.- Specified by:
write
in interfaceChannel
- Parameters:
message
- the message to writeremoteAddress
- where to send the specified message. This method is identical toChannel.write(Object)
ifnull
is specified here.- Returns:
- the
ChannelFuture
which will be notified when the write request succeeds or fails
-
getAttachment
public java.lang.Object getAttachment()
Description copied from interface:Channel
- Specified by:
getAttachment
in interfaceChannel
- Returns:
null
if no object was attached ornull
was attached
-
setAttachment
public void setAttachment(java.lang.Object attachment)
Description copied from interface:Channel
Attaches an object to thisChannel
to store a stateful information- Specified by:
setAttachment
in interfaceChannel
-
toString
public java.lang.String toString()
Returns theString
representation of this channel. The returned string contains the ID, local address, and remote address of this channel for easier identification.- Overrides:
toString
in classjava.lang.Object
-
getIdString
private java.lang.String getIdString()
-
-