Sea 0.2.1

Class AsyncPeerHandler

  extended bygov.lbl.dsd.sea.EventHandler
      extended bygov.lbl.dsd.sea.nio.AsyncPeerHandler
Direct Known Subclasses:
AsyncEchoServer, AsyncPingPong

public abstract class AsyncPeerHandler
extends EventHandler

Abstract base class simplifying the implementation of asynchronous event handlers sending requests to a peer, and receiving responses from the peer; Assumes an auto-closing peer.

Override protected onXYZ event handling methods for application specific behaviour.

For example, a typical non-blocking peer event handler accumulates partial reads along the following lines:

public class MyPeerHandler extends PeerEventHandler {
        private static final Charset CHARSET = Charset.forName("UTF-8");

        public void onAccepted(ChannelResponse.Accepted rsp) {
                rsp.getKey().attach(new gov.lbl.dsd.sea.nio.util.ByteArrayList());
                                new ChannelRequest.Register(this.getStage(), rsp.getKey()
                                                .channel(), SelectionKey.OP_READ));

        public void onRead(ChannelResponse.Read rsp) {
                ByteArrayList bytesRead = (ByteArrayList) rsp.getKey().attachment();
                rsp.getPeer().getReadBufferPool().put(rsp.getBuffer()); // recycle buffer
                if (bytesRead.size() >= 4) {
                        // message header containing payload length has arrived
                        int payloadLength = bytesRead.getInt(0);
                        if (bytesRead.size() >= 4 + payloadLength) {
                                // we have received the entire variable length payload
                                String payload = bytesRead.getString(4, bytesRead.size(), CHARSET);

                                // do something useful with payload


$Revision: 1.18 $, $Date: 2004/06/01 20:47:20 $

Method Summary
 void handle(Object event)
          Called when an event has been received
Methods inherited from class gov.lbl.dsd.sea.EventHandler
destroy, init
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Method Detail


public void handle(Object event)
Called when an event has been received

Specified by:
handle in class EventHandler

Sea 0.2.1