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', 65=>'Login 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.
12 13 14 15 16 |
# File 'lib/rserve/packet.rb', line 12 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
17 18 19 |
# File 'lib/rserve/packet.rb', line 17 def cont_len @cont.nil? ? 0 : @cont.length end |
#error? ⇒ Boolean
23 24 25 |
# File 'lib/rserve/packet.rb', line 23 def error? @cmd&15==2 end |
#get_error_description(stat) ⇒ Object
29 30 31 |
# File 'lib/rserve/packet.rb', line 29 def get_error_description(stat) ERROR_DESCRIPTIONS[stat] end |
#ok? ⇒ Boolean
20 21 22 |
# File 'lib/rserve/packet.rb', line 20 def ok? @cmd&15==1 end |
#stat ⇒ Object
26 27 28 |
# File 'lib/rserve/packet.rb', line 26 def stat (@cmd>>24)&127 end |
#to_s ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/rserve/packet.rb', line 32 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 |