Clover coverage report -
Coverage timestamp: Sun Oct 12 2003 22:57:21 PDT
file stats: LOC: 198   Methods: 7
NCLOC: 100   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
GetNextIDCommand.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
 
 3   
 VRMoo Server - Virtual Reality Object Oriented MUD Server
 4   
 Copyright (C) 2001 - 2003  VRMoo Development Team
 5   
 
 6   
 
 7   
 This program is free software; you can redistribute it and/or modify
 8   
 it under the terms of the GNU General Public License as published by
 9   
 the Free Software Foundation; either version 2 of the License, or
 10   
 (at your option) any later version.
 11   
 
 12   
 This program is distributed in the hope that it will be useful,
 13   
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 14   
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15   
 GNU General Public License for more details.
 16   
 
 17   
 You should have received a copy of the GNU General Public License
 18   
 along with this program; if not, write to the Free Software
 19   
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 20   
 
 21   
 
 22   
 For information about VRMoo and its authors, please visit the website:
 23   
 http://www.vrmoo.org/
 24   
 
 25   
 */
 26   
 
 27   
 package org.vrmoo.server.command;
 28   
 
 29   
 import java.io.BufferedReader;
 30   
 import java.io.PrintWriter;
 31   
 import java.util.HashMap;
 32   
 import java.util.Iterator;
 33   
 import java.util.Map;
 34   
 
 35   
 import org.vrmoo.common.exception.VRMooParseException;
 36   
 import org.vrmoo.common.util.CommandProcessor;
 37   
 import org.vrmoo.common.util.FileUtility;
 38   
 import org.vrmoo.common.util.ParserUtility;
 39   
 
 40   
 import org.vrmoo.server.client.AllClients;
 41   
 import org.vrmoo.server.client.ClientHandler;
 42   
 
 43   
 /**
 44   
  * This class is a singleton used for responding to requests from the client for
 45   
  * the next unique ID for various objects that are managed by the server.
 46   
  *
 47   
  * @author Jeff Weston
 48   
  */
 49   
 public class GetNextIDCommand implements CommandProcessor
 50   
 {
 51   
     /**
 52   
      * The prefix for the command handled by this processor.
 53   
      */
 54   
     private static final String COMMAND_PREFIX = "getNextIDCommand:";
 55   
 
 56   
     /**
 57   
      * The single instance of this class.
 58   
      */
 59   
     private static GetNextIDCommand instance;
 60   
 
 61   
     /**
 62   
      * The map of next unique identifiers being managed by this class.
 63   
      */
 64   
     private Map nextIDMap;
 65   
 
 66   
     /**
 67   
      * This class is a singleton, so make the constructor private.
 68   
      */
 69  0
     private GetNextIDCommand( )
 70   
     {
 71  0
         nextIDMap = new HashMap( );
 72   
     }
 73   
 
 74   
     /**
 75   
      * Get the single instance of this class.
 76   
      *
 77   
      * @return the single instance of this class
 78   
      */
 79  0
     public static synchronized GetNextIDCommand getInstance( )
 80   
     {
 81  0
         if ( instance == null )
 82   
         {
 83  0
             instance = new GetNextIDCommand( );
 84   
         }
 85  0
         return instance;
 86   
     }
 87   
 
 88   
     /**
 89   
      * Returns the command prefix used to recognize commands that this
 90   
      * processor can handle.
 91   
      *
 92   
      * @return the command prefix
 93   
      */
 94  0
     public String getCommandPrefix( )
 95   
     {
 96  0
         return COMMAND_PREFIX;
 97   
     }
 98   
 
 99   
     /**
 100   
      * Process a command matching the command prefix established by the
 101   
      * <code>getCommandPrefix()</code> method.
 102   
      *
 103   
      * @param command     the command to process
 104   
      * @param extraData   extra data that isn't part of the command but may
 105   
      *                      be needed for dealing with the command (such as
 106   
      *                      the ID of the client that sent the command)
 107   
      *
 108   
      * @throws VRMooParseException for any parsing errors that occur while
 109   
      *           processing the command
 110   
      */
 111  0
     public synchronized void processCommand( String command, String extraData )
 112   
             throws VRMooParseException
 113   
     {
 114  0
         String key = command.substring( COMMAND_PREFIX.length( ) ).trim( );
 115  0
         int id = getNextID( key );
 116  0
         ClientHandler client =
 117   
                 AllClients.getClientByID( ParserUtility.parseInt( extraData ) );
 118  0
         client.writeLine( "getNextIDCommand: setNextID: " + id );
 119   
     }
 120   
 
 121   
     /**
 122   
      * Get the next unique ID for a given key.
 123   
      *
 124   
      * @param key   the key to get the next unique ID for
 125   
      *
 126   
      * @return the next unique ID for the given key
 127   
      */
 128  0
     public synchronized int getNextID( String key )
 129   
     {
 130  0
         if ( ! nextIDMap.containsKey( key ) )
 131   
         {
 132  0
             nextIDMap.put( key, new Integer( 1 ) );
 133   
         }
 134  0
         Integer nextID = ( Integer ) nextIDMap.get( key );
 135  0
         nextIDMap.put( key, new Integer( nextID.intValue( ) + 1 ) );
 136  0
         return nextID.intValue( );
 137   
     }
 138   
 
 139   
     /**
 140   
      * Save all of the unique IDs managed by this class out to disk.
 141   
      */
 142  0
     public synchronized void saveData( )
 143   
     {
 144  0
         try
 145   
         {
 146  0
             PrintWriter writer =
 147   
                     FileUtility.getPrintWriter( "data/identifiers.dat" );
 148   
 
 149  0
             Iterator iter = nextIDMap.keySet( ).iterator( );
 150   
 
 151  0
             while ( iter.hasNext( ) )
 152   
             {
 153  0
                 String key = ( String ) iter.next( );
 154  0
                 Integer value = ( Integer ) nextIDMap.get( key );
 155  0
                 writer.println( key + "=" + value.intValue( ) );
 156   
             }
 157  0
             writer.close( );
 158   
         }
 159   
         catch ( Exception e )
 160   
         {
 161  0
             e.printStackTrace( );
 162   
         }
 163   
     }
 164   
 
 165   
     /**
 166   
      * Load all of the unique IDs managed by this class from disk.
 167   
      */
 168  0
     public synchronized void loadData( )
 169   
     {
 170  0
         try
 171   
         {
 172  0
             BufferedReader reader =
 173   
                     FileUtility.getBufferedReader( "data/identifiers.dat" );
 174   
 
 175  0
             String line = reader.readLine( );
 176   
 
 177  0
             while ( line != null )
 178   
             {
 179  0
                 int split = line.indexOf( "=" );
 180  0
                 if ( ( split >= 0 ) && ( line.length( ) > split ) )
 181   
                 {
 182  0
                     String key = line.substring( 0, split );
 183  0
                     String value = line.substring( split + 1 );
 184  0
                     int identifier = ParserUtility.parseInt( value );
 185  0
                     Integer id = new Integer( identifier );
 186  0
                     nextIDMap.put( key, id );
 187   
                 }
 188  0
                 line = reader.readLine( );
 189   
             }
 190  0
             reader.close( );
 191   
         }
 192   
         catch ( Exception e )
 193   
         {
 194  0
             e.printStackTrace( );
 195   
         }
 196   
     }
 197   
 }
 198