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.
52
53
54
55
56
|
# File 'lib/redisrpc.rb', line 52
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/redisrpc.rb', line 58
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 = JSON.generate 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 = JSON.parse 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, charset = %w{ 1 2 3 4 5 6 7 8 9 0 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z}) ⇒ Object
89
90
91
|
# File 'lib/redisrpc.rb', line 89
def rand_string(size=8, charset=%w{ 1 2 3 4 5 6 7 8 9 0 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z})
return (0...size).map{ charset.to_a[rand(charset.size)] }.join
end
|
#respond_to?(sym) ⇒ Boolean
93
94
95
|
# File 'lib/redisrpc.rb', line 93
def respond_to?(sym)
return true
end
|