Class: Jscall::FetchServer

Inherits:
Object
  • Object
show all
Defined in:
lib/jscall/browser.rb

Constant Summary collapse

@@webpage =
'/jscall/jscall.html'
@@run_cmd =
case RbConfig::CONFIG['host_os']
when /linux/
    'xdg-open'
when /darwin|mac os/
    'open'
else
    'start'
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port, urls) ⇒ FetchServer

Returns a new instance of FetchServer.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/jscall/browser.rb', line 76

def initialize(port, urls)
    @url_map = urls
    @send_buffer = Thread::Queue.new
    @receive_buffer = Thread::Queue.new
    @server_running = false

    @server = WEBrick::HTTPServer.new(
        # :DoNotReverseLookup => true,
        :DocumentRoot => './',
        :BindAddress => '0.0.0.0',
        :Port => port,
        :ServerType => Thread,
        :Logger => WEBrick::Log.new(nil, WEBrick::Log::ERROR),
        :AccessLog => []
    )

    @server.mount_proc('/') do |req, res|
        peer_address  = req.peeraddr[3]
        if peer_address != '127.0.0.1'
            $stderr.puts "access denied address=#{peer_address}"
            raise WEBrick::HTTPStatus::Forbidden
        end

        if req.path.start_with?('/cmd/')
            read_stream(req, res)
        else
            read_file(req, res)
        end
    end
end

Class Method Details

.open_command=(name) ⇒ Object



72
73
74
# File 'lib/jscall/browser.rb', line 72

def self.open_command=(name)
    @@run_cmd = name
end

Instance Method Details

#autoclose=(value) ⇒ Object



157
158
159
# File 'lib/jscall/browser.rb', line 157

def autoclose=(value)
    false
end

#closeObject



148
149
150
151
# File 'lib/jscall/browser.rb', line 148

def close
    puts('done')
    @server_running = false
end

#closed?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/jscall/browser.rb', line 153

def closed?
    !@server_running
end

#getsObject



171
172
173
# File 'lib/jscall/browser.rb', line 171

def gets
    @receive_buffer.pop
end

#openObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/jscall/browser.rb', line 129

def open
    if @server_running
        close
    else
        @server_running = true
        if @server.status != :Running
            Signal.trap(:INT){ @server.shutdown }
            @server.start
            Thread.pass
        end
    end
    raise "A web page was reloaded" unless @receive_buffer.empty?
    status = system "#{@@run_cmd} http://localhost:#{@server[:Port]}#{@@webpage}"
    raise "cannot launch a web browser by '#{@@run_cmd}'" if status.nil?
    unless @receive_buffer.pop == 'start'
        raise 'failed to initialize JavaScript'
    end
end

#puts(msg) ⇒ Object



166
167
168
169
# File 'lib/jscall/browser.rb', line 166

def puts(msg)
    @send_buffer.push(msg)
    Thread.pass
end

#read_file(req, res) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/jscall/browser.rb', line 114

def read_file(req, res)
    path = req.path
    if path.start_with?('/jscall/')
        root = "#{__dir__}/../"
    else
        value = @url_map[path]
        if value.nil?
            root = @server.config[:DocumentRoot]
        else
            root = value[1]
        end
    end
    WEBrick::HTTPServlet::FileHandler.new(@server, root).service(req, res)
end

#read_stream(req, res) ⇒ Object



107
108
109
110
111
112
# File 'lib/jscall/browser.rb', line 107

def read_stream(req, res)
    body = req.body
    @receive_buffer.push(if body.nil? then '' else body end)
    res.content_type = "text/plain"
    res.body = @send_buffer.pop
end

#shutdownObject



161
162
163
164
# File 'lib/jscall/browser.rb', line 161

def shutdown
    close
    @server.stop
end