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 ([email protected])
* @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 <[EMAIL PROTECTED]> 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