Sha256: ec087dea1e66597e54887cd44b7e0ff0e9dd0fbe70ad23c69ef0932c1e27b092

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

module JRPC
  module Transport
    class SocketBase

      class Error < StandardError
      end

      class TimeoutError < Error
        def initialize
          super(self.class.to_s.split('::').last)
        end
      end

      class ReadTimeoutError < TimeoutError
      end

      class WriteTimeoutError < TimeoutError
      end

      class ConnectionTimeoutError < TimeoutError
      end

      class ConnectionFailedError < Error
      end

      class WriteFailedError < Error
      end

      class ReadFailedError < Error
      end

      attr_reader :options, :read_timeout, :write_timeout

      def self.connect(options)
        connection = new(options)
        yield(connection)
      ensure
        connection.close if connection
      end

      def initialize(options)
        @server = options.fetch(:server)
        @read_timeout = options.fetch(:read_timeout, nil)
        @write_timeout = options.fetch(:write_timeout, nil)
        @connect_timeout = options.fetch(:connect_timeout, nil)
        @connect_retry_count = options.fetch(:connect_retry_count, 0)
        @options = options
      end

      def connect
        retries = @connect_retry_count

        while retries >= 0
          begin
            connect_socket
            break
          rescue Error => e
            retries -= 1
            raise e if retries < 0
          end
        end
      end

      def read(_length, _timeout = @read_timeout)
        raise NotImplementedError
      end

      def write(_data, _timeout = @write_timeout)
        raise NotImplementedError
      end

      def close
        raise NotImplementedError
      end

      def closed?
        raise NotImplementedError
      end

      private

      def connect_socket
        raise NotImplementedError
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jrpc-1.1.3 lib/jrpc/transport/socket_base.rb
jrpc-1.1.2 lib/jrpc/transport/socket_base.rb