Class: Tamashii::Manager::Client
- Inherits:
-
Server::Connection::Base
- Object
- Server::Connection::Base
- Tamashii::Manager::Client
- Includes:
- ClientManager
- Defined in:
- lib/tamashii/manager/client.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#channel ⇒ Object
readonly
Returns the value of attribute channel.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#last_beat_timestamp ⇒ Object
readonly
Returns the value of attribute last_beat_timestamp.
-
#last_response_time ⇒ Object
readonly
Returns the value of attribute last_response_time.
-
#tag ⇒ Object
Returns the value of attribute tag.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #accept(type, id) ⇒ Object
- #authorized? ⇒ Boolean
- #beat ⇒ Object
- #emit_error(message) ⇒ Object
- #heartbeat_callback(beat_time) ⇒ Object
- #id ⇒ Object
- #on_close ⇒ Object
- #on_message(data) ⇒ Object
- #on_open ⇒ Object
- #send(packet) ⇒ Object
- #type ⇒ Object
Methods included from ClientManager
Instance Attribute Details
#channel ⇒ Object (readonly)
Returns the value of attribute channel.
10 11 12 |
# File 'lib/tamashii/manager/client.rb', line 10 def channel @channel end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
9 10 11 |
# File 'lib/tamashii/manager/client.rb', line 9 def env @env end |
#last_beat_timestamp ⇒ Object (readonly)
Returns the value of attribute last_beat_timestamp.
12 13 14 |
# File 'lib/tamashii/manager/client.rb', line 12 def @last_beat_timestamp end |
#last_response_time ⇒ Object (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 |
#tag ⇒ Object
Returns the value of attribute tag.
15 16 17 |
# File 'lib/tamashii/manager/client.rb', line 15 def tag @tag end |
#url ⇒ Object (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
31 32 33 |
# File 'lib/tamashii/manager/client.rb', line 31 def !@id.nil? end |
#beat ⇒ Object
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() Manager.logger.error("Client #{id} has error => #{}") 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 |
#id ⇒ Object
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_close ⇒ Object
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 (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 (reason) rescue => e (e) end |
#on_open ⇒ Object
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 |
#type ⇒ Object
22 23 24 |
# File 'lib/tamashii/manager/client.rb', line 22 def type Type::CLIENT.key(@type) end |