Sha256: 27f8c963ece3296a39bcead18720c233e60ce5e89119babaf832c9f3c3c4cadf

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

require 'protobuf/rpc/connectors/base'

module Protobuf
  module Rpc
    module Connectors
      class Socket < Base
        include Protobuf::Rpc::Connectors::Common
        include Protobuf::Logger::LogMethods
        
        def send_request
          check_async
          initialize_stats
          connect_to_rpc_server
          post_init # calls _send_request
          read_response
        end

        private

        def check_async
          if async?
            log_error "[client-#{self.class}] Cannot run in async mode"
            raise "Cannot use Socket client in async mode" 
          else
            log_debug "[client-#{self.class}] Async check passed" 
         end
        end

        def close_connection
          @socket.close
          log_debug "[client-#{self.class}] Connector closed" 
        end

        def connect_to_rpc_server
          @socket = TCPSocket.new(options[:host], options[:port])
          log_debug "[client-#{self.class}] Connection established #{options[:host]}:#{options[:port]}" 
        end

        # Method to determine error state, must be used with Connector api
        def error?
          log_debug "[client-#{self.class}] Error state : #{@socket.closed?}" 
          @socket.closed?
        end

        def read_data
          size_io = StringIO.new

          while((size_reader = @socket.getc) != "-")
            size_io << size_reader
          end
          str_size_io = size_io.string

          "#{str_size_io}-#{@socket.read(str_size_io.to_i)}"
        end

        def read_response
          @buffer << read_data 
          parse_response if @buffer.flushed?
        end

        def send_data(data)
          @socket.write(data)
          @socket.flush
          log_debug "[client-#{self.class}] write closed" 
        end

      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
protobuf-1.1.3 lib/protobuf/rpc/connectors/socket.rb
protobuf-1.1.2 lib/protobuf/rpc/connectors/socket.rb
protobuf-1.1.1 lib/protobuf/rpc/connectors/socket.rb
protobuf-1.1.0.beta2 lib/protobuf/rpc/connectors/socket.rb