class QB::IPC::STDIO::Client::Connection

@todo document Connection class.

Attributes

env_var_name[R]

TODO document `env_var_name` attribute.

@return [String]

global_original[R]

TODO document `global_original` attribute.

@return [attr_type]

log[R]

@return [Connection]

name[R]

TODO document `name` attribute.

@return [Symbol]

path[R]

TODO document `path` attribute.

@return [Pathname?]

socket[R]

TODO document `socket` attribute.

@return [UNIXSocket?]

stderr[R]

@return [Connection]

stdin[R]

@return [Connection]

stdout[R]

@return [Connection]

type[R]

TODO document `type` attribute.

@return [:in | :out]

Public Class Methods

new(name:, type: @name = name) click to toggle source

Instantiate a new `Connection`.

# File lib/qb/ipc/stdio/client.rb, line 101
def initialize name:, type:
  @name = name
  @type = type
  @global_original = nil
  @path = nil
  @socket = nil
  @env_var_name = QB::IPC::STDIO.path_env_var_name name
end

Public Instance Methods

connect!() click to toggle source
# File lib/qb/ipc/stdio/client.rb, line 119
def connect!
  if connected?
    raise "#{ inspect } is already connected!"
  end
  
  if get_path!
    @global_original = global_get
    @socket = UNIXSocket.new path.to_s
    global_set! socket
  end
end
connected?() click to toggle source

Instance Methods

# File lib/qb/ipc/stdio/client.rb, line 114
def connected?
  !!socket
end
disconnect!() click to toggle source
# File lib/qb/ipc/stdio/client.rb, line 132
def disconnect!
  return unless connected?
  
  logger.debug "Disconnecting...",
    name: name,
    socket: socket,
    global_original: global_original
  
  # Restore the original global and `nil` it out (if we have one)
  if global_original
    global_set! global_original
    @global_original = nil
  end
  
  # Flush the socket if it's an out-bound
  socket.flush if type == :out
  
  # And close and `nil` it
  socket.close
  @socket = nil
end

Protected Instance Methods

connections() click to toggle source

@return [Array<Connection>]

The three {Connection} instances.
# File lib/qb/ipc/stdio/client.rb, line 246
def connections
  [ @stdin, @stdout, @stderr, @log ]
end
get_path!() click to toggle source

Get the socket path from the ENV and set `@path` to it (which may be `nil` if we don't find anything).

@return [Pathname?]

# File lib/qb/ipc/stdio/client.rb, line 163
def get_path!
  @path = ENV[ env_var_name ]
  @path = path.to_pn if path
  path
end
global_get() click to toggle source

Get the IO global (`$stdin`, `$stdout`, `$stderr`) if {#name} lines up for one of those.

@return [IO?]

# File lib/qb/ipc/stdio/client.rb, line 175
def global_get
  case name
  when :in
    $stdin
  when :out
    $stdout
  when :err
    $stderr
  end
end
global_set!(value) click to toggle source

Set the IO global (`$stdin`, `$stdout`, `$stderr`) to `value` if {#name} lines up for one of those.

@param [IO] value @return [void]

# File lib/qb/ipc/stdio/client.rb, line 193
def global_set! value
  case name
  when :in
    $stdin = value
  when :out
    $stdout = value
  when :err
    $stderr = value
  end
end