Class: GameMachine::ClientManager

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

Constant Summary

Constants inherited from Actor::Base

Actor::Base::ON_RECEIVE_HOOKS

Instance Attribute Summary collapse

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, model_filter, #onReceive, player_controller, #receive_message, #schedule_message, #sender, set_player_controller

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



4
5
6
# File 'lib/game_machine/client_manager.rb', line 4

def channel
  @channel
end

#client_to_playerObject (readonly)

Returns the value of attribute client_to_player.



4
5
6
# File 'lib/game_machine/client_manager.rb', line 4

def client_to_player
  @client_to_player
end

#local_actorsObject (readonly)

Returns the value of attribute local_actors.



4
5
6
# File 'lib/game_machine/client_manager.rb', line 4

def local_actors
  @local_actors
end

#local_clientsObject (readonly)

Returns the value of attribute local_clients.



4
5
6
# File 'lib/game_machine/client_manager.rb', line 4

def local_clients
  @local_clients
end

#playersObject (readonly)

Returns the value of attribute players.



4
5
6
# File 'lib/game_machine/client_manager.rb', line 4

def players
  @players
end

#remote_clientsObject (readonly)

Returns the value of attribute remote_clients.



4
5
6
# File 'lib/game_machine/client_manager.rb', line 4

def remote_clients
  @remote_clients
end

Class Method Details

.cluster_connectionsObject

Cluster only connections. We should not be handling things like entity tracking for this type of connection. This is for stuff that is not latency sensitive such as chat, groups, etc..



22
23
24
# File 'lib/game_machine/client_manager.rb', line 22

def self.cluster_connections
  local_players.select {|player_id,type| type == 'cluster'}
end

.local_connectionsObject

Includes region + combined.



15
16
17
# File 'lib/game_machine/client_manager.rb', line 15

def self.local_connections
  local_players.select {|player_id,type| type != 'cluster'}
end

.local_playersObject



6
7
8
9
10
11
12
# File 'lib/game_machine/client_manager.rb', line 6

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

.send_to_player(message) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/game_machine/client_manager.rb', line 26

def self.send_to_player(message)
  if local_players.has_key?(message.player.id)
    Actor::Base.find(message.player.id).tell(message)
  else
    find.tell(message)
  end
end

Instance Method Details

#cluster_connection?(client_connection) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
130
131
132
133
# File 'lib/game_machine/client_manager.rb', line 124

def cluster_connection?(client_connection)
  case client_connection.type
  when 'cluster'
    true
  when 'combined'
    true
  else
    false
  end
end

#create_client_event(client_id, player_id, event) ⇒ Object



191
192
193
194
195
# File 'lib/game_machine/client_manager.rb', line 191

def create_client_event(client_id,player_id,event)
  client_event = MessageLib::ClientEvent.new.set_client_id(client_id).
    set_event(event).set_player_id(player_id)
  client_event.set_sender_id(Akka.instance.address + '|' + self.class.name)
end

#create_client_manager_event(client_id, player_id, event) ⇒ Object



186
187
188
189
# File 'lib/game_machine/client_manager.rb', line 186

def create_client_manager_event(client_id,player_id,event)
  client_manager_event = MessageLib::ClientManagerEvent.new.
    set_client_id(client_id).set_player_id(player_id).set_event(event)
end

#on_receive(message) ⇒ Object



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
# File 'lib/game_machine/client_manager.rb', line 47

def on_receive(message)

  if message.is_a?(MessageLib::Entity)

    # Outgoing message to player
    if message.send_to_player
      if self.class.local_players.has_key?(message.player.id)
        Actor::Base.find(message.player.id).tell(message)
      else
       send_to_remote_player(message)
      end

    # client events come from other client managers
    elsif message.has_client_event
      if message.client_event.sender_id.match(/#{@server}/)
        return
      end
      process_client_event(message.client_event)
    
    # Unregister requests come from clients only
    elsif message.has_client_manager_unregister
      unregister_sender(message)

    # Register requests come from actors and clients
    elsif message.has_client_manager_register
      register_sender(message)
    else
      unhandled(message)
    end
  elsif message.is_a?(JavaLib::ClusterEvent::MemberUp)
    update_new_member(address)
  else
    unhandled(message)
  end
end

#post_init(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/game_machine/client_manager.rb', line 34

def post_init(*args)
  @server = Akka.instance.address
  @channel = 'client_events'
  @local_actors = {}
  @remote_clients = {}
  @local_clients = {}
  @players = {}
  @client_to_player = {}
  subscribe(channel)
  ClusterMonitor.find.tell('notify_on_up',get_self)
  @cluster = JavaLib::Cluster.get(getContext.system)
end

#process_client_event(client_event) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/game_machine/client_manager.rb', line 109

def process_client_event(client_event)
  if client_event.event == 'disconnected'
    remote_clients.delete(client_event.client_id)
    client_to_player.delete(client_event.client_id)
    players.delete(client_event.player_id)
    GameMachine.logger.debug("#{self.class.name} client #{client_event.client_id} disconnected")
  elsif client_event.event == 'connected'
    remote_ref = sender_id_to_actor_ref(client_event.sender_id)
    remote_clients[client_event.client_id] = remote_ref
    players[client_event.player_id] = client_event.client_id
    client_to_player[client_event.client_id] = client_event.player_id
    GameMachine.logger.debug("#{self.class.name} #{Application.config.name} client #{client_event.client_id} connected")
  end
end

#register_sender(message) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/game_machine/client_manager.rb', line 154

def register_sender(message)
  register_msg = message.client_manager_register
  register_type = register_msg.register_type
  name = register_msg.name
  events = register_msg.events

  # Client register
  if register_type == 'client'
    if cluster_connection?(message.client_connection)
      send_client_event(name,message.player.id,'connected')
    end
    local_clients[name] = message.client_connection
    players[message.player.id] = name
    self.class.local_players[message.player.id] = message.client_connection.type
    get_sender.tell(message,get_self)
    GameMachine.logger.debug("#{self.class.name} client #{name} registered")
  # Actor register
  elsif register_type == 'actor'
    GameMachine.logger.debug "#{self.class.name} Actor #{name} registered"
    local_actors[name] = events
  end
end

#send_client_event(client_id, player_id, event) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/game_machine/client_manager.rb', line 197

def send_client_event(client_id,player_id,event)
  client_event = create_client_event(client_id,player_id,event)
  entity = MessageLib::Entity.new.set_id('0').set_client_event(client_event)
  publish = MessageLib::Publish.new
  publish.set_topic(channel).set_message(entity)
  MessageQueue.find.tell(publish,get_self)
end

#send_client_manager_event(client_id, player_id, event) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/game_machine/client_manager.rb', line 178

def send_client_manager_event(client_id,player_id,event)
  local_actors.each do |name,events|
    client_manager_event = create_client_manager_event(client_id,player_id,event)
    Actor::Base.find(name).tell(client_manager_event)
    GameMachine.logger.info "#{self.class.name} Send #{event} to #{name}"
  end
end

#send_to_remote_player(message) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/game_machine/client_manager.rb', line 96

def send_to_remote_player(message)
  if client_id = players.fetch(message.player.id,nil)
    if remote_ref = remote_clients.fetch(client_id,nil)
      remote_ref.tell(message)
    end
  end
end

#sender_id_to_actor_ref(sender_id) ⇒ Object



104
105
106
107
# File 'lib/game_machine/client_manager.rb', line 104

def sender_id_to_actor_ref(sender_id)
  server,name = sender_id.split('|')
  Actor::Base.find_remote(server,name)
end

#subscribe(topic) ⇒ Object



205
206
207
208
# File 'lib/game_machine/client_manager.rb', line 205

def subscribe(topic)
  message = MessageLib::Subscribe.new.set_topic(topic)
  MessageQueue.find.tell(message,get_self)
end

#unregister_sender(message) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/game_machine/client_manager.rb', line 135

def unregister_sender(message)
  unregister_msg = message.client_manager_unregister
  register_type = unregister_msg.register_type
  name = unregister_msg.name
  if register_type == 'client'
    if cluster_connection?(message.client_connection)
      send_client_event(name,message.player.id,'disconnected')
    end

    # Notify registered actors using a client manager event
    send_client_manager_event(name,message.player.id,'disconnected')

    local_clients.delete(name)
    players.delete(message.player.id)
    self.class.local_players.delete(message.player.id)
    GameMachine.logger.debug("#{self.class.name} client #{name} unregistered")
  end
end

#update_new_member(message) ⇒ Object

TODO have the remote ack this, and resend it periodically until we get ack



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/game_machine/client_manager.rb', line 84

def update_new_member(message)
  client_events = MessageLib::ClientEvents.new
  client_to_player.each do |client_id,player_id|
    client_events.add_client_event(
      create_client_event(client_id,player_id,'connected')
    )
  end
  entity = MessageLib::Entity.new.set_id('0').set_client_events(client_events)
  remote_ref = Actor::Base.find_remote(message.member.address.to_string,self.class.name)
  remote_ref.tell(entity,get_self)
end