Class: Lwes::Serialization::IpAddr

Inherits:
BinData::Primitive
  • Object
show all
Defined in:
lib/lwes/serialization.rb

Instance Method Summary collapse

Instance Method Details

#getObject



69
70
71
# File 'lib/lwes/serialization.rb', line 69

def get
  self.int32_to_ip(self.data)
end

#int32_to_ip(num) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/lwes/serialization.rb', line 58

def int32_to_ip(num)
  ary = []
  
  4.times do
    ary << (num & 0xff)
    num = num >> 8
  end

  ary.join(".")
end

#ip_to_int32(str) ⇒ Object



53
54
55
56
# File 'lib/lwes/serialization.rb', line 53

def ip_to_int32(str)
  ary = str.to_s.split(".").collect{ |byte| byte.to_i }.slice(0,4)
  ((ary[3] & 0xff) << 24) + ((ary[2] & 0xff) << 16) + ((ary[1] & 0xff) << 8) + (ary[0] & 0xff)
end

#set(val) ⇒ Object



73
74
75
# File 'lib/lwes/serialization.rb', line 73

def set(val)
  self.data = ip_to_int32(val)
end