home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-05-08 | 4.2 KB | 141 lines |
- /*
- * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
- *
- * This SOURCE CODE FILE, which has been provided by Borland as part
- * of a Borland product for use ONLY by licensed users of the product,
- * includes CONFIDENTIAL and PROPRIETARY information of Borland.
- *
- * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
- * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
- * THE PRODUCT.
- *
- * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
- * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
- * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
- * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
- * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
- * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
- * CODE FILE.
- */
- package borland.samples.apps.chess.server;
-
- import java.net.*;
- import java.util.*;
- import java.io.*;
- import borland.samples.apps.chess.client.ServerMessage;
-
- public class ChessServer extends Thread
- {
- static Hashtable playerlist = new Hashtable();
- static Hashtable gamelist = new Hashtable();
- static boolean availablePort[];
- static int maxConnections = 50;
- final static int basePort = 101;
- static boolean shutdown = false;
- static int warningTime = 2;
- static int socketnum ;
-
- public static void main(String args[]) {
- ChessServer m = new ChessServer();
- for (int i = 0;i< args.length;i++) {
- if (args[i].startsWith("connections=")) {
- try {
- maxConnections = Integer.parseInt(args[i].substring(12));
- }
- catch (Exception e){
- System.out.println("connections param " + args[i] + " not a valid int");
- }
- }
- else
- System.out.println("don\'t know what to do with " + args[i]);
-
- }
- socketnum = basePort;
- m.start();
- }
-
- static void portIdle(int portNum) {
- System.out.println("giving back port " + portNum);
- availablePort[portNum - basePort] = true;
- }
-
- static int getPortNumber() {
- for (int i=socketnum - basePort;i<maxConnections;i++) {
- if (availablePort[i]) {
- socketnum = i + basePort + 1;
- availablePort[i] = false;
- return i + basePort;
- }
- }
- System.out.println("looking from the beginning");
- for (int i=0;i<maxConnections;i++) {
- if (availablePort[i]) {
- socketnum = i + basePort + 1;
- availablePort[i] = false;
- return i + basePort;
- }
- }
- return 0;
- }
-
- public void run() {
- File shutdownfile = new File("shutdown.txt");
- if (shutdownfile.exists())
- shutdownfile.renameTo(new File("shutdown.xyz"));
- PlayerId.readUserFile();
- Thread h = new HelloThread();
- h.start();
- while (true) {
- synchronized (this) {
- try {
- wait(60000); //60000 = one minute
- }
- catch (InterruptedException e) {
- }
- }
- if (shutdownfile.exists()) {
- System.out.println("we be shutting down ");
- shutdown = true;
- synchronized (h) {
- h.stop();
- }
- sendWarning (shutdownfile);
- suspendGames();
- PlayerId.writeUserList();
- break;
- }
- PlayerId.writeUserList();
- } //end of while (true)
- System.out.println("TTFN");
- System.exit(0);
- }
-
- public void suspendGames(){
- SendThread np;
- for (Enumeration e = ChessServer.playerlist.elements();e.hasMoreElements();){
- np = (SendThread)e.nextElement();
- np.addMsg(0,"Bye","");
- }
- }
-
- public void sendWarning(File shutdownFile){
- //could read the delay time out of the shutdown file
- SendThread np;
- System.out.println("tell people they have " + warningTime + " minutes");
- for (Enumeration e = ChessServer.playerlist.elements() ; e.hasMoreElements() ;) {
- np = (SendThread)e.nextElement();
- if (np != null ) {
- np.addMsg(0,"Status","Chess Server shutting down in "+ warningTime + " minutes - sorry");
- }
- }
- synchronized (this) {
- try {
- wait(warningTime * 60000);
- }
- catch (InterruptedException e) {}
- }
- }
- } //end of ChessServer class
-
-
-