Sha256: 57c9b742593229eaa1d78850bb2db7367acaa70fac188b8700babb3c9791bd42

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require "chattyproc/version"

module ChattyProc
  class PipeServer

    # Public: Sets up a new PipeServer
    def initialize
      @callee = []
      @caller = []
      @callee[0], @caller[1] = IO.pipe
      @caller[0], @callee[1] = IO.pipe

      @me = nil
    end

    # Public: Identifies the current process as the callee process
    def callee!
      @me = @callee

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

    # Public: Identifies the current process as the caller process
    def caller!
      @me = @caller

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

    # Public: Writes a message to the appropriate pipe. The message can be
    # anything that will Marshal cleanly.
    def write(message)
      encoded_message = [Marshal.dump(message)].pack("m0")
      @me[1].puts(encoded_message)
    end

    # Public: Reads a message from the appropriate pipe and unmarshalls it
    def read
      raw_message = @me[0].gets

      return nil if raw_message.nil?

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

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chattyproc-1.0.0 lib/chattyproc.rb