|
Sea 0.2.1 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object gov.lbl.dsd.sea.nio.Peer
Efficient and scalable NIO based non-blocking single-threaded network peer that can be used both as a client and a server, both for TCP and UDP transports; Can handle large amounts of concurrent client and server connections in a single thread; Automatically deals with the subtle grunt work of life-cycle issues, threading, I/O multiplexing and NIO gotchas; This is the main work horse of this package.
General usage pattern:
start()
or by enqueuing a
AdminRequest.Start
gov.lbl.dsd.sea.nio.event
package via enqueue(request)
onto the peer. The peer will process the requests and enqueue responses to
the requests onto the asynchronous observer stage of the given network
channel (connection), for example an AsyncPeerHandler
's stage.stop()
or by enqueuing a
AdminRequest.Stop
See the demo client and servers in the gov.lbl.dsd.sea.nio.demo
package for simple and complex example usage.
For a nice introduction to java networking, see here and there and also there .
Constructor Summary | |
Peer()
Constructs a new peer; the peer is not yet started. |
Method Summary | |
Peer |
addConnectAddress(Stage observer,
InetSocketAddress address)
Tells the peer to start a TCP client connection to the given remote address. |
Peer |
addListenPort(Stage observer,
int port)
Tells the peer to start listening as a server for TCP connections on the given port. |
void |
enqueue(Object event)
Hand a request event from the gov.lbl.dsd.sea.nio.event package
to the peer; the request will be processed the next time the peer thread
comes around the select loop. |
boolean |
getAutoClose()
Returns the channel closing policy. |
BufferPool |
getReadBufferPool()
Returns the buffer pool to be used on reading from the network. |
void |
setAutoClose(boolean autoClose)
Sets the channel closing policy to be applied when IOExceptions are thrown. |
void |
setFailFast(boolean failFast)
Sets the policy to be applied when IOExceptions are thrown. |
void |
setReadBufferPool(BufferPool readBufferPool)
Sets the buffer pool to be used on reading from the network. |
void |
setSocketOptions(SocketOpts options)
Set the socket options to be used for newly accepted connections as well as client connections (on OP_ACCEPT and OP_CONNECT). |
void |
start()
The main selector loop; Runs the peer selector in the current thread. |
void |
stop()
Cleanly shut the peer down, releasing acquired resources. |
String |
toDebugString()
Returns a summary string representation of the receiver. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public Peer()
Method Detail |
public Peer addListenPort(Stage observer, int port) throws IOException
observer
- the stage onto which responses to the new server channel
should be enqueued (may be null)port
- the port to listen on
IOException
public Peer addConnectAddress(Stage observer, InetSocketAddress address) throws IOException
observer
- the stage onto which responses to the new client channel
should be enqueued (may be null)address
- the address to connect to
IOException
public void setFailFast(boolean failFast)
true
throws an exception in the peer thread, whereas
false
logs and forwards the exception to the observer (if
possible), in the hope that we can continue functioning, possibly after
corrective actions taken by the observer (such as sending a
ChannelRequest.Close).
failFast
- the policiy to usepublic void setAutoClose(boolean autoClose)
When autoClose==true
the peer automatically closes a
channel whenever an IOException or EOS (end-of-stream) is encountered,
and enqueues a ChannelResponse.Closed
response to the observer (subject to the failFast mode).
When autoClose==false
the peer does not close the channel,
and leaves it up to the user to appropriately react to the exception
(e.g. by issueing a
ChannelRequest.Close
).
In ANY case, a ChannelResponse with the IOException is first posted to
the observer (e.g. a
ChannelResponse.Closed
or
ChannelResponse.Write
or
ChannelResponse.Read
or
ChannelResponse.Connected
or
ChannelResponse.Registered
). If
autoClose==true
this is followed by a by a
ChannelResponse.Closed
response
containing the very same exception (unless closing throws yet another
exception). (Again, all subject to the failFast mode).
Most applications using autoClose==true
can therefore
ignore responses containing exceptions, unless it is a
ChannelResponse.Closed
.
autoClose
- the policiy to usepublic boolean getAutoClose()
public void setReadBufferPool(BufferPool readBufferPool)
readBufferPool
- the pool to use.public BufferPool getReadBufferPool()
public void setSocketOptions(SocketOpts options)
options
- the options to be set for the socket.public void enqueue(Object event)
gov.lbl.dsd.sea.nio.event
package
to the peer; the request will be processed the next time the peer thread
comes around the select loop.
event
- the event to hand to the peer.public void start()
public void stop()
public String toDebugString()
|
Sea 0.2.1 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |