Class: MessagePack::RPC::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/msgpack/rpc/address.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Address

BSD



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/msgpack/rpc/address.rb', line 39

def initialize(host, port)
  raw = Socket.pack_sockaddr_in(port, host)
  family = raw.unpack('S')[0]
  if family == Socket::AF_INET
    @serial = raw[2,6]
  elsif family == Socket::AF_INET6
    @serial = raw[2,2] + raw[8,16]
  else
    raise "Unknown address family: #{family}"
  end
end

Class Method Details

.load(raw) ⇒ Object



116
117
118
# File 'lib/msgpack/rpc/address.rb', line 116

def self.load(raw)
  Address.new *parse(raw)
end

.parse(raw) ⇒ Object



112
113
114
# File 'lib/msgpack/rpc/address.rb', line 112

def self.parse(raw)
  Socket.unpack_sockaddr_in(parse_sockaddr(raw)).reverse
end

.parse_sockaddr(raw) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/msgpack/rpc/address.rb', line 86

def self.parse_sockaddr(raw)
  raw.force_encoding('ASCII-8BIT')
  if raw.length == 6
    addr = Socket.pack_sockaddr_in(0, '0.0.0.0')
    addr[2,6] = raw[0,6]
  else
    addr = Socket.pack_sockaddr_in(0, '::')
    addr[2,2]  = raw[0,2]
    addr[8,16] = raw[2,16]
  end
  addr
end

Instance Method Details

#<=>(o) ⇒ Object



136
137
138
# File 'lib/msgpack/rpc/address.rb', line 136

def <=>(o)
  dump <=> o.dump
end

#==(o) ⇒ Object



152
153
154
# File 'lib/msgpack/rpc/address.rb', line 152

def ==(o)
  eql?(o)
end

#connectable?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/msgpack/rpc/address.rb', line 73

def connectable?
  port != 0
end

#dumpObject



120
121
122
# File 'lib/msgpack/rpc/address.rb', line 120

def dump
  @serial
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/msgpack/rpc/address.rb', line 144

def eql?(o)
  o.class == Address && dump.eql?(o.dump)
end

#hashObject



148
149
150
# File 'lib/msgpack/rpc/address.rb', line 148

def hash
  dump.hash
end

#hostObject



65
66
67
# File 'lib/msgpack/rpc/address.rb', line 65

def host
  unpack[0]
end

#inspectObject



140
141
142
# File 'lib/msgpack/rpc/address.rb', line 140

def inspect
  "#<#{self.class} #{to_s} @serial=#{@serial.inspect}>"
end

#portObject



69
70
71
# File 'lib/msgpack/rpc/address.rb', line 69

def port
  unpack[1]
end

#sockaddrObject



77
78
79
# File 'lib/msgpack/rpc/address.rb', line 77

def sockaddr
  Address.parse_sockaddr(@serial)
end

#to_aObject



132
133
134
# File 'lib/msgpack/rpc/address.rb', line 132

def to_a
  unpack
end

#to_msgpack(out = '') ⇒ Object



124
125
126
# File 'lib/msgpack/rpc/address.rb', line 124

def to_msgpack(out = '')
  @serial.to_msgpack(out)
end

#to_sObject



128
129
130
# File 'lib/msgpack/rpc/address.rb', line 128

def to_s
  unpack.join(':')
end

#unpackObject



81
82
83
# File 'lib/msgpack/rpc/address.rb', line 81

def unpack
  Address.parse(@serial)
end