Class: Flammarion::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/flammarion/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/flammarion/server.rb', line 4

def initialize
  @windows = {}
  @socket_paths = {}
  @started = false
  Thread.new do
    begin
      start_server_internal
    rescue SystemExit
      raise
    rescue Exception => e
      if binding.respond_to? :pry
        binding.pry
      else
        raise
      end
    end
  end
  sleep 0.5 while not @started

  # This is a hack. For some reason, you need to wait a bit for everything
  # to get written.
  at_exit { sleep 0.1 }
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



3
4
5
# File 'lib/flammarion/server.rb', line 3

def port
  @port
end

Instance Method Details

#log(str) ⇒ Object



96
97
98
# File 'lib/flammarion/server.rb', line 96

def log(str)
  # Kernel.puts str
end

#register_window(window) ⇒ Object



100
101
102
103
104
105
# File 'lib/flammarion/server.rb', line 100

def register_window(window)
  @new_path ||= 0
  @new_path += 1
  @windows["/w#{@new_path}"] = window
  return "w#{@new_path}"
end

#start_server_internalObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/flammarion/server.rb', line 27

def start_server_internal
  EM.run {
  @port = 7870
  @port = rand(65000 - 1024) + 1024 if Gem.win_platform?
  begin
    EM::WebSocket.run(:host => "0.0.0.0", :port => @port) do |ws|
      ws.onopen { |handshake|
        log "WebSocket connection open"
        if @windows.include?(handshake.path)
          @windows[handshake.path].sockets << ws
          @windows[handshake.path].on_connect.call() if @windows[handshake.path].on_connect
          @socket_paths[ws] = handshake.path
        else
          log "No such window: #{handshake.path}"
        end
      }

      ws.onclose do
        log "Connection closed";
        @windows[@socket_paths[ws]].disconnect(ws) if @windows[@socket_paths[ws]]
      end

      ws.onmessage { |msg|
        Thread.new do
          begin
            @windows[@socket_paths[ws]].process_message(msg)
          rescue Exception => e
            Kernel.puts "#{e.message.to_s.red}\n#{e.backtrace.join("\n").light_red}"
            if binding.respond_to? :pry
              binding.pry
            else
              raise
            end
          end
        end
      }

      ws.onerror { |err|
        if binding.respond_to? :pry
          binding.pry
        else
          raise
        end
      }
    end
  rescue RuntimeError => e
    if e.message == "no acceptor (port is in use or requires root privileges)"
      @port = rand(65000 - 1024) + 1024
      Kernel.puts "New port: #{port}"
      retry
    else
      raise
    end
  end
  @started = true
  }
rescue Exception => e
  unless e.is_a? SystemExit
    Kernel.puts "Error in server:"
    binding.pry if binding.respond_to? :pry
    Kernel.puts e.message
    Kernel.puts e.backtrace.inspect
  end
  raise
end

#stopObject



93
94
# File 'lib/flammarion/server.rb', line 93

def stop
end