Class: TgMq::InputShaper

Inherits:
Object
  • Object
show all
Defined in:
lib/tg_mq/input_shaper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: TgMq.config.logger) ⇒ InputShaper

Returns a new instance of InputShaper.



5
6
7
8
9
10
11
12
13
14
# File 'lib/tg_mq/input_shaper.rb', line 5

def initialize(logger: TgMq.config.logger)
  @logger = TgMq.setup_logger(logger).tagged(self.class)

  @frames = Hash.new do |h, message_id|
    h[message_id] = {
      buffer: '',
      frames: []
    }
  end
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/tg_mq/input_shaper.rb', line 3

def logger
  @logger
end

Instance Method Details

#pushdata(text) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tg_mq/input_shaper.rb', line 16

def pushdata(text)
  if (frame = Frame.parse(text))
    logger.info "receive frame [#{frame.id}]"
    frameset = @frames[frame.message_id]
    data = frameset[:buffer] += frame.chunk
    frameset[:frames] << frame

    if data.end_with?(Frame::END_MSG)
      @frames.delete(frame.message_id)
      return data[0..-5]
    end
  else
    logger.warn "skip [#{text.first(15)}]: not a frame"
  end

  nil
end