⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverconfigmanager.java

📁 Vyger offers a D & D and Rogue-like environment in a graphical online roleplay game.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

         if(parent!=null)
            pMonitor.close();
    }

  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

   /** To add a new ServerConfig to our list.
    * @param newConfig config to add
    */
      protected void addConfig( ServerConfig newConfig ) {
          if (configs == null) {
              configs = new ServerConfig[1];
              configs[0] = newConfig;
          } else {
              ServerConfig myConfigs[] = new ServerConfig[configs.length+1];
              System.arraycopy(configs, 0, myConfigs, 0, configs.length);
              myConfigs[configs.length] = newConfig;
              configs = myConfigs;
          }
      }

  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

   /** To get a ServerConfig file from its associated serverID.
    *
    *  NOTE : serverID != originalServerID. For a player the originalServerID is the
    *  serverID of the server that created his game account. But as his account moves
    *  from one server to another, the player's current serverID ( where he now is )
    *  can be different from his originalServerID.
    *
    * @param serverID server ID.
    * @return null if the serverconfig was not found, the ServerConfig object otherwise.
    */
     public ServerConfig getServerConfig( int serverID ) {
     
         if(configs==null)
            return null;
     
         for( int i=0; i<configs.length; i++ )
             if( configs[i].getServerID()==serverID ) {
                 updateServerConfig( configs[i] ); // trying to update
                 return configs[i];
             }
         return null;
     }

  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

   /** We search for the wanted server config without performing any updates.
    */
     protected ServerConfig findServerConfig( int serverID ) {
     
         if(configs==null)
            return null;
     
         for( int i=0; i<configs.length; i++ )
             if( configs[i].getServerID()==serverID )
                 return configs[i];

         return null;
     }

  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

    /** To get the following server ID.
     * @param previousServerID the previous ID we tried
     * @return the immediately following server ID, -1 if there is none
     */
     public int getNextServerID(int previousServerID) {

          if(configs==null || configs.length==0) return -1;

          int min=-1;

       // Search the minimum
          for( int i=0; i<configs.length; i++ ) {
             int id = configs[i].getServerID();

             if( id>previousServerID && (id<min || min==-1) )
                 min=id;
          }
          
          return min;
     }

  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To get the number of servers.
   */  
   public int size() {
     if (configs==null)
         return 0;
     return configs.length;
   }
  
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  
  /** To get a ServerConfig file from its index in the array <b>configs</b>.
   *
   * @param index server index.
   */
   public ServerConfig serverConfigAt(int index) {
      if(configs==null) return null;
      return configs[index];
   }

  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To report a dead server. We'll then try to check the server address.
   * @param serverID server id which represents the dead server
   * @return another address you can try for this server, if this address fails
   *     you can really consider the server dead. If we return null it means
   *     that we don't where able to update the server address...
   */
    public String reportDeadServer( int serverID ) {

        ServerConfig currentConfig = findServerConfig(serverID);

        if(currentConfig==null) {
            Debug.signal( Debug.ERROR, this, "Failed to find local config of server "+currentConfig.getServerID()+".");
            return null;
        }

     // We check the cache timestamp
        if( currentConfig.getLastUpdateTime()+UPDATE_PERIOD > System.currentTimeMillis() )
            return null; // we recently checked the address

        String fileURL = remoteServerConfigHomeURL+SERVERS_PREFIX+currentConfig.getServerID()+SERVERS_SUFFIX;

     // We load the address file
        String newAdr = FileTools.getTextFileFromURL( fileURL+SERVERS_ADDRESS_SUFFIX );
        newAdr = checkAddressFormat(newAdr); // we check the format

        if( newAdr.length()==0 ) {
            Debug.signal( Debug.ERROR, this, "Failed to get new Server "+currentConfig.getServerID()+" address. Reverting to previous one.");
            return null;
        }

        if( !updateServerConfig( null, newAdr, currentConfig ) )
            Debug.signal(Debug.ERROR,this,"For some reason we failed to save the new address...");

        return newAdr; // new address the user can try...
    }

  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** Check the format of a DNS name or IP address.
   *  @param adr address to check
   *  @return the address without any ending special chars.
   */
    public String checkAddressFormat( String adr ) {
    	 if(adr==null)
    	    return "";
    	
      // We search for bad chars at the end of the file.
      // the end chars must be either letters (end of a domain name fr, com, net etc...)
      // or numbers (end of an IP address).
         adr = adr.trim();

         for( int i=adr.length()-1; i!=0; i-- ) {
              char c = adr.charAt(i);
              
              if( ('a'<=c && c<='z') || ('A'<=c && c<='Z') ||
                  ('0'<=c && c<='9') || c=='.' )
                  return adr.substring(0,i+1);
         }
         
         return ""; // not a valid address !!!
    }

  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

                      /******** PERSISTENCE METHODS **********/

  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** Loads the server config associated to the given serverID.
   *
   * @param serverID id of the server which config is to be loaded.
   * @return server config
   */
   public ServerConfig loadServerConfig( int serverID ) {

        String serverFile = rManager.getExternalServerConfigsDir()
                                      +SERVERS_PREFIX+serverID+SERVERS_SUFFIX;

        ServerConfig config = (ServerConfig) rManager.loadObject( serverFile );
        String serverAdr = rManager.loadText( serverFile+SERVERS_ADDRESS_SUFFIX );
        serverAdr = checkAddressFormat( serverAdr );

        if( config==null || serverAdr.length()==0 ) {
            Debug.signal( Debug.ERROR, this, "Failed to load server config: "
                                   +"no valid server config"+serverID+" files found !");
            return null;
        }
          
        config.setServerName( serverAdr );
        return config;
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** Saves the server config to the SERVERS_HOME directory.
   *
   *  @param serverConfig server config
   *  @return true in case of success, false if an error occured.
   */
   public boolean saveServerConfig( ServerConfig serverConfig ) {

        String serverFile = rManager.getExternalServerConfigsDir()
                            +SERVERS_PREFIX+serverConfig.getServerID()+SERVERS_SUFFIX;

        if( !rManager.saveObject( serverConfig, serverFile ) || ( serverConfig.getServerName()!=null &&
            !rManager.saveText( serverFile+SERVERS_ADDRESS_SUFFIX, serverConfig.getServerName() ) ) ) {
            Debug.signal( Debug.ERROR, this, "Failed to save server config: "
                         +"couldn't save "+serverFile+SERVERS_ADDRESS_SUFFIX+" file !");
            return false;
        }

       return true;
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** Updates a server config of the SERVERS_HOME directory.
   *  The oldServerConfig fields are updated with the new ones.
   *  The newConfigText paramter can be null : we then only save the new address.
   *
   *  @param newConfigText new config loaded from an URL. Can be null.
   *  @param newAdrText new server address loaded from an URL
   *  @param oldServerConfig server config
   *  @return true in case of success, false if an error occured.
   */
   public boolean updateServerConfig( String newConfigText, String newAdrText, ServerConfig oldServerConfig ) {

        String serverFile = rManager.getExternalServerConfigsDir()
                              +SERVERS_PREFIX+oldServerConfig.getServerID()+SERVERS_SUFFIX;

        if( newConfigText!=null && !rManager.saveText( serverFile, newConfigText ) ) {
            Debug.signal( Debug.ERROR, this, "Failed to update server config: "
                                              +"failed to save "+serverFile+" file !");
            return false;
        }

        if( !rManager.saveText( serverFile+SERVERS_ADDRESS_SUFFIX, newAdrText ) ) {
            Debug.signal( Debug.ERROR, this, "Failed to update server config: "
                                   +"failed to save "+serverFile+SERVERS_ADDRESS_SUFFIX+" file !");
            return false;
        }

     // We load the newly saved config...
        if( newConfigText!=null ) {
            ServerConfig newConfig = (ServerConfig) rManager.loadObject( serverFile );

            if( newConfig.getServerID()!= oldServerConfig.getServerID() ) {
                Debug.signal( Debug.ERROR, this, "Failed to update server config: "
                            +"new Config was not saved: it hasn't the expected Server ID !");
                saveServerConfig( oldServerConfig );
                return false;
            }

            newConfig.setServerName( newAdrText );
            oldServerConfig.update( newConfig );
        }
        else
            oldServerConfig.setServerName( newAdrText );

      return true;
   }

 /*------------------------------------------------------------------------------------*/

  /** To create a server config from a text file representing a previously saved ServerConfig.
   *
   *  @param newConfigText new config loaded from an URL.
   *  @param newAdrText new server address loaded from an URL
   *  @param serverID server Id
   *  @return the created ServerConfig in case of success, null if an error occured.
   */
   public ServerConfig createServerConfig( String newConfigText, String newAdrText, int serverID ) {

        String serverFile = rManager.getExternalServerConfigsDir()
                                            +SERVERS_PREFIX+serverID+SERVERS_SUFFIX;

        File serversHomeDir = new File( rManager.getExternalServerConfigsDir() );

        if(!serversHomeDir.exists()) {
            serversHomeDir.mkdir();
            Debug.signal(Debug.WARNING,this,"Server configs dir was not found. Created dir...");
        }

        if( !rManager.saveText( serverFile, newConfigText ) )
            return null; // Save Failed

        if( !rManager.saveText( serverFile+SERVERS_ADDRESS_SUFFIX, newAdrText ) ) {
            new File(serverFile).delete();
            return null; // Save Failed
        }

        return loadServerConfig(serverID); // We load the newly saved config...
   }

 /*------------------------------------------------------------------------------------*/

  /** Loads all the server config files found in SERVERS_HOME.
   *
   * @param serverID id of the server which config is to be loaded.
   * @return server config
   */
   public ServerConfig[] loadServerConfigs() {

      String serversHome = rManager.getExternalServerConfigsDir();

      File serversHomeDir = new File(serversHome);

      if(!serversHomeDir.exists()) {
      	 serversHomeDir.mkdir();
      	 Debug.signal(Debug.WARNING,this,"Server configs dir was not found. Created dir...");
      	 return null;
      }

      File configFileList[] = serversHomeDir.listFiles();

      if(configFileList==null) {
      	 Debug.signal(Debug.CRITICAL,this,"No server file loaded...");
      	 return null;
      }

     // We count how many server config files we have...
       int nbFiles=0;

        for( int i=0; i<configFileList.length; i++ )
           if(configFileList[i].isFile() && configFileList[i].getName().endsWith(SERVERS_SUFFIX) )
              nbFiles++;
       
     // create ServerConfig array
        if(nbFiles==0)
           return null;

        ServerConfig configList[] = new ServerConfig[nbFiles];
        int index=0;

        for( int i=0; i<configFileList.length; i++ )
           if(configFileList[i].isFile() && configFileList[i].getName().endsWith(SERVERS_SUFFIX) ){

               String serverFile = serversHome + configFileList[i].getName();
               configList[index] = (ServerConfig) rManager.loadObject( serverFile );

               if(configList[index]==null) {
                  Debug.signal(Debug.ERROR, this, "Failed to load "+serverFile);
                  index++;
                  continue;
               }

               String serverName = rManager.loadText( serverFile+SERVERS_ADDRESS_SUFFIX );
               serverName = checkAddressFormat(serverName);

               if( serverName.length()==0 )
                   Debug.signal( Debug.ERROR, this, "Failed to load server config: no"
                                 +serverFile+SERVERS_ADDRESS_SUFFIX+" file found !");

               configList[index].setServerName( serverName );
               configList[index].clearLastUpdateTime(); // clear timestamp set by this operation
               index++;
           }

        return configList;
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -