package edu.ouc.reactor;

import java.io.IOException;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;

/**
 * Server handler
 * 
 * @author wqx
 */
public class Handler implements Runnable {

	private SocketChannel sc;

	private SelectionKey sk;

	private volatile State state;

	private static int DEFAULT_BUFSIZE = 1024;

	private ByteBuffer inputBuf = ByteBuffer.allocate(DEFAULT_BUFSIZE);

	private ByteBuffer outputBuf = ByteBuffer.allocate(DEFAULT_BUFSIZE);

	public Handler(Selector sel,SocketChannel channel) throws IOException{
		sc = channel;
		sc.configureBlocking(false);
		sk = sc.register(sel, SelectionKey.OP_READ);
		sk.attach(this);
		sel.wakeup();
	}
	@Override
	public void run() {

		switch(state){
		case READING:doRead();break; 
		case SENDING:doSend();break;
		}
	}
	private void doRead(){
		try {
			sc.read(inputBuf);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(inputComplete()){//
			process();
			state = State.SENDING;
			//עOP_WRITE
			sk.interestOps(SelectionKey.OP_WRITE);
		}
	}

	private boolean inputComplete(){
		//1.Ƿ
		//2.д
		return true;
	}

	private void doSend(){
		try {
			while(outputBuf.hasRemaining()){
				sc.write(outputBuf);
			}
			sk.cancel();
		} catch (IOException e) {}
	}
	/**
	 * process request and get response
	 * 
	 * @param request
	 * @return
	 */
	private void process(){
		System.out.println("process request and produce response");

		inputBuf.flip();
		byte[] dest = new byte[inputBuf.remaining()];
		inputBuf.get(dest);
		String msg = new String(dest,Charset.forName("UTF-8"));

		String resp = "";

		switch(msg){
		case "request1" : resp = "response1";break;
		case "request2" : resp = "response2";break;
		case "request3" : resp = "response3";break;
		default : resp = "";
		}
		
		try{
			outputBuf.put(resp.getBytes());
		}catch(BufferOverflowException e){
			//ͻӦýݲ
		}
	}

	enum State{
		READING,SENDING;
		public boolean isReadable(){
			return this == READING;
		}
		public boolean isWritable(){
			return this == SENDING;
		}
	}
}

@@ -37,2268 +37,118 @@
 ava.
-io.IOException;%0D%0Aimport java.nio.BufferOverflowException;%0D%0Aimport java.nio.ByteBuffer;%0D%0Aimport java.nio.channels.SelectionKey;%0D%0Aimport java.nio.channels.Selector;%0D%0Aimport java.nio.channels.SocketChannel;%0D%0Aimport java.nio.charset.Charset;%0D%0A%0D%0A/**%0D%0A * Server handler%0D%0A * %0D%0A * @author wqx%0D%0A */%0D%0Apublic class Handler implements Runnable %7B%0D%0A%0D%0A%09private SocketChannel sc;%0D%0A%0D%0A%09private SelectionKey sk;%0D%0A%0D%0A%09private volatile State state;%0D%0A%0D%0A%09private static int DEFAULT_BUFSIZE = 1024;%0D%0A%0D%0A%09private ByteBuffer inputBuf = ByteBuffer.allocate(DEFAULT_BUFSIZE);%0D%0A%0D%0A%09private ByteBuffer outputBuf = ByteBuffer.allocate(DEFAULT_BUFSIZE);%0D%0A%0D%0A%09public Handler(Selector sel,SocketChannel channel) throws IOException%7B%0D%0A%09%09sc = channel;%0D%0A%09%09sc.configureBlocking(false);%0D%0A%09%09sk = sc.register(sel, SelectionKey.OP_READ);%0D%0A%09%09sk.attach(this);%0D%0A%09%09sel.wakeup();%0D%0A%09%7D%0D%0A%09@Override%0D%0A%09public void run() %7B%0D%0A%0D%0A%09%09switch(state)%7B%0D%0A%09%09case READING:doRead();break; %0D%0A%09%09case SENDING:doSend();break;%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09private void doRead()%7B%0D%0A%09%09try %7B%0D%0A%09%09%09sc.read(inputBuf);%0D%0A%09%09%7D catch (IOException e) %7B%0D%0A%09%09%09e.printStackTrace();%0D%0A%09%09%7D%0D%0A%09%09if(inputComplete())%7B//%0D%0A%09%09%09process();%0D%0A%09%09%09state = State.SENDING;%0D%0A%09%09%09//%D7%A2OP_WRITE%0D%0A%09%09%09sk.interestOps(SelectionKey.OP_WRITE);%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%0D%0A%09private boolean inputComplete()%7B%0D%0A%09%09//1.%C7%B7%0D%0A%09%09//2.%D0%B4%0D%0A%09%09return true;%0D%0A%09%7D%0D%0A%0D%0A%09private void doSend()%7B%0D%0A%09%09try %7B%0D%0A%09%09%09while(outputBuf.hasRemaining())%7B%0D%0A%09%09%09%09sc.write(outputBuf);%0D%0A%09%09%09%7D%0D%0A%09%09%09sk.cancel();%0D%0A%09%09%7D catch (IOException e) %7B%7D%0D%0A%09%7D%0D%0A%09/**%0D%0A%09 * process request and get response%0D%0A%09 * %0D%0A%09 * @param request%0D%0A%09 * @return%0D%0A%09 */%0D%0A%09private void process()%7B%0D%0A%09%09System.out.println(%22process request and produce response%22);%0D%0A%0D%0A%09%09inputBuf.flip();%0D%0A%09%09byte%5B%5D dest = new byte%5BinputBuf.remaining()%5D;%0D%0A%09%09inputBuf.get(dest);%0D%0A%09%09String msg = new String(dest,Charset.forName(%22UTF-8%22));%0D%0A%0D%0A%09%09String resp = %22%22;%0D%0A%0D%0A%09%09switch(msg)%7B%0D%0A%09%09case %22request1%22 : resp = %22response1%22;break;%0D%0A%09%09case %22request2%22 : resp = %22response2%22;break;%0D%0A%09%09case %22request3%22 : resp = %22response3%22;break;%0D%0A%09%09default : resp = %22%22;%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09try%7B%0D%0A%09%09%09outputBuf.put(resp.getBytes());%0D%0A%09%09%7Dcatch(BufferOverflowException e)%7B%0D%0A%09%09%09//%CD%BB%D3%A6%C3%BD%DD%B2%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%0D%0A%09enum State%7B%0D%0A%09%09READING,SENDING;%0D%0A%09%09public boolean isReadable()%7B%0D%0A%09%09%09return this == READING;%0D%0A%09%09%7D%0D%0A%09%09public boolean isWritable()%7B%0D%0A%09%09%09return this == SENDING;%0D%0A%09%09%7D%0D%0A%09%7D
+nio.ByteBuffer;%0D%0A%0D%0Apublic interface Handler %7B%0D%0A%09%0D%0A%09void processRequest(Processor processor, ByteBuffer msg);%0D%0A
 %0D%0A%7D%0D

add Handler interface