Class: RhoDevelopment::WebServer

Inherits:
Object
  • Object
show all
Defined in:
lib/build/development/web_server.rb

Overview

TODO: create DocumentRoot on start and remove it on stop

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWebServer

instance methods



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/build/development/web_server.rb', line 86

def initialize
  document_root = Configuration::document_root

  puts "Webserver URL: #{Configuration::webserver_ip}:#{Configuration::webserver_port}".primary
  puts "Webserver document root: #{document_root}".primary
  print 'Cleaning document root directory... '.primary
  FileUtils.rm_rf("#{document_root}/.", {:secure => true})
  puts 'done'.success
  @tasks = Queue.new
  @web_server = WEBrick::HTTPServer.new(
      :BindAddress => "0.0.0.0",
      :Port => Configuration::webserver_port,
      :DocumentRoot => document_root,
      :ServerType => WEBrick::SimpleServer
  )
  self.configure
end

Instance Attribute Details

#auto_update_pidObject

Returns the value of attribute auto_update_pid.



10
11
12
# File 'lib/build/development/web_server.rb', line 10

def auto_update_pid
  @auto_update_pid
end

Class Method Details

.alive?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/build/development/web_server.rb', line 52

def self.alive?
  url = Configuration::webserver_alive_request
  begin
    http = Net::HTTP.new(url.host, url.port)
    http.open_timeout = 5
    response = http.get(url.path)
    return response.code == '200'
  rescue *Configuration::handledNetworkExceptions => e
    return false
  end
end

.dispatch_task(aTask) ⇒ Object



80
81
82
# File 'lib/build/development/web_server.rb', line 80

def self.dispatch_task(aTask)
  aTask.dispatchToUrl(Configuration::webserver_uri)
end

.ensure_runningObject

class instance methods



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/build/development/web_server.rb', line 14

def self.ensure_running
  if Network::available_addresses.empty?
    puts 'Available network interfaces are not found'.warning
    exit 1
  end
  unless self.alive?
    case RbConfig::CONFIG['host_os']
      when /mswin|mingw32|windows/i
        cmd = "start \"development webserver\" /d \"#{Configuration::application_root}\" rake dev:webserver:privateStart"
      when /darwin/i
        cmd = "osascript -e 'tell app \"Terminal\" \n do script \" cd #{Configuration::application_root}; rake dev:webserver:privateStart\" \n end tell'"
      when /linux/i
        cmd = "cd #{Configuration::application_root} && rake dev:webserver:privateStart &"
    end
    system cmd
    until self.alive? do
      sleep(1)
    end
  end
end

.get_auto_update_pidObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/build/development/web_server.rb', line 35

def self.get_auto_update_pid
  url = Configuration::auto_update_pid_request
  begin
    http = Net::HTTP.new(url.host, url.port)
    http.open_timeout = 5
    response = http.get(url.path)
    pid = response.body.to_i
    return pid != 0 ? pid : nil
  rescue *Configuration::handledNetworkExceptions => e
    return nil
  end
end

.set_auto_update_pid(pid) ⇒ Object



48
49
50
# File 'lib/build/development/web_server.rb', line 48

def self.set_auto_update_pid(pid)
  Net::HTTP.post_form(Configuration::auto_update_pid_request, {'pid' => pid})
end

.stopObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/build/development/web_server.rb', line 64

def self.stop
  url = Configuration::shut_down_webserver_request
  begin
    http = Net::HTTP.new(url.host, url.port)
    http.open_timeout = 5
    response = http.get(url.path)
    if response.code == 200
      puts 'Web server was shut down'.primary
    else
      puts "#{response.body}".warning
    end
  rescue *Configuration::handledNetworkExceptions => e
    puts 'Web server is not answering'.warning
  end
end

Instance Method Details

#add_task(aTask) ⇒ Object



125
126
127
# File 'lib/build/development/web_server.rb', line 125

def add_task(aTask)
  @tasks << aTask
end

#configureObject



104
105
106
107
108
109
110
# File 'lib/build/development/web_server.rb', line 104

def configure
  @web_server.mount('/alive', Alive)
  @web_server.mount('/tasks/new', NewTask, self)
  @web_server.mount('/shutdown', Shutdown)
  @web_server.mount('/response_from_device', ResponseFromDevice, self)
  @web_server.mount('/auto_update_pid', AutoUpdatePID, self)
end

#startObject



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/build/development/web_server.rb', line 112

def start
  @run_thread = Thread.new do
    loop do
      unless @tasks.empty?
        _task = @tasks.pop
        _task.execute
      end
      sleep 1
    end
  end
  @web_server.start
end