Class: LogPorter::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/logporter/namespace.rb,
lib/logporter/server.rb

Defined Under Namespace

Classes: Connection, DefaultHandler, TLSConfig

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Server

Create a new server to listen with ‘options’ is a hash of:

:net => the network layer to use (:udp, :tcp, :tls)
:port => the port to listen on
:wire => the wire format (:raw, :syslog)
:handler => the handler instance. Must respond to 'receive_event'


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/logporter/server.rb', line 37

def initialize(options)
  @logger = Logger.new(STDOUT)
  @network = options[:net]
  @port = options[:port] || 514
  @wire = options[:wire] || :raw
  @handler = options[:handler] || LogPorter::Server::DefaultHandler.new
  @attributes = options[:attributes] || Hash.new

  if @network == :tls
    @tls = TLSConfig.new
    @tls.private_key_file = options[:private_key]
    @tls.cert_chain_file = options[:certificate_chain]
    @tls.verify_peer = options[:verify_peer] || false
  else
    @tls = nil
  end
end

Instance Attribute Details

#attributesObject (readonly)

Arbitrary attributes for this server. You can store whatever you want here. This is a hash



25
26
27
# File 'lib/logporter/server.rb', line 25

def attributes
  @attributes
end

#loggerObject

The logger object



28
29
30
# File 'lib/logporter/server.rb', line 28

def logger
  @logger
end

#networkObject (readonly)

The network layer, :tcp, :udp, or :tls



18
19
20
# File 'lib/logporter/server.rb', line 18

def network
  @network
end

#portObject (readonly)

The port we are listening on



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

def port
  @port
end

#tlsObject (readonly)

TLS options, only meaningful if @network == :tls



15
16
17
# File 'lib/logporter/server.rb', line 15

def tls
  @tls
end

#wireObject (readonly)

The wire format (syslog, raw, etc)



21
22
23
# File 'lib/logporter/server.rb', line 21

def wire
  @wire
end

Instance Method Details

#receive_event(event, client_addr, client_port) ⇒ Object



106
107
108
# File 'lib/logporter/server.rb', line 106

def receive_event(event, client_addr, client_port)
  @handler.receive_event(event, self, client_addr, client_port)
end

#startObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/logporter/server.rb', line 57

def start
  # We use next_tick here in case you are invoking this method from outside
  # of EventMachine; this allows you to do this:
  #
  #   s = LogPorter::server.new ...
  #   s.start
  #
  #   EventMachine.run()
  if EventMachine.reactor_running?
    _start
  else
    EventMachine.next_tick { _start }
  end
end

#stopObject



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/logporter/server.rb', line 111

def stop
  @logger.info "Stopping #{@network}/#{@port}"
  case @network
    when :tcp
      EventMachine::stop_server(@socket)
    when :tls
      EventMachine::stop_server(@socket)
    when :udp
      @socket.close_connection(true)
  end
end