深入阅读Mina源码(3) ―― Mina之IOAdapter(二)

 (接:深入阅读Mina源码(3) —— Mina之IOAdapter(一))

 

4. AbstractPollingIoAcceptor

 

这个类在org.apache.mina.core.polling,这个包一共有四个类,这个类就可以代表整个包的内容了,细致说一下,之后就不再深入介绍这个包了。从名字可以看出包里面的类主要完成轮询。

AbstractPollingIoAcceptor主要完成了AbstractIoAcceptor定义的实际实现方法,如unbind0、bindInternal、dispose0,另外下方了一些更细致的accept、open、select、colse方法让子类实现,也清晰的可以看出它主要为Socket提供父类的一些实现,同时也有了NIO的影子。

看一下AbstractPollingIoAcceptor的属性字段,可以看到

 

 

Java代码 复制代码 收藏代码
  1. /** A lock used to protect the selector to be waked up before it's created */
  2. private final Semaphore lock = new Semaphore(1);
  3.  
  4. private final IoProcessor<S> processor;
  5.  
  6. private final boolean createdProcessor;
  7.  
  8. private final Queue<AcceptorOperationFuture> registerQueue = new ConcurrentLinkedQueue<AcceptorOperationFuture>();
  9.  
  10. private final Queue<AcceptorOperationFuture> cancelQueue = new ConcurrentLinkedQueue<AcceptorOperationFuture>();
  11.  
  12. private final Map<SocketAddress, H> boundHandles = Collections.synchronizedMap(new HashMap<SocketAddress, H>());
  13.  
  14. private final ServiceOperationFuture disposalFuture = new ServiceOperationFuture();
  15.  
  16. /** A flag set when the acceptor has been created and initialized */
  17. private volatile boolean selectable;
  18.  
  19. /** The thread responsible of accepting incoming requests */
  20. private AtomicReference<Acceptor> acceptorRef = new AtomicReference<Acceptor>();
  21.  
  22. protected boolean reuseAddress = false;
  23.  
  24. /**
  25. * Define the number of socket that can wait to be accepted. Default
  26. * to 50 (as in the SocketServer default).
  27. */
  28. protected int backlog = 50;
/** A lock used to protect the selector to be waked up before it's created */
    private final Semaphore lock = new Semaphore(1);

    private final IoProcessor<S> processor;

    private final boolean createdProcessor;

    private final Queue<AcceptorOperationFuture> registerQueue = new ConcurrentLinkedQueue<AcceptorOperationFuture>();

    private final Queue<AcceptorOperationFuture> cancelQueue = new ConcurrentLinkedQueue<AcceptorOperationFuture>();

    private final Map<SocketAddress, H> boundHandles = Collections.synchronizedMap(new HashMap<SocketAddress, H>());

    private final ServiceOperationFuture disposalFuture = new ServiceOperationFuture();

    /** A flag set when the acceptor has been created and initialized */
    private volatile boolean selectable;

    /** The thread responsible of accepting incoming requests */
    private AtomicReference<Acceptor> acceptorRef = new AtomicReference<Acceptor>();

    protected boolean reuseAddress = false;

    /**
     * Define the number of socket that can wait to be accepted. Default
     * to 50 (as in the SocketServer default).
     */
    protected int backlog = 50;

这个类讲下去确实有种无从下手的感觉,这里注意一个acceptorRef是处理请求的AtomicReference类型的Acceptor,Acceptor是一个用于接收客户端请求,里面代码比较多,但是我感觉此部分是整个类比较关键的实现部分,需要仔细读一下,但是实在不太好讲,这里可以同样只关注整体,不关注细节,包括processor一些方法的内部完成。我只总结一个流程:

 

 

主要的两个方法把父类中的注释写一下,方便理解,看了注释再看代码就比较容易想明白了:

 

bindInternal:Starts the acceptor, and register the given addresses

unbind0:Implement this method to perform the actual unbind operation.

 

你可能感兴趣的:(阅读,3)