Class: RemoteSyslog::TlsEndpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_syslog/tls_endpoint.rb

Defined Under Namespace

Classes: Handler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, port, options = {}) ⇒ TlsEndpoint

Returns a new instance of TlsEndpoint.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/remote_syslog/tls_endpoint.rb', line 36

def initialize(address, port, options = {})
  @address            = address
  @port               = port.to_i
  @client_cert_chain  = options[:client_cert_chain]
  @client_private_key = options[:client_private_key]
  @queue_limit        = options[:queue_limit] || 10_000
  @logger             = options[:logger] || Logger.new(STDERR)

  if options[:server_cert]
    @server_cert = OpenSSL::X509::Certificate.new(File.read(options[:server_cert]))
  end

  # Try to resolve the address
  resolve_address

  # Every 60 seconds we'll see if the address has changed
  EventMachine.add_periodic_timer(60) do
    resolve_address
  end

  connect
end

Instance Attribute Details

#client_cert_chainObject (readonly)

Returns the value of attribute client_cert_chain.



32
33
34
# File 'lib/remote_syslog/tls_endpoint.rb', line 32

def client_cert_chain
  @client_cert_chain
end

#client_private_keyObject (readonly)

Returns the value of attribute client_private_key.



32
33
34
# File 'lib/remote_syslog/tls_endpoint.rb', line 32

def client_private_key
  @client_private_key
end

#connectionObject

Returns the value of attribute connection.



31
32
33
# File 'lib/remote_syslog/tls_endpoint.rb', line 31

def connection
  @connection
end

#loggerObject (readonly)

Returns the value of attribute logger.



34
35
36
# File 'lib/remote_syslog/tls_endpoint.rb', line 34

def logger
  @logger
end

#server_certObject (readonly)

Returns the value of attribute server_cert.



32
33
34
# File 'lib/remote_syslog/tls_endpoint.rb', line 32

def server_cert
  @server_cert
end

Instance Method Details

#addressObject



72
73
74
# File 'lib/remote_syslog/tls_endpoint.rb', line 72

def address
  @cached_ip || @address
end

#connectObject



76
77
78
79
# File 'lib/remote_syslog/tls_endpoint.rb', line 76

def connect
  logger.debug "Connecting to #{address}:#{@port}"
  EventMachine.connect(address, @port, TlsEndpoint::Handler, self)
end

#resolve_addressObject



65
66
67
68
69
70
# File 'lib/remote_syslog/tls_endpoint.rb', line 65

def resolve_address
  request = EventMachine::DnsResolver.resolve(@address)
  request.callback do |addrs|
    @cached_ip = addrs.first
  end
end

#unbindObject



81
82
83
84
85
86
87
# File 'lib/remote_syslog/tls_endpoint.rb', line 81

def unbind
  @connection = nil

  EventMachine.add_timer(1) do
    connect
  end
end

#write(value) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/remote_syslog/tls_endpoint.rb', line 89

def write(value)
  if @connection
    if @queue
      @connection.send_data(@queue.join("\n") + "\n")
      @queue = nil
    end
    @connection.send_data(value + "\n")
  else
    (@queue ||= []) << value

    # Make sure our queue does not get to be too big
    @queue.shift if @queue.length > @queue_limit
  end
end