Class: Packet::MetaPimp
Overview
Class acts as a pimp for workers, which doesn’t have a manually created pimp The idea behind a manually added pimp is to let client handle low level messaging beween workers. A meta pimp, does it for you.
Instance Attribute Summary collapse
-
#callback_hash ⇒ Object
initializer of pimp.
-
#invokable_worker_methods ⇒ Object
Returns the value of attribute invokable_worker_methods.
-
#worker_key ⇒ Object
Returns the value of attribute worker_key.
-
#worker_name ⇒ Object
Returns the value of attribute worker_name.
-
#worker_status ⇒ Object
Returns the value of attribute worker_status.
Attributes inherited from Pimp
#fd_write_end, #lifeline, #outbound_data, #pid, #reactor, #signature, #workers
Instance Method Summary collapse
- #handle_object(data_options = {}) ⇒ Object
- #pimp_init ⇒ Object
- #process_request(data_options = {}) ⇒ Object
- #process_response(data_options = {}) ⇒ Object
-
#receive_data(p_data) ⇒ Object
will be invoked whenever there is a response from the worker.
- #save_worker_result(data_options = { }) ⇒ Object
- #save_worker_status(data_options = { }) ⇒ Object
-
#send_request(data_options = { }) ⇒ Object
can be used to send request to correspoding worker.
Methods inherited from Pimp
#initialize, #send_data, #send_fd
Methods included from ClassHelpers
iattr_accessor, inheritable_attribute, metaclass
Methods included from NbioHelper
#dump_object, #gen_worker_key, #object_dump, #packet_classify, #read_data, #write_and_schedule, #write_once
Constructor Details
This class inherits a constructor from Packet::Pimp
Instance Attribute Details
#callback_hash ⇒ Object
initializer of pimp
7 8 9 |
# File 'lib/packet/packet_meta_pimp.rb', line 7 def callback_hash @callback_hash end |
#invokable_worker_methods ⇒ Object
Returns the value of attribute invokable_worker_methods.
9 10 11 |
# File 'lib/packet/packet_meta_pimp.rb', line 9 def invokable_worker_methods @invokable_worker_methods end |
#worker_key ⇒ Object
Returns the value of attribute worker_key.
8 9 10 |
# File 'lib/packet/packet_meta_pimp.rb', line 8 def worker_key @worker_key end |
#worker_name ⇒ Object
Returns the value of attribute worker_name.
8 9 10 |
# File 'lib/packet/packet_meta_pimp.rb', line 8 def worker_name @worker_name end |
#worker_status ⇒ Object
Returns the value of attribute worker_status.
8 9 10 |
# File 'lib/packet/packet_meta_pimp.rb', line 8 def worker_status @worker_status end |
Instance Method Details
#handle_object(data_options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/packet/packet_meta_pimp.rb', line 27 def handle_object = {} case [:type] when :request process_request() when :response process_response() when :status save_worker_status() when :result save_worker_result() end end |
#pimp_init ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/packet/packet_meta_pimp.rb', line 11 def pimp_init @callback_hash ||= {} @worker_status = nil @worker_result = nil @worker_key = nil @tokenizer = Packet::BinParser.new end |
#process_request(data_options = {}) ⇒ Object
49 50 51 52 53 |
# File 'lib/packet/packet_meta_pimp.rb', line 49 def process_request( = {}) if((requested_worker = [:requested_worker]) && (reactor.live_workers[requested_worker])) reactor.live_workers[requested_worker].send_request() end end |
#process_response(data_options = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/packet/packet_meta_pimp.rb', line 55 def process_response( = {}) if callback_signature = [:callback_signature] callback = callback_hash[callback_signature] # there coule be bug when you are trying to send the data back to the client begin callback.invoke() rescue end elsif client_signature = [:client_signature] begin reactor.connections[client_signature][:instance].worker_receive() rescue end end end |
#receive_data(p_data) ⇒ Object
will be invoked whenever there is a response from the worker
20 21 22 23 24 25 |
# File 'lib/packet/packet_meta_pimp.rb', line 20 def receive_data p_data @tokenizer.extract(p_data) do |b_data| t_data = Marshal.load(b_data) handle_object(t_data) end end |
#save_worker_result(data_options = { }) ⇒ Object
40 41 42 |
# File 'lib/packet/packet_meta_pimp.rb', line 40 def save_worker_result( = { }) @worker_result = [:data] end |
#save_worker_status(data_options = { }) ⇒ Object
44 45 46 47 |
# File 'lib/packet/packet_meta_pimp.rb', line 44 def save_worker_status( = { }) # @worker_status = data_options[:data] reactor.update_result(worker_key,[:data]) end |
#send_request(data_options = { }) ⇒ Object
can be used to send request to correspoding worker
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/packet/packet_meta_pimp.rb', line 72 def send_request( = { }) if callback = [:callback] callback_hash[callback.signature] = callback .delete(:callback) [:callback_signature] = callback.signature send_data() else send_data() end end |