Sha256: 4bb0f7112891b61325f9946758c94cb930f8c79b7e531ec130c292d1114967fc

Contents?: true

Size: 1022 Bytes

Versions: 1

Compression:

Stored size: 1022 Bytes

Contents

require 'socket'

module LogStashLogger
  module Device
    class Socket < Base
      DEFAULT_HOST = '0.0.0.0'

      attr_reader :host, :port

      def initialize(opts)
        @port = opts[:port] || fail(ArgumentError, "Port is required")
        @host = opts[:host] || DEFAULT_HOST
      end

      def write(message)
        with_connection do
          super
        end
      end

      def flush
        return unless connected?
        with_connection do
          super
        end
      end

      def to_io
        with_connection do
          @io
        end
      end

      def connected?
        !!@io
      end

      protected

      # Implemented by TCP and UDP devices
      def connect
        fail NotImplementedError
      end

      # Ensure the block is executed with a valid connection
      def with_connection(&block)
        connect unless @io
        yield
      rescue => e
        warn "#{self.class} - #{e.class} - #{e.message}"
        close
        @io = nil
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logstash-logger-0.5.0 lib/logstash-logger/device/socket.rb