Class: TConsole::PipeServer

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

Instance Method Summary collapse

Constructor Details

#initializePipeServer

Returns a new instance of PipeServer.



4
5
6
7
8
9
10
11
# File 'lib/tconsole/pipe_server.rb', line 4

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

  @me = nil
end

Instance Method Details

#callee!Object

Identifies the current process as the callee process



14
15
16
17
18
19
20
# File 'lib/tconsole/pipe_server.rb', line 14

def callee!
  @me = @callee

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

#caller!Object

Identifies the current process as the caller process



23
24
25
26
27
28
29
# File 'lib/tconsole/pipe_server.rb', line 23

def caller!
  @me = @caller

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

#readObject

Reads a message from the appropriate pipe and unmarshalls it



39
40
41
42
43
44
45
# File 'lib/tconsole/pipe_server.rb', line 39

def read
  raw_message = @me[0].gets

  return nil if raw_message.nil?

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

#write(message) ⇒ Object

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



33
34
35
36
# File 'lib/tconsole/pipe_server.rb', line 33

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