Class: UUID::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/uuid.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ Client

Returns a new instance of Client.



458
459
460
461
# File 'lib/uuid.rb', line 458

def initialize(address)
  @socket = connect(address)
  at_exit { close }
end

Instance Method Details

#closeObject

Close the socket.



496
497
498
499
# File 'lib/uuid.rb', line 496

def close
  @socket.shutdown if @socket
  @socket = nil
end

#connect(address) ⇒ Object

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



475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/uuid.rb', line 475

def connect(address)
  return address unless String === address
  if address[0] == ?/
    sock = UNIXSocket.new(address)
  elsif address =~ /^(\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

#generate(format = :default) ⇒ Object

Talks to server and returns new UUID in specified format.

Raises:

  • (ArgumentError)


464
465
466
467
468
469
470
471
# File 'lib/uuid.rb', line 464

def generate(format = :default)
  @socket.write "\0"
  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

#inspectObject



491
492
493
# File 'lib/uuid.rb', line 491

def inspect
  @socket ? "Server on #{Socket.unpack_sockaddr_in(@socket.getsockname).reverse!.join(':')}" : "Connection closed"
end

#next_sequenceObject

:nodoc: Stubbed to do nothing.



488
489
# File 'lib/uuid.rb', line 488

def next_sequence #:nodoc: Stubbed to do nothing.
end