Class TaskJuggler::WebServer
In: lib/taskjuggler/daemon/WebServer.rb
Parent: Daemon

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

Methods

new   start   stop  

Included Modules

DaemonConnectorMixin

Attributes

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

Public Class methods

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

[Source]

# File lib/taskjuggler/daemon/WebServer.rb, line 32
    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

[Source]

# File lib/taskjuggler/daemon/WebServer.rb, line 59
    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', "Cannot find the \#{dir} directory. This is usually the result of an\nimproper TaskJuggler installation. If you know the directory, you can use the\nTASKJUGGLER_DATA_PATH environment variable to specify the location. The\nvariable should be set to the path without the /data at the end. Multiple\ndirectories must be separated by colons.\n"
                    )
        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 the web server.

[Source]

# File lib/taskjuggler/daemon/WebServer.rb, line 123
    def stop
      if @server
        @server.shutdown
        @server = nil
      end
      super
    end

[Validate]