Commits

Dave Cheney committed 26acdf7

refactored code

  • Participants
  • Parent commits c69881c

Comments (0)

Files changed (6)

 # no rubygems, push the libs onto the load path manually
 $: << "#{File.dirname(__FILE__)}/../lib"
 
-require 'heartbeat/openvz.pb'
-require 'heartbeat/timer'
-require 'heartbeat/host'
+require 'heartbeat/area'
 require 'socket'
 
 HEARTBEAT_PORT = 7777
 
-
 socket = UDPSocket.new
 socket.bind('0.0.0.0', HEARTBEAT_PORT)
 
-def announce_new_host(host)
-  puts "New host arrived #{host}"
-end
+host_manager = HostManager.new :area => 1, :zone => 1
+container_manager = ContainerManager.new :manager => host_manager
+aread = AreaDaemon.new:socket => socket, :host_manager => host_manager, :container_manager => container_manager
 
-def announce_awol_host(host)
-  puts "Host #{host} has gone awol"
-end
-
-def announce_new_container(container)
-  puts "New container arrived #{container}"
-end
-
-HOSTS=Hash.new
-
-def process(data)
-  announcement = Openvz::Announcement.new.parse_from_string(data)
-  # puts "Announcement from [zone:#{announcement.zone}, area:#{announcement.area}, host:#{announcement.host}]: Containers: #{announcement.containers.collect {|c| c.id}.join(',')}"
-  
-  host = HOSTS[announcement.host]
-  if host.nil? then
-    host = Host.new(announcement.host)
-    HOSTS[announcement.host] = host
-    announce_new_host host
-  end
-  # reset the hosts' timer
-  host.timer.reset!
-  
-end
-
-def expire
-  HOSTS.each_value do |host|
-    if host.timer.expired? then
-      HOSTS.delete(host)
-      announce_awol_host host 
-    end
-  end
-  
-end
-
-loop do
-  ready = IO.select([socket], [], [], 1)
-  if ready then
-    data, sender = socket.recvfrom(4096)
-    process data
-  else
-    # puts "nothing to read"
-  end
-  expire
-end
+aread.run!

File bin/heartbeatd

 ZONE = 1
 AREA = 1
 HOST = IPSocket.getaddress(`hostname -f`.strip)
+HOST_ADDR = IPAddr.new(HOST)
 
-puts "Announcing: [zone:#{ZONE}, area:#{AREA}, host:#{HOST}]"
+puts "Announcing: [zone:#{ZONE}, area:#{AREA}, host:#{HOST_ADDR}]"
 
 HEARTBEAT_PORT = 7777
 
   announcement = Openvz::Announcement.new
   announcement.zone = ZONE
   announcement.area = AREA
-  announcement.host = HOST.to_i
+  announcement.host = HOST_ADDR.to_i
   announcement.sequence = Time::now.to_i
   announcement.containers = parse_proc_veinfo
   announcement

File lib/heartbeat/area.rb

+require 'heartbeat/container'
+require 'heartbeat/openvz.pb'
+
+class AreaDaemon 
+
+  def initialize(opts)
+    @host_manager = opts[:host_manager]
+    @container_manager = opts[:container_manager]
+    @socket = opts[:socket]
+  end
+  
+  def process(data)
+    announcement = Openvz::Announcement.new.parse_from_string(data)
+    puts "Announcement from [zone:#{announcement.zone}, area:#{announcement.area}, host:#{announcement.host}]: Containers: #{announcement.containers.collect {|c| c.id}.join(',')}"
+    host = @host_manager.announce(announcement.host)
+    announcement.containers.each { |c| @container_manager.announce(host, c) }
+  end
+  
+  def run!
+    loop do
+      ready = IO.select([@socket], [], [], 1)
+      if ready then
+        data, sender = @socket.recvfrom(4096)
+        process data
+      else
+        # puts "nothing to read"
+      end
+      
+      @host_manager.expire
+      @container_manager.expire
+    end
+    
+  end
+      
+end

File lib/heartbeat/container.rb

+require 'heartbeat/timer'
+require 'heartbeat/host'
+
+class Container
+  attr_reader :host, :id
+
+  def initialize(host, id)
+    @host = host
+    @id = id
+  end
+    
+end
+
+class ContainerManager 
+  attr_reader :manager
+  
+  
+  def initialize(manager)
+    @manager = manager #HostManager
+  end
+  
+  def announce(host, container)
+    
+  end
+  
+  def expire
+    
+  end
+  
+end
+    

File lib/heartbeat/host.rb

 require 'heartbeat/timer'
+require 'ipaddr'
 
-class Base
-  attr_reader :admin_state, :operational_state
-end
+class Host
+  attr_reader :zone, :area, :addr
 
-class Host < Base
-  attr_reader :zone, :area, :id, :timer
-
-  def initialize(id)
+  def initialize(addr)
     @zone = 1
     @area = 1
-    @id = id
-    @timer = Timer.new(3) # 3 second timeout until considered dead
+    @addr = addr
   end
   
   def to_s
   
 end
 
-class Container < Base
-  attr_reader :host, :id, :timer
+class HostManager 
+  attr_reader :zone, :area
 
-  def initialize(host, id)
-    @host = host
-    @id = id
-    @timer = Timer.new(3) # 3 second timeout until considered dead
+  def initialize(opts)
+    @zone = opts[:zone]
+    @area = opts[:area]
+    @hosts = {}
   end
   
+  def announce(host_addr)
+    addr = IPAddr.new(host_addr, Socket::AF_INET)
+    timed = @hosts[addr]
+    unless timed then
+      host = Host.new(addr)
+      timed = Timed.new(host)
+      @hosts[addr] = timed
+      # announce host
+    end
+    timed.reset!
+  end
   
-end
+  def expire
+    
+  end
+  
+    
+end

File lib/heartbeat/timer.rb

     Time.now > @expiry
   end
     
-end
+end
+
+class Timed < Timer
+  attr_reader :value
+  
+  def initialize(value)
+    super(3)
+    @value = value
+  end
+  
+  def reset!
+    super
+    value
+  end
+end
+