Class: Swiftcore::AnaloggerProtocol

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/swiftcore/Analogger.rb,
lib/swiftcore/Analogger/AnaloggerProtocol.rb

Constant Summary collapse

Rcolon =
/:/
LoggerClass =
Analogger
MaxMessageLength =
8192
MaxLengthBytes =
MaxMessageLength.to_s.length

Instance Method Summary collapse

Instance Method Details

#post_initObject



299
300
301
# File 'lib/swiftcore/Analogger.rb', line 299

def post_init
  setup
end

#receive_data(data) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
# File 'lib/swiftcore/Analogger/AnaloggerProtocol.rb', line 18

def receive_data data
  @logchunk << data
  decompose = true
  while decompose
    unless @length
      if @logchunk.length - @pos > 7
        l = @logchunk[@pos + 0..@pos + 3].to_i
        ck = @logchunk[@pos + 4..@pos + 7].to_i
        if l == ck and l < MaxMessageLength
          @length = l
        else
          decompose = false
          peer = get_peername
          peer = peer ? ::Socket.unpack_sockaddr_in(peer)[1] : 'UNK'
          if l == ck
            LoggerClass.add_log([:default, :error, "Max Length Exceeded from #{peer} -- #{l}/#{MaxMessageLength}"])
            send_data(-"error: max length exceeded\n")
            close_connection_after_writing
          else
            LoggerClass.add_log([:default, :error, "checksum failed from #{peer} -- #{l}/#{ck}"])
            send_data(-"error: checksum failed\n")
            close_connection_after_writing
          end
        end
      end
    end

    if @length && @length < 8
      decompose = false
    end

    if @length and @length > 0 and @logchunk.length - @pos >= @length
      msg = nil
      msg = @logchunk[@pos..@length + @pos - 1].split(Rcolon, 4)
      @pos += @length
      unless @authenticated
        if msg.last == LoggerClass.key
          @authenticated = true
          send_data(-"accepted\n")
        else
          send_data(-"denied\n")
          close_connection_after_writing
        end
      else
        msg[0] = nil
        msg.shift
        LoggerClass.add_log(msg)
      end
      @length = nil
    else
      decompose = false
    end
  end
  if @pos >= @logchunk.length
    @logchunk.clear
    @pos = 0
  end
end

#send_data(data) ⇒ Object



14
15
16
# File 'lib/swiftcore/Analogger/AnaloggerProtocol.rb', line 14

def send_data data
  super data
end

#setupObject



7
8
9
10
11
12
# File 'lib/swiftcore/Analogger/AnaloggerProtocol.rb', line 7

def setup
  @length = nil
  @pos = 0
  @logchunk = ''
  @authenticated = nil
end