puReadFilter.addProtocolFinder(new SimpleProtocolFinder(protocolId));
puReadFilter.addProtocolHandler(new SimpleProtocolHandler(protocolId));
where SimpleProtocolFinder - is the simple ProtocolFinder implementation, which checks incoming request to start for specific bytes
(passed in the constructor).
SimpleProtocolHandler - is simple protocol specific handler implementation. It processes recognized application layer protocol.
So, basically for each application layer protocol, you'll need to implement ProtocolFinder and ProtocolHandler. Where ProtocolFinder will have logic for application layer protocol recognition, and ProtocolHandler - logic for application layer protocol processing.
To see example - please look at unit tests of grizzly-portunif module: com.sun.grizzly.portunif.PUBasicTest
Thanks.
Alexey.
public class SimpleProtocolFinder implements ProtocolFinder {
private String protocolId;
private byte[]
protocolIdBytes;
public SimpleProtocolFinder(String protocolId) {
this.protocolId = protocolId;
protocolIdBytes = protocolId.getBytes();
}
public String find(Context context, PUProtocolRequest protocolRequest) throws IOException {
ByteBuffer buffer = protocolRequest.getByteBuffer();
int position = buffer.position();
int limit = buffer.limit();
try {
buffer.flip();
// Check if request bytes are starting from protocolId bytes
if (buffer.remaining() >= protocolId.length()) {
for(int i=0; i<protocolId.length(); i++) {
if (buffer.get(i) != protocolIdBytes[i]) {
// It's not our protocol
return null;
}
}
// Found!
return protocolId;
}
} finally {
buffer.limit(limit);
buffer.position(position);
}
return null;
}
}
public class SimpleProtocolHandler implements ProtocolHandler {
private String protocolId;
public SimpleProtocolHandler(String protocolId) {
this.protocolId = protocolId;
}
// Which protocols we support
public String[] getProtocols() {
return new String[] {protocolId};
}
public boolean handle(Context context, PUProtocolRequest protocolRequest) throws IOException {
ByteBuffer buffer = protocolRequest.getByteBuffer();
SelectionKey key = protocolRequest.getSelectionKey();
buffer.flip();
/**
------------- Here you process a request ---------- */
return true;
}
// Key is expired, can we cancel it?
public boolean expireKey(SelectionKey key) {
return true;
}
// Use some other ByteBuffer, where PUReadFilter will read an incoming data
public ByteBuffer getByteBuffer() {
// Use thread associated byte buffer
return null;
}
}