Handwritten reactor (netty reactor model)

 

public class Dispacther implements Runnable{

	private String host = "127.0.0.1";
	private int port = 8080;
	
	public final Selector selector;  
    public final ServerSocketChannel serverSocketChannel;
	
    public Dispacther() throws IOException {
    	selector=Selector.open();
    	serverSocketChannel=ServerSocketChannel.open();  
    	InetSocketAddress inetSocketAddress=new InetSocketAddress(this.host,this.port);  
        serverSocketChannel.socket().bind(inetSocketAddress);  
        serverSocketChannel.configureBlocking(false);
        
        SelectionKey selectionKey=serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        
        selectionKey.attach(new Acceptor(this));
	}
    
	
	@Override
	public void run() {
		try {  
            while(!Thread.interrupted()){  
                selector.select();  
                Set<SelectionKey> selectionKeys= selector.selectedKeys();  
                Iterator<SelectionKey> it=selectionKeys.iterator();   
                while(it.hasNext()){   
                    SelectionKey selectionKey=it.next();  
                    dispatch(selectionKey);  
                    selectionKeys.clear();  
                }  
            }  
        } catch (IOException e) {  
            e.printStackTrace ();  
        }
	}

	 void dispatch(SelectionKey key) {  
	        Runnable r = (Runnable)(key.attachment());    
	        if (r != null){    
	            r.run();  
	        }    
	    }   
}

 

 

 

public class Acceptor implements Runnable{

	private Dispacther dispacther;
	
	
	public Acceptor(Dispacther dispacther) {
		this.dispacther = dispacther;
	}


	@Override
	public void run() {
		 try {  
	            SocketChannel socketChannel=dispacther.serverSocketChannel.accept();  
	            if(socketChannel!=null)
	                new SocketHandler(dispacther.selector, socketChannel);  
	        } catch (IOException e) {  
	            e.printStackTrace ();  
	        }  
	}

	 

}

 

 

public class SocketHandler implements Runnable {

   private SocketChannel socketChannel;  
   private Charset charset = Charset.forName("UTF-8");
   private Selector selector;
   
   public SocketHandler(Selector selector,SocketChannel socketChannel) throws IOException{  
        this.socketChannel=socketChannel;  
        this.selector = selector;
        socketChannel.configureBlocking(false);   
        SelectionKey selectionKey=socketChannel.register(selector, 0);  
  
        selectionKey.attach(this);     
        selectionKey.interestOps(SelectionKey.OP_READ);    
        selector.wakeup();  
    }  

	@Override
	public void run() {
		  try { 	
			    ByteBuffer buff = ByteBuffer.allocate(1024);
				String content = "";
				while (socketChannel.read(buff) > 0) {
					socketChannel.read(buff);
					buff.flip();
					content += charset.decode(buff);
				}
				if(!"".equals(content)){
					System.out.println(  " content : " + content);
					for (SelectionKey key : this.selector.keys()) {
						Channel targetChannel = key.channel();
						if (targetChannel instanceof SocketChannel) {
							SocketChannel dest = (SocketChannel) targetChannel;
							dest.write(charset.encode(content));
							dest.close();
						}
					}
				}
	        } catch (IOException e) {  
	            e.printStackTrace ();  
	        }  
	}

	 

	 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

Donate to developers 

Driven by interest, I write 免费something with joy and sweat. I hope you like my work and can support it at the same time. Of course, if you have money to support a money field (support Alipay, WeChat, and the buckle group), if you have no money to support a personal field, thank you.

 

Personal homepage : http://knight-black-bob.iteye.com/



 
 
 Thank you for your sponsorship, I will do better!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326306596&siteId=291194637