Class: Uninterruptible::FileDescriptorServer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_object) ⇒ FileDescriptorServer

Creates a new FileDescriptorServer and starts a listenting socket server

Parameters:

  • Any (IO)

    IO object that will be shared by this server



11
12
13
14
15
# File 'lib/uninterruptible/file_descriptor_server.rb', line 11

def initialize(io_object)
  @io_object = io_object

  start_socket_server
end

Instance Attribute Details

#io_objectObject (readonly)

Returns the value of attribute io_object.



6
7
8
# File 'lib/uninterruptible/file_descriptor_server.rb', line 6

def io_object
  @io_object
end

#socket_serverObject (readonly)

Returns the value of attribute socket_server.



6
7
8
# File 'lib/uninterruptible/file_descriptor_server.rb', line 6

def socket_server
  @socket_server
end

Instance Method Details

#closeObject

Close the socket server and tidy up any created files



34
35
36
37
38
39
# File 'lib/uninterruptible/file_descriptor_server.rb', line 34

def close
  socket_server.close

  File.delete(socket_path)
  Dir.rmdir(socket_directory)
end

#serve_file_descriptorObject

Accept the next client connection and send it the file descriptor

Raises:

  • (RuntimeError)

    Raises a runtime error if the socket server is closed



25
26
27
28
29
30
31
# File 'lib/uninterruptible/file_descriptor_server.rb', line 25

def serve_file_descriptor
  raise "File descriptor server has been closed" if socket_server.closed?

  client = socket_server.accept
  client.send_io(io_object.to_io)
  client.close
end

#socket_pathString

Returns Location on disk where socket server is listening.

Returns:

  • (String)

    Location on disk where socket server is listening



18
19
20
# File 'lib/uninterruptible/file_descriptor_server.rb', line 18

def socket_path
  @socket_path ||= File.join(socket_directory, 'fd.sock')
end