Class: LogCourier::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/log-courier/server.rb

Overview

Implementation of the server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/log-courier/server.rb', line 35

def initialize(options = {})
  @options = {
    logger: nil,
    transport: 'tls',
    disable_handshake: false,
  }.merge!(options)

  @logger = @options[:logger]

  case @options[:transport]
  when 'tcp', 'tls'
    require 'log-courier/server_tcp'
    @server = ServerTcp.new(@options)
  else
    raise 'input/courier: \'transport\' must be tcp or tls'
  end

  # Grab the port back and update the logger context
  @port = @server.port
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



33
34
35
# File 'lib/log-courier/server.rb', line 33

def port
  @port
end

Instance Method Details

#run(&block) ⇒ Object



56
57
58
59
60
61
62
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
97
# File 'lib/log-courier/server.rb', line 56

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, message, comm|
        case signature
        when 'PING'
          process_ping message, comm
        when 'JDAT'
          process_jdat message, comm, @event_queue
        else
          if comm.peer.nil?
            @logger&.warn 'Unknown message received', from: 'unknown'
          else
            @logger&.warn 'Unknown message received', from: comm.peer
          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
      event = @event_queue.pop
      break if event.nil?

      block.call event
    end
  ensure
    # Signal the server thread to stop
    unless server_thread.nil?
      server_thread.raise ShutdownSignal
      server_thread.join
    end
  end
  nil
end

#stopObject



99
100
101
102
# File 'lib/log-courier/server.rb', line 99

def stop
  @event_queue << nil
  nil
end