Class: Riemann::Client::UDP

Inherits:
Riemann::Client show all
Defined in:
lib/riemann/client/udp.rb

Constant Summary collapse

MAX_SIZE =
16_384

Constants inherited from Riemann::Client

HOST, PORT, TIMEOUT

Instance Attribute Summary collapse

Attributes inherited from Riemann::Client

#tcp, #udp

Instance Method Summary collapse

Methods inherited from Riemann::Client

#<<, #[], #bulk_send, #connect, #normalize_events, #query, #timeout

Constructor Details

#initialize(opts = {}) ⇒ UDP

rubocop:disable Lint/MissingSuper



10
11
12
13
14
15
# File 'lib/riemann/client/udp.rb', line 10

def initialize(opts = {}) # rubocop:disable Lint/MissingSuper
  @host     = opts[:host] || HOST
  @port     = opts[:port] || PORT
  @max_size = opts[:max_size] || MAX_SIZE
  @socket   = nil
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/riemann/client/udp.rb', line 8

def host
  @host
end

#max_sizeObject

Returns the value of attribute max_size.



8
9
10
# File 'lib/riemann/client/udp.rb', line 8

def max_size
  @max_size
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/riemann/client/udp.rb', line 8

def port
  @port
end

Instance Method Details

#closeObject



23
24
25
26
# File 'lib/riemann/client/udp.rb', line 23

def close
  @socket.close if connected?
  @socket = nil
end

#connected?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/riemann/client/udp.rb', line 28

def connected?
  @socket && !@socket.closed?
end

#read_message(_socket) ⇒ Object

Read a message from a stream

Raises:



33
34
35
# File 'lib/riemann/client/udp.rb', line 33

def read_message(_socket)
  raise Unsupported
end

#send_maybe_recv(message) ⇒ Object

Raises:



41
42
43
44
45
46
47
# File 'lib/riemann/client/udp.rb', line 41

def send_maybe_recv(message)
  encoded_string = message.encode.to_s
  raise TooBig unless encoded_string.length < @max_size

  socket.send(encoded_string, 0, @host, @port)
  nil
end

#send_recv(_message) ⇒ Object

Raises:



37
38
39
# File 'lib/riemann/client/udp.rb', line 37

def send_recv(_message)
  raise Unsupported
end

#socketObject



17
18
19
20
21
# File 'lib/riemann/client/udp.rb', line 17

def socket
  return @socket if connected?

  @socket = UDPSocket.new
end