JSettlers v1.0.6 API
Built ${date}

soc.server
Class SOCServer

java.lang.Object
  extended byjava.lang.Thread
      extended bysoc.server.genericServer.Server
          extended bysoc.server.SOCServer
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Runnable, java.io.Serializable

public class SOCServer
extends Server

A server for Settlers of Catan

Author:
Robert S. Thomas Note: This is an attempt at being more modular. 5/13/99 RST Note: Hopfully fixed all of the deadlock problems. 12/27/01 RST
See Also:
Serialized Form

Field Summary
protected  SOCChannelList channelList
          list of chat channels
protected  java.util.Hashtable gameDataFiles
          table of game data files
protected  SOCGameList gameList
          list of soc games
protected  int maxConnections
          Maximum number of connections allowed
protected  int numberOfGamesFinished
          the total number of games finished
protected  int numberOfGamesStarted
          the total number of games that have been started
protected  int numberOfUsers
          total number of users
 int port
          The TCP port we listen on.
protected  java.util.Hashtable robotDismissRequests
          table of requestst for robots to leave games
protected  java.util.Hashtable robotJoinRequests
          table of requests for robots to join games
protected  java.util.Vector robots
          A list of robots connected to this server
static java.lang.String SERVERNAME
          Name used when sending messages from the server.
protected  long startTime
          the time that this server was started
 
Fields inherited from class soc.server.genericServer.Server
conns, error, inQueue, numberOfConnections
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
SOCServer(int p, int mc, java.lang.String databaseUserName, java.lang.String databasePassword)
          Create a Settlers of Catan server listening on port p.
 
Method Summary
protected  void broadcastGameStats(SOCGame ga)
          this broadcasts game information to all people connected used to display the scores on the player client
 void checkForExpiredGames()
          check for games that have expired and destroy them if games are about to expire, send a warning
protected  boolean checkTurn(Connection c, SOCGame ga)
          make sure it's the player's turn
 void connectToChannel(Connection c, java.lang.String ch)
          Adds a connection to a chat channel.
 boolean connectToGame(Connection c, java.lang.String ga)
          Adds a connection to a game.
 void destroyGame(java.lang.String gm)
          destroy the game WARNING: MUST HAVE THE gameList.takeMonitor() before calling this method
protected  void giveDevCard(java.lang.String mes, SOCGame game)
          this is a debugging command that gives a dev card to a player
protected  void giveResources(java.lang.String mes, SOCGame game)
          this is a debugging command that gives resources to a player
 java.util.Vector leaveAllChannels(Connection c)
          the connection c leaves all channels it was in
 java.util.Vector leaveAllGames(Connection c)
          the connection c leaves all games it was in
 boolean leaveChannel(Connection c, java.lang.String ch, boolean channelListLock)
          the connection c leaves the channel ch WARNING: MUST HAVE THE channelList.takeMonitorForChannel(ch) before calling this method
 void leaveConnection(Connection c)
          things to do when the connection c leaves
 boolean leaveGame(Connection c, java.lang.String gm, boolean gameListLock)
          the connection c leaves the game gm WARNING: MUST HAVE THE gameList.takeMonitorForGame(gm) before calling this method
static void main(java.lang.String[] args)
          Starting the server from the command line
 void messageToChannel(java.lang.String ch, SOCMessage mes)
          Send a message to the given channel
 void messageToChannelWithMon(java.lang.String ch, SOCMessage mes)
          Send a message to the given channel WARNING: MUST HAVE THE gameList.takeMonitorForChannel(ch) before calling this method
 void messageToGame(java.lang.String ga, SOCMessage mes)
          Send a message to the given game
 void messageToGameExcept(java.lang.String gn, java.util.Vector ex, SOCMessage mes)
          Send a message to all the connections in a game excluding some.
 void messageToGameWithMon(java.lang.String ga, SOCMessage mes)
          Send a message to the given game WARNING: MUST HAVE THE gameList.takeMonitorForGame(ga) before calling this method
 void messageToPlayer(Connection c, SOCMessage mes)
          Send a message to a player and record it
 void newConnection(Connection c)
          Things to do when a new connection comes
 void processCommand(java.lang.String s, Connection c)
          Treat the incomming messages Note: When there is a choice, always use local information over information from the message.
protected  void recordGameEvent(java.lang.String gameName, java.lang.String event)
          record events that happen during the game
protected  void reportBankTrade(SOCGame ga, SOCResourceSet give, SOCResourceSet get)
          report that the current player traded with the bank
protected  void reportRobbery(SOCGame ga, SOCPlayer pe, SOCPlayer vi, int rsrc)
          The current player is stealing from another player.
protected  void reportTrade(SOCGame ga, int offering, int accepting)
          report a trade that has taken place
protected  void sendGameState(SOCGame ga)
          send the current state of the game with a message
protected  void startGame(SOCGame ga)
          do the stuff you need to do to start a game
protected  void storeGameScores(SOCGame ga)
          if all the players stayed for the whole game, record the scores in the database
 
Methods inherited from class soc.server.genericServer.Server
addConnection, broadcast, connectionCount, getConnections, isUp, removeConnection, removeConnectionCleanup, run, serverDown, stopServer, treat
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SERVERNAME

public static final java.lang.String SERVERNAME
Name used when sending messages from the server.

See Also:
Constant Field Values

port

public int port
The TCP port we listen on.


maxConnections

protected int maxConnections
Maximum number of connections allowed


robots

protected java.util.Vector robots
A list of robots connected to this server


channelList

protected SOCChannelList channelList
list of chat channels


gameList

protected SOCGameList gameList
list of soc games


robotJoinRequests

protected java.util.Hashtable robotJoinRequests
table of requests for robots to join games


robotDismissRequests

protected java.util.Hashtable robotDismissRequests
table of requestst for robots to leave games


gameDataFiles

protected java.util.Hashtable gameDataFiles
table of game data files


startTime

protected long startTime
the time that this server was started


numberOfGamesStarted

protected int numberOfGamesStarted
the total number of games that have been started


numberOfGamesFinished

protected int numberOfGamesFinished
the total number of games finished


numberOfUsers

protected int numberOfUsers
total number of users

Constructor Detail

SOCServer

public SOCServer(int p,
                 int mc,
                 java.lang.String databaseUserName,
                 java.lang.String databasePassword)
Create a Settlers of Catan server listening on port p.

Parameters:
p - the port that the server listens on
mc - the maximum number of connections allowed
databaseUserName - the user name for accessing the database
databasePassword - the password for the user
Method Detail

connectToChannel

public void connectToChannel(Connection c,
                             java.lang.String ch)
Adds a connection to a chat channel. WARNING: MUST HAVE THE channelList.takeMonitorForChannel(ch) before calling this method

Parameters:
c - the Connection to be added
ch - the name of the channel

leaveChannel

public boolean leaveChannel(Connection c,
                            java.lang.String ch,
                            boolean channelListLock)
the connection c leaves the channel ch WARNING: MUST HAVE THE channelList.takeMonitorForChannel(ch) before calling this method

Parameters:
c - the connection
ch - the channel
channelListLock - true if we have the channelList monitor
Returns:
true if we destroyed the channel

connectToGame

public boolean connectToGame(Connection c,
                             java.lang.String ga)
Adds a connection to a game.

Parameters:
c - the Connection to be added
ga - the name of the game
Returns:
true if c was not a member of ch before

leaveGame

public boolean leaveGame(Connection c,
                         java.lang.String gm,
                         boolean gameListLock)
the connection c leaves the game gm WARNING: MUST HAVE THE gameList.takeMonitorForGame(gm) before calling this method

Parameters:
c - the connection
gm - the game
gameListLock - true if we have the gameList.takeMonitor() lock
Returns:
true if the game was destroyed

destroyGame

public void destroyGame(java.lang.String gm)
destroy the game WARNING: MUST HAVE THE gameList.takeMonitor() before calling this method

Parameters:
gm - the name of the game

leaveAllChannels

public java.util.Vector leaveAllChannels(Connection c)
the connection c leaves all channels it was in

Parameters:
c - the connection
Returns:
the channels it was in

leaveAllGames

public java.util.Vector leaveAllGames(Connection c)
the connection c leaves all games it was in

Parameters:
c - the connection
Returns:
the games it was in

messageToChannel

public void messageToChannel(java.lang.String ch,
                             SOCMessage mes)
Send a message to the given channel

Parameters:
ch - the name of the channel
mes - the message to send

messageToChannelWithMon

public void messageToChannelWithMon(java.lang.String ch,
                                    SOCMessage mes)
Send a message to the given channel WARNING: MUST HAVE THE gameList.takeMonitorForChannel(ch) before calling this method

Parameters:
ch - the name of the channel
mes - the message to send

messageToPlayer

public void messageToPlayer(Connection c,
                            SOCMessage mes)
Send a message to a player and record it

Parameters:
c - the player connection
mes - the message to send

messageToGame

public void messageToGame(java.lang.String ga,
                          SOCMessage mes)
Send a message to the given game

Parameters:
ga - the name of the game
mes - the message to send

messageToGameWithMon

public void messageToGameWithMon(java.lang.String ga,
                                 SOCMessage mes)
Send a message to the given game WARNING: MUST HAVE THE gameList.takeMonitorForGame(ga) before calling this method

Parameters:
ga - the name of the game
mes - the message to send

messageToGameExcept

public void messageToGameExcept(java.lang.String gn,
                                java.util.Vector ex,
                                SOCMessage mes)
Send a message to all the connections in a game excluding some.

Parameters:
gn - the name of the game
ex - the list of exceptions
mes - the message

leaveConnection

public void leaveConnection(Connection c)
things to do when the connection c leaves

Overrides:
leaveConnection in class Server
Parameters:
c - the connection

newConnection

public void newConnection(Connection c)
Things to do when a new connection comes

Overrides:
newConnection in class Server
Parameters:
c - the new Connection

processCommand

public void processCommand(java.lang.String s,
                           Connection c)
Treat the incomming messages Note: When there is a choice, always use local information over information from the message. For example, use the nickname from the connection to get the player information rather than the player information from the message. This makes it harder to send false messages making players do things they didn't want to do.

Specified by:
processCommand in class Server
Parameters:
s - String containing the message
c - the Connection that sent the Message

reportRobbery

protected void reportRobbery(SOCGame ga,
                             SOCPlayer pe,
                             SOCPlayer vi,
                             int rsrc)
The current player is stealing from another player. Send messages saying what was stolen.

Parameters:
ga - the game
pe - the perpetrator
vi - the the victim
rsrc - what was stolen

sendGameState

protected void sendGameState(SOCGame ga)
send the current state of the game with a message

Parameters:
ga - the game

reportTrade

protected void reportTrade(SOCGame ga,
                           int offering,
                           int accepting)
report a trade that has taken place

Parameters:
ga - the game
offering - the number of the player making the offer
accepting - the number of the player accepting the offer

reportBankTrade

protected void reportBankTrade(SOCGame ga,
                               SOCResourceSet give,
                               SOCResourceSet get)
report that the current player traded with the bank

Parameters:
ga - the game
give - the number of the player making the offer
get - the number of the player accepting the offer

checkTurn

protected boolean checkTurn(Connection c,
                            SOCGame ga)
make sure it's the player's turn

Parameters:
c - the connection
ga - the game
Returns:
true if it is the player's turn

startGame

protected void startGame(SOCGame ga)
do the stuff you need to do to start a game

Parameters:
ga - the game

storeGameScores

protected void storeGameScores(SOCGame ga)
if all the players stayed for the whole game, record the scores in the database

Parameters:
ga - the game

recordGameEvent

protected void recordGameEvent(java.lang.String gameName,
                               java.lang.String event)
record events that happen during the game

Parameters:
gameName - the name of the game
event - the event

giveResources

protected void giveResources(java.lang.String mes,
                             SOCGame game)
this is a debugging command that gives resources to a player


broadcastGameStats

protected void broadcastGameStats(SOCGame ga)
this broadcasts game information to all people connected used to display the scores on the player client


checkForExpiredGames

public void checkForExpiredGames()
check for games that have expired and destroy them if games are about to expire, send a warning


giveDevCard

protected void giveDevCard(java.lang.String mes,
                           SOCGame game)
this is a debugging command that gives a dev card to a player


main

public static void main(java.lang.String[] args)
Starting the server from the command line

Parameters:
args - arguments: port number

JSettlers v1.0.6 API
Built ${date}