Class: ManiaRPC::ManiaClient

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#connectionObject (readonly)

Returns the value of attribute connection.



125
126
127
# File 'lib/maniaplanet_rpc/maniaplanet_client.rb', line 125

def connection
  @connection
end

#response_mapObject

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(message)
  @callback_map[message.first].call message
  @callback_map[:all].call message
end