Class: Stenohttp2::Server::PingHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/stenohttp2/server/ping_handler.rb

Constant Summary collapse

TIMESTAMP_FORMAT =
'%Y-%m-%d-%H-%M'.freeze
IDENTIFIERS =
[
  ENV.fetch('SERVER_IDENTIFIER'),
  ::Stenohttp2::Client::Client::CLIENT_IDENTIFIER
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server: true) ⇒ PingHandler

Returns a new instance of PingHandler.



46
47
48
49
50
51
52
# File 'lib/stenohttp2/server/ping_handler.rb', line 46

def initialize(server: true)
  @server = server
  @messages_left = 0
  @file = nil
  @payload = nil
  super()
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



12
13
14
# File 'lib/stenohttp2/server/ping_handler.rb', line 12

def file
  @file
end

#messages_leftObject

Returns the value of attribute messages_left.



12
13
14
# File 'lib/stenohttp2/server/ping_handler.rb', line 12

def messages_left
  @messages_left
end

#payloadObject

Returns the value of attribute payload.



12
13
14
# File 'lib/stenohttp2/server/ping_handler.rb', line 12

def payload
  @payload
end

#send_responseObject

Returns the value of attribute send_response.



12
13
14
# File 'lib/stenohttp2/server/ping_handler.rb', line 12

def send_response
  @send_response
end

Instance Method Details

#handle(frame) ⇒ Object

rubocop:disable Metrics/AbcSize



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/stenohttp2/server/ping_handler.rb', line 55

def handle(frame)
  # We are only interested in PING farmes without ACK flag
  # because the ones with ACK include just the repeated contend
  return unless frame[:type] == :ping && !frame[:flags].include?(:ack)

  self.payload = frame[:payload]

  start_reciving and return if IDENTIFIERS.include?(payload)

  return if waiting?

  consume_count and return unless count_consumed?

  if messages_left.positive?
    file.write(payload)
    consume_message
  end

  return unless messages_left.zero?

  file.close
  ready_to_respond
end

#read_or_create_fileObject

rubocop:enable Metrics/AbcSize



80
81
82
83
84
# File 'lib/stenohttp2/server/ping_handler.rb', line 80

def read_or_create_file
  return unless file.nil?

  self.file = File.open(file_name, 'a')
end