Class: MessagePack::RPC::Future

Inherits:
Object
  • Object
show all
Defined in:
lib/msgpack/rpc/future.rb

Overview

Future describes result of remote procedure call that is initially not known, because it is not yet received. You can wait and get the result with get method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, loop, callback = nil) ⇒ Future

:nodoc:



26
27
28
29
30
31
32
33
34
35
# File 'lib/msgpack/rpc/future.rb', line 26

def initialize(session, loop, callback = nil)  #:nodoc:
	@timeout = session.timeout
	@loop = loop
	@callback_handler = callback
	@error_handler = nil
	@result_handler = nil
	@set = false
	@error = nil
	@result = nil
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



37
38
39
# File 'lib/msgpack/rpc/future.rb', line 37

def error
  @error
end

#loopObject (readonly)

Returns the value of attribute loop.



36
37
38
# File 'lib/msgpack/rpc/future.rb', line 36

def loop
  @loop
end

#resultObject

Returns the value of attribute result.



37
38
39
# File 'lib/msgpack/rpc/future.rb', line 37

def result
  @result
end

Instance Method Details

#attach_callback(proc = nil, &block) ⇒ Object

call-seq:

attach_callback {|future|  }

Attaches a callback method that is called when the result of remote method is received.



76
77
78
# File 'lib/msgpack/rpc/future.rb', line 76

def attach_callback(proc = nil, &block)
	@callback_handler = proc || block
end

#attach_error_handler(proc = nil, &block) ⇒ Object

For IDL



81
82
83
# File 'lib/msgpack/rpc/future.rb', line 81

def attach_error_handler(proc = nil, &block)  #:nodoc:
	@error_handler = proc || block
end

#attach_result_handler(proc = nil, &block) ⇒ Object

For IDL



86
87
88
# File 'lib/msgpack/rpc/future.rb', line 86

def attach_result_handler(proc = nil, &block)  #:nodoc:
	@result_handler = proc || block
end

#getObject

Wait for receiving result of remote procedure call and returns its result. If the remote method raises error, then this method raises RemoteError. If the remote procedure call failed with timeout, this method raises TimeoutError. Otherwise this method returns the result of remote method.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/msgpack/rpc/future.rb', line 43

def get
	join
	if error.nil?
		if @result_handler
			return @result_handler.call(@result)
		else
			return @result
		end
	end
	if @result.nil?
		# compatible error
		raise RuntimeError.new(@error)
	end
	if @error_handler
		@error_handler.call(@error, @result)
	end
	raise RPCError.create(@error, @result)
end

#joinObject

Wait for receiving result of remote procedure call. This method returns self. If a callback method is attached, it will be called.



65
66
67
68
69
70
# File 'lib/msgpack/rpc/future.rb', line 65

def join
	until @set
		@loop.run_once
	end
	self
end

#set_result(err, res) ⇒ Object

:nodoc:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/msgpack/rpc/future.rb', line 90

def set_result(err, res)  #:nodoc:
	@error  = err
	@result = res
	@set = true
	if @callback_handler
		if @callback_handler.arity == 2
			# FIXME backward compatibility
			@callback_handler.call(error, result)
		else
			@callback_handler.call(self)
		end
	end
	self
rescue
	self
end

#step_timeoutObject

:nodoc:



107
108
109
110
111
112
113
114
# File 'lib/msgpack/rpc/future.rb', line 107

def step_timeout  #:nodoc:
	if @timeout < 1
		true
	else
		@timeout -= 1
		false
	end
end