Class Server

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

public class Server extends com.sgware.serialsoc.SecureSerialServerSocket
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
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    protected SSLSocket
     
    protected void
     
    protected Agent
    create(Socket socket)
     
    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.
    Returns a status object that summarizes the sessions currently running and agents currently waiting to start sessions.
    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
    Every agent calls this method exactly once as soon as it connects to the server.
    protected void
    Every agent calls this method exactly once after it has disconnected from the server.
    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(WorldModel world, Agent gm, Agent player)
    This method is called each time a new session starts between two compatible agents.
    protected void
     

    Methods inherited from class com.sgware.serialsoc.SimpleSerialServerSocket

    disconnect, getAddress, getPort

    Methods inherited from class com.sgware.serialsoc.SerialServerSocket

    await, close, getStatus, has, onAccept, onConnect, onDisconnect, onStart, run, tick

    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
  • Constructor Details

    • Server

      public Server(File logs, File status, File sessions, File database, int port) throws IOException
      Constructs a server from its various components.
      Parameters:
      logs - the directory where system messages will be recorded, or null if the server should not record system logs
      status - the file to which the server's current status will be written, or null if the server should not write its status to file
      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 should 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
    • connect

      protected void connect() throws IOException
      Overrides:
      connect in class com.sgware.serialsoc.SimpleSerialServerSocket
      Throws:
      IOException
    • accept

      protected SSLSocket accept() throws IOException
      Overrides:
      accept in class com.sgware.serialsoc.SecureSerialServerSocket
      Throws:
      IOException
    • create

      protected Agent create(Socket socket) throws IOException
      Overrides:
      create in class com.sgware.serialsoc.SimpleSerialServerSocket
      Throws:
      IOException
    • execute

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

      public ServerStatus getServerStatus()
      Returns a status object that summarizes the sessions currently running and agents currently waiting to start sessions. This list will include agents and worlds that are not publicly listed in the database and is not meant to be shown to users.
      Returns:
      a server summary
    • 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
    • onConnect

      protected void onConnect(Agent agent) throws Exception
      Every agent calls this method exactly once as soon as it connects to the server.
      Parameters:
      agent - the agent that has just connected
      Throws:
      Exception - if a problem occurs while handling the connection
    • 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 pair 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(WorldModel 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
    • 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
    • onDisconnect

      protected void onDisconnect(Agent agent) throws Exception
      Every agent calls this method exactly once after it has disconnected from the server.
      Parameters:
      agent - the agent that has just disconnected
      Throws:
      Exception - if a problem occurs while handling the disconnection
    • 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