Class HttpServer

java.lang.Object
org.ka2ddo.yaac.webserver.HttpServer
All Implemented Interfaces:
Runnable

public class HttpServer extends Object implements Runnable
This class implements a very simple HTTP server. It spawns a HttpConnectionThread instance for each incoming connection request.
Author:
Andrew Pavlin, KA2DDO
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    HttpServer(int listenPort, int refreshRateInSec)
    Create a HttpServer instance on the specified listening port.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Get the port the mini-webserver is currently listening on.
    Obtain an Iterator across all the GETable pages known to this web server.
    Obtain an Iterator across all the pages known to this web server.
    int
    Get the automatic page refresh rate setting currently sent to browsers.
    boolean
    registerPage(String method, String path, PathHandler handler)
    Register a page handler for a particular path within the website.
    boolean
    Register a page handler for a particular path within the website, using the "GET" HTTP method.
    void
    run()
    Accept incoming HTTP connection requests and spawn a handler thread for each browser connection.
    void
    setListenPort(int listenPort)
    Change the port number that the mini-webserver should listen on.
    void
    setRefreshRateInSec(int refreshRateInSec)
    Set the automatic page refresh rate setting currently sent to browsers.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • HttpServer

      public HttpServer(int listenPort, int refreshRateInSec)
      Create a HttpServer instance on the specified listening port.
      Parameters:
      listenPort - TCP port number to accept incoming HTTP requests on
      refreshRateInSec - refresh interval in seconds for web pages that do auto-refreshing
  • Method Details

    • run

      public void run()
      Accept incoming HTTP connection requests and spawn a handler thread for each browser connection.
      Specified by:
      run in interface Runnable
    • getRefreshRateInSec

      public int getRefreshRateInSec()
      Get the automatic page refresh rate setting currently sent to browsers.
      Returns:
      page refresh rate in seconds between refreshes
    • setRefreshRateInSec

      public void setRefreshRateInSec(int refreshRateInSec) throws IllegalArgumentException
      Set the automatic page refresh rate setting currently sent to browsers.
      Parameters:
      refreshRateInSec - page refresh rate in seconds between refreshes
      Throws:
      IllegalArgumentException - if specified refresh rate is shorter than 5 seconds
    • getListenPort

      public int getListenPort()
      Get the port the mini-webserver is currently listening on.
      Returns:
      TCP port number for YAAC's internal webserver
    • setListenPort

      public void setListenPort(int listenPort)
      Change the port number that the mini-webserver should listen on. Note this will not take effect until the next time YAAC is started. Changing this port number will shut down the existing listening socket for this HttpServer instance and create a new one on the new port number.
      Parameters:
      listenPort - TCP port number for YAAC's internal webserver
    • registerPage

      public boolean registerPage(String path, PathHandler handler)
      Register a page handler for a particular path within the website, using the "GET" HTTP method.
      Parameters:
      path - String of leading part of URL path
      handler - PathHandler to generate a response page for the path
      Returns:
      boolean true if a page is being replaced, false if this is a new page
    • registerPage

      public boolean registerPage(String method, String path, PathHandler handler)
      Register a page handler for a particular path within the website.
      Parameters:
      method - String of HTTP method for accessing this page
      path - String of leading part of URL path
      handler - PathHandler to generate a response page for the path
      Returns:
      boolean true if a page is being replaced, false if this is a new page
    • getPathIterator

      public Iterator<String> getPathIterator()
      Obtain an Iterator across all the GETable pages known to this web server.
      Returns:
      Iterator of Strings
    • getPathIterator

      public Iterator<String> getPathIterator(String method)
      Obtain an Iterator across all the pages known to this web server.
      Parameters:
      method - String of HTTP method for accessing these pages
      Returns:
      Iterator of Strings