Class Server

java.lang.Object
com.sgware.serialsoc.SerialServerSocket
edu.uky.cs.nil.tt.Server
All Implemented Interfaces:
com.sgware.serialsoc.CheckedRunnable, AutoCloseable, Callable<Void>

public class Server extends com.sgware.serialsoc.SerialServerSocket
The server listens for new connections from the network, reports which story worlds and agent types it supports, and makes matches between agents so they can collaborate in storytelling sessions.
Author:
Stephen G. Ware
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final Database
    A set of story worlds and agent types supported by this server
    final Log
    Used to record system messages and data from completed sessions
    final int
    The network port on which the server listens for new connections
  • Constructor Summary

    Constructors
    Constructor
    Description
    Server(File log, File sessions, File database, int port)
    Constructs a server from its various components.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected SSLSocket
     
    protected SSLServerSocket
     
    protected Agent
     
    protected void
    execute(com.sgware.serialsoc.CheckedRunnable runnable)
     
    Returns a list of story world/agent pairs that are currently waiting and could be matched immediately.
    boolean
    Returns true if this server has been instructed to begin shutting down.
    protected boolean
    This method finds a single pair of compatible agents who are waiting for a session and, if one is found, starts a session for them.
    protected void
    This method tries to find pairs of compatible agents who are waiting for sessions and, if any are found, starts sessions for them.
    protected void
     
    protected void
    onEnd(Session session, Agent gm, Agent player)
    This method is called each time a session ends.
    protected void
     
    protected void
    onJoin(Join join)
    This method is called each time a new agent sends its join message.
    protected void
    onStart(World world, Agent gm, Agent player)
    This method is called each time a new session starts between two compatible agents.
    protected void
     
    protected void
    This method is called regularly by the clock thread to check things that are time-sensative but not triggered by events, such as disconnecting agents who have taken too long to send a message.

    Methods inherited from class com.sgware.serialsoc.SerialServerSocket

    close, createReader, createWriter, getServerSocket, onStart, run

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.sgware.serialsoc.CheckedRunnable

    call
  • Field Details

    • log

      public final Log log
      Used to record system messages and data from completed sessions
    • database

      public final Database database
      A set of story worlds and agent types supported by this server
    • port

      public final int port
      The network port on which the server listens for new connections
  • Constructor Details

    • Server

      public Server(File log, File sessions, File database, int port) throws IOException
      Constructs a server from its various components.
      Parameters:
      log - the log file where system messages will be recorded, or null if the server should not record a log
      sessions - the directory where the server should write completed session files, or null if the server should not record session data
      database - the existing database the server should use (if the file exists), or the file where the server should save its database (if the file does not exist), or null if the server start with an empty database and not save it to file
      port - the network port on which the server should listen for new connections
      Throws:
      IOException - if a problem occurs while reading the various files created or read during setup
  • Method Details

    • getStopped

      public boolean getStopped()
      Returns true if this server has been instructed to begin shutting down.
      Returns:
      true if the server has begun shutting down, false otherwise
    • createServer

      protected SSLServerSocket createServer() throws IOException
      Overrides:
      createServer in class com.sgware.serialsoc.SerialServerSocket
      Throws:
      IOException
    • accept

      protected SSLSocket accept(ServerSocket server) throws IOException
      Overrides:
      accept in class com.sgware.serialsoc.SerialServerSocket
      Throws:
      IOException
    • createSocket

      protected Agent createSocket(Socket socket) throws Exception
      Overrides:
      createSocket in class com.sgware.serialsoc.SerialServerSocket
      Throws:
      Exception
    • execute

      protected void execute(com.sgware.serialsoc.CheckedRunnable runnable)
      Overrides:
      execute in class com.sgware.serialsoc.SerialServerSocket
    • getAvailable

      public Connect.Available[] getAvailable()
      Returns a list of story world/agent pairs that are currently waiting and could be matched immediately. Only pairs where both the world and agent are publicly listed in the server's database will be returned.
      Returns:
      a list of world/agent pairs that are currently waiting for matches
    • onJoin

      protected void onJoin(Join join) throws Exception
      This method is called each time a new agent sends its join message. This method will register the new join request and try to make matches between pairs of compatible agents.
      Parameters:
      join - a new agent's join message, specifying the details of its request
      Throws:
      Exception - if an exception occurs while processing the agent's join request or while making matches between agents
    • makeMatches

      protected void makeMatches() throws Exception
      This method tries to find pairs of compatible agents who are waiting for sessions and, if any are found, starts sessions for them.
      Throws:
      Exception - if a problem occurs while matching agents
    • makeMatch

      protected boolean makeMatch() throws Exception
      This method finds a single pair of compatible agents who are waiting for a session and, if one is found, starts a session for them.
      Returns:
      true if a compatible pairs of agents was found and a session started, or false if not
      Throws:
      Exception - if a problem occurs while matching agents
    • onStart

      protected void onStart(World world, Agent gm, Agent player) throws Exception
      This method is called each time a new session starts between two compatible agents.
      Parameters:
      world - the story world in which the session will take place
      gm - the agent playing the game master role
      player - the agent playing the player role
      Throws:
      Exception - if a problem occurs while starting the new session
    • tick

      protected void tick() throws Exception
      This method is called regularly by the clock thread to check things that are time-sensative but not triggered by events, such as disconnecting agents who have taken too long to send a message.
      Throws:
      Exception - if a problem occurs while this method is running
    • onEnd

      protected void onEnd(Session session, Agent gm, Agent player) throws Exception
      This method is called each time a session ends. Note that this method is not called immediately after its story ends, but only after both agents have either acknowledged the end via stop messages or by disconnecting, which means all reports will have been logged.
      Parameters:
      session - the session which has ended
      gm - the agent playing the role of game master in the ended session
      player - the agent playing the role of player in the ended session
      Throws:
      Exception - if a problem occurs while ending the session
    • onException

      protected void onException(Exception exception)
      Overrides:
      onException in class com.sgware.serialsoc.SerialServerSocket
    • onClose

      protected void onClose()
      Overrides:
      onClose in class com.sgware.serialsoc.SerialServerSocket
    • onStop

      protected void onStop() throws IOException
      Overrides:
      onStop in class com.sgware.serialsoc.SerialServerSocket
      Throws:
      IOException