Sha256: f89dbc866b010063f77c97350f8caa0c173554ea6697af60eb837b9396204547

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

require 'rubygems'
require 'celerity'


module Culerity
  class CelerityServer
    
    def initialize(_in, _out)
      @proxies = {}
      @browsers = []

      while(true)
        call, block = eval _in.gets.to_s.strip
        return if call == "_exit_"
        next(close_browsers) if call == "_close_browsers_"
        unless call.nil?
          begin
            result = target(call.first).send call[1], *call[2..-1], &block
            _out << "[:return, #{proxify result}]\n"
          rescue => e
            _out << "[:exception, \"#{e.class.name}\", #{e.message.inspect}, #{e.backtrace.inspect}]\n"
          end
        end

      end
      
    end
    
    private
    
    def configure_browser(options)
      @browser_options = options
    end
    
    def new_browser(options, number = nil)
      number ||= @browsers.size
      @browsers[number] = Celerity::Browser.new(options || @browser_options || {})
      "browser#{number}"
    end

    def close_browsers
      @browsers.each { |browser| browser.close }
      @browsers = []
    end

    def browser(number)
      unless @browsers[number]
        new_browser(nil, number)
      end
      @browsers[number]
    end
    
    def target(object_id)
      if object_id =~ /browser(\d+)/
        browser($1.to_i)
      elsif object_id == 'celerity'
        self
      else
        @proxies[object_id]
      end
    end
    
    def proxify(result)
      if result.is_a?(Array)
        "[" + result.map {|x| proxify(x) }.join(", ") + "]"
      elsif [String, TrueClass, FalseClass, Fixnum, Float, NilClass].include?(result.class)
        result.inspect
      else
        @proxies[result.object_id] = result
        "Culerity::RemoteObjectProxy.new(#{result.object_id}, @io)"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
culerity-0.2.8 lib/culerity/celerity_server.rb
fletcherm-culerity-0.2.7 lib/culerity/celerity_server.rb
culerity-0.2.7 lib/culerity/celerity_server.rb