Parent

Included Modules

RText::Service

Public Class Methods

new(service_provider, options={}) click to toggle source

Creates an RText backend service. Options:

:timeout
  idle time in seconds after which the service will terminate itelf

:logger
  a logger object on which the service will write its logging output

:on_startup: 
  a Proc which is called right after the service has started up
  can be used to output version information
# File lib/rtext/service.rb, line 29
def initialize(service_provider, options={})
  @service_provider = service_provider
  @timeout = options[:timeout] || 60
  @logger = options[:logger]
  @on_startup = options[:on_startup]
end

Public Instance Methods

message_received(sock, obj) click to toggle source
# File lib/rtext/service.rb, line 94
def message_received(sock, obj)
  if check_request(obj) 
    @logger.debug("request: "+obj.inspect) if @logger
    response = { "type" => "response", "invocation_id" => obj["invocation_id"] }
    case obj["command"]
    when "load_model"
      load_model(sock, obj, response)
    when "content_complete"
      content_complete(sock, obj, response)
    when "link_targets" 
      link_targets(sock, obj, response)
    when "find_elements"
      find_elements(sock, obj, response)
    when "stop"
      @logger.info("RText service, stopping now (stop requested)") if @logger
      @stop_requested = true
    else
      @logger.warn("unknown command #{obj["command"]}") if @logger
      response["type"] = "unknown_command_error"
      response["command"] = obj["command"] 
    end
    @logger.debug("response: "+response.inspect) if response && @logger
    send_response(sock, response)
  end
end
run() click to toggle source
# File lib/rtext/service.rb, line 36
def run
  server = create_server 
  puts "RText service, listening on port #{server.addr[1]}"
  @on_startup.call if @on_startup
  $stdout.flush

  last_access_time = Time.now
  last_flush_time = Time.now
  @stop_requested = false
  sockets = []
  request_data = {}
  while !@stop_requested
    begin
      sock = server.accept_nonblock
      sock.sync = true
      sockets << sock
      @logger.info "accepted connection" if @logger
    rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR, Errno::EWOULDBLOCK
    rescue Exception => e
      @logger.warn "unexpected exception during socket accept: #{e.class}"
    end
    sockets.dup.each do |sock|
      data = nil
      begin
        data = sock.read_nonblock(100000)
      rescue Errno::EWOULDBLOCK
      rescue IOError, EOFError, Errno::ECONNRESET, Errno::ECONNABORTED
        sock.close
        request_data[sock] = nil
        sockets.delete(sock)
      rescue Exception => e
        # catch Exception to make sure we don't crash due to unexpected exceptions
        @logger.warn "unexpected exception during socket read: #{e.class}"
        sock.close
        request_data[sock] = nil
        sockets.delete(sock)
      end
      if data
        last_access_time = Time.now
        request_data[sock] ||= ""
        request_data[sock].concat(data)
        while obj = extract_message(request_data[sock])
          message_received(sock, obj)
        end
      end
    end
    IO.select([server] + sockets, [], [], 1)
    if Time.now > last_access_time + @timeout
      @logger.info("RText service, stopping now (timeout)") if @logger
      break 
    end
    if Time.now > last_flush_time + FlushInterval
      $stdout.flush
      last_flush_time = Time.now
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.