Sha256: 4b478730792894514da2f2b3d1e38f01f767e20d8a54e7e3a5c56e1283081592

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

module BERTRPC
  class Action
    include Encodes

    def initialize(svc, req, mod, fun, args)
      @svc = svc
      @req = req
      @mod = mod
      @fun = fun
      @args = args
    end

    def execute
      bert_request = encode_ruby_request([@req.kind, @mod, @fun, @args])
      bert_response = transaction(bert_request)
      decode_bert_response(bert_response)
    end

    #private

    def write(sock, bert)
      sock.write([bert.length].pack("N"))
      sock.write(bert)
    end

    def transaction(bert_request)
      sock = TCPSocket.new(@svc.host, @svc.port)

      if @req.options
        if @req.options[:cache] && @req.options[:cache][0] == :validation
          token = @req.options[:cache][1]
          info_bert = encode_ruby_request([:info, :cache, [:validation, token]])
          write(sock, info_bert)
        end
      end

      write(sock, bert_request)
      lenheader = sock.read(4)
      raise ProtocolError.new(ProtocolError::NO_HEADER) unless lenheader
      len = lenheader.unpack('N').first
      bert_response = sock.read(len)
      raise ProtocolError.new(ProtocolError::NO_DATA) unless bert_response
      sock.close
      bert_response
    rescue Errno::ECONNREFUSED
      raise ConnectionError.new("Unable to connect to #{@svc.host}:#{@svc.port}")
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
mojombo-bertrpc-0.3.2 lib/bertrpc/action.rb
bertrpc-0.3.4 lib/bertrpc/action.rb
bertrpc-0.3.2 lib/bertrpc/action.rb