Class: Packet

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

Overview

The Packet object stores various pieces of information regarding both sent and recieved packets.

Author:

  • Joseph Beck

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • (String, String, String, String, String)


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

#dataObject

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

#recieverObject

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

#senderObject

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

#typeObject

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

Parameters:

  • (NilClass)

Returns:

  • (Boolean)


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.

Parameters:

  • (NilClass)

Returns:

  • (Boolean)


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.

Parameters:

  • (String)

Returns:

  • (Boolean)


93
94
95
96
# File 'client/packet.rb', line 93

def pertains_to_user?(username)
  ((@reciever == username) ||
   (@reciever == 'all'))
end

#to_arrayArray

The to_array method returns the instance of the Packet as an array.

Parameters:

  • (NilClass)

Returns:

  • (Array)


40
41
42
# File 'client/packet.rb', line 40

def to_array
  [@sender, @reciever, @type, @data, @check_sum]
end

#to_jade_objectJadeObject

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.

Parameters:

  • (NilClass)

Returns:



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_strString

The to_str method just returns the instance of the Packet as a simple string with no formatting.

Parameters:

  • (NilClass)

Returns:

  • (String)


65
66
67
# File 'client/packet.rb', line 65

def to_str
  "#{@sender}, #{@reciever}, #{@type}, #{@data}, #{@check_sum}"
end