Class: Rserve::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/rserve/packet.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd, cont) ⇒ Packet

Returns a new instance of Packet.


11
12
13
14
15
# File 'lib/rserve/packet.rb', line 11

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

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd


4
5
6
# File 'lib/rserve/packet.rb', line 4

def cmd
  @cmd
end

#contObject (readonly)

Returns the value of attribute cont


3
4
5
# File 'lib/rserve/packet.rb', line 3

def cont
  @cont
end

Instance Method Details

#cont_lenObject


16
17
18
# File 'lib/rserve/packet.rb', line 16

def cont_len
  @cont.nil? ? 0 : @cont.length
end

#error?Boolean

Returns:

  • (Boolean)

22
23
24
# File 'lib/rserve/packet.rb', line 22

def error?
  @cmd&15==2
end

#get_error_description(stat) ⇒ Object


28
29
30
# File 'lib/rserve/packet.rb', line 28

def get_error_description(stat)
  ERROR_DESCRIPTIONS[stat]
end

#ok?Boolean

Returns:

  • (Boolean)

19
20
21
# File 'lib/rserve/packet.rb', line 19

def ok?
  @cmd&15==1
end

#statObject


25
26
27
# File 'lib/rserve/packet.rb', line 25

def stat
  (@cmd>>24)&127
end

#to_sObject


31
32
33
34
35
36
37
38
# File 'lib/rserve/packet.rb', line 31

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