Parent

RExec::Connection

This class represents an abstract connection to another ruby process. The interface does not impose any structure on the way this communication link works, except for the fact you can send and receive objects. You can implement whatever kind of idiom you need for communication on top of this library.

Depending on how you set things up, this could connect to a local ruby process, or a remote ruby process via `ssh`.

To set up a connection, you need to use {start_server}.

Attributes

exceptions[RW]

Whether to send exceptions across the wire, or handle normally (e.g. print to stdout):

handler[RW]

The object that will handle remote proxy invocations.

proxy[R]

The proxy object that will dispatch RPCs.

Public Class Methods

build(process, options, &block) click to toggle source
# File lib/rexec/connection.rb, line 22
def self.build(process, options, &block)
        cin = process.input
        cout = process.output
        cerr = process.error

        # We require both cin and cout to be connected in order for connection to work
        raise InvalidConnectionError.new("Input (#{cin}) or Output (#{cout}) is not connected!") unless cin and cout

        yield cin

        cin.puts("\0004")

        return self.new(cout, cin, cerr)
end
new(input, output, error = nil) click to toggle source

Create a new connection. You need to supply a pipe for reading input, a pipe for sending output, and optionally a pipe for errors to be read from.

# File lib/rexec/connection.rb, line 39
def initialize(input, output, error = nil)
        @input = input
        @output = output

        @running = true
        @exceptions = :send

        @error = error

        @receive_mutex = Mutex.new
        @send_mutex = Mutex.new
end

Public Instance Methods

dump_errors(to = $stderr) click to toggle source

Dump any text which has been written to $stderr in the child process.

# File lib/rexec/connection.rb, line 117
def dump_errors(to = $stderr)
        if @error and !@error.closed?
                while true
                        result = IO.select([@error], [], [], 0)

                        break if result == nil

                        to.puts @error.readline.chomp
                end
        end
end
error() click to toggle source

The pipe used for receiving errors. On the client side this pipe is writable, on the server side this pipe is readable. You should avoid using it on the client side and simply use $stderr.

# File lib/rexec/connection.rb, line 73
def error
        @error
end
input() click to toggle source

The pipe used for reading data

# File lib/rexec/connection.rb, line 62
def input
        @input
end
output() click to toggle source

The pipe used for writing data

# File lib/rexec/connection.rb, line 67
def output
        @output
end
receive_object() click to toggle source

Receive an object from the connection. This function is thread-safe. This function may block.

# File lib/rexec/connection.rb, line 130
def receive_object
        object = nil

        @receive_mutex.synchronize do
                begin
                        object = Marshal.load(@input)
                rescue EOFError
                        object = nil
                        @running = false
                end
        end

        if object and object.kind_of?(Exception)
                raise object
        end

        return object
end
run(&block) click to toggle source

This is a very simple runloop. It provides an object when it is received.

# File lib/rexec/connection.rb, line 91
def run(&block)
        while @running
                pipes = IO.select([@input])

                if pipes[0].size > 0
                        object = receive_object

                        if object == nil
                                @running = false
                                return
                        end

                        begin
                                yield object
                        rescue Exception => ex
                                if @exceptions == :send
                                        send_object(ex)
                                else
                                        raise
                                end
                        end
                end
        end
end
running?() click to toggle source

Return whether or not the connection is running.

# File lib/rexec/connection.rb, line 86
def running?
        @running
end
send_object(*objects) click to toggle source

Send object(s). This function is thread-safe.

# File lib/rexec/connection.rb, line 150
def send_object(*objects)
        @send_mutex.synchronize do
                objects.each do |o|
                        data = Marshal.dump(o)
                        @output.write(data)
                end

                @output.flush
        end
end
stop() click to toggle source

Stop the connection, and close the output pipe.

# File lib/rexec/connection.rb, line 78
def stop
        if @running
                @running = false
                @output.close
        end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.