Class: ChattyProc::PipeServer

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

Instance Method Summary collapse

Constructor Details

#initializePipeServer

Public: Sets up a new PipeServer



7
8
9
10
11
12
13
14
# File 'lib/chattyproc.rb', line 7

def initialize
  @callee = []
  @caller = []
  @callee[0], @caller[1] = IO.pipe
  @caller[0], @callee[1] = IO.pipe

  @me = nil
end

Instance Method Details

#callee!Object

Public: Identifies the current process as the callee process



17
18
19
20
21
22
23
# File 'lib/chattyproc.rb', line 17

def callee!
  @me = @callee

  @caller.each do |io|
    io.close
  end
end

#caller!Object

Public: Identifies the current process as the caller process



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

def caller!
  @me = @caller

  @callee.each do |io|
    io.close
  end
end

#readObject

Public: Reads a message from the appropriate pipe and unmarshalls it



42
43
44
45
46
47
48
# File 'lib/chattyproc.rb', line 42

def read
  raw_message = @me[0].gets

  return nil if raw_message.nil?

  Marshal.load(raw_message.unpack("m")[0])
end

#write(message) ⇒ Object

Public: Writes a message to the appropriate pipe. The message can be anything that will Marshal cleanly.



36
37
38
39
# File 'lib/chattyproc.rb', line 36

def write(message)
  encoded_message = [Marshal.dump(message)].pack("m0")
  @me[1].puts(encoded_message)
end