users@grizzly.java.net

Re: Seeking Some Guidance on Using Grizzly / NIO

From: Oleksiy Stashok <Oleksiy.Stashok_at_Sun.COM>
Date: Thu, 30 Apr 2009 23:48:24 +0200

Hi Edwin,

the problem is that acceptWithoutRegistration() is called by framework
in the Selector thread, which means all I/O operations processing will
block until control will not be returned.
That's why connect() could not be executed there.

Seems like the other approach, I was talking about in prev. mail, will
work better here (and I like it more)...
Connect should be done in YourOwnFilter, when first data comes to
original connection. So you'll need to check Map, if there is no
"corresponding" connection - create it.

I think this is better approach, because you'll create a
"corresponding" connection only when you're really sure, that some
data should be transferred.

WBR,
Alexey.

>
>
> i think i've got to take it one step at a time... :p
>
> This is what i have now... what it currently tries to do is:
> 1. Listen for incoming connections on port 8888
> 2. Whenever a new incoming connection is accepted, establish a
> corresponding outgoing connection (in this case, to localhost:9999).
>
> package com.ncs.amaretto.proxy.core;
>
> import java..io.IOException;
> import java.net.InetSocketAddress;
> import java.net.Socket;
> import java.nio.channels.SelectableChannel;
> import java.nio.channels.SelectionKey;
> import java.nio.channels.SocketChannel;
>
> import com.sun.grizzly.Controller;
> import com.sun.grizzly.DefaultCallbackHandler;
> import com.sun.grizzly.TCPConnectorHandler;
> import com.sun.grizzly.TCPSelectorHandler;
>
> public final class GrizzlyApplication
> {
> public static final void main(final String[] args) throws Exception
> {
> Controller controller = new Controller();
>
> TCPSelectorHandler selectorHandler = new
> MyTCPSelectorHandler(controller);
> selectorHandler.setPort(8888);
>
> controller.addSelectorHandler(selectorHandler);
>
> controller.start();
> }
>
> private static final class MyTCPSelectorHandler extends
> TCPSelectorHandler
> {
> private final Controller controller;
>
> MyTCPSelectorHandler(final Controller controller)
> {
> this.controller = controller;
> }
>
> @Override
> public final SelectableChannel acceptWithoutRegistration(final
> SelectionKey key) throws IOException
> {
> SelectableChannel channel = super.acceptWithoutRegistration(key);
>
> System.out.println("channel.getClass().getName() = " +
> channel.getClass().getName());
>
> if (channel instanceof SocketChannel)
> {
> SocketChannel socketChannel = (SocketChannel) channel;
> Socket socket = socketChannel.socket();
>
> System.out.println("socket.getInetAddress().getHostAddress() = "
> + socket.getInetAddress().getHostAddress());
> System.out.println("socket.getPort() = " + socket.getPort());
> }
>
> TCPConnectorHandler connectorHandler = new TCPConnectorHandler();
>
> connectorHandler.setController(this.controller);
>
> connectorHandler.connect((new InetSocketAddress("localhost",
> 9999)), (new DefaultCallbackHandler(connectorHandler)), this);
>
> SelectableChannel outChannel =
> connectorHandler.getUnderlyingChannel();
>
> System.out.println("outChannel = " + outChannel);
>
> return channel;
> }
> }
> }
>
>
> Questions:
>
> 1. The line
>
> connectorHandler.connect((new InetSocketAddress("localhost", 9999)),
> (new DefaultCallbackHandler(connectorHandler)), this);
>
> takes very long to establish a connection (~ 10 sec). Is there
> anything i've done wrongly?
>
> 2. The line
>
> SelectableChannel outChannel =
> connectorHandler.getUnderlyingChannel();
>
> returns outChannel as NULL. How can i get hold of the channel
> associated with the outgoing connection?
>
>
>
> Thanks and Regards,
> Edwin
>
>
>
> New Email names for you!
> Get the Email name you&#39;ve always wanted on the new @ymail and
> @rocketmail.
> Hurry before someone else does!
> http://mail.promotions.yahoo.com/newdomains/sg/
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>