Class: Tamashii::Manager::Client

Inherits:
Server::Connection::Base
  • Object
show all
Includes:
ClientManager
Defined in:
lib/tamashii/manager/client.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClientManager

included

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



10
11
12
# File 'lib/tamashii/manager/client.rb', line 10

def channel
  @channel
end

#envObject (readonly)

Returns the value of attribute env.



9
10
11
# File 'lib/tamashii/manager/client.rb', line 9

def env
  @env
end

#last_beat_timestampObject (readonly)

Returns the value of attribute last_beat_timestamp.



12
13
14
# File 'lib/tamashii/manager/client.rb', line 12

def last_beat_timestamp
  @last_beat_timestamp
end

#last_response_timeObject (readonly)

Returns the value of attribute last_response_time.



13
14
15
# File 'lib/tamashii/manager/client.rb', line 13

def last_response_time
  @last_response_time
end

#tagObject

Returns the value of attribute tag.



15
16
17
# File 'lib/tamashii/manager/client.rb', line 15

def tag
  @tag
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/tamashii/manager/client.rb', line 9

def url
  @url
end

Instance Method Details

#accept(type, id) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tamashii/manager/client.rb', line 35

def accept(type, id)
  @id = id
  @type = type
  @channel = Channel.subscribe(self)
  packet = Tamashii::Packet.new(
    Tamashii::Type::AUTH_RESPONSE,
    @channel.id,
    true
  )
  Client[id] = self
  send packet.dump
end

#authorized?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/tamashii/manager/client.rb', line 31

def authorized?
  !@id.nil?
end

#beatObject



48
49
50
51
52
53
# File 'lib/tamashii/manager/client.rb', line 48

def beat
  beat_time = Time.now
  @socket.ping("heart-beat-at-#{beat_time}") do
    heartbeat_callback(beat_time)
  end
end

#emit_error(message) ⇒ Object



85
86
87
# File 'lib/tamashii/manager/client.rb', line 85

def emit_error(message)
  Manager.logger.error("Client #{id} has error => #{message}")
end

#heartbeat_callback(beat_time) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/tamashii/manager/client.rb', line 55

def heartbeat_callback(beat_time)
  @last_beat_timestamp = Time.now
  @last_response_time = @last_beat_timestamp - beat_time
  Manager.logger.debug(
    "[#{id}] Heart beat #{beat_time} " \
    "returns at #{@last_beat_timestamp}!" \
    " Delay: #{(@last_response_time * 1000).round} ms"
  )
end

#idObject



17
18
19
20
# File 'lib/tamashii/manager/client.rb', line 17

def id
  return "<Unauthorized : #{@env['REMOTE_ADDR']}>" if @id.nil?
  @id
end

#on_closeObject



80
81
82
83
# File 'lib/tamashii/manager/client.rb', line 80

def on_close
  Manager.logger.info("Client #{id} closed connection")
  Client.remove_client(id)
end

#on_message(data) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/tamashii/manager/client.rb', line 70

def on_message(data)
  Manager.logger.debug("Receive Data: #{data}")
  return unless data.is_a?(Array)
  Tamashii::Resolver.resolve(Tamashii::Packet.load(data), client: self)
rescue Error::AuthorizationError => reason
  close_on_authorize_failed(reason)
rescue => e
  on_message_error(e)
end

#on_openObject



65
66
67
68
# File 'lib/tamashii/manager/client.rb', line 65

def on_open
  Client.ensure_heartbeat_enabled(@event_loop)
  Manager.logger.info("Client #{id} is ready")
end

#send(packet) ⇒ Object



26
27
28
29
# File 'lib/tamashii/manager/client.rb', line 26

def send(packet)
  packet = packet.dump if packet.is_a?(Tamashii::Packet)
  @socket.transmit(packet)
end

#typeObject



22
23
24
# File 'lib/tamashii/manager/client.rb', line 22

def type
  Type::CLIENT.key(@type)
end