home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enxle1f6 / src / games / battle / shared / comm / shellcommand.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  2.4 KB  |  106 lines

  1. /*
  2.  * @(#)ShellCommand.java
  3.  */
  4. package games.Battle.shared.comm;
  5.  
  6. /**
  7.  * ShellCommand is a collection of tags for messages passed between the client
  8.  * shell and the server core. This packets preceeds other packets which contain
  9.  * the real data of the command, and serve as a type tag that indicates what
  10.  * data will follow.
  11.  *
  12.  * @version 1.00
  13.  * @author Alex Nicolaou
  14.  * @author Jay Steele
  15.  */
  16.  
  17. public class ShellCommand extends BattlePacket {
  18.     /**
  19.      * JOIN joins a game
  20.      */
  21.     public static byte JOIN  = 0;
  22.     /**
  23.      * QUIT quits a game
  24.      */
  25.     public static byte QUIT  = 1;
  26.     /**
  27.      * WHO preceeds a list of WhoInfo packets.
  28.      */
  29.     public static byte WHO   = 2;
  30.     /**
  31.      * GAMES proceeds a list of GameInfo packets.
  32.      */
  33.     public static byte GAMES = 3;
  34.     /**
  35.      * SHUTDOWN tells the server to shut down.
  36.      */
  37.     public static byte SHUTDOWN   = 4;
  38.     /**
  39.      * DISCONNECT quits the connection with the server.
  40.      */
  41.     public static byte DISCONNECT = 5;
  42.     /**
  43.      * UPDATEID lets the player change their preferences, info, and password.
  44.      */
  45.     public static byte UPDATEID   = 6;
  46.     /**
  47.      * CHATMSG preceeds an InfoPacket with the text of the player's choice.
  48.      */
  49.     public static byte CHATMSG    = 7;
  50.  
  51.     /**
  52.      * bytes for transmitting the command
  53.      */
  54.     byte[] cmd = new byte[2];
  55.  
  56.     /**
  57.      * Construct an empty ShellCommand to be read from an input stream.
  58.      */
  59.     public ShellCommand() {
  60.         cmd[0] = 0;
  61.         cmd[1] = 0;
  62.     }
  63.  
  64.     /**
  65.      * Construct a complete shell command.
  66.      */
  67.     public ShellCommand(byte command, int gameid) {
  68.         cmd[0] = command;
  69.         cmd[1] = (byte)gameid;
  70.     }
  71.  
  72.     /**
  73.      * Return the command code, which must be one of JOIN, QUIT, etc.
  74.      */
  75.     public byte getCommand() { 
  76.         return cmd[0];
  77.     }
  78.  
  79.     /**
  80.      * Return the gameId that this command applies to. 0 in the case of
  81.      * a command that isn't associated with a particular game.
  82.      */
  83.     public int getGameId() { 
  84.         return ((int)cmd[1])&0x00ff;
  85.     }
  86.  
  87.     /**
  88.      * Return the byte representation.
  89.      */
  90.     protected byte[] toBytes() {
  91.         return cmd;
  92.     }
  93.  
  94.     /**
  95.      * With the given array of bytes, fill this command object
  96.      * with data.
  97.      * @param data the data to fill the command with.
  98.      */
  99.     protected void fromBytes(byte[] data) {
  100.         // probably not worth the overhead of memcpy() since native calls 
  101.         // are probably costlier than just doing the assignments.
  102.         cmd[0] = data[0];
  103.         cmd[1] = data[1];
  104.     }
  105. }
  106.