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.
398 399 400 401 |
# File 'lib/uuid.rb', line 398 def initialize(address) @socket = connect(address) at_exit { close } end |
Instance Method Details
#close ⇒ Object
Close the socket.
436 437 438 439 |
# File 'lib/uuid.rb', line 436 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.
415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/uuid.rb', line 415 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.
404 405 406 407 408 409 410 411 |
# File 'lib/uuid.rb', line 404 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
431 432 433 |
# File 'lib/uuid.rb', line 431 def inspect @socket ? "Server on #{Socket.unpack_sockaddr_in(@socket.getsockname).reverse!.join(':')}" : "Connection closed" end |
#next_sequence ⇒ Object
:nodoc: Stubbed to do nothing.
428 429 |
# File 'lib/uuid.rb', line 428 def next_sequence #:nodoc: Stubbed to do nothing. end |