class UUID::Client

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

Public Class Methods

new(address) click to toggle source
    # File lib/uuid.rb
458 def initialize(address)
459   @socket = connect(address)
460   at_exit { close }
461 end

Public Instance Methods

close() click to toggle source

Close the socket.

    # File lib/uuid.rb
496 def close
497   @socket.shutdown if @socket
498   @socket = nil
499 end
connect(address) click to toggle source

Returns UNIXSocket or TCPSocket from address. Returns argument if not a string, so can pass through.

    # File lib/uuid.rb
475 def connect(address)
476   return address unless String === address
477   if address[0] == ?/
478     sock = UNIXSocket.new(address)
479   elsif address =~ /^(\d+\.\d+\.\d+\.\d+):(\d+)$/
480     sock = TCPSocket.new($1, $2.to_i)
481   else
482     raise ArgumentError, "Don't know how to connect to #{address}"
483   end
484   sock.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1) if defined?(TCP_NODELAY)
485   sock
486 end
generate(format = :default) click to toggle source

Talks to server and returns new UUID in specified format.

    # File lib/uuid.rb
464 def generate(format = :default)
465   @socket.write "\0"
466   uuid = @socket.read(36)
467   return uuid if format == :default
468   template = FORMATS[format]
469   raise ArgumentError, "invalid UUID format #{format.inspect}" unless template
470   template % uuid.split("-").map { |p| p.to_i(16) }
471 end
inspect() click to toggle source
    # File lib/uuid.rb
491 def inspect
492   @socket ? "Server on #{Socket.unpack_sockaddr_in(@socket.getsockname).reverse!.join(':')}" : "Connection closed"
493 end