當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby DRbProtocol模塊用法及代碼示例

本文簡要介紹ruby語言中 DRb::DRbProtocol模塊 的用法。

Module 管理 drb 使用的底層網絡協議。

默認情況下,drb 使用 DRbTCPSocket 協議。可以定義其他協議。協議必須定義以下類方法:

[open(uri, config)] Open a client connection to the server at +uri+,
                    using configuration +config+.  Return a protocol
                    instance for this connection.
[open_server(uri, config)] Open a server listening at +uri+,
                           using configuration +config+.  Return a
                           protocol instance for this listener.
[uri_option(uri, config)] Take a URI, possibly containing an option
                          component (e.g. a trailing '?param=val'),
                          and return a [uri, option] tuple.

如果 URI 不能識別它們支持的協議(例如標準Ruby 協議的“druby:”),所有這些方法都應該引發 DRbBadScheme 錯誤。這就是 DRbProtocol 模塊如何在給定 URI 的情況下確定哪個協議實現服務於該協議。

open_server 返回的協議實例必須有以下方法:

接受

接受與服務器的新連接。返回能夠與客戶端通信的協議實例。

關閉

關閉服務器連接。

烏裏

獲取此服務器的 URI

open 返回的協議實例必須有以下方法:

send_request(參考,msg_id,arg,b)

使用給定的消息 ID 和參數向 ref 發送請求。這最容易通過調用 DRbMessage.send_request 來實現,提供位於當前協議之上的流。

recv_reply

接收來自服務器的回複並將其作為 [success-boolean, reply-value] 對返回。這最容易通過調用 DRb.recv_reply 來實現,提供位於當前協議之上的流。

活?

這種聯係還存在嗎?

關閉

關閉此連接。

open_server() .accept() 返回的協議實例必須具有以下方法:

recv_request

接收來自客戶端的請求並返回一個 [object, message, args, block] 元組。這最容易通過調用 DRbMessage.recv_request 來實現,提供位於當前協議之上的流。

send_reply(成功,結果)

向客戶發送回複。這最容易通過調用 DRbMessage.send_reply 來實現,提供位於當前協議之上的流。

關閉

關閉此連接。

使用 add_protocol 方法向 DRbProtocol 模塊注冊新協議。

有關其他協議的示例,請參閱 drb/unix.rb 中的 DRbUNIXSocket ,以及完整 drb 發行版中的 sample/http0.rb 和 sample/http0serv.rb 中的 HTTP0。

相關用法


注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 DRbProtocol模塊。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。