LimeWire Consolidated API

org.limewire.nio
Class AbstractNBSocket

java.lang.Object
  extended by java.net.Socket
      extended by org.limewire.nio.NBSocket
          extended by org.limewire.nio.AbstractNBSocket
All Implemented Interfaces:
NIOMultiplexor, ConnectObserver, IOErrorObserver, ReadObserver, ReadWriteObserver, Shutdownable, WriteObserver, ReadTimeout, SoTimeout
Direct Known Subclasses:
NIOSocket

public abstract class AbstractNBSocket
extends NBSocket
implements ConnectObserver, ReadWriteObserver, NIOMultiplexor, ReadTimeout, SoTimeout

Implements all common functionality that a non-blocking socket must contain. Specifically, AbstractNBSocket handles the multiplexing aspect of handing off reading, writing and connecting to other Observers (org.limewire.nio.observer).

Additionally, AbstractNBSocket traverses the chain of readers and writers to read leftover data and ensure remaining data is written.

AbstractNBSocket also exposes a common blocking input and output stream.


Constructor Summary
AbstractNBSocket()
           
 
Method Summary
 void close()
          Closes the socket & all streams, waking up any waiting locks.
 void connect(SocketAddress addr)
          Connects to addr with no timeout
 void connect(SocketAddress addr, int timeout)
          Connects to addr with the given timeout (in milliseconds)
 boolean connect(SocketAddress addr, int timeout, ConnectObserver observer)
          Connects to the specified address within the given timeout (in milliseconds).
protected abstract  InterestReadableByteChannel getBaseReadChannel()
          Retrieves the channel which should be used as the base channel for all reading operations.
protected abstract  InterestWritableByteChannel getBaseWriteChannel()
          Retrieves the channel which should be used as the base channel for all writing operations.
 InputStream getInputStream()
          Returns the InputStream from the NIOInputStream.
 OutputStream getOutputStream()
          Returns the OutputStream from the NIOOutputStream.
 long getReadTimeout()
          Gets the read timeout for this socket.
 void handleConnect(Socket s)
          Notification that a connect can occur.
 void handleIOException(IOException iox)
          Notification that an IOException occurred while processing a read, connect, or write.
 void handleRead()
          Notification that a read can occur.
 boolean handleWrite()
          Notification that a write can occur.
 void setInitialReader()
          Sets the initial reader value.
 void setInitialWriter()
          Sets the initial writer value.
 void setReadObserver(ChannelReadObserver newReader)
          Sets the new ReadObserver.
 void setShutdownObserver(Shutdownable observer)
          Sets the Shutdown observer.
 void setWriteObserver(ChannelWriter newWriter)
          Sets the new WriteObserver.
 void shutdown()
          Shuts down this socket & all its streams.
protected abstract  void shutdownImpl()
          Performs any operations required for shutting down this socket.
protected  void shutdownObservers()
          Shuts down all observers.
 
Methods inherited from class java.net.Socket
bind, getChannel, getInetAddress, getKeepAlive, getLocalAddress, getLocalPort, getLocalSocketAddress, getOOBInline, getPort, getReceiveBufferSize, getRemoteSocketAddress, getReuseAddress, getSendBufferSize, getSoLinger, getSoTimeout, getTcpNoDelay, getTrafficClass, isBound, isClosed, isConnected, isInputShutdown, isOutputShutdown, sendUrgentData, setKeepAlive, setOOBInline, setPerformancePreferences, setReceiveBufferSize, setReuseAddress, setSendBufferSize, setSocketImplFactory, setSoLinger, setSoTimeout, setTcpNoDelay, setTrafficClass, shutdownInput, shutdownOutput, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.limewire.nio.timeout.SoTimeout
getSoTimeout
 

Constructor Detail

AbstractNBSocket

public AbstractNBSocket()
Method Detail

getBaseReadChannel

protected abstract InterestReadableByteChannel getBaseReadChannel()
Retrieves the channel which should be used as the base channel for all reading operations.


getBaseWriteChannel

protected abstract InterestWritableByteChannel getBaseWriteChannel()
Retrieves the channel which should be used as the base channel for all writing operations.

If the base write channel is chained (that is, if there are multiple writing layers that will always be used) then this must return the top-most layer. That layer will be installed beneath the bottom layer that is set on the Socket. All layers except the last must implement ChannelWriter, so they can be iterated over in order to set the last writer.


shutdownImpl

protected abstract void shutdownImpl()
Performs any operations required for shutting down this socket. shutdownImpl method will only be called once per Socket.


setInitialReader

public final void setInitialReader()
Sets the initial reader value.


setInitialWriter

public final void setInitialWriter()
Sets the initial writer value.


setShutdownObserver

public final void setShutdownObserver(Shutdownable observer)
Sets the Shutdown observer. This observer is useful for when the Socket is created, but connect has not been called yet. This observer will be notified when the socket is shutdown.

Specified by:
setShutdownObserver in class NBSocket

setReadObserver

public final void setReadObserver(ChannelReadObserver newReader)
Sets the new ReadObserver.

The deepest ChannelReader in the chain first has its source set to the prior reader (assuming it implemented ReadableByteChannel) and a read is notified, in order to read any buffered data. The source is then set to the Socket's channel and interest in reading is turned on.

Specified by:
setReadObserver in interface NIOMultiplexor

setWriteObserver

public final void setWriteObserver(ChannelWriter newWriter)
Sets the new WriteObserver.

If a ThrottleWriter is one of the ChannelWriters, the attachment of the ThrottleWriter is set to be this.

The deepest ChannelWriter in the chain has its source set to be a new InterestWriteChannel, which will be used as the hub to receive and forward interest events from/to the channel.

If this is called while the existing WriteObserver still has data left to write, then an IllegalStateException is thrown.

Specified by:
setWriteObserver in interface NIOMultiplexor

handleConnect

public final void handleConnect(Socket s)
                         throws IOException
Notification that a connect can occur.

This passes it off on to the delegating connecter and then forgets the connecter for the duration of the connection.

Specified by:
handleConnect in interface ConnectObserver
Throws:
IOException

handleRead

public final void handleRead()
                      throws IOException
Notification that a read can occur. This passes it off to the delegating reader.

Specified by:
handleRead in interface ReadObserver
Throws:
IOException

handleWrite

public final boolean handleWrite()
                          throws IOException
Notification that a write can occur. This passes it off to the delegating writer.

Specified by:
handleWrite in interface WriteObserver
Returns:
true there is still data to be written, otherwise false.
Throws:
IOException

close

public final void close()
Closes the socket & all streams, waking up any waiting locks.

Overrides:
close in class Socket

connect

public final void connect(SocketAddress addr)
                   throws IOException
Connects to addr with no timeout

Overrides:
connect in class Socket
Throws:
IOException

connect

public final void connect(SocketAddress addr,
                          int timeout)
                   throws IOException
Connects to addr with the given timeout (in milliseconds)

Overrides:
connect in class Socket
Throws:
IOException

connect

public boolean connect(SocketAddress addr,
                       int timeout,
                       ConnectObserver observer)
Connects to the specified address within the given timeout (in milliseconds). The given ConnectObserver will be notified of success or failure. In the event of success, observer.handleConnect is called. In a failure, observer.shutdown is called. observer.handleIOException is never called.

Returns true if this was able to connect immediately. The observer is still notified about the success even it it was immediate.

Specified by:
connect in class NBSocket

getInputStream

public final InputStream getInputStream()
                                 throws IOException
Returns the InputStream from the NIOInputStream. Internally, this is a blocking Pipe from the non-blocking SocketChannel.

Overrides:
getInputStream in class Socket
Throws:
IOException

getOutputStream

public final OutputStream getOutputStream()
                                   throws IOException
Returns the OutputStream from the NIOOutputStream. Internally, this is a blocking Pipe from the non-blocking SocketChannel.

Overrides:
getOutputStream in class Socket
Throws:
IOException

getReadTimeout

public long getReadTimeout()
Gets the read timeout for this socket.

Specified by:
getReadTimeout in interface ReadTimeout
Returns:
0 implies the timeout option is disabled (i.e.. timeout of infinity). A negative return implies an error.

handleIOException

public final void handleIOException(IOException iox)
Notification that an IOException occurred while processing a read, connect, or write.

Specified by:
handleIOException in interface IOErrorObserver

shutdown

public final void shutdown()
Shuts down this socket & all its streams.

Specified by:
shutdown in interface Shutdownable

shutdownObservers

protected void shutdownObservers()
Shuts down all observers.


LimeWire Consolidated API

Copyright © 2009. All Rights Reserved.