Class: GameMachine::GameSystems::Agents::Controller
- Inherits:
-
Actor::Base
- Object
- JavaLib::GameActor
- Actor::Base
- GameMachine::GameSystems::Agents::Controller
show all
- Defined in:
- lib/game_machine/game_systems/agents/controller.rb
Constant Summary
Constants inherited
from Actor::Base
Actor::Base::ON_RECEIVE_HOOKS
Instance Attribute 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, model_filter, #onReceive, player_controller, #receive_message, #schedule_message, #sender, set_player_controller
Instance Attribute Details
#address ⇒ Object
Returns the value of attribute address.
7
8
9
|
# File 'lib/game_machine/game_systems/agents/controller.rb', line 7
def address
@address
end
|
#agent_config ⇒ Object
Returns the value of attribute agent_config.
7
8
9
|
# File 'lib/game_machine/game_systems/agents/controller.rb', line 7
def agent_config
@agent_config
end
|
#children ⇒ Object
Returns the value of attribute children.
7
8
9
|
# File 'lib/game_machine/game_systems/agents/controller.rb', line 7
def children
@children
end
|
#current_agents ⇒ Object
Returns the value of attribute current_agents.
7
8
9
|
# File 'lib/game_machine/game_systems/agents/controller.rb', line 7
def current_agents
@current_agents
end
|
Instance Method Details
#child_name(agent_name) ⇒ Object
94
95
96
|
# File 'lib/game_machine/game_systems/agents/controller.rb', line 94
def child_name(agent_name)
"agent_#{agent_name}"
end
|
#create_agents(agents) ⇒ Object
66
67
68
|
# File 'lib/game_machine/game_systems/agents/controller.rb', line 66
def create_agents(agents)
agents.map {|agent_name,klass| create_child(agent_name,klass)}
end
|
#create_child(agent_name, klass_name) ⇒ Object
80
81
82
83
84
85
86
87
|
# File 'lib/game_machine/game_systems/agents/controller.rb', line 80
def create_child(agent_name,klass_name)
klass = klass_name.constantize
name = child_name(agent_name)
builder = Actor::Builder.new(klass,agent_name)
child = builder.with_parent(context).with_name(name).start
@children[agent_name] = Actor::Ref.new(child,klass.name)
GameMachine.logger.info "Agent #{agent_name} created"
end
|
#destroy_agents(agents) ⇒ Object
62
63
64
|
# File 'lib/game_machine/game_systems/agents/controller.rb', line 62
def destroy_agents(agents)
agents.map {|agent_name,klass| destroy_child(agent_name)}
end
|
#destroy_child(agent_name) ⇒ Object
70
71
72
73
74
75
76
77
78
|
# File 'lib/game_machine/game_systems/agents/controller.rb', line 70
def destroy_child(agent_name)
if @children.has_key?(agent_name)
tell_child(agent_name,JavaLib::PoisonPill.get_instance)
@children.delete(agent_name)
GameMachine.logger.info "Agent #{agent_name} killed"
else
GameMachine.logger.info "Agent #{agent_name} not found, unable to kill"
end
end
|
#local_address ⇒ Object
98
99
100
101
|
# File 'lib/game_machine/game_systems/agents/controller.rb', line 98
def local_address
name = AppConfig.instance.config.name
Akka.address_for(name)
end
|
#local_agents ⇒ Object
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/game_machine/game_systems/agents/controller.rb', line 103
def local_agents
{}.tap do |agents|
agent_config.agent_names.each do |name|
node = Akka.instance.hashring.node_for(name)
if node == address
agents[name] = agent_config.klass_for_name(name)
end
end
end
end
|
#on_receive(message) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/game_machine/game_systems/agents/controller.rb', line 20
def on_receive(message)
if message.is_a?(String)
if message == 'agent_tick'
@children.each do |name,actor_ref|
actor_ref.tell('update',get_self)
end
elsif message == 'check_config'
if agent_config.reload?
agent_config.load!
update_agents
end
elsif message == 'cluster_update'
update_agents
end
end
end
|
#post_init(*args) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/game_machine/game_systems/agents/controller.rb', line 8
def post_init(*args)
@agent_config = args.first
@agent_config.load!
@address = local_address
@current_agents = local_agents
@children = {}
create_agents(current_agents)
schedule_message('agent_tick',100)
schedule_message('check_config',10000)
ClusterMonitor.find.tell('register_observer',get_self)
end
|
#tell_child(agent_name, message) ⇒ Object
89
90
91
92
|
# File 'lib/game_machine/game_systems/agents/controller.rb', line 89
def tell_child(agent_name,message)
name = child_name(agent_name)
@children[agent_name].tell(message,nil)
end
|
#update_agents ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/game_machine/game_systems/agents/controller.rb', line 38
def update_agents
agents_to_create = {}
agents_to_destroy = {}
updated_agents = local_agents
current_agents.each do |name,klass|
unless updated_agents.has_key?(name)
agents_to_destroy[name] = klass
end
end
updated_agents.each do |name,klass|
unless current_agents.has_key?(name)
agents_to_create[name] = klass
end
end
@current_agents = updated_agents
destroy_agents(agents_to_destroy)
create_agents(agents_to_create)
end
|