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 449 def initialize(address) 450 @socket = connect(address) 451 at_exit { close } 452 end
Public Instance Methods
close()
click to toggle source
Close the socket.
# File lib/uuid.rb 487 def close 488 @socket.shutdown if @socket 489 @socket = nil 490 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 466 def connect(address) 467 return address unless String === address 468 if address[0] == ?/ 469 sock = UNIXSocket.new(address) 470 elsif address =~ /^(\d+\.\d+\.\d+\.\d+):(\d+)$/ 471 sock = TCPSocket.new($1, $2.to_i) 472 else 473 raise ArgumentError, "Don't know how to connect to #{address}" 474 end 475 sock.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1) if defined?(TCP_NODELAY) 476 sock 477 end
generate(format = :default)
click to toggle source
Talks to server and returns new UUID
in specified format.
# File lib/uuid.rb 455 def generate(format = :default) 456 @socket.write "\0" 457 uuid = @socket.read(36) 458 return uuid if format == :default 459 template = FORMATS[format] 460 raise ArgumentError, "invalid UUID format #{format.inspect}" unless template 461 template % uuid.split("-").map { |p| p.to_i(16) } 462 end
inspect()
click to toggle source
# File lib/uuid.rb 482 def inspect 483 @socket ? "Server on #{Socket.unpack_sockaddr_in(@socket.getsockname).reverse!.join(':')}" : "Connection closed" 484 end