Class: GameMachine::ClusterMonitor

Inherits:
Actor::Base
  • Object
show all
Defined in:
server/lib/game_machine/cluster_monitor.rb

Constant Summary

Constants inherited from Actor::Base

Actor::Base::ON_RECEIVE_HOOKS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Actor::Base

aspect, aspects, find, find_by_address, find_distributed, find_distributed_local, find_remote, hashring, local_path, logger, model_filter, #onReceive, player_controller, #receive_message, #schedule_message, #schedule_message_once, #sender, set_player_controller

Class Method Details

.add_cluster_member(address, member) ⇒ Object


20
21
22
# File 'server/lib/game_machine/cluster_monitor.rb', line 20

def self.add_cluster_member(address,member)
  cluster_members[address] = member
end

.add_remote_member(address, member) ⇒ Object


32
33
34
# File 'server/lib/game_machine/cluster_monitor.rb', line 32

def self.add_remote_member(address,member)
  remote_members[address] = member
end

.cluster_membersObject


4
5
6
7
8
9
10
# File 'server/lib/game_machine/cluster_monitor.rb', line 4

def self.cluster_members
  if @cluster_members
    @cluster_members
  else
    @cluster_members = java.util.concurrent.ConcurrentHashMap.new
  end
end

.remote_membersObject


12
13
14
15
16
17
18
# File 'server/lib/game_machine/cluster_monitor.rb', line 12

def self.remote_members
  if @remote_members
    @remote_members
  else
    @remote_members = java.util.concurrent.ConcurrentHashMap.new
  end
end

.remove_cluster_member(address) ⇒ Object


24
25
26
# File 'server/lib/game_machine/cluster_monitor.rb', line 24

def self.remove_cluster_member(address)
  cluster_members.delete(address)
end

.remove_remote_member(address) ⇒ Object


28
29
30
# File 'server/lib/game_machine/cluster_monitor.rb', line 28

def self.remove_remote_member(address)
  remote_members.delete(address)
end

Instance Method Details

#notify_member_downObject


54
55
56
# File 'server/lib/game_machine/cluster_monitor.rb', line 54

def notify_member_down
  @notify_on_down_observers.each {|i| i[1].tell(i[0],get_self)}
end

#notify_member_upObject


50
51
52
# File 'server/lib/game_machine/cluster_monitor.rb', line 50

def notify_member_up
  @notify_on_up_observers.each {|i| i[1].tell(i[0],get_self)}
end

#notify_observersObject


58
59
60
# File 'server/lib/game_machine/cluster_monitor.rb', line 58

def notify_observers
  @observers.each {|observer| observer.tell('cluster_update',get_self)}
end

#on_receive(message) ⇒ Object


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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'server/lib/game_machine/cluster_monitor.rb', line 62

def on_receive(message)
  if message.is_a?(String)
    if message == 'register_observer'
      @observers << sender
    elsif message == 'notify_on_up'
      @notify_on_up_observers << [message,sender]
    elsif message == 'notify_on_down'
      @notify_on_down_observers << [message,sender]
    end
  elsif message.is_a?(JavaLib::ClusterEvent::SeenChanged)

  elsif message.is_a?(JavaLib::ClusterEvent::MemberRemoved)
    address = message.member.address.to_string
    Akka.instance.hashring.remove_node(address)
    self.class.remove_cluster_member(address)
    self.class.remove_remote_member(address)

    notify_observers
    notify_member_down

    self.class.logger.info "MemberRemoved #{address}"
  elsif message.is_a?(JavaLib::ClusterEvent::MemberUp)
    address = message.member.address.to_string
    self.class.add_cluster_member(address,message.member)
    Akka.instance.hashring.add_node(address)

    unless address == @cluster.self_address.to_string
      self.class.add_remote_member(address,message.member)
    end

    notify_member_up
    notify_observers
    self.class.logger.info "MemberUp #{address}"
  elsif message.is_a?(JavaLib::ClusterEvent::ClusterMetricsChanged)

  elsif message.is_a?(JavaLib::ClusterEvent::CurrentClusterState)
    message.get_members.each do |member|
      address = member.address.to_string
      self.class.add_cluster_member(address,member)
      Akka.instance.hashring.add_node(address)
      unless address == @cluster.self_address.to_string
        self.class.add_remote_member(address,member)
      end
    end

    notify_observers

  else
    #GameMachine.logger.info("Unrecognized message #{message}")
  end
end

#preStartObject


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'server/lib/game_machine/cluster_monitor.rb', line 36

def preStart
  if getContext.system.name == 'cluster'
    @cluster = JavaLib::Cluster.get(getContext.system)
    Akka.instance.init_cluster!(@cluster.self_address.to_string)
    @cluster.subscribe(getSelf,
                       JavaLib::ClusterEvent::MemberEvent.java_class,
                       JavaLib::ClusterEvent::UnreachableMember.java_class)
    self.class.logger.info "Cluster address #{@cluster.self_address.to_string}"
  end
  @observers = []
  @notify_on_up_observers = []
  @notify_on_down_observers = []
end