home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 October A / Pcwk10a98.iso / Inprise / TRIAL / JBUILDER / JSAMPLES.Z / ChessServer.java < prev    next >
Text File  |  1998-05-08  |  4KB  |  141 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. package borland.samples.apps.chess.server;
  21.  
  22. import java.net.*;
  23. import java.util.*;
  24. import java.io.*;
  25. import borland.samples.apps.chess.client.ServerMessage;
  26.  
  27. public class ChessServer extends Thread
  28. {
  29.   static Hashtable playerlist = new Hashtable();
  30.   static Hashtable gamelist = new Hashtable();
  31.   static boolean availablePort[];
  32.   static int maxConnections = 50;
  33.   final static int basePort = 101;
  34.   static boolean shutdown = false;
  35.   static int warningTime = 2;
  36.   static int socketnum ;
  37.  
  38.   public static void main(String args[]) {
  39.     ChessServer m = new ChessServer();
  40.     for (int i = 0;i< args.length;i++) {
  41.       if (args[i].startsWith("connections=")) {
  42.         try {
  43.           maxConnections = Integer.parseInt(args[i].substring(12));
  44.         }
  45.         catch (Exception e){
  46.           System.out.println("connections param " + args[i] + " not a valid int");
  47.         }
  48.       }
  49.       else
  50.         System.out.println("don\'t know what to do with " + args[i]);
  51.  
  52.     }
  53.     socketnum = basePort;
  54.     m.start();
  55.   }
  56.  
  57.   static void portIdle(int portNum) {
  58.     System.out.println("giving back port " + portNum);
  59.     availablePort[portNum - basePort] = true;
  60.   }
  61.  
  62.   static int getPortNumber() {
  63.     for (int i=socketnum - basePort;i<maxConnections;i++) {
  64.       if (availablePort[i]) {
  65.         socketnum = i + basePort + 1;
  66.         availablePort[i] = false;
  67.         return i + basePort;
  68.       }
  69.     }
  70.     System.out.println("looking from the beginning");
  71.     for (int i=0;i<maxConnections;i++) {
  72.       if (availablePort[i]) {
  73.         socketnum = i + basePort + 1;
  74.         availablePort[i] = false;
  75.         return i + basePort;
  76.       }
  77.     }
  78.     return 0;
  79.   }
  80.  
  81.   public void run() {
  82.     File shutdownfile = new File("shutdown.txt");
  83.     if (shutdownfile.exists())  
  84.       shutdownfile.renameTo(new File("shutdown.xyz"));
  85.     PlayerId.readUserFile();
  86.     Thread h = new HelloThread();
  87.     h.start();
  88.     while (true)  {
  89.       synchronized (this) {
  90.         try {
  91.           wait(60000);   //60000 = one minute
  92.         }
  93.         catch (InterruptedException e) {
  94.         }
  95.       }
  96.       if (shutdownfile.exists())  {
  97.         System.out.println("we be shutting down ");
  98.         shutdown = true;
  99.         synchronized (h) {
  100.           h.stop();
  101.         }
  102.         sendWarning (shutdownfile);
  103.         suspendGames();
  104.         PlayerId.writeUserList();
  105.         break;
  106.       }
  107.       PlayerId.writeUserList();
  108.     } //end of while (true)
  109.     System.out.println("TTFN");
  110.     System.exit(0);
  111.   }
  112.  
  113.   public void suspendGames(){
  114.     SendThread np;
  115.     for (Enumeration e = ChessServer.playerlist.elements();e.hasMoreElements();){
  116.       np = (SendThread)e.nextElement();
  117.       np.addMsg(0,"Bye","");
  118.     }
  119.   }
  120.  
  121.   public void sendWarning(File shutdownFile){
  122.     //could read the delay time out of the shutdown file
  123.     SendThread np;
  124.     System.out.println("tell people they have " + warningTime + " minutes");
  125.     for (Enumeration e = ChessServer.playerlist.elements() ; e.hasMoreElements() ;) {
  126.       np = (SendThread)e.nextElement();
  127.       if (np != null ) {
  128.         np.addMsg(0,"Status","Chess Server shutting down in "+ warningTime + " minutes - sorry");
  129.       }
  130.     }
  131.     synchronized (this) {
  132.     try {
  133.       wait(warningTime * 60000);
  134.     }
  135.     catch (InterruptedException e) {}
  136.     }
  137.   }
  138. } //end of ChessServer class
  139.  
  140.  
  141.