Class: SensuGenerator::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu_generator/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



8
9
10
11
12
13
14
# File 'lib/sensu_generator/server.rb', line 8

def initialize
  @config  = Application.config
  @logger  = Application.logger
  @trigger = Application.trigger
  logger.info "Server: starting server."
  listen_and_serve
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/sensu_generator/server.rb', line 6

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/sensu_generator/server.rb', line 6

def logger
  @logger
end

Instance Method Details

#closeObject



37
38
39
40
# File 'lib/sensu_generator/server.rb', line 37

def close
  @server.close
  @server = nil
end

#listen_and_serveObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sensu_generator/server.rb', line 16

def listen_and_serve
  @server = TCPServer.new(server_addr, server_port)
  logger.info "Server: started server on #{@server.addr}"

  while client = @server.accept
    logger.info "Server: client #{client.addr} connected"
    Thread.start(client) do
      begin
        data = client.read
        process data
        client.close
      rescue => e
        client&.close
        raise ServerError, "Server: error occured #{e.inspect} #{e.backtrace}\n"
      end
    end
  end
rescue
  close
end