Class: LogCourier::Server
- Inherits:
-
Object
- Object
- LogCourier::Server
- Defined in:
- lib/log-courier/server.rb
Overview
Implementation of the server
Instance Attribute Summary collapse
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Server
constructor
A new instance of Server.
- #run(&block) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Server
Returns a new instance of Server.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/log-courier/server.rb', line 36 def initialize( = {}) @options = { logger: nil, transport: 'tls', }.merge!() @logger = @options[:logger] @logger['plugin'] = 'input/courier' unless @logger.nil? case @options[:transport] when 'tcp', 'tls' require 'log-courier/server_tcp' @server = ServerTcp.new(@options) when 'plainzmq', 'zmq' require 'log-courier/server_zmq' @server = ServerZmq.new(@options) else fail 'input/courier: \'transport\' must be tcp, tls, plainzmq or zmq' end # Grab the port back and update the logger context @port = @server.port @logger['port'] = @port unless @logger.nil? # Load the json adapter @json_adapter = MultiJson.adapter.instance @json_options = { raw: true } end |
Instance Attribute Details
#port ⇒ Object (readonly)
Returns the value of attribute port.
34 35 36 |
# File 'lib/log-courier/server.rb', line 34 def port @port end |
Instance Method Details
#run(&block) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/log-courier/server.rb', line 65 def run(&block) # TODO: Make queue size configurable event_queue = EventQueue.new 1 server_thread = nil begin server_thread = Thread.new do # Receive messages and process them @server.run do |signature, , comm| case signature when 'PING' process_ping , comm when 'JDAT' process_jdat , comm, event_queue else if comm.peer.nil? @logger.warn 'Unknown message received', :from => 'unknown' unless @logger.nil? else @logger.warn 'Unknown message received', :from => comm.peer unless @logger.nil? end # Don't kill a client that sends a bad message # Just reject it and let it send it again, potentially to another server comm.send '????', '' end end end loop do block.call event_queue.pop end ensure # Signal the server thread to stop unless server_thread.nil? server_thread.raise ShutdownSignal server_thread.join end end return end |