class TaskJuggler::WebServer

The WebServer class provides a self-contained HTTP server that can serve HTML versions of Report objects that are generated on the fly.

Attributes

authKey[RW]
port[RW]
uriFile[RW]
webServerPort[RW]

Public Class Methods

new() click to toggle source

Create a web server object that runs in a separate thread.

Calls superclass method TaskJuggler::Daemon.new
# File lib/taskjuggler/daemon/WebServer.rb, line 31
def initialize
  super
  # For security reasons, this will probably not change. All DRb
  # operations are limited to localhost only. The client and the sever
  # must have access to the identical file system.
  @host = '127.0.0.1'
  # The default TCP/IP port. ASCII code decimals for 'T' and 'J'.
  @port = 8474
  # The file with the server URI in case port is 0.
  @uriFile = File.join(Dir.getwd, '.tj3d.uri')
  # We don't have a default key. The user must provice a key in the config
  # file. Otherwise the daemon will not start.
  @authKey = nil

  # Reference to WEBrick object.
  @webServer = nil

  # Port used by the web server
  @webServerPort = 8080

  Kernel.trap('TERM') do
    debug('webserver_term_signal', 'TERM signal received. Exiting...')
    # When the OS sends us a TERM signal, we try to exit gracefully.
    stop
  end
end

Public Instance Methods

start() click to toggle source
Calls superclass method TaskJuggler::Daemon#start
# File lib/taskjuggler/daemon/WebServer.rb, line 58
    def start
      # In daemon mode, we fork twice and only the 2nd child continues here.
      super()

      debug('', "Starting web server")
      config = { :Port => @webServerPort }
      begin
        @server = WEBrick::HTTPServer.new(config)
        info('webserver_port',
             "Web server is listening on port #{@webServerPort}")
      rescue
        fatal('webrick_start_failed', "Cannot start WEBrick: #{$!}")
      end

      begin
        @server.mount('/', WelcomePage, nil)
      rescue
        fatal('welcome_page_mount_failed',
              "Cannot mount WEBrick welcome page: #{$!}")
      end

      begin
        @server.mount('/taskjuggler', ReportServlet,
                      [ @authKey, @host, @port, @uri ])
      rescue
        fatal('broker_page_mount_failed',
              "Cannot mount WEBrick broker page: #{$!}")
      end

      # Serve some directories via the FileHandler servlet.
      %w( css icons scripts ).each do |dir|
        unless (fullDir = AppConfig.dataDirs("data/#{dir}")[0])
          error('dir_not_found', <<"EOT"
Cannot find the #{dir} directory. This is usually the result of an
improper TaskJuggler installation. If you know the directory, you can use the
TASKJUGGLER_DATA_PATH environment variable to specify the location. The
variable should be set to the path without the /data at the end. Multiple
directories must be separated by colons.
EOT
                    )
        end

        begin
          @server.mount("/#{dir}", WEBrick::HTTPServlet::FileHandler, fullDir)
        rescue
          fatal('dir_mount_failed',
                "Cannot mount directory #{dir} in WEBrick: #{$!}")
        end
      end

      # Install signal handler to exit gracefully on CTRL-C.
      intHandler = Kernel.trap('INT') do
        stop
      end

      begin
        @server.start
      rescue
        fatal('web_server_error', "Web server error: #{$!}")
      end
    end
stop() click to toggle source

Stop the web server.

Calls superclass method TaskJuggler::Daemon#stop
# File lib/taskjuggler/daemon/WebServer.rb, line 121
def stop
  if @server
    @server.shutdown
    @server = nil
  end
  super
end