Class: Flammarion::Server Private

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

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Server.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/flammarion/server.rb', line 6

def initialize
  @windows = {}
  @socket_paths = {}
  @started = false
  @launch_thread = Thread.current
  @server_thread = Thread.new do
    begin
      start_http_server
      start_server_internal
    rescue StandardError
      handle_exception($!)
    end
  end
  sleep 0.01 while not @started
end

Instance Attribute Details

#portObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



5
6
7
# File 'lib/flammarion/server.rb', line 5

def port
  @port
end

#server_threadObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



5
6
7
# File 'lib/flammarion/server.rb', line 5

def server_thread
  @server_thread
end

#webrick_portObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



5
6
7
# File 'lib/flammarion/server.rb', line 5

def webrick_port
  @webrick_port
end

Instance Method Details

#handle_exception(e) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



98
99
100
# File 'lib/flammarion/server.rb', line 98

def handle_exception(e)
  @launch_thread.raise(e)
end

#log(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def log(str)
  # Kernel.puts str

end

#register_window(window) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



102
103
104
105
106
107
# File 'lib/flammarion/server.rb', line 102

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

#start_http_serverObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/flammarion/server.rb', line 27

def start_http_server
  source_dir = File.absolute_path(File.join(File.dirname(File.absolute_path(__FILE__)), "/../html/source/"))
  begin
    @webrick_port = rand(65000 - 1024) + 1024
    @webrick = WEBrick::HTTPServer.new(Host: '127.0.0.1', Port: @webrick_port, DocumentRoot: source_dir, AccessLog: [], Logger: WEBrick::Log.new(File.open(File::NULL, 'w')))
  rescue RuntimeError, Errno::EADDRINUSE, Errno::EACCES
    if $!.message == "no acceptor (port is in use or requires root privileges)" or $!.is_a? Errno::EADDRINUSE or $!.is_a? Errno::EACCES
      @webrick_port = rand(65000 - 1024) + 1024
      retry
    else
      raise
    end
  end
  Thread.new { @webrick.start }
end

#start_server_internalObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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
# File 'lib/flammarion/server.rb', line 43

def start_server_internal
  @port = 7870
  @port = rand(65000 - 1024) + 1024 if Gem.win_platform? || wsl_platform

  begin
    @server = Rubame::Server.new("127.0.0.1", @port)
    log "WebServer started on port #{@port}"
    while true do
      @started = true
      @server.run do |ws|
        ws.onopen {
          log "WebSocket connection open"
          if @windows.include?(ws.handshake.path)
            @windows[ws.handshake.path].sockets << ws
            @windows[ws.handshake.path].on_connect.call() if @windows[ws.handshake.path].on_connect
            @socket_paths[ws] = 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
              handle_exception($!)
            end
          end
        }
      end
    end
  rescue RuntimeError, Errno::EADDRINUSE, Errno::EACCES
    if $!.message == "no acceptor (port is in use or requires root privileges)" or $!.is_a? Errno::EADDRINUSE or $!.is_a? Errno::EACCES
      @port = rand(65000 - 1024) + 1024
      retry
    else
      raise
    end
  end
  @started = true
end

#stopObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



91
92
# File 'lib/flammarion/server.rb', line 91

def stop
end

#wsl_platformObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



22
23
24
25
# File 'lib/flammarion/server.rb', line 22

def wsl_platform
  return File.file?('/proc/version') &&
    File.open('/proc/version', &:gets).downcase.include?("microsoft")
end