Class: UUID::Client
- Inherits:
-
Object
- Object
- UUID::Client
- Defined in:
- lib/uuid.rb
Overview
Instance Method Summary collapse
-
#close ⇒ Object
Close the socket.
-
#connect(address) ⇒ Object
Returns UNIXSocket or TCPSocket from address.
-
#generate(format = :default) ⇒ Object
Talks to server and returns new UUID in specified format.
-
#initialize(address) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
-
#next_sequence ⇒ Object
:nodoc: Stubbed to do nothing.
Constructor Details
#initialize(address) ⇒ Client
Returns a new instance of Client.
451 452 453 454 |
# File 'lib/uuid.rb', line 451 def initialize(address) @socket = connect(address) at_exit { close } end |
Instance Method Details
#close ⇒ Object
Close the socket.
489 490 491 492 |
# File 'lib/uuid.rb', line 489 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.
468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/uuid.rb', line 468 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.
457 458 459 460 461 462 463 464 |
# File 'lib/uuid.rb', line 457 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 |
#inspect ⇒ Object
484 485 486 |
# File 'lib/uuid.rb', line 484 def inspect @socket ? "Server on #{Socket.unpack_sockaddr_in(@socket.getsockname).reverse!.join(':')}" : "Connection closed" end |
#next_sequence ⇒ Object
:nodoc: Stubbed to do nothing.
481 482 |
# File 'lib/uuid.rb', line 481 def next_sequence #:nodoc: Stubbed to do nothing. end |