Class: RedisRPC::Client
- Inherits:
-
Object
show all
- Defined in:
- lib/redisrpc.rb
Instance Method Summary
collapse
Constructor Details
#initialize(redis_server, message_queue, timeout = 0) ⇒ Client
Returns a new instance of Client.
46
47
48
49
50
|
# File 'lib/redisrpc.rb', line 46
def initialize(redis_server, message_queue, timeout=0)
@redis_server = redis_server
@message_queue = message_queue
@timeout = timeout
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/redisrpc.rb', line 52
def method_missing(sym, *args, &block)
function_call = {'name' => sym.to_s, 'args' => args}
response_queue = @message_queue + ':rpc:' + rand_string
rpc_request = {'function_call' => function_call, 'response_queue' => response_queue}
message = MultiJson.dump rpc_request
if $DEBUG
$stderr.puts 'RPC Request: ' + message
end
@redis_server.rpush @message_queue, message
result = @redis_server.blpop response_queue, @timeout
if result.nil?
raise TimeoutException
end
message_queue, message = result
if $DEBUG
if message_queue != response_queue
fail 'assertion failed'
end
$stderr.puts 'RPC Response: ' + message
end
rpc_response = MultiJson.load message
exception = rpc_response['exception']
if exception != nil
raise RemoteException, exception
end
if not rpc_response.has_key? 'return_value'
raise RemoteException, 'Malformed RPC Response message: ' + rpc_response
end
return rpc_response['return_value']
end
|
Instance Method Details
#rand_string(size = 8) ⇒ Object
83
84
85
|
# File 'lib/redisrpc.rb', line 83
def rand_string(size=8)
return rand(36**size).to_s(36).upcase.rjust(size,'0')
end
|
#respond_to?(sym) ⇒ Boolean
87
88
89
|
# File 'lib/redisrpc.rb', line 87
def respond_to?(sym)
return true
end
|