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

📄 simplereplicamanager.java

📁 中間件開發详细说明:清华大学J2EE教程讲义(ppt)-Tsinghua University J2EE tutorial lectures (ppt) [上载源码成为会员下载此源码] [成为VIP会
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Title:        GridSim Toolkit * Description:  GridSim (Grid Simulation) Toolkit for Modeling and Simulation *               of Parallel and Distributed Systems such as Clusters and Grids * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html */package gridsim.datagrid;import gridsim.datagrid.storage.*;import gridsim.datagrid.index.*;import eduni.simjava.*;import gridsim.*;import java.util.*;/** * This is a class which contains the basic functionality of a Replica Manager * in a Data Grid. The current functionlity provided by this implementation * includes the following: * <ul> * <li> Adding a master file or a replica to the storage and register it to the * RC. * <li> Removing master and replica files from the storage and deregister from * RC. * <li> Sending requested files to users. * <li> Managing a {@link gridsim.datagrid.DataGridlet}, i.e. transferring the * neccessary files to the local storage and pass the gridlet to the * AllocationPolicy for execution. * </ul> *  * @author Uros Cibej and Anthony Sulistio * @since GridSim Toolkit 4.0 */public class SimpleReplicaManager extends ReplicaManager {    private ArrayList filesWaitingForAddACK_; // waiting list for add    private ArrayList filesWaitingForDeleteACK_; // waiting list for delete    private ArrayList masterFilesWaitingForAddACK_;    private ArrayList masterFilesWaitingForDeleteACK_;    private ArrayList priorityFile_;    // the list of all DataGridlets waiting to acquire the needed files    private ArrayList waitingDataGridlet_;    //  -------------------INITIALIZATION-------------------------------    /**     * Creates a new Replica Manager object     * @param name          the name to be associated with this entity     * @param resourceName  the name of the DataGrid resource     * @throws Exception This happens when one of the following scenarios occur:     *      <ul>     *          <li> creating this entity before initializing GridSim package     *          <li> the given name is <tt>null</tt> or empty     *      </ul>     * @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[],     *          String)     */    public SimpleReplicaManager(String name, String resourceName)            throws ParameterException {        super(name, resourceName);        commonInit();    }    /** Initializes all attributes */    private void commonInit() {        super.storageList_ = new ArrayList();        filesWaitingForAddACK_ = new ArrayList();        filesWaitingForDeleteACK_ = new ArrayList();        masterFilesWaitingForAddACK_ = new ArrayList();        masterFilesWaitingForDeleteACK_ = new ArrayList();        waitingDataGridlet_ = new ArrayList();        priorityFile_ = new ArrayList();    }    //  -------------------STORAGE/FILE MANIPULATION METHODS------------------    /**     * Adds a file to the local storage. However, the file is not registered     * to the Replica Catalogue.     * <br>     * In this implementation, it looks through all the available     * storages if there is some space. It stores on the first storage that has     * enough space. In addition, we assume all files are read-only.     * Hence, existing files can not be overwritten.     *     * @param file  a file to be placed on the local resource     * @return an integer number denoting whether this operation is successful     *         or not     * @see gridsim.datagrid.DataGridTags#FILE_ADD_SUCCESSFUL     * @see gridsim.datagrid.DataGridTags#FILE_ADD_ERROR_STORAGE_FULL     */    protected int addFile(File file) {        // at the moment we assume all files are read only. To overwrite a file        // we use FILE_MODIFY        if (super.contains(file.getName()) == true) {            return DataGridTags.FILE_ADD_ERROR_EXIST_READ_ONLY;        }        // check storage space first        if (storageList_.size() <= 0) {            return DataGridTags.FILE_ADD_ERROR_STORAGE_FULL;        }        Storage tempStorage = null;        double time = 0;        int msg = DataGridTags.FILE_ADD_ERROR_STORAGE_FULL;        for (int i = 0; i < storageList_.size(); i++) {            tempStorage = (Storage) storageList_.get(i);            if (tempStorage.getAvailableSpace() >= file.getSize()) {                time = tempStorage.addFile(file);                msg = DataGridTags.FILE_ADD_SUCCESSFUL;                break;            }        }        return msg;    }    /**     * Adds a list of storage elements to the DataGrid resource     * @param storageList   a list of storage elements to be added     * @return <tt>true</tt> if successful, <tt>false</tt> otherwise     */    public boolean addStorage(List storageList) {        if (storageList == null) {            return false;        }        boolean result = false;        try {            storageList_.addAll(storageList);            result = true;        } catch (Exception e) {            result = false;        }        return result;    }    /**     * Adds a storage element to the DataGrid resource     * @param storage   the storage element to be added     * @return <tt>true</tt> if successful, <tt>false</tt> otherwise     */    public boolean addStorage(Storage storage) {        if (storage == null) {            return false;        }        storageList_.add(storage);        return true;    }    /**     * Deletes a file from the local storage, and registers     * the change to the designated Replica Catalogue.     * @param fileName  the filename of the file to be deleted.     * @return an integer number denoting whether this operation is successful     *         or not     * @see gridsim.datagrid.DataGridTags#FILE_DELETE_SUCCESSFUL     * @see gridsim.datagrid.DataGridTags#FILE_DELETE_ERROR_READ_ONLY     */    protected int deleteFile(String fileName) {        int msg = deleteFileFromStorage(fileName, false, false);        if (msg == DataGridTags.FILE_DELETE_SUCCESSFUL) {            msg = super.deregisterDeletedFile(fileName,                    DataGridTags.CTLG_DELETE_REPLICA);        }        return msg;    }    /**     * Deletes the file from the storage. Also, check whether it is     * possible to delete the file from the storage.     *     * @param fileName      the name of the file to be deleted     * @param deleteMaster  do we want to delete the master file or not     * @param justTest      <tt>true</tt> if you just want to test the file, or     *                      <tt>false</tt> if you want to actually delete it     * @return the error message as defined in     *         {@link gridsim.datagrid.DataGridTags}     * @see gridsim.datagrid.DataGridTags#FILE_DELETE_SUCCESSFUL     * @see gridsim.datagrid.DataGridTags#FILE_DELETE_ERROR_ACCESS_DENIED     * @see gridsim.datagrid.DataGridTags#FILE_DELETE_ERROR     */    private int deleteFileFromStorage(String fileName, boolean deleteMaster,            boolean justTest) {        Storage tempStorage = null;        File tempFile = null;        double time = 0;        int msg = DataGridTags.FILE_DELETE_ERROR;        for (int i = 0; i < storageList_.size(); i++) {            tempStorage = (Storage) storageList_.get(i);            tempFile = tempStorage.getFile(fileName);            if (tempFile != null) {                // if want to delete a master copy, then you can't                if (tempFile.isMasterCopy() == true && deleteMaster == false) {                    msg = DataGridTags.FILE_DELETE_ERROR_ACCESS_DENIED;                }                // if a file is a replica, but want to delete a master one                else if (tempFile.isMasterCopy() == false                        && deleteMaster == true) {                    msg = DataGridTags.FILE_DELETE_ERROR_ACCESS_DENIED;                } else {                    // if you want to actually delete this file                    if (justTest == false) {                        time = tempStorage.deleteFile(fileName, tempFile);                    }                    msg = DataGridTags.FILE_DELETE_SUCCESSFUL;                }            }        } // end for        return msg;    }    /**     * Gets a physical file based on its name     * @param fileName  the file name to be retrieved     * @return the physical file or <tt>null</tt> if not found     */    protected File getFile(String fileName) {        Storage tempStorage = null;        File tempFile = null;        for (int i = 0; i < storageList_.size(); i++) {            tempStorage = (Storage) storageList_.get(i);            tempFile = tempStorage.getFile(fileName);            if (tempFile != null) {                double time = tempFile.getTransactionTime();                break;            } else {                tempFile = null;            }        }        return tempFile;    }    //  -------------------MAIN METHODS FOR MANAGING EVENTS-------------------    /**     * The main method of the data manager, which is responsible for managing     * all the incoming events.     */    public void body() {        // a loop that is looking for internal events only        Sim_event ev = new Sim_event();        while (Sim_system.running()) {            super.sim_get_next(ev);            // if the simulation finishes then exit the loop            if (ev.get_tag() == GridSimTags.END_OF_SIMULATION) {                break;            }            processEvent(ev);        }        // CHECK for ANY INTERNAL EVENTS WAITING TO BE processed        while (super.sim_waiting() > 0) {            super.sim_get_next(ev);            System.out.println(super.get_name() + ".body(): Ignoring events");        }    }    /**     * Processes an incoming event     * @return <tt>true</tt> if successful, <tt>false</tt> otherwise     */    public boolean processEvent(Sim_event ev) {        boolean result = true;        File file = null;        switch (ev.get_tag()) {        //----USER REQUESTS------        case DataGridTags.FILE_ADD_MASTER:            processAddMasterFile(ev);            break;        case DataGridTags.FILE_DELETE_MASTER:            processDeleteMasterFile(ev);            break;        case DataGridTags.FILE_ADD_REPLICA:            processAddReplica(ev);            break;        case DataGridTags.FILE_DELETE_REPLICA:            processDeleteReplica(ev);            break;        case DataGridTags.FILE_REQUEST:            processFileRequest(ev);            break;        case DataGridTags.FILE_MODIFY:            System.out                    .println(super.get_name()                            + ".processOtherEvent(): FILE_MODIFY is not implemented yet");            break;        //------------CATALOGUE RESULTS/RESPONSES----------        case DataGridTags.CTLG_ADD_REPLICA_RESULT:            this.processCatalogueAddResult(ev);            break;        case DataGridTags.CTLG_DELETE_REPLICA_RESULT:            this.processCatalogueDeleteResult(ev);            break;        case DataGridTags.CTLG_ADD_MASTER_RESULT:            this.processMasterAddResult(ev);            break;        case DataGridTags.CTLG_DELETE_MASTER_RESULT:            processMasterDeleteResult(ev);            break;        case DataGridTags.FILE_DELETE_SUCCESSFUL:            this.processCatalogueDeleteResult(ev);            break;        //------------DATA GRIDLET STUFF----------        case DataGridTags.CTLG_REPLICA_DELIVERY:            receiveReplicaLocation(ev);            break;        case DataGridTags.DATAGRIDLET_SUBMIT:            DataGridlet dg = (DataGridlet) ev.get_data();            receiveDataGridlet(dg);

⌨️ 快捷键说明

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