Module: RubySkynet::Common

Included in:
Client
Defined in:
lib/ruby_skynet/common.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

BINARY_ENCODING =
Encoding.find("binary")

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



34
35
36
37
38
39
# 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') ⇒ 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


14
15
16
17
18
19
20
21
22
23
24
25
26
# 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