Class WebServer

java.lang.Object
net.sf.colossus.webserver.WebServer
All Implemented Interfaces:
IRunWebServer, IWebServer

public class WebServer extends Object implements IWebServer, IRunWebServer
The main class for the WebServer - brings up the WebServer GUI - starts the ServerSocket and listens there for WebClients - based on actions coming from clients, keeps book of "instant" and "running" games (both GameInfo objects), and tell the GameInfo objects when to start the game.
Author:
Clemens Katzer
  • Field Details

    • LOGGER

      private static final Logger LOGGER
    • options

      private WebServerOptions options
    • portBookKeeper

      private PortBookKeeper portBookKeeper
    • gui

      private IWebServerGUI gui
    • mailObject

      private final IColossusMail mailObject
    • MIN_FREE_GAME_PORTS

      private static final int MIN_FREE_GAME_PORTS
      See Also:
    • loginMessage

      private static final ArrayList<String> loginMessage
    • runGUI

      private final boolean runGUI
      Controls whether the GUI is shown or not. At the moment this is configured only by the possibility of doing so: if the environment supports running a GUI, we will, if not, we won't. It could be combined with a command line option to suppress the GUI even if it would be possible to show one.
    • shutdownRequested

      private boolean shutdownRequested
    • proposedGamesFilename

      private String proposedGamesFilename
    • proposedGamesListModified

      private boolean proposedGamesListModified
    • maxClients

      private final int maxClients
    • userDB

      private UserDB userDB
    • proposedGames

      private final HashMap<String,GameInfo> proposedGames
    • runningGames

      private final ArrayList<GameInfo> runningGames
    • endingGames

      private final ArrayList<GameInfo> endingGames
    • sep

      private static final String sep
      See Also:
    • serverPort

      private final int serverPort
      Server port where we listen for WebClient connections
    • serverSocket

      private ServerSocket serverSocket
      Server actual socket where we listen for WebClient connections
    • generalChat

      private final ChatChannel generalChat
    • watchDog

      private final ClientWatchDog watchDog
    • whenFormatter

      private final FormatWhen whenFormatter
    • dumpInfoFile

      private PrintWriter dumpInfoFile
  • Constructor Details

    • WebServer

      public WebServer(String optionsFile)
  • Method Details

    • main

      public static void main(String[] args)
    • doReadGamesFromFile

      private void doReadGamesFromFile()
    • doReadUsersFromFile

      private void doReadUsersFromFile()
    • doReadLoginMessage

      private void doReadLoginMessage()
    • rereadLoginMessage

      public void rereadLoginMessage()
      Triggered by remode admin connection
      Specified by:
      rereadLoginMessage in interface IWebServer
    • runSocketServer

      void runSocketServer()
    • getGeneralChat

      public ChatChannel getGeneralChat()
    • writeBackUsers

      public void writeBackUsers()
    • updateLoggedinStatus

      public void updateLoggedinStatus(User u, WebServerClient wsc)
    • initiateShutdown

      public void initiateShutdown(String byUserName)
    • shutdownServer

      public void shutdownServer()
      Specified by:
      shutdownServer in interface IWebServer
    • makeDummyConnection

      public void makeDummyConnection()
    • waitForUser

      private boolean waitForUser()
    • reject

      private void reject(Socket socket)
    • closeAllWebServerClientSocketThreads

      private void closeAllWebServerClientSocketThreads()
    • getPlayerName

      String getPlayerName()
      Each user server thread's name is set to it user's name.
    • getUserDB

      public UserDB getUserDB()
    • findUserByName

      public User findUserByName(String name)
    • verifyLogin

      public String verifyLogin(String username, String password)
    • getPortBookKeeper

      public PortBookKeeper getPortBookKeeper()
    • getPortProvider

      public IPortProvider getPortProvider()
      Specified by:
      getPortProvider in interface IRunWebServer
    • updateGUI

      public void updateGUI()
    • watchGame

      public void watchGame(String gameId, String userName)
      Specified by:
      watchGame in interface IWebServer
    • sendWatchGameInfo

      public void sendWatchGameInfo(String userName, String gameId, String host, int port)
    • dumpInfo

      public void dumpInfo()
      Dump server state info into a static file, at the moment dump-info.txt
      Specified by:
      dumpInfo in interface IWebServer
    • dumpInfo

      public void dumpInfo(PrintWriter pw)
      Dump info into given PrintWriter
      Parameters:
      pw - PrintWriter for the file to print dump to
    • isInvolvedInInstantGame

      private GameInfo isInvolvedInInstantGame(String initiatorName)
    • proposeGame

      public GameInfo proposeGame(String initiator, String variant, String viewmode, long startAt, int duration, String summary, String expire, boolean unlimitedMulligans, boolean balancedTowers, int min, int target, int max)
      Specified by:
      proposeGame in interface IWebServer
    • cancelIfNecessary

      public void cancelIfNecessary(User user)
    • reEnrollIfNecessary

      public void reEnrollIfNecessary(WebServerClient newclient)
    • tellAllGamesFromListToOne

      public void tellAllGamesFromListToOne(WebServerClient client, ArrayList<GameInfo> games)
    • tellAllProposedGamesToOne

      public void tellAllProposedGamesToOne(WebServerClient client)
    • tellAllRunningGamesToOne

      public void tellAllRunningGamesToOne(WebServerClient client)
    • allTellGameInfo

      public void allTellGameInfo(GameInfo gi)
      Specified by:
      allTellGameInfo in interface IRunWebServer
    • tellEnrolledGameStartsSoon

      public void tellEnrolledGameStartsSoon(GameInfo gi)
      Specified by:
      tellEnrolledGameStartsSoon in interface IRunWebServer
    • tellEnrolledGameStartsNow

      public void tellEnrolledGameStartsNow(GameInfo gi, String host, int port)
      Specified by:
      tellEnrolledGameStartsNow in interface IRunWebServer
    • gameStarted

      public void gameStarted(GameInfo gi)
      Specified by:
      gameStarted in interface IRunWebServer
    • gameFailed

      public void gameFailed(GameInfo gi, String reason)
      Specified by:
      gameFailed in interface IRunWebServer
    • enrollUserToGame

      public void enrollUserToGame(String gameId, String username)
      Specified by:
      enrollUserToGame in interface IWebServer
    • unenrollUserFromGame

      public void unenrollUserFromGame(String gameId, String username)
      Specified by:
      unenrollUserFromGame in interface IWebServer
    • cancelGame

      public void cancelGame(String gameId, String byUser)
      Specified by:
      cancelGame in interface IWebServer
    • startGame

      public void startGame(String gameId, User byUser)
      Specified by:
      startGame in interface IWebServer
    • attemptStartOnServer

      private void attemptStartOnServer(GameInfo gi, User byUser)
    • startGameOnPlayerHost

      public void startGameOnPlayerHost(String gameId, String hostingPlayer, String playerHost, int port)
      A game was started by a WebClient user locally on his computer and is ready to accept the other players as remote client; so we notify them and tell them host and port to where to connect.
      Specified by:
      startGameOnPlayerHost in interface IWebServer
    • informStartedByPlayer

      public void informStartedByPlayer(String gameId)
      Specified by:
      informStartedByPlayer in interface IWebServer
    • informAllEnrolledAbout

      public void informAllEnrolledAbout(GameInfo gi, String message)
      Specified by:
      informAllEnrolledAbout in interface IRunWebServer
    • systemMessageToAll

      public void systemMessageToAll(String message)
    • informAllEnrolledThatStartFailed

      public void informAllEnrolledThatStartFailed(GameInfo gi, String reason, User byUser)
    • requestUserAttention

      public void requestUserAttention(long when, String sender, boolean isAdmin, String recipient, String message, int beepCount, long beepInterval, boolean windows)
      Specified by:
      requestUserAttention in interface IWebServer
    • informPingFailed

      private void informPingFailed(String sender, String failMessage)
    • informLocallyGameOver

      public void informLocallyGameOver(String gameId)
      Specified by:
      informLocallyGameOver in interface IWebServer
    • updateUserCounts

      public void updateUserCounts()
    • allTellUserCounts

      public void allTellUserCounts()
    • chatSubmit

      public void chatSubmit(String chatId, String sender, String message)
      Specified by:
      chatSubmit in interface IWebServer
    • handlePingQuotedName

      public void handlePingQuotedName(String sender, String pingCommand)
    • handlePing

      public void handlePing(String sender, String pingCommand)
    • tellLastChatMessagesToOne

      public void tellLastChatMessagesToOne(WebServerClient client, String chatId)
    • sendMessageOfTheDayToOne

      public void sendMessageOfTheDayToOne(WebServerClient client, String chatId)
    • sendOldVersionWarningToOne

      public void sendOldVersionWarningToOne(WebServerClient client, String userName, String chatId)
    • readLoginMessageFromFile

      private void readLoginMessageFromFile(String filename)
    • logout

      public void logout()
      Specified by:
      logout in interface IWebServer
    • registerUser

      public String registerUser(String username, String password, String email)
    • confirmRegistration

      public String confirmRegistration(String username, String confirmationCode)
    • changeProperties

      public String changeProperties(String username, String oldPW, String newPW, String email, Boolean isAdminObj)
      Specified by:
      changeProperties in interface IWebServer
    • updateOnline

      boolean updateOnline(GameInfo gi)
      When a user logged in or out, this is called for every GameInfo to update how many of the enrolled players are currently online.
      Parameters:
      userDB - TODO
      Returns:
      true if the count of online users was changed i.e. GameInfo needs to be updated to all clients
    • countProposedGames

      private int countProposedGames(boolean shallBeScheduled)
    • findByGameId

      private GameInfo findByGameId(String gameId)
    • findFromRunningGames

      private GameInfo findFromRunningGames(String gameId)
    • getGameOnServer

      private IGameRunner getGameOnServer(GameInfo gi)
    • startOneGame

      private String startOneGame(GameInfo gi)
    • unregisterGame

      public void unregisterGame(GameInfo gi, int port)
      unregister a game from runningGames (or proposedGames), and keep in endingGames until it's reaped
      Specified by:
      unregisterGame in interface IRunWebServer
    • unregisterGamePlayerPC

      public void unregisterGamePlayerPC(GameInfo gi)
      unregister a game (run on player's PC) from runningGames, keep in endingGames until it's reaped
    • readGamesFromFile

      private void readGamesFromFile(String filename, HashMap<String,GameInfo> proposedGames)
    • saveGamesIfNeeded

      public void saveGamesIfNeeded()
    • storeGamesToFile

      private void storeGamesToFile(String filename)
    • getStringOption

      public String getStringOption(String key)
    • getMaximumGameIdFromFiles

      private int getMaximumGameIdFromFiles()
      Searches the game directory tree for highest game number for which a game directory had been earlier created. Tree is expected to have groups per each 100 games; example: base base/nn00-nn99 base/nn00-nn99/nn00 base/nn00-nn99/nn04 base/nn00-nn99/nn98 base/mm00-mm99/mm12 base/mm00-mm99/mm87 Eventually, when we reach gameId 10000+, groupdirs will have the form base/kkk00-kkk99 base/kkk00-kkk99/kkk02 base/kkk00-kkk99/kkk87 Example, if last created game dir was 6789 ( "base/6700-6799/6789" ), this returns 6789.
      Returns:
      The highest game number for which a directory already exists, (otherwise 0 if therre is no dir at all)