gov.lbl.dsd.sea.nio
Class AgentEventHandler
java.lang.Object
gov.lbl.dsd.sea.EventHandler
gov.lbl.dsd.sea.nio.AgentEventHandler
- Direct Known Subclasses:
- EchoServer, HttpClient, PingPong, PingPongBench, StreamingBench, TimeServer
- public abstract class AgentEventHandler
- extends EventHandler
Abstract base class simplifying the implementation of event
handlers sending requests to an agent, and receiving responses from the agent.
Override protected onXYZ
event handling methods for application specific behaviour.
For example, a typical non-blocking agent event handler accumulates partial reads along the following lines:
public class MyAgentEventHandler extends AgentEventHandler {
// simple variable msg size protocol: MESSAGE = [payLoadLength(4 bytes) payLoad]
private static final Charset CHARSET = Charset.forName("UTF-8");
protected void onAccepted(ChannelResponse.Accepted rsp) {
// prepare read buffer and register READ interest
rsp.getKey().attach(new gov.lbl.dsd.sea.nio.util.ArrayByteList());
rsp.getAgent().enqueue(
new ChannelRequest.Register(this.getStage(), rsp.getKey()
.channel(), SelectionKey.OP_READ));
}
protected void onRead(ChannelResponse.Read rsp) {
ArrayByteList readBuffer = (ArrayByteList) rsp.getKey().attachment();
readBuffer.add(rsp.getBuffer());
rsp.getAgent().getReadBufferPool().put(rsp.getBuffer()); // recycle buffer
while (readBuffer.size() >= 4) {
// message header containing payload length has arrived
int payloadLength = readBuffer.asByteBuffer().getInt(0);
if (4 + payloadLength > readBuffer.size()) {
break; // payload not yet fully received, wait for more data
}
else {
// we have received the entire variable length payload
String payload = readBuffer.toString(4, 4 + payloadLength, CHARSET);
readBuffer.remove(0, 4 + payloadLength); // remove header and payload
// do something useful with payload, here we just print it
System.out.println("payload=" + payload);
}
}
}
// A more efficient but less readable/understandable alternative:
protected void onRead(ChannelResponse.Read rsp) {
ArrayByteList readBuffer = (ArrayByteList) rsp.getKey().attachment();
readBuffer.add(rsp.getBuffer());
rsp.getAgent().getReadBufferPool().put(rsp.getBuffer()); // recycle buffer
int i = 0;
while (i + 4 <= readBuffer.size()) {
// message header containing payload length has arrived
int payloadLength = readBuffer.asByteBuffer().getInt(i);
if (i + 4 + payloadLength > readBuffer.size()) {
// payload not yet fully received - wait for more data
break;
}
else {
i += 4;
// we have received the entire variable length payload
String payload = readBuffer.toString(i, i + payloadLength, CHARSET);
i += payLoadLength;
// do something useful with payload, here we just print it
System.out.println("payload=" + payload);
}
}
// remove fully processed headers and payloads, if any
readBuffer.remove(0, i);
}
...
}
- Version:
- $Revision: 1.12 $, $Date: 2004/08/04 23:24:56 $
Method Summary |
void |
handle(Object event)
Called when an event has been received |
handle
public void handle(Object event)
- Called when an event has been received
- Specified by:
handle
in class EventHandler