I played a bit with AIO.... It's nice, but not yet well documented. I wrote a small&primitive echo-server to get familiar with the new technique. See attached files.

Will now try to adapt this to a new AioSocketAcceptor + AioSocketConnector. This will take some time ... (is there a kind of "how to add new transports to MINA" documentation?) If this is done&works, MINA team can check how to integrate/adaot to MINA 2.x and 3.x

br,
Alex


package de.root1.plainaio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutionException;

/**
 *
 * @author achr
 */
public class PlainEchoServer {

    public static void main(String[] args) throws IOException, 
InterruptedException, ExecutionException {

        final AsynchronousServerSocketChannel listener = 
AsynchronousServerSocketChannel.open();
        
        listener.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        
        listener.bind(new InetSocketAddress(1234));
        
        System.out.println("Bound ...");

        listener.accept(null, new CompletionHandler<AsynchronousSocketChannel, 
Void>() {

            public void completed(final AsynchronousSocketChannel asc, Void 
attachment) {
                
                // to be able to accept next
                listener.accept(null, this);
                
                System.out.println("got client connection");
                
                final ByteBuffer rx = ByteBuffer.allocate(1024);

                final Object session = new Object();
                System.out.println("Session: " + session);

                asc.read(rx, session, new CompletionHandler<Integer, Object>() {

                    public void completed(Integer result, Object attachment) {
                        System.out.println("rx: " + attachment + " completed 
with " + result + " bytes read");
                        rx.flip();
                        
                        // reply echo
                        asc.write(rx, session, new CompletionHandler<Integer, 
Object>() {

                            public void completed(Integer result, Object 
attachment) {
                                System.out.println("tx: " + attachment + " 
completed with " + result + " bytes written");
                            }

                            public void failed(Throwable exc, Object 
attachment) {
                                System.err.println("tx: " + attachment + " 
failed with:");
                                exc.printStackTrace();
                            }
                        });


                        try {
                            asc.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }

                    public void failed(Throwable exc, Object attachment) {
                        System.err.println("rx: " + attachment + " failed 
with:");
                        exc.printStackTrace();
                    }
                });
            }

            public void failed(Throwable exc, Void attachment) {
            }
        });

        while (true) {
            System.out.print(".");
            Thread.sleep(3000);
        }


    }
}
package de.root1.plainaio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.Future;

/**
 *
 * @author achr
 */
public class PlainEchoClient {
    
    public static void main(String[] args) throws IOException, 
InterruptedException {
        System.out.println("Connecting ...");
        final AsynchronousSocketChannel client = 
AsynchronousSocketChannel.open();
        System.out.println("Connected.");
        Future<Void> connect = client.connect(new 
InetSocketAddress("localhost", 1234));
        
        while (!connect.isDone()) {
            Thread.sleep(10);
        }
        
        String textToSend = "Ping1234";
        
        ByteBuffer tx = ByteBuffer.wrap(textToSend.getBytes());
        final ByteBuffer rx = ByteBuffer.wrap(textToSend.getBytes());
        rx.clear();
        
        Object session = new Object();
        System.out.println("Session: "+session);
        
        
        
        client.write(tx, session, new CompletionHandler<Integer, Object>(){

            public void completed(Integer result, Object attachment) {
                System.out.println("tx: "+attachment + " completed with " + 
result + " bytes written");
            }

            public void failed(Throwable exc, Object attachment) {
                System.err.println("tx: "+attachment + " failed with:");
                exc.printStackTrace();
            }
            
        });
        
        client.read(rx, session, new CompletionHandler<Integer, Object>(){

            public void completed(Integer result, Object attachment) {
                System.out.println("rx: "+attachment + " completed with " + 
result + " bytes read: "+rx);
                rx.flip();
                int stringSize = rx.capacity();
                byte[] stringBytes = new byte[stringSize];
                rx.get(stringBytes);
                System.out.println("Received echo: "+new String(stringBytes));
                
                try {
                    client.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                System.exit(0);
            }

            public void failed(Throwable exc, Object attachment) {
                System.err.println("rx: "+attachment + " failed with:");
                exc.printStackTrace();
            }
            
        });
        while (true) {
            System.out.print(".");
            Thread.sleep(1000);
        }
    }
    
}

Reply via email to