Hi,

I had a discussion with Trustin about latency and I wrote a little test
(based of reserver example). The values looks pretty low (1ms). I wonder
if my test is correct.

Julien
import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.ConnectFuture;
import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoAcceptor;
import org.apache.mina.common.IoAcceptorConfig;
import org.apache.mina.common.IoHandler;
import org.apache.mina.common.IoSession;
import org.apache.mina.transport.socket.nio.SocketAcceptor;
import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
import org.apache.mina.transport.socket.nio.SocketConnector;

public class Main {
    /** Choose your favorite port number. */
    private static final int PORT = 8080;
    
    private static Thread worker;
    public static void main( String[] args )
    {
        IoAcceptor acceptor = new SocketAcceptor();
        IoAcceptorConfig config = new SocketAcceptorConfig();
        
        // Bind
        try {
			acceptor.bind(
			        new InetSocketAddress( PORT ),
			        new EchoProtocolHandler(),
			        config );
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        System.out.println( "Listening on port " + PORT );
        
        // spawning a client
        SocketConnector connector=new SocketConnector();
        ConnectFuture future=connector.connect(new InetSocketAddress("127.0.0.1",PORT),new IoHandler() {

			public void sessionCreated(IoSession arg0) throws Exception {
				
			}

			public void sessionOpened(IoSession arg0) throws Exception {
				System.err.println("Connected !");
				Main.worker=new Worker(arg0);
				Main.worker.start();
			}

			public void sessionClosed(IoSession arg0) throws Exception {
				
			}

			public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
				
			}

			public void exceptionCaught(IoSession arg0, Throwable arg1) throws Exception {
				arg1.printStackTrace();
			}

			public void messageReceived(IoSession arg0, Object arg1) throws Exception {
				synchronized (Main.worker) {
					Main.worker.notifyAll();

				}
				
			}

			public void messageSent(IoSession arg0, Object arg1) throws Exception {
			}
        	
        	
        });
        
        try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        System.err.println("Connecting..");
        future.join();
        
    }
    
    
    
    private static class Worker extends Thread {
    	private ByteBuffer buff;
    	private IoSession session;
    	private long sent;
    	public Worker(IoSession session) {
    		this.session=session;

    			
    	}
    	
    	 
    	public void run() {
    		for(;;) {
    			// send a test
    			synchronized (this) {
    				try {
    		    		buff=ByteBuffer.wrap( "fsmùlfksdmglkdblmdfbkmergblkdmv".getBytes());
    					sent=System.nanoTime();
    					session.write(buff);
						this.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
    			long delta=System.nanoTime()-sent;
    			System.err.println("nano : "+delta+" "+delta/1000000f+" ms");
    		}
    	}
    }
}

import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.TransportType;
import org.apache.mina.transport.socket.nio.SocketSessionConfig;
public class EchoProtocolHandler extends IoHandlerAdapter
{
    public void sessionCreated( IoSession session )
    {
        if( session.getTransportType() == TransportType.SOCKET )
        {
            ( ( SocketSessionConfig ) session.getConfig() ).setReceiveBufferSize( 2048 );
            ( ( SocketSessionConfig ) session.getConfig() ).setTcpNoDelay(true);
        }
        
        session.setIdleTime( IdleStatus.BOTH_IDLE, 10 );

    }
    
    public void sessionIdle( IoSession session, IdleStatus status )
    {
    }

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

    public void messageReceived( IoSession session, Object message ) throws Exception
    {
        if( !( message instanceof ByteBuffer ) )
        {
            return;
        }

        ByteBuffer rb = ( ByteBuffer ) message;
        // Write the received data back to remote peer
        ByteBuffer wb = ByteBuffer.allocate( rb.remaining() );
        wb.put( rb );
        wb.flip();
        session.write( wb );
    }
}

Reply via email to