Every server needs a client. Client provides you with the single ultimate method: generate. Typically you’ll use this instead of the local UUID generator:
UUID.server = UUID::SOCKET_NAME
# File lib/uuid.rb, line 447 def initialize(address) @socket = connect(address) at_exit { close } end
Close the socket.
# File lib/uuid.rb, line 485 def close @socket.shutdown if @socket @socket = nil end
Returns UNIXSocket or TCPSocket from address. Returns argument if not a string, so can pass through.
# File lib/uuid.rb, line 464 def connect(address) return address unless String === address if address[0] == // sock = UNIXSocket.new(address) elsif address =~ %r^(\d+\.\d+\.\d+\.\d+):(\d+)$/ sock = TCPSocket.new($1, $2.to_i) else raise ArgumentError, "Don't know how to connect to #{address}" end sock.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1) if defined?(TCP_NODELAY) sock end
Talks to server and returns new UUID in specified format.
# File lib/uuid.rb, line 453 def generate(format = :default) @socket.write "\00"" uuid = @socket.read(36) return uuid if format == :default template = FORMATS[format] raise ArgumentError, "invalid UUID format #{format.inspect}" unless template template % uuid.split("-").map { |p| p.to_i(16) } end
# File lib/uuid.rb, line 480 def inspect @socket ? "Server on #{Socket.unpack_sockaddr_in(@socket.getsockname).reverse!.join(':')}" : "Connection closed" end