Class: Packet
- Inherits:
-
Object
- Object
- Packet
- Defined in:
- client/packet.rb
Overview
The Packet object stores various pieces of information regarding both sent and recieved packets.
Instance Attribute Summary collapse
-
#:sender, :reciever, :type, :data(: sender, : reciever, : type, : data) ⇒ Object
These variables are assigned attr_accessor so that they can be accessed outside the object.
-
#data ⇒ Object
These variables are assigned attr_accessor so that they can be accessed outside the object.
-
#reciever ⇒ Object
These variables are assigned attr_accessor so that they can be accessed outside the object.
-
#sender ⇒ Object
These variables are assigned attr_accessor so that they can be accessed outside the object.
-
#type ⇒ Object
These variables are assigned attr_accessor so that they can be accessed outside the object.
Instance Method Summary collapse
-
#check_packet? ⇒ Boolean
check_packet? checks the validity of the packet via the check sum.
-
#empty? ⇒ Boolean
empty? checks whether the instance of the packet is empty.
-
#initialize(sender, reciever, type, data, check_sum = '1') ⇒ NilClass
constructor
The initialize method takes in all arguments of the packet and can default the check sum to 1.
-
#pertains_to_user?(username) ⇒ Boolean
pertains_to_user? checks whether the recieved packet pertains to this client.
-
#to_array ⇒ Array
The to_array method returns the instance of the Packet as an array.
-
#to_jade_object ⇒ JadeObject
This method converts a Packet to a JadeObject.
-
#to_str ⇒ String
The to_str method just returns the instance of the Packet as a simple string with no formatting.
Constructor Details
#initialize(sender, reciever, type, data, check_sum = '1') ⇒ NilClass
The initialize method takes in all arguments of the packet and can default the check sum to 1.
27 28 29 30 31 32 33 |
# File 'client/packet.rb', line 27 def initialize(sender, reciever, type, data, check_sum = '1') @sender = sender @reciever = reciever @type = type @data = data @check_sum = check_sum end |
Instance Attribute Details
#:sender, :reciever, :type, :data(: sender, : reciever, : type, : data) ⇒ Object
These variables are assigned attr_accessor so that they can be accessed outside the object. They are needed in many different places.
20 |
# File 'client/packet.rb', line 20 attr_accessor :sender, :reciever, :type, :data |
#data ⇒ Object
These variables are assigned attr_accessor so that they can be accessed outside the object. They are needed in many different places.
20 21 22 |
# File 'client/packet.rb', line 20 def data @data end |
#reciever ⇒ Object
These variables are assigned attr_accessor so that they can be accessed outside the object. They are needed in many different places.
20 21 22 |
# File 'client/packet.rb', line 20 def reciever @reciever end |
#sender ⇒ Object
These variables are assigned attr_accessor so that they can be accessed outside the object. They are needed in many different places.
20 21 22 |
# File 'client/packet.rb', line 20 def sender @sender end |
#type ⇒ Object
These variables are assigned attr_accessor so that they can be accessed outside the object. They are needed in many different places.
20 21 22 |
# File 'client/packet.rb', line 20 def type @type end |
Instance Method Details
#check_packet? ⇒ Boolean
check_packet? checks the validity of the packet via the check sum. @todo: fully implement the check sum system
84 85 86 |
# File 'client/packet.rb', line 84 def check_packet? @check_sum == '1' end |
#empty? ⇒ Boolean
empty? checks whether the instance of the packet is empty.
74 75 76 |
# File 'client/packet.rb', line 74 def empty? !@sender.nil? && !@reciever.nil? && !@type.nil? && !@data.nil? && !@check_sum.nil? end |
#pertains_to_user?(username) ⇒ Boolean
pertains_to_user? checks whether the recieved packet pertains to this client.
93 94 95 96 |
# File 'client/packet.rb', line 93 def pertains_to_user?(username) ((@reciever == username) || (@reciever == 'all')) end |
#to_array ⇒ Array
The to_array method returns the instance of the Packet as an array.
40 41 42 |
# File 'client/packet.rb', line 40 def to_array [@sender, @reciever, @type, @data, @check_sum] end |
#to_jade_object ⇒ JadeObject
This method converts a Packet to a JadeObject. It creates JadePairs of each variable within the packet. A JadeObject is then created from an array of the JadePairs.
51 52 53 54 55 56 57 58 |
# File 'client/packet.rb', line 51 def to_jade_object sender_pair = JadePair.new('sender', @sender) reciever_pair = JadePair.new('reciever', @reciever) type_pair = JadePair.new('type', @type) data_pair = JadePair.new('data', @data) check_sum_pair = JadePair.new('checkSum', @check_sum) JadeObject.new([sender_pair, reciever_pair, type_pair, data_pair, check_sum_pair]) end |
#to_str ⇒ String
The to_str method just returns the instance of the Packet as a simple string with no formatting.
65 66 67 |
# File 'client/packet.rb', line 65 def to_str "#{@sender}, #{@reciever}, #{@type}, #{@data}, #{@check_sum}" end |