Sha256: 25d0259a113bd03aa77e0edef76ec39c6fefe6abc96acd8a3b0289c63cc5f4e4

Contents?: true

Size: 1.29 KB

Versions: 5

Compression:

Stored size: 1.29 KB

Contents

module RemoteSyslog
  class TlsEndpoint
    class Handler < EventMachine::Connection
      def initialize(endpoint)
        @endpoint = endpoint
        @endpoint.connection = self
        super()
      end

      def connection_completed
        start_tls
      end

      def unbind
        @endpoint.unbind
      end
    end

    attr_accessor :connection

    def initialize(address, port)
      @address = address
      @port    = port.to_i

      # 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

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

    def address
      @cached_ip || @address
    end

    def connect
      EventMachine.connect(address, @port, TlsEndpoint::Handler, self)
    end

    def unbind
      @connection = nil
      connect
    end

    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
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
remote_syslog-1.4.2 lib/remote_syslog/tls_endpoint.rb
remote_syslog-1.4.1 lib/remote_syslog/tls_endpoint.rb
remote_syslog-1.4.0 lib/remote_syslog/tls_endpoint.rb
remote_syslog-1.3.1 lib/remote_syslog/tls_endpoint.rb
remote_syslog-1.3.0 lib/remote_syslog/tls_endpoint.rb