You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Paul Chen <ch...@gmail.com> on 2006/08/02 01:23:06 UTC

Question: A Mina client connects to a http server

Hello all,

I'm writing a Mina client to connect to an Apache http server.
The sockets all established fine, then I try to use session.write
and nothing happened. The http log didn't even show the
access/error traces from this Mina client.

    public void sessionOpened( IoSession session )
    {
        System.out.println("# sessionOpened");
        // send requests
        ...
            session.write( m );
    }

Any tips are highly appreciated.

Thanks

Re: Question: A Mina client connects to a http server

Posted by Paul Chen <ch...@gmail.com>.
Yes, that's the case. Thanks a lot.

On 8/2/06, Niklas Therning <ni...@trillian.se> wrote:
>
> Paul Chen wrote:
> > I'm modifying the mina proxy example, by moving the code
> > of connecting to server from sessionOpened() into messageReceived().
> > Because
> > I want to determine which server to
> > connect to based on the mesg received in  messageReceived().
> >
> > Attached is the modified ClientToProxyIoHandler.java
> >
> > The proxy server started ok w/ following dump to screen:
> >
> > ## ClientToProxyIoHandler>
> > Listening on port 8081
> >
> > When hitting the server w/ a http request, it hangs here:
> >
> > ## ClientToProxyIoHandler.sessionCreated>
> > Aug 2, 2006 1:29:51 AM org.slf4j.impl.JCLLoggerAdapter info
> > INFO: [/127.0.0.1:3973] OPENED
> > ## ClientToProxyIoHandler.sessionOpened>
> >
> > If the code of server connecting is moved up to sessionOpened(),
> > everything works fine.
> >
> > Thanks
> >>
> >>
> >>
> >
> You need to remove the line
>
> session.setTrafficMask( TrafficMask.NONE );
>
> from sessionCreated(). In the original example it's needed to prevent
> messageReceived() from being called before we have established the proxy
> to server connection. We could do without it but then we would need to
> buffer any data received before the connection has been established.
>
> You won't need the setTrafficMask() calls at all I think since you have
> changed the code which connects to join on the ConnectFuture. Please
> note that this may be less efficient since it blocks the thread until
> the connection is established or fails.
>
> HTH
>
> --
> Niklas Therning
> Software Architect
> www.spamdrain.net
>
>

Re: Question: A Mina client connects to a http server

Posted by Niklas Therning <ni...@trillian.se>.
Paul Chen wrote:
> I'm modifying the mina proxy example, by moving the code
> of connecting to server from sessionOpened() into messageReceived().
> Because
> I want to determine which server to
> connect to based on the mesg received in  messageReceived().
>
> Attached is the modified ClientToProxyIoHandler.java
>
> The proxy server started ok w/ following dump to screen:
>
> ## ClientToProxyIoHandler>
> Listening on port 8081
>
> When hitting the server w/ a http request, it hangs here:
>
> ## ClientToProxyIoHandler.sessionCreated>
> Aug 2, 2006 1:29:51 AM org.slf4j.impl.JCLLoggerAdapter info
> INFO: [/127.0.0.1:3973] OPENED
> ## ClientToProxyIoHandler.sessionOpened>
>
> If the code of server connecting is moved up to sessionOpened(),
> everything works fine.
>
> Thanks
>>
>>
>>
>
You need to remove the line

session.setTrafficMask( TrafficMask.NONE );

from sessionCreated(). In the original example it's needed to prevent
messageReceived() from being called before we have established the proxy
to server connection. We could do without it but then we would need to
buffer any data received before the connection has been established.

You won't need the setTrafficMask() calls at all I think since you have
changed the code which connects to join on the ConnectFuture. Please
note that this may be less efficient since it blocks the thread until
the connection is established or fails.

HTH

-- 
Niklas Therning
Software Architect
www.spamdrain.net


Re: Question: A Mina client connects to a http server

Posted by Paul Chen <ch...@gmail.com>.
I'm modifying the mina proxy example, by moving the code
of connecting to server from sessionOpened() into messageReceived(). Because
I want to determine which server to
connect to based on the mesg received in  messageReceived().

Attached is the modified ClientToProxyIoHandler.java

The proxy server started ok w/ following dump to screen:

## ClientToProxyIoHandler>
Listening on port 8081

When hitting the server w/ a http request, it hangs here:

## ClientToProxyIoHandler.sessionCreated>
Aug 2, 2006 1:29:51 AM org.slf4j.impl.JCLLoggerAdapter info
INFO: [/127.0.0.1:3973] OPENED
## ClientToProxyIoHandler.sessionOpened>

If the code of server connecting is moved up to sessionOpened(),
everything works fine.

Thanks

---- Modified ClientToProxyIoHandler.java
package org.apache.mina.examples.proxy;

import java.net.InetSocketAddress;

import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.ConnectFuture;
import org.apache.mina.common.IoConnector;
import org.apache.mina.common.IoFilter;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.TrafficMask;
import org.apache.mina.filter.LoggingFilter;
import org.apache.mina.transport.socket.nio.SocketConnector;
import org.apache.mina.util.SessionLog;

/**
 * Handles the client to proxy part of the proxied connection.
 *
 * @author The Apache Directory Project (mina-dev@directory.apache.org)
 * @version $Rev$, $Date$
 *
 */
public class ClientToProxyIoHandler extends AbstractProxyIoHandler
{
    private final IoConnector connector;

    private InetSocketAddress address;

    private ServerToProxyIoHandler connectorHandler;

    private static IoFilter LOGGING_FILTER = new LoggingFilter();

    public ClientToProxyIoHandler( )
    {
        System.out.println("## ClientToProxyIoHandler>");
        connectorHandler = new ServerToProxyIoHandler();
        this.connector = new SocketConnector();
    }

    private void connectToSvc(IoSession session, String host, int port)
    {
        System.out.println("## ClientToProxyIoHandler.connectToSvc>");
        address = new InetSocketAddress(host, port);

        ConnectFuture future = connector.connect(
                address,
                connectorHandler);

        future.join();

        try
        {
            future.getSession().setAttachment( session );
            session.setAttachment( future.getSession() );
            future.getSession().setTrafficMask( TrafficMask.ALL );
        }
        catch( Exception e )
        {
            // Connect failed
            e.printStackTrace();
            session.close();
        }
        finally
        {
            session.setTrafficMask( TrafficMask.ALL );
        }
    }

    public void sessionOpened( final IoSession session ) throws Exception
    {
        System.out.println("## ClientToProxyIoHandler.sessionOpened>");
        // Postpone connection after messageReceived
        /*
        connectToSvc(session, "localhost", 8080);
        connector.connect( address, connectorHandler ).setCallback(
                new IoFuture.Callback()
        {
            public void operationComplete( IoFuture f )
            {
                System.out.println("##
ClientToProxyIoHandler.operationComplete>");
                ConnectFuture future = ( ConnectFuture ) f;
                try
                {
                    future.getSession().setAttachment( session );
                    session.setAttachment( future.getSession() );
                    future.getSession().setTrafficMask( TrafficMask.ALL );
                }
                catch( Exception e )
                {
                    // Connect failed
                    session.close();
                }
                finally
                {
                    session.setTrafficMask( TrafficMask.ALL );
                }
            }
        } );
        */
    }

    public void sessionCreated( IoSession session ) throws Exception
    {
        System.out.println("## ClientToProxyIoHandler.sessionCreated>");
        session.getFilterChain().addLast( "logger", LOGGING_FILTER );
        session.setTrafficMask( TrafficMask.NONE );
    }

    public void sessionClosed( IoSession session ) throws Exception
    {
        System.out.println("## ClientToProxyIoHandler.sessionClosed>");
        if( session.getAttachment() != null )
        {
            ( ( IoSession ) session.getAttachment() ).setAttachment( null );
            ( ( IoSession ) session.getAttachment() ).close();
            session.setAttachment( null );
        }
    }

    public void messageReceived( IoSession session, Object message ) throws
Exception
    {
        ByteBuffer rb = ( ByteBuffer ) message;
        System.out.println("## ClientToProxyIoHandler.messageReceived> " +
                rb.getString( CHARSET.newDecoder() ));
        rb.rewind();
        // Postpone conn to svc from sessionOpened() to here
        connectToSvc(session, "localhost", 8080);
        ByteBuffer wb = ByteBuffer.allocate( rb.remaining() );
        rb.mark();
        wb.put( rb );
        wb.flip();
        ( ( IoSession ) session.getAttachment() ).write( wb );
        rb.reset();
        SessionLog.info( session, rb.getString( CHARSET.newDecoder() ) );
    }

    public void messageSent( IoSession session, Object message )
      throws Exception
    {
        ByteBuffer rb = ( ByteBuffer ) message;
        System.out.println("## ClientToProxyIoHandler.messageSent> " +
                rb.getString( CHARSET.newDecoder() ));
        rb.rewind();
    }

    public void exceptionCaught( IoSession session, Throwable cause )
    {
        session.close();
        cause.printStackTrace();
    }
}


On 8/1/06, Niklas Therning <ni...@trillian.se> wrote:
>
> Paul Chen wrote:
> > Hello all,
> >
> > I'm writing a Mina client to connect to an Apache http server.
> > The sockets all established fine, then I try to use session.write
> > and nothing happened. The http log didn't even show the
> > access/error traces from this Mina client.
> >
> >    public void sessionOpened( IoSession session )
> >    {
> >        System.out.println("# sessionOpened");
> >        // send requests
> >        ...
> >            session.write( m );
> >    }
> >
> > Any tips are highly appreciated.
> >
> > Thanks
> >
> Please give some more info. Are you using any filters, codec, etc? You
> should enable logging (by adding a LoggingFilter to your filter chain)
> on the MINA side to see exactly what happens. Post the log output to
> this list and we might be able to help you further.
>
> --
> Niklas Therning
> Software Architect
> www.spamdrain.net
>
>

Re: Question: A Mina client connects to a http server

Posted by Niklas Therning <ni...@trillian.se>.
Paul Chen wrote:
> Hello all,
>
> I'm writing a Mina client to connect to an Apache http server.
> The sockets all established fine, then I try to use session.write
> and nothing happened. The http log didn't even show the
> access/error traces from this Mina client.
>
>    public void sessionOpened( IoSession session )
>    {
>        System.out.println("# sessionOpened");
>        // send requests
>        ...
>            session.write( m );
>    }
>
> Any tips are highly appreciated.
>
> Thanks
>
Please give some more info. Are you using any filters, codec, etc? You
should enable logging (by adding a LoggingFilter to your filter chain)
on the MINA side to see exactly what happens. Post the log output to
this list and we might be able to help you further.

-- 
Niklas Therning
Software Architect
www.spamdrain.net