Class: QB::IPC::STDIO::Client::Connection

Inherits:
Object
  • Object
show all
Includes:
NRSER::Log::Mixin
Defined in:
lib/qb/ipc/stdio/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, type:) ⇒ Connection

Instantiate a new Connection.



101
102
103
104
105
106
107
108
# 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

Instance Attribute Details

#env_var_nameString (readonly)

TODO document env_var_name attribute.

Returns:

  • (String)


94
95
96
# File 'lib/qb/ipc/stdio/client.rb', line 94

def env_var_name
  @env_var_name
end

#global_originalattr_type (readonly)

TODO document global_original attribute.

Returns:

  • (attr_type)


87
88
89
# File 'lib/qb/ipc/stdio/client.rb', line 87

def global_original
  @global_original
end

#nameSymbol (readonly)

TODO document name attribute.

Returns:

  • (Symbol)


59
60
61
# File 'lib/qb/ipc/stdio/client.rb', line 59

def name
  @name
end

#pathPathname? (readonly)

TODO document path attribute.

Returns:

  • (Pathname?)


73
74
75
# File 'lib/qb/ipc/stdio/client.rb', line 73

def path
  @path
end

#socketUNIXSocket? (readonly)

TODO document socket attribute.

Returns:

  • (UNIXSocket?)


80
81
82
# File 'lib/qb/ipc/stdio/client.rb', line 80

def socket
  @socket
end

#type:in | :out (readonly)

TODO document type attribute.

Returns:

  • (:in | :out)


66
67
68
# File 'lib/qb/ipc/stdio/client.rb', line 66

def type
  @type
end

Instance Method Details

#connect!Object



119
120
121
122
123
124
125
126
127
128
129
# 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?Boolean

Instance Methods

Returns:

  • (Boolean)


114
115
116
# File 'lib/qb/ipc/stdio/client.rb', line 114

def connected?
  !!socket
end

#disconnect!Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 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