Sha256: 3e4e323b525e61b5e57ca7a50a3e107aed3c1b7640bfdae855bf86bbf3856a0c

Contents?: true

Size: 938 Bytes

Versions: 6

Compression:

Stored size: 938 Bytes

Contents

module Rserve
  class Packet
    attr_reader :cont
    attr_reader :cmd

    ERROR_DESCRIPTIONS={
      2=>'Invalid expression',
      3=>'Parse error',
    127=>'Unknown variable/method'}

    def initialize(cmd, cont)
      raise "cont [#{cont.class} - #{cont.to_s}] should respond to :length" if !cont.nil? and !cont.respond_to? :length
      @cmd=cmd
      @cont=cont
    end
    def cont_len
      @cont.nil? ? 0 : @cont.length
    end
    def ok?
      @cmd&15==1
    end
    def error?
      @cmd&15==2
    end
    def stat
      (@cmd>>24)&127
    end
    def get_error_description(stat)
      ERROR_DESCRIPTIONS[stat]
    end
    def to_s
      if error?
        status="error:'#{get_error_description(stat)}'(#{stat})"
      else
        status="ok"
      end
      "Packet[cmd=#{@cmd},len="+((cont.nil?)?"<nil>":(""+cont.length.to_s))+", con='"+(cont.nil?  ? "<nil>" : cont.pack("C*"))+"', status=#{status}]"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rserve-client-0.3.2 lib/rserve/packet.rb
rserve-client-0.3.1 lib/rserve/packet.rb
rserve-client-0.3.0 lib/rserve/packet.rb
rserve-client-0.2.5 lib/rserve/packet.rb
rserve-client-0.2.4 lib/rserve/packet.rb
rserve-client-0.2.3 lib/rserve/packet.rb