Class: OnesnooperServer::RequestHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/onesnooper_server/request_handler.rb

Overview

Request handler for validating and dispatching datagram processing on defined types of datagrams. Invalid or unknown datagrams will be processed by the ‘OnesnooperServer::Datagrams::InvalidDatagram` sub-handler.

Constant Summary collapse

MONITORING_DATA_REGEXP =

Validation constant for incoming datagrams

/^MONITOR\s(?<result>[[:alpha:]]+)\s(?<host_id>\d+)\s(?<payload>\S+)$/
DATAGRAMS =

Registered allowed datagram processing classes & the default fallback

{
  "SUCCESS" => ::OnesnooperServer::Datagrams::SuccessDatagram,
  "FAILURE" => ::OnesnooperServer::Datagrams::FailureDatagram,
}

Class Method Summary collapse

Class Method Details

.parse(monitoring_datagram, source_ip, source_port, store_instances) ⇒ Object

Static parsing method for identifying types of incoming datagrams and choosing the right processing class for each datagram. Always returns an instance responding to ‘run(callback)`.

Parameters:

  • monitoring_datagram (Object)

    datagram payload for processing

  • source_ip (String)

    IP address of the client

  • source_port (String)

    port number of the client

  • store_instances (Array)

    array of pre-initialized store instances



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/onesnooper_server/request_handler.rb', line 26

def self.parse(monitoring_datagram, source_ip, source_port, store_instances)
  unless valid_data?(monitoring_datagram)
    ::OnesnooperServer::Log.error "[#{self.name}] Dropping invalid monitoring data #{monitoring_datagram.inspect}"
    return DATAGRAMS.default.new
  end

  unless valid_peer?(source_ip)
    ::OnesnooperServer::Log.warn "[#{self.name}] Dropping monitoring data from #{source_ip}, not allowed!"
    return DATAGRAMS.default.new
  end

  match_data = monitoring_datagram.match(MONITORING_DATA_REGEXP)
  return DATAGRAMS.default.new unless match_data

  DATAGRAMS[match_data[:result]].new({
    host_id: match_data[:host_id],
    payload: match_data[:payload],
    stores: store_instances,
  })
end