users@grizzly.java.net

Re: How to handle OP_CONNECT ?

From: Oleksiy Stashok <Oleksiy.Stashok_at_Sun.COM>
Date: Wed, 25 Feb 2009 14:31:22 +0100

Hi Matthias,

please try to create SelectorHandler like this:

TCPSelectorHandler tcpSelectorHandler = new TCPSelectorHandler() {
     @Override
     public boolean onAcceptInterest(SelectionKey key,
             Context ctx) throws IOException{
         SelectableChannel channel = acceptWithoutRegistration(key);

         if (channel != null) {
             configureChannel(channel);
             SelectionKey readKey =
                     channel.register(selector, SelectionKey.OP_READ |
SelectionKey.OP_WRITE);
             readKey.attach(System.currentTimeMillis());
         }
         return false;
     }
}

this way your Filter will be called, when accepted connection will be
ready for writing - so you'll be able to send data.

Pls. let me know if it works fine for you.

Thanks.

WBR,
Alexey.


On Feb 25, 2009, at 14:24 , Matthias Schmidt wrote:

> 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
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>