lib/ruby_skynet/common.rb in ruby_skynet-0.4.0.pre2 vs lib/ruby_skynet/common.rb in ruby_skynet-0.4.0
- old
+ new
@@ -1,5 +1,7 @@
+require 'socket'
+
module RubySkynet
module Common
# Returns a BSON document read from the socket.
# Returns nil if the operation times out or if a network
@@ -16,11 +18,17 @@
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 "Celluloid is not returning #{sz} requested bytes. #{bytebuf.length} bytes returned" unless sz == bytebuf.length
+ raise "Socket is not returning #{sz} requested bytes. #{bytebuf.length} bytes returned" unless sz == bytebuf.length
return BSON.deserialize(bytebuf)
+ end
+
+ # 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
+ def self.local_ip_address(remote_ip = '64.233.187.99')
+ @@local_ip_address ||= ::UDPSocket.open {|s| s.connect(remote_ip, 1); s.addr.last }
end
end
end
\ No newline at end of file