Class: Phut::VhostDaemon

Inherits:
Object
  • Object
show all
Defined in:
lib/phut/vhost_daemon.rb

Overview

vhost daemon process rubocop:disable ClassLength

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ VhostDaemon

Returns a new instance of VhostDaemon.



20
21
22
23
24
25
26
27
# File 'lib/phut/vhost_daemon.rb', line 20

def initialize(options)
  @options = options.dup
  @options[:device] = @options.fetch(:interface)
  @options[:log_dir] = File.expand_path(@options[:log_dir])
  @options[:pid_dir] = File.expand_path(@options[:pid_dir])
  @options[:socket_dir] = File.expand_path(@options[:socket_dir])
  reset_stats
end

Class Method Details

.process(name, socket_dir) ⇒ Object



16
17
18
# File 'lib/phut/vhost_daemon.rb', line 16

def self.process(name, socket_dir)
  DRbObject.new_with_uri(unix_domain_socket(name, socket_dir))
end

.unix_domain_socket(name, socket_dir) ⇒ Object



12
13
14
# File 'lib/phut/vhost_daemon.rb', line 12

def self.unix_domain_socket(name, socket_dir)
  "drbunix:#{socket_dir}/vhost.#{name}.ctl"
end

Instance Method Details

#arp_table=(arp_table) ⇒ Object



88
89
90
# File 'lib/phut/vhost_daemon.rb', line 88

def arp_table=(arp_table)
  @options[:arp_table] = arp_table
end

#deviceObject



41
42
43
# File 'lib/phut/vhost_daemon.rb', line 41

def device
  @options[:device]
end

#device=(name) ⇒ Object



45
46
47
# File 'lib/phut/vhost_daemon.rb', line 45

def device=(name)
  @options[:device] = name
end

#ip_addressObject



33
34
35
# File 'lib/phut/vhost_daemon.rb', line 33

def ip_address
  @options.fetch :ip_address
end

#killObject



66
67
68
# File 'lib/phut/vhost_daemon.rb', line 66

def kill
  @kill = true
end

#mac_addressObject



37
38
39
# File 'lib/phut/vhost_daemon.rb', line 37

def mac_address
  @options.fetch :mac_address
end

#nameObject



29
30
31
# File 'lib/phut/vhost_daemon.rb', line 29

def name
  @options.fetch :name
end

#reset_statsObject



83
84
85
86
# File 'lib/phut/vhost_daemon.rb', line 83

def reset_stats
  @packets_sent = []
  @packets_received = []
end

#runObject



49
50
51
52
53
54
55
56
# File 'lib/phut/vhost_daemon.rb', line 49

def run
  start_logging
  create_pid_file
  start_daemon
  @stop = false
rescue
  shutdown
end

#running?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/phut/vhost_daemon.rb', line 58

def running?
  FileTest.exists?(pid_file)
end

#send_packets(dest, npackets) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/phut/vhost_daemon.rb', line 70

def send_packets(dest, npackets)
  return unless lookup_arp_table(dest.ip_address)
  udp = create_udp_packet(dest)
  npackets.times do
    write_to_raw_socket udp
    @logger.info "Sent to #{dest.name}: #{udp}"
  end
end

#statsObject



79
80
81
# File 'lib/phut/vhost_daemon.rb', line 79

def stats
  { tx: @packets_sent, rx: @packets_received }
end

#stopObject



62
63
64
# File 'lib/phut/vhost_daemon.rb', line 62

def stop
  @stop = true
end