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 skeletalChannelimplementation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classAbstractChannel.ChannelCloseFuture
-
Field Summary
Fields Modifier and Type Field Description (package private) static java.util.concurrent.ConcurrentMap<java.lang.Integer,Channel>allChannelsprivate java.lang.Objectattachmentprivate AbstractChannel.ChannelCloseFuturecloseFutureprivate ChannelFactoryfactoryprivate java.lang.Integeridprivate intinterestOpsprivate Channelparentprivate ChannelPipelinepipelineprivate static java.util.Randomrandomprivate java.lang.StringstrValprivate booleanstrValConnectedCache for the string representation of this channelprivate ChannelFuturesucceededFutureprivate intunwritableprivate 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 protectedAbstractChannel(java.lang.Integer id, Channel parent, ChannelFactory factory, ChannelPipeline pipeline, ChannelSink sink)(Internal use only) Creates a new temporary instance with the specified ID.protectedAbstractChannel(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.IntegerallocateId(Channel channel)ChannelFuturebind(java.net.SocketAddress localAddress)Binds this channel to the specified local address asynchronously.private voidclearUserDefinedWritability(int index)ChannelFutureclose()Closes this channel asynchronously.intcompareTo(Channel o)Compares the ID of the two channels.ChannelFutureconnect(java.net.SocketAddress remoteAddress)Connects this channel to the specified remote address asynchronously.ChannelFuturedisconnect()Disconnects this channel from the current remote address asynchronously.booleanequals(java.lang.Object o)Returnstrueif and only if the specified object is identical with this channel (i.e:this == o).java.lang.ObjectgetAttachment()ChannelFuturegetCloseFuture()Returns theChannelFuturewhich will be notified when this channel is closed.ChannelFactorygetFactory()Returns theChannelFactorywhich created this channel.java.lang.IntegergetId()Returns the unique integer ID of this channel.private java.lang.StringgetIdString()intgetInterestOps()Returns the currentinterestOpsof this channel.protected intgetInternalInterestOps()ChannelgetParent()Returns the parent of this channel.ChannelPipelinegetPipeline()Returns theChannelPipelinewhich handlesChannelEvents associated with this channel.protected ChannelFuturegetSucceededFuture()Returns the cachedSucceededChannelFutureinstance.protected ChannelFuturegetUnsupportedOperationFuture()Returns theFailedChannelFuturewhose cause is anUnsupportedOperationException.booleangetUserDefinedWritability(int index)Returnstrueif and only if the user-defined writability flag at the specified index is set totrue.inthashCode()Returns the ID of this channel.booleanisOpen()Returnstrueif and only if this channel is open.booleanisReadable()Returnstrueif and only if the I/O thread will read a message from this channel.booleanisWritable()Returnstrueif and only if the I/O thread will perform the requested write operation immediately.voidsetAttachment(java.lang.Object attachment)Attaches an object to thisChannelto store a stateful informationprotected booleansetClosed()Marks this channel as closed.ChannelFuturesetInterestOps(int interestOps)Changes theinterestOpsof this channel asynchronously.protected voidsetInternalInterestOps(int interestOps)Sets theinterestOpsproperty of this channel immediately.ChannelFuturesetReadable(boolean readable)Suspends or resumes the read operation of the I/O thread asynchronously.protected booleansetUnwritable()private voidsetUserDefinedWritability(int index)voidsetUserDefinedWritability(int index, boolean writable)Sets a user-defined writability flag at the specified index.protected booleansetWritable()java.lang.StringtoString()Returns theStringrepresentation of this channel.ChannelFutureunbind()Unbinds this channel from the current local address asynchronously.private static intwritabilityMask(int index)ChannelFuturewrite(java.lang.Object message)Sends a message to this channel asynchronously.ChannelFuturewrite(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.nullif 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.nullif 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:ChannelReturns the unique integer ID of this channel.
-
getParent
public Channel getParent()
Description copied from interface:ChannelReturns the parent of this channel.
-
getFactory
public ChannelFactory getFactory()
Description copied from interface:ChannelReturns theChannelFactorywhich created this channel.- Specified by:
getFactoryin interfaceChannel
-
getPipeline
public ChannelPipeline getPipeline()
Description copied from interface:ChannelReturns theChannelPipelinewhich handlesChannelEvents associated with this channel.- Specified by:
getPipelinein interfaceChannel
-
getSucceededFuture
protected ChannelFuture getSucceededFuture()
Returns the cachedSucceededChannelFutureinstance.
-
getUnsupportedOperationFuture
protected ChannelFuture getUnsupportedOperationFuture()
Returns theFailedChannelFuturewhose cause is anUnsupportedOperationException.
-
hashCode
public final int hashCode()
Returns the ID of this channel.- Overrides:
hashCodein classjava.lang.Object
-
equals
public final boolean equals(java.lang.Object o)
Returnstrueif and only if the specified object is identical with this channel (i.e:this == o).- Overrides:
equalsin classjava.lang.Object
-
compareTo
public final int compareTo(Channel o)
Compares the ID of the two channels.- Specified by:
compareToin interfacejava.lang.Comparable<Channel>
-
isOpen
public boolean isOpen()
Description copied from interface:ChannelReturnstrueif 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:
trueif and only if this channel was not marked as closed yet
-
bind
public ChannelFuture bind(java.net.SocketAddress localAddress)
Description copied from interface:ChannelBinds this channel to the specified local address asynchronously.- Specified by:
bindin interfaceChannel- Parameters:
localAddress- where to bind- Returns:
- the
ChannelFuturewhich will be notified when the bind request succeeds or fails
-
unbind
public ChannelFuture unbind()
Description copied from interface:ChannelUnbinds this channel from the current local address asynchronously.- Specified by:
unbindin interfaceChannel- Returns:
- the
ChannelFuturewhich will be notified when the unbind request succeeds or fails
-
close
public ChannelFuture close()
Description copied from interface:ChannelCloses 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:
closein interfaceChannel- Returns:
- the
ChannelFuturewhich will be notified when the close request succeeds or fails
-
getCloseFuture
public ChannelFuture getCloseFuture()
Description copied from interface:ChannelReturns theChannelFuturewhich will be notified when this channel is closed. This method always returns the same future instance.- Specified by:
getCloseFuturein interfaceChannel
-
connect
public ChannelFuture connect(java.net.SocketAddress remoteAddress)
Description copied from interface:ChannelConnects this channel to the specified remote address asynchronously.- Specified by:
connectin interfaceChannel- Parameters:
remoteAddress- where to connect- Returns:
- the
ChannelFuturewhich will be notified when the connection request succeeds or fails
-
disconnect
public ChannelFuture disconnect()
Description copied from interface:ChannelDisconnects this channel from the current remote address asynchronously.- Specified by:
disconnectin interfaceChannel- Returns:
- the
ChannelFuturewhich will be notified when the disconnection request succeeds or fails
-
getInterestOps
public int getInterestOps()
Description copied from interface:ChannelReturns the currentinterestOpsof this channel.- Specified by:
getInterestOpsin 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:ChannelChanges theinterestOpsof this channel asynchronously.- Specified by:
setInterestOpsin interfaceChannel- Parameters:
interestOps- the newinterestOps- Returns:
- the
ChannelFuturewhich will be notified when theinterestOpschange request succeeds or fails
-
getInternalInterestOps
protected int getInternalInterestOps()
-
setInternalInterestOps
protected void setInternalInterestOps(int interestOps)
Sets theinterestOpsproperty 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:ChannelReturnstrueif 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:
isReadablein interfaceChannel
-
isWritable
public boolean isWritable()
Description copied from interface:ChannelReturnstrueif and only if the I/O thread will perform the requested write operation immediately. Any write requests made when this method returnsfalseare 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:
isWritablein interfaceChannel
-
getUserDefinedWritability
public final boolean getUserDefinedWritability(int index)
Description copied from interface:ChannelReturnstrueif and only if the user-defined writability flag at the specified index is set totrue.- Specified by:
getUserDefinedWritabilityin interfaceChannel
-
setUserDefinedWritability
public final void setUserDefinedWritability(int index, boolean writable)Description copied from interface:ChannelSets a user-defined writability flag at the specified index.- Specified by:
setUserDefinedWritabilityin 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:ChannelSuspends 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:
setReadablein interfaceChannel- Parameters:
readable-trueto resume the read operation andfalseto suspend the read operation- Returns:
- the
ChannelFuturewhich will be notified when theinterestOpschange request succeeds or fails
-
write
public ChannelFuture write(java.lang.Object message)
Description copied from interface:ChannelSends 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 withNotYetConnectedExceptionand an'exceptionCaught'event will be triggered.- Specified by:
writein interfaceChannel- Parameters:
message- the message to write- Returns:
- the
ChannelFuturewhich 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:ChannelSends 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 withNotYetConnectedExceptionand an'exceptionCaught'event will be triggered.- Specified by:
writein interfaceChannel- Parameters:
message- the message to writeremoteAddress- where to send the specified message. This method is identical toChannel.write(Object)ifnullis specified here.- Returns:
- the
ChannelFuturewhich will be notified when the write request succeeds or fails
-
getAttachment
public java.lang.Object getAttachment()
Description copied from interface:Channel- Specified by:
getAttachmentin interfaceChannel- Returns:
nullif no object was attached ornullwas attached
-
setAttachment
public void setAttachment(java.lang.Object attachment)
Description copied from interface:ChannelAttaches an object to thisChannelto store a stateful information- Specified by:
setAttachmentin interfaceChannel
-
toString
public java.lang.String toString()
Returns theStringrepresentation of this channel. The returned string contains the ID, local address, and remote address of this channel for easier identification.- Overrides:
toStringin classjava.lang.Object
-
getIdString
private java.lang.String getIdString()
-
-