users@grizzly.java.net

How to handle OP_CONNECT ?

From: Matthias Schmidt <schmidtm_at_sun.com>
Date: Wed, 25 Feb 2009 14:24:29 +0100

Hi *,

based on Jean-Francois Grizzly intro:

http://weblogs.java.net/blog/jfarcand/archive/2008/02/writing_a_tcpud_1.html

I wrote a small Daytime server ( cf RFC 867 ) just to get in touch
with grizzly.

I used a ProtocolFilter to push the string to the client. (This might
be the first mistake here...)

The DefaultSelectionKeyHandler only processes the ProtocolChain when
there is data to read.
Absolutely nothing happens on OP_CONNECT.

Question: How can/should i code a server which reacts on an incoming
connection?
At the moment i have to hit the keyboard once after i issue:

telnet localhost 1405

Whereas the original unix daytime server just sends the string and
exits on an incoming connection.

if played around with overloading onConnectOp, onConnectInterest
without success.

cheers,

Matthias


> public class Main {
>
> private static int PORT = 1405;
>
> public static void main(String[] args) {
> Controller controller = new Controller();
>
> TCPSelectorHandler tcpSelectorHandler = new
> TCPSelectorHandler();
>
> tcpSelectorHandler.setPort(PORT);
>
> controller.addSelectorHandler(tcpSelectorHandler);
>
> Pipeline mySharedPipeline = new DefaultPipeline();
> mySharedPipeline.setMaxThreads(5);
>
> controller.setPipeline(mySharedPipeline);
>
> ProtocolChainInstanceHandler pciHandler =
> new ProtocolChainInstanceHandler() {
>
> final private ProtocolChain protocolChain = new
> DefaultProtocolChain();
>
> public ProtocolChain poll() {
> return protocolChain;
> }
>
> public boolean offer(ProtocolChain instance) {
> return true;
> }
> };
>
> controller.setProtocolChainInstanceHandler(pciHandler);
>
> ProtocolChain protocolChain = pciHandler.poll();
> protocolChain.addFilter(new DaytimeWriterFilter());
>
> try {
> System.out.println("Starting Daytime server running on
> port " + PORT);
> controller.start();
> } catch (IOException ex) {
> Logger.getLogger(Main.class.getName()).log(Level.SEVERE,
> null, ex);
> }
>
> }
> }
>
> class DaytimeWriterFilter implements ProtocolFilter {
>
> public boolean execute(Context ctx) throws IOException {
> if (ctx.getProtocol() == Controller.Protocol.TCP) {
> SelectableChannel channel =
> ctx.getSelectionKey().channel();
>
> String now = "This is grizzly daytime server speaking.
> It's: " +
> (new Date()).toString() + "\n";
>
> ByteBuffer buffer = ByteBuffer.wrap(now.getBytes());
>
> OutputWriter.flushChannel(channel, buffer);
>
> channel.close();
> buffer.clear();
> }
>
> return false;
> }
>
> public boolean postExecute(Context arg0) throws IOException {
> return false; // game over
> }
>
> }
>




Matthias Schmidt Tel: (++49) 6227 356 236
Sun Microsystems GmbH Fax: (++49) 6227 356 222
Altrottstr. 31 Mobil: (++49) 171 5767209
D-69190 Walldorf ICQ: 435738815