Class: LogPorter::Server::Connection

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
Protocol::Syslog3164
Defined in:
lib/logporter/server/connection.rb

Instance Method Summary collapse

Methods included from Protocol::Syslog3164

#parse_rfc3164, #syslog3164_init

Constructor Details

#initialize(server) ⇒ Connection

Returns a new instance of Connection.



12
13
14
15
# File 'lib/logporter/server/connection.rb', line 12

def initialize(server)
  @server = server
  super()
end

Instance Method Details

#post_initObject



17
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
# File 'lib/logporter/server/connection.rb', line 17

def post_init
  super

  if @server.network == :tls
    if RUBY_PLATFORM == "java"
      $STDERR.puts "Warning... EventMachine doesn't support TLS on JRuby :("
    end

    # Calls EventMachine::Connection#start_tls
    start_tls(
      :private_key_file => @server.tls.private_key_file,
      :cert_chain_file => @server.tls.cert_chain_file,
      :verify_peer => @server.tls.verify_peer
    )
  end
  
  @count = 0

  # TODO(sissel): Document why this.
  case @server.wire
    when :raw
      class << self
        alias_method :receive_line, :receive_line_raw
      end
    when :syslog
      class << self
        alias_method :receive_line, :receive_line_syslog
      end
    when :syslog_no_parse_time
      class << self
        alias_method :receive_line, :receive_line_syslog
      end
    else
      raise "Unsupported protocol #{@server.wire}"
  end

    #@server.logger.error "Exception: #{e.inspect}"
    #@server.logger.error "Backtrace: #{e.backtrace}"
  if @server.network != :udp
    peer = get_peername
    if peer.is_a?(Array) # new em-netty::Connection.get_peername
      @client_address, @client_port = peer
    else
      @client_port, @client_address = Socket.unpack_sockaddr_in(peer)
    end
    puts "New client: #{@client_address}:#{@client_port}"
  end
end

#receive_data(data) ⇒ Object

end



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/logporter/server/connection.rb', line 71

def receive_data(data)
  if @server.network == :udp
    peer = get_peername
    if peer.is_a?(Array) # new em-netty::Connection.get_peername
      client_address, client_port = peer
    else
      client_port, client_address = Socket.unpack_sockaddr_in(peer)
    end
    receive_line(data, client_address, client_port)
  else
    client_port = @client_port
    client_address = @client_address
    @buffer ||= BufferedTokenizer.new
    @buffer.extract(data).each do |line|
      receive_line(line.chomp, client_address, client_port)
    end
  end
end

#receive_line_raw(line, address, port) ⇒ Object

def receive_data



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/logporter/server/connection.rb', line 90

def receive_line_raw(line, address, port)
  event = LogPorter::Event.new

  # TODO(sissel): Look for an alternative to Time#strftime since it is
  # insanely slow.
  event.pri = "13" # RFC3164 says unknown pri == 13.
  event.timestamp = Time.now
  event.hostname = address
  event.message = line
  event.raw = true

  @server.receive_event(event, address, port)
  stats
end

#receive_line_syslog(line, address, port) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/logporter/server/connection.rb', line 105

def receive_line_syslog(line, address, port)
  event = LogPorter::Event.new
  if parse_rfc3164(line, event, { :parse_time => (@server.wire == :syslog) })
  #elsif parse_rfc5424(line, event)
  else 
    # Unknown message format, add syslog headers.
    event.pri = "13" # RFC3164 says unknown pri == 13.
    event.timestamp = Time.now
    event.hostname = address
    event.message = line
  end

  @server.receive_event(event, address, port)
  stats
end

#statsObject

def receive_line_syslog



121
122
123
124
125
126
127
128
129
# File 'lib/logporter/server/connection.rb', line 121

def stats
  @start ||= Time.now
  @count += 1
  if (Time.now - @start) > 10
    @server.logger.info "Rate: #{@count / (Time.now - @start)}"
    @start = Time.now
    @count = 0
  end
end