Module: RubySkynet::Common

Defined in:
lib/ruby_skynet/common.rb

Class Method Summary collapse

Class Method Details

.local_ip_address(remote_ip = 'google.com') ⇒ Object

Returns the local ip address being used by this machine to talk to the internet. By default connects to Google and determines what IP Address is used locally



30
31
32
# File 'lib/ruby_skynet/common.rb', line 30

def self.local_ip_address(remote_ip = 'google.com')
  @@local_ip_address ||= ::UDPSocket.open {|s| s.connect(remote_ip, 1); s.addr.last }
end

.read_bson_document(socket) ⇒ Object

Returns a BSON document read from the socket. Returns nil if the operation times out or if a network

connection failure occurs


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby_skynet/common.rb', line 10

def self.read_bson_document(socket)
  bytebuf = BSON::ByteBuffer.new
  # Read 4 byte size of following BSON document
  bytes = socket.read(4)

  # No more data
  return unless bytes

  # Read BSON document
  sz = bytes.unpack("V")[0]
  raise "Invalid Data received from server:#{bytes.inspect}" unless sz

  bytebuf.append!(bytes)
  bytebuf.append!(socket.read(sz - 4))
  raise "Socket is not returning #{sz} requested bytes. #{bytebuf.length} bytes returned" unless sz == bytebuf.length
  return BSON.deserialize(bytebuf)
end