1. 程式人生 > >netty原始碼分析(七)Acceptor與Dispatcher角色分析

netty原始碼分析(七)Acceptor與Dispatcher角色分析

理解Reactor模式的另外一篇文章是一篇論文:《reactor-siemens》,論文的環境是日誌伺服器的例子:
這裡寫圖片描述
客戶端將日誌傳送到日誌伺服器,日誌伺服器將日誌通過各種裝置輸出。
意圖:
The Reactor design pattern handles service requests that are
delivered concurrently to an application by one or more
clients. Each service in an application may consist of
serveral methods and is represented by a separate event handler
that is responsible for dispatching service-specific requests.
Dispatching of event handlers is performed by an initiation
dispatcher, which manages the registered event handlers.
Demultiplexing of service requests is performed by a
synchronous event demultiplexer.
Reactor 設計模式是為了處理由一個或多個客戶端向一個應用傳送的請求,應用中的每個服務有單獨的時間處理器組成,事件處理器的作用是特定服務的請求進行分發,時間處理器的分發是由initiation dispatcher來進行,initiation dispatcher會管理諸多的事件處理器,服務請求的分離是由同步的事件分離器處理。

我們拿出來論文的核心知識點說一下:
這裡寫圖片描述

圖A是初始化的過程:
1. The logging server (1) registers the Logging Acceptor with the Initiation Dispatcher to handle connection requests;
日誌服務通過初始化分發器註冊到日誌Acceptor 上去,用來處理連線請求。
2. The logging server invokes the handle events method (2) of the Initiation Dispatcher;
日誌伺服器呼叫初始化分發器的處理事件方法。
3. The Initiation Dispatcher invokes the synchronous event demultiplexing select (3) operation to wait for connection requests or logging data to arrive;
初始化分發器通用同步的事件分離方法(select())來等待連線請求或者資料的到達。
4. A client connects (4) to the logging server;
一個客戶端連線到服務
5. The Logging Acceptor is notified by the Initiation Dispatcher (5) of the new connection request;
當一個連線請求過來時,日誌接收器被初始化分發器喚醒。
6. The Logging Acceptor accepts (6) the new connection;
日誌接收器接受新的連線 。
7. The Logging Acceptor creates (7) a Logging Handler to service the new client;
日誌接收器建立一個日誌處理器來服務新的客戶端。
8. Logging Handler registers (8) its socket handle with the Initiation Dispatcher and instructs the dispatcher to notify it when the socket becomes “ready for reading.”
日誌處理器通過初始化分發器註冊到處理他的socket 上去,並且指示分發器當socket 變成準備好的時候提醒他。

圖B是後續其他客戶端連線的處理過程:
1. The client sends (1) a logging record;
一個客戶端傳送一個日誌。
2. The Initiation Dispatcher notifies (2) the associated Logging Handler when a client logging record is queued on its socket handle by OS;
當一個客戶端的日誌進入到處理器的佇列, 初始化分發器通知相關聯的日誌處理器。
3. The record is received (3) in a non-blocking manner (steps 2 and 3 repeat until the logging record has been received completely);
日誌接受通過非阻塞的方式通過重複步驟2和步驟3直到所有日誌接受完畢。
4. The Logging Handler processes the logging record and writes (4) it to the standard output.
日誌處理器處理日誌,並且寫到標準輸出裡邊。
5. The Logging Handler returns (5) control to the Initiation Dispatcher’s event loop.
處理完畢返回到 Initiation Dispatcher時間迴圈。