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.
- #process_jdat(message, comm, event_queue) ⇒ Object
- #process_ping(message, comm) ⇒ Object
- #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 |
# File 'lib/log-courier/server.rb', line 36 def initialize( = {}) @options = { logger: nil, transport: 'tls' }.merge!() @logger = @options[:logger] 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 raise '[LogCourierServer] \'transport\' must be tcp, tls, plainzmq or zmq' end # Grab the port back @port = @server.port # Load the json adapter @json_adapter = MultiJson.adapter.instance @json_options = { raw: true, use_bigdecimal: 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
#process_jdat(message, comm, event_queue) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/log-courier/server.rb', line 109 def process_jdat(, comm, event_queue) # Now we have the data, aim to respond within 5 seconds ack_timeout = Time.now.to_i + 5 # OK - first is a nonce - we send this back with sequence acks # This allows the client to know what is being acknowledged # Nonce is 16 so check we have enough if .length < 17 raise ProtocolError, "JDAT message too small (#{.length})" end nonce = [0...16] # The remainder of the message is the compressed data block = StringIO.new Zlib::Inflate.inflate([16....length]) # Message now contains JSON encoded events # They are aligned as [length][event]... so on # We acknowledge them by their 1-index position in the stream # A 0 sequence acknowledgement means we haven't processed any yet sequence = 0 events = [] length_buf = '' data_buf = '' loop do ret = .read 4, length_buf if ret.nil? # Finished! break elsif length_buf.length < 4 raise ProtocolError, "JDAT length extraction failed (#{ret} #{length_buf.length})" end length = length_buf.unpack('N').first # Extract message ret = .read length, data_buf if ret.nil? or data_buf.length < length @logger.warn() raise ProtocolError, "JDAT message extraction failed #{ret} #{data_buf.length}" end data_buf.force_encoding('utf-8') # Ensure valid encoding unless data_buf.valid_encoding? data_buf.chars.map do |c| c.valid_encoding? ? c : "\xEF\xBF\xBD" end end # Decode the JSON begin event = @json_adapter.load(data_buf, @json_options) rescue MultiJson::ParseError => e @logger.warn("[LogCourierServer] JSON parse failure, falling back to plain-text: #{e}") unless @logger.nil? event = { 'message' => data_buf } end # Queue the event begin event_queue.push event, [0, ack_timeout - Time.now.to_i].max rescue TimeoutError # Full pipeline, partial ack # NOTE: comm.send can raise a Timeout::Error of its own @logger.debug "[LogCourierServer] Partially acknowledging message #{nonce.hash} sequence #{sequence}" unless @logger.nil? comm.send 'ACKN', [nonce, sequence].pack('A*N') ack_timeout = Time.now.to_i + 5 retry end sequence += 1 end # Acknowledge the full message # NOTE: comm.send can raise a Timeout::Error @logger.debug "[LogCourierServer] Acknowledging message #{nonce.hash} sequence #{sequence}" unless @logger.nil? comm.send 'ACKN', [nonce, sequence].pack('A*N') end |
#process_ping(message, comm) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/log-courier/server.rb', line 98 def process_ping(, comm) # Size of message should be 0 if .length != 0 raise ProtocolError, "unexpected data attached to ping message (#{.length})" end # PONG! # NOTE: comm.send can raise a Timeout::Error of its own comm.send 'PONG', '' end |
#run(&block) ⇒ Object
63 64 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 |
# File 'lib/log-courier/server.rb', line 63 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 @logger.warn("[LogCourierServer] Unknown message received from #{comm.peer}") unless @logger.nil? # 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 end |