Module: Rum::Server

Defined in:
lib/rum/server.rb

Defined Under Namespace

Modules: IRBCompletion

Constant Summary collapse

EvalBinding =
TOPLEVEL_BINDING

Class Method Summary collapse

Class Method Details

.close_connectionsObject

Temporary hack. MacRuby crashes on the ensure clause above when the server thread is killed. This function allows for manually closing the connections.



33
34
35
# File 'lib/rum/server.rb', line 33

def close_connections
  [@server, @connection].compact.each { |e| e.close unless e.closed? }
end

.error_message(exception) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/rum/server.rb', line 59

def error_message(exception)
  # Hide the internals from the backtrace
  backtrace = exception.backtrace.reject { |frame| frame =~ /^#{__FILE__}/ }
  msg = ["Rum-Server: Evaluation Error."]
  msg << "#{exception.class}: #{exception}"
  msg += backtrace
  msg.join "\n"
end

.handle(connection) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/rum/server.rb', line 48

def handle(connection)
  return nil unless message = connection.receive
  result = begin
             eval(message, EvalBinding).inspect
           rescue Exception => exception
             error_message(exception)
           end
  connection.dispatch(result)
rescue SystemCallError # connection errors
end

.startObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rum/server.rb', line 12

def start
  return if @thread
  @thread = Thread.new do
    @server = TCPServer.new('127.0.0.1', Remote.default_port)
    puts "Server started."
    begin
      loop do
        @connection = Remote::Connection.new(@server.accept)
        handle(@connection)
        @connection.close
      end
    ensure # clean up when thread gets killed
      close_connections
    end
  end
end

.stopObject



37
38
39
40
41
42
43
44
# File 'lib/rum/server.rb', line 37

def stop
  if @thread
    @thread.kill
    # Kill can return before the thread has finished execution. A Ruby bug?
    @thread.join
    @thread = nil
  end
end

.threadObject

This makes it easier for code to check if it runs inside the server thread.



8
9
10
# File 'lib/rum/server.rb', line 8

def thread
  @thread
end