Class: Rserve::Packet
- Inherits:
-
Object
- Object
- Rserve::Packet
- 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
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#cont ⇒ Object
readonly
Returns the value of attribute cont.
Instance Method Summary collapse
- #cont_len ⇒ Object
- #error? ⇒ Boolean
- #get_error_description(stat) ⇒ Object
-
#initialize(cmd, cont) ⇒ Packet
constructor
A new instance of Packet.
- #ok? ⇒ Boolean
- #stat ⇒ Object
- #to_s ⇒ Object
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
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
4 5 6 |
# File 'lib/rserve/packet.rb', line 4 def cmd @cmd end |
#cont ⇒ Object (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_len ⇒ Object
16 17 18 |
# File 'lib/rserve/packet.rb', line 16 def cont_len @cont.nil? ? 0 : @cont.length end |
#error? ⇒ 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
19 20 21 |
# File 'lib/rserve/packet.rb', line 19 def ok? @cmd&15==1 end |
#stat ⇒ Object
25 26 27 |
# File 'lib/rserve/packet.rb', line 25 def stat (@cmd>>24)&127 end |
#to_s ⇒ Object
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 |