Class: ManiaRPC::ManiaClient
- Inherits:
-
Object
- Object
- ManiaRPC::ManiaClient
- Defined in:
- lib/maniaplanet_rpc/maniaplanet_client.rb
Overview
This class provides the interface for a ManiaPlanet Server by sending/receiving requests as well as callbacks.
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#response_map ⇒ Object
Returns the value of attribute response_map.
Instance Method Summary collapse
-
#all(&block) ⇒ Object
Call this with a block to receive all callbacks.
-
#call(method, *args, &block) ⇒ Object
Calls an RPC method with the given name and arguments, optionally handling the result within the passed block.
-
#initialize(ip, port) ⇒ ManiaClient
constructor
A new instance of ManiaClient.
-
#on(what, &block) ⇒ Object
Call this with a block to handle a callback with the given name.
- #parse_callback(message) ⇒ Object
Constructor Details
#initialize(ip, port) ⇒ ManiaClient
Returns a new instance of ManiaClient.
128 129 130 131 132 133 134 135 |
# File 'lib/maniaplanet_rpc/maniaplanet_client.rb', line 128 def initialize(ip, port) @response_map = {} @callback_map = {} @connection = ManiaConnection.new ip, port, self Thread.new do @connection.start end end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
125 126 127 |
# File 'lib/maniaplanet_rpc/maniaplanet_client.rb', line 125 def connection @connection end |
#response_map ⇒ Object
Returns the value of attribute response_map.
126 127 128 |
# File 'lib/maniaplanet_rpc/maniaplanet_client.rb', line 126 def response_map @response_map end |
Instance Method Details
#all(&block) ⇒ Object
Call this with a block to receive all callbacks. Can be used in conjunction with the on method.
150 151 152 |
# File 'lib/maniaplanet_rpc/maniaplanet_client.rb', line 150 def all(&block) @callback_map[:all] = block end |
#call(method, *args, &block) ⇒ Object
Calls an RPC method with the given name and arguments, optionally handling the result within the passed block. Note: The response is asynchronous.
157 158 159 160 161 162 163 |
# File 'lib/maniaplanet_rpc/maniaplanet_client.rb', line 157 def call(method, *args, &block) if block_given? @response_map[@connection.call(method, *args)] = block else @response_map[@connection.call(method, *args)] = proc {} end end |
#on(what, &block) ⇒ Object
Call this with a block to handle a callback with the given name.
144 145 146 |
# File 'lib/maniaplanet_rpc/maniaplanet_client.rb', line 144 def on(what, &block) @callback_map[what] = block end |
#parse_callback(message) ⇒ Object
137 138 139 140 |
# File 'lib/maniaplanet_rpc/maniaplanet_client.rb', line 137 def parse_callback() @callback_map[.first].call @callback_map[:all].call end |