module RubySkynet::Common

Constants

BINARY_ENCODING

Public Class Methods

included(base) click to toggle source
# File lib/ruby_skynet/common.rb, line 34
def self.included(base)
  base.extend ClassMethods
  base.class_eval do
    include SemanticLogger::Loggable
  end
end
local_ip_address(remote_ip = 'google.com') click to toggle source

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

# 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) click to toggle source

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

connection failure occurs
# File lib/ruby_skynet/common.rb, line 14
def self.read_bson_document(socket)
  # Read 4 byte size of following BSON document
  if bytes = socket.read(4)
    bytes.force_encoding(BINARY_ENCODING)
    # Read BSON document
    sz = bytes.unpack("V")[0]
    raise "Invalid Data received from server:#{bytes.inspect}" unless sz

    bytes << socket.read(sz - 4)
    raise "Socket is not returning #{sz} requested bytes. #{bytes.length} bytes returned" unless sz == bytes.length
    Hash.from_bson(StringIO.new(bytes))
  end
end