Class: DbRole

Inherits:
Role
  • Object
show all
Includes:
Server
Defined in:
lib/db_role.rb

Constant Summary collapse

PORT =
9200

Constants inherited from Role

Role::INDEX

Instance Method Summary collapse

Methods included from Server

#run_server, #shutdown_server

Methods inherited from Role

#disable_link, #enable_link, get_role, #init_links, role

Constructor Details

#initialize(manifest) ⇒ DbRole

Returns a new instance of DbRole.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/db_role.rb', line 13

def initialize(manifest)
  super
  @queues = Hash.new { |h,k| h[k] = [] }
  init_links 'slaves', @manifest['slaves'] do |event,addr|
    case event
    when 'enable'
      @queues[addr].each { |msg|
        begin
          slave_sock = DemoSocket.connect(addr, PORT)
          slave_sock.send msg
          slave_sock.recv
        rescue Errno::ECONNREFUSED, Errno::ECONNABORTED
        end
      }
    end
  end

  @data = {
    'lorem' => { :value => 'ipsum', :updated => Time.now.to_i },
    'augue' => { :value => 'porta', :updated => Time.now.to_i },
    'congue' => { :value => 'turpis', :updated => Time.now.to_i },
    'vitae' => { :value => 'consequat', :updated => Time.now.to_i },
    'accumsan' => { :value => 'nascetur', :updated => Time.now.to_i },
    'diam' => { :value => 'dolor', :updated => Time.now.to_i },
    'parturient' => { :value => 'viverra', :updated => Time.now.to_i },
  }
end

Instance Method Details

#startObject Also known as: enable



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/db_role.rb', line 41

def start
  @slave_sockets = {}

  semaphore = Mutex.new

  run_server '0.0.0.0', PORT.to_s do |sock|
    msg = sock.recv

    reply = nil
    semaphore.synchronize {
      case msg['cmd']
      when 'GET'
        record = @data[msg['key']]
        reply = {
          'key' => msg['key'],
          'value' => record[:value],
          'updated' => record[:updated]
        }
      when 'PUT'
        msg['updated'] ||= Time.now.to_i

        @data[msg['key']] = { :value => msg['value'], :updated => msg['updated'] }

        @active_links['slaves'].each do |slave_addr|
          begin
            slave_sock = DemoSocket.connect(slave_addr, PORT)
            slave_sock.send msg
            slave_sock.recv
          rescue Errno::ECONNREFUSED, Errno::ECONNABORTED
          end
        end

        @disabled_links['slaves'].each do |slave_addr|
          @queues[slave_addr] << msg
        end

        record = @data[msg['key']]
        reply = {
          'key' => msg['key'],
          'value' => record[:value],
          'updated' => record[:updated]
        }
      when 'LIST'
        reply = @data.map { |k,record|
          { 'key' => k,
            'value' => record[:value],
            'updated' => record[:updated] }
        }
      end
    }

    sock.send(reply)
    sock.close
  end
end

#stateObject



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

def state
  super.merge('extra' => { 'slave' => @manifest['slave'] })
end

#stopObject Also known as: disable



97
98
99
100
101
102
# File 'lib/db_role.rb', line 97

def stop
  shutdown_server '0.0.0.0', PORT.to_s
  @slave_sockets.each do |addr, socket|
    socket.close
  end
end