Class: Specwrk::IPC

Inherits:
Object
  • Object
show all
Defined in:
lib/specwrk/ipc.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_pid: Process.pid, parent_socket: nil, child_socket: nil) ⇒ IPC

Returns a new instance of IPC.



14
15
16
17
18
19
# File 'lib/specwrk/ipc.rb', line 14

def initialize(parent_pid: Process.pid, parent_socket: nil, child_socket: nil)
  @parent_pid = parent_pid

  @parent_socket, @child_socket = parent_socket, child_socket
  @parent_socket, @child_socket = UNIXSocket.pair if @parent_socket.nil? && @child_socket.nil?
end

Instance Attribute Details

#child_socketObject (readonly)

Returns the value of attribute child_socket.



5
6
7
# File 'lib/specwrk/ipc.rb', line 5

def child_socket
  @child_socket
end

#parent_socketObject (readonly)

Returns the value of attribute parent_socket.



5
6
7
# File 'lib/specwrk/ipc.rb', line 5

def parent_socket
  @parent_socket
end

Class Method Details

.from_child_fd(fd, parent_pid:) ⇒ Object



7
8
9
10
11
12
# File 'lib/specwrk/ipc.rb', line 7

def self.from_child_fd(fd, parent_pid:)
  new(
    parent_pid: parent_pid,
    child_socket: UNIXSocket.for_fd(fd)
  )
end

Instance Method Details

#readObject



25
26
27
28
29
30
31
32
# File 'lib/specwrk/ipc.rb', line 25

def read
  IO.select([socket])

  data = socket.gets&.chomp
  return if data.nil? || data.length.zero? || data == "INT"

  data
end

#write(msg) ⇒ Object



21
22
23
# File 'lib/specwrk/ipc.rb', line 21

def write(msg)
  socket.puts msg.to_s
end