@@ -0,0 +1,2763 @@
+package kilim.examples;%0A%0Aimport java.io.EOFException;%0Aimport java.io.IOException;%0Aimport java.nio.ByteBuffer;%0A%0Aimport kilim.Pausable;%0Aimport kilim.nio.EndPoint;%0Aimport kilim.nio.NioSelectorScheduler;%0Aimport kilim.nio.SessionTask;%0A%0A%0A/**%0A * A echo server example%0A * %0A * @author boyan%0A * @Date 2010-8-21%0A * %0A */%0Apublic class EchoServer extends SessionTask %7B%0A    static boolean server = false;%0A    static int port = 8007;%0A    static int workerCount = Runtime.getRuntime().availableProcessors() * 2;%0A%0A%0A    static private void usage() %7B%0A        System.err.println(%22Run java kilim.examples.EchoServer %5Bport%5D %5BworkerCount%5D in one window%22);%0A    %7D%0A%0A%0A    static void parsePort(String portstr) %7B%0A        try %7B%0A            port = Integer.parseInt(portstr);%0A        %7D%0A        catch (Exception e) %7B%0A            usage();%0A        %7D%0A    %7D%0A%0A%0A    static void parseWorkerCount(String portstr) %7B%0A        try %7B%0A            workerCount = Integer.parseInt(portstr);%0A        %7D%0A        catch (Exception e) %7B%0A            usage();%0A        %7D%0A    %7D%0A%0A%0A    public static void main(String args%5B%5D) throws Exception %7B%0A%0A        usage();%0A%0A        if (args.length %3E 0) %7B%0A            parsePort(args%5B0%5D);%0A        %7D%0A        if (args.length %3E 1) %7B%0A            parseWorkerCount(args%5B1%5D);%0A        %7D%0A        EchoServer.run();%0A%0A    %7D%0A%0A    private ByteBuffer readBuffer = ByteBuffer.allocate(16 * 1024);%0A%0A%0A    public static void run() throws IOException %7B%0A%0A        NioSelectorScheduler nio = new NioSelectorScheduler(workerCount);%0A%0A        nio.listen(port, EchoServer.class);%0A    %7D%0A%0A%0A    @Override%0A    public void execute() throws Pausable, Exception %7B%0A        // System.out.println(%22%5B%22 + this.id + %22%5D Connection rcvd%22);%0A        try %7B%0A            while (true) %7B%0A                EndPoint ep = this.getEndPoint();%0A                this.readBuffer = ep.read(this.readBuffer); // Pauses until at%0A                // least%0A                this.readBuffer.flip();%0A%0A                if (this.readBuffer.hasRemaining()) %7B%0A                    byte%5B%5D data = new byte%5Bthis.readBuffer.remaining()%5D;%0A                    this.readBuffer.get(data);%0A                    // System.out.println(%22recv %22 + data.length);%0A                    ByteBuffer buff = ByteBuffer.allocate(data.length);%0A                    buff.put(data);%0A                    buff.flip();%0A                    ep.write(buff);%0A                %7D%0A                this.readBuffer.compact();%0A                // System.out.println(%22%5B%22 + this.id + %22%5D Echoed pkt%22);%0A            %7D%0A        %7D%0A        catch (EOFException eofe) %7B%0A            // System.out.println(%22%5B%22 + this.id + %22%5D Connection terminated%22);%0A        %7D%0A        catch (IOException ioe) %7B%0A            // System.out.println(%22%5B%22 + this.id + %22%5D IO Exception: %22 +%0A            // ioe.getMessage());%0A        %7D%0A    %7D%0A%7D%0A

add EchoServer