Class: OnesnooperServer::UDPHandler

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/onesnooper_server/udp_handler.rb

Overview

Handler for incoming UDP datagrams. Implements required methods for direct use with EventMachine listeners. This handler will not respond to incoming datagrams as its primary purpose is to record mirrored monitoring traffic.

Constant Summary collapse

DATAGRAM_PREFIX =

Allowed datagram prefix

'MONITOR'
STORES =

Registered allowed stores for monitoring data

{
  "mongodb" => ::OnesnooperServer::Stores::MongodbStore,
  "mysql"   => ::OnesnooperServer::Stores::MysqlStore,
  "sqlite"  => ::OnesnooperServer::Stores::SqliteStore,
}

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ UDPHandler

Returns a new instance of UDPHandler.



18
19
20
21
22
23
24
# File 'lib/onesnooper_server/udp_handler.rb', line 18

def initialize(*args)
  super
  @store_instances = store_instances(
    ::OnesnooperServer::Settings.store,
    ::OnesnooperServer::Settings.stores
  )
end

Instance Method Details

#receive_data(monitoring_datagram) ⇒ Object

Receives data and triggers processing of the given datagram. Main internal processing triggered from this method should always happen asynchronously (i.e., using EventMachine.defer or Deferrable classes).

Parameters:

  • monitoring_datagram (String)

    incoming data payload



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/onesnooper_server/udp_handler.rb', line 32

def receive_data(monitoring_datagram)
  monitoring_datagram.chomp!
  source_port, source_ip = Socket.unpack_sockaddr_in(get_peername)
  unless monitoring_datagram.start_with?(DATAGRAM_PREFIX)
    ::OnesnooperServer::Log.warn "[#{self.class.name}] Discarding datagram from " \
                                 "#{source_ip}:#{source_port} (not #{DATAGRAM_PREFIX})"
    return
  end

  ::OnesnooperServer::Log.debug "[#{self.class.name}] Received #{monitoring_datagram.inspect} " \
                                "from #{source_ip}:#{source_port}"
  ::OnesnooperServer::RequestHandler.parse(
    monitoring_datagram,
    source_ip,
    source_port,
    @store_instances
  ).run(callback)
end