Class: Packet::Worker

Inherits:
Object
  • Object
show all
Includes:
Core
Defined in:
lib/packet/packet_worker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Core

included

Constructor Details

#initializeWorker

Returns a new instance of Worker.



31
32
33
34
35
# File 'lib/packet/packet_worker.rb', line 31

def initialize
  super
  @read_ios << msg_reader
  @tokenizer = BinParser.new
end

Instance Attribute Details

#worker_optionsObject

Returns the value of attribute worker_options.



9
10
11
# File 'lib/packet/packet_worker.rb', line 9

def worker_options
  @worker_options
end

#worker_startedObject

Returns the value of attribute worker_started.



9
10
11
# File 'lib/packet/packet_worker.rb', line 9

def worker_started
  @worker_started
end

Class Method Details

.inherited(subklass) ⇒ Object

copy the inherited attribute in class thats inheriting this class



25
26
27
# File 'lib/packet/packet_worker.rb', line 25

def self.inherited(subklass)
  subklass.send(:"connection_callbacks=",connection_callbacks)
end

.is_worker?Boolean

Returns:

  • (Boolean)


29
# File 'lib/packet/packet_worker.rb', line 29

def self.is_worker?; true; end

.start_worker(messengers = {}) ⇒ Object

method initializes the eventloop for the worker



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/packet/packet_worker.rb', line 12

def self.start_worker(messengers = {})
  # @fd_reader = args.shift if args.length > 2
  @msg_writer = messengers[:write_end]
  @msg_reader = messengers[:read_end]

  t_instance = new
  t_instance.worker_options = messengers[:options]
  t_instance.worker_init if t_instance.respond_to?(:worker_init)
  t_instance.start_reactor
  t_instance
end

Instance Method Details

#handle_internal_messages(t_sock) ⇒ Object

method handles internal requests from internal sockets



52
53
54
55
56
57
58
59
60
# File 'lib/packet/packet_worker.rb', line 52

def handle_internal_messages(t_sock)
  begin
    t_data = read_data(t_sock)
    receive_internal_data(t_data)
  rescue DisconnectError => sock_error
    # Means, when there is an error from sockets from which we are reading better just terminate
    terminate_me()
  end
end

#invoke_callbackObject

message returns data to parent process, using UNIX Sockets



84
85
86
# File 'lib/packet/packet_worker.rb', line 84

def invoke_callback
  raise "Not implemented for worker"
end

#invoke_internal_functionObject

method checks if client has asked to execute a internal function



79
80
81
# File 'lib/packet/packet_worker.rb', line 79

def invoke_internal_function
  raise "Not implemented for worker"
end

#log(log_data) ⇒ Object



69
70
71
# File 'lib/packet/packet_worker.rb', line 69

def log log_data
  send_data(:requested_worker => :log_worker,:data => log_data,:type => :request)
end

#receive_data(p_data) ⇒ Object

method receives data from external TCP Sockets



74
75
76
# File 'lib/packet/packet_worker.rb', line 74

def receive_data p_data
  raise "Not implemented for worker"
end

#receive_internal_data(data) ⇒ Object



62
63
64
65
66
67
# File 'lib/packet/packet_worker.rb', line 62

def receive_internal_data data
  @tokenizer.extract(data) do |b_data|
    data_obj = Marshal.load(b_data)
    receive_data(data_obj)
  end
end

#send_data(p_data) ⇒ Object



37
38
39
# File 'lib/packet/packet_worker.rb', line 37

def send_data p_data
  dump_object(p_data,msg_writer)
end

#send_request(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/packet/packet_worker.rb', line 41

def send_request(options = {})
  t_data = options[:data]
  if t_callback = options[:callback]
    callback_hash[t_callback.signature] = t_callback
    send_data(:data => t_data,:function => options[:function],:callback_signature => t_callback.signature)
  else
    send_data(:data => t_data,:function => options[:function],:requested_worker => options[:worker],:requesting_worker => worker_name,:type => :request)
  end
end