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

📄 advancereservation.java

📁 一个非常著名的网格模拟器,能够运行网格调度算法!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* * 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 * * $Id: AdvanceReservation.java,v 1.40 2005/10/21 09:23:20 anthony Exp $ */package gridsim;import java.util.*;import gridsim.*;import gridsim.net.*;import gridsim.filter.*;import eduni.simjava.Sim_type_p;import eduni.simjava.Sim_event;/** * This class handles all Advanced Reservation (AR) functionalities, such as * create, modify, cancel and query. There are two types of reservation: * <ul> *     <li> advanced reservation: requests a reservation in future time. *     <li> immediate reservation: requests a reservation immediately, i.e. *          the current time is used as the start time with/without specifying *          duration or end time. <br> *          In this class, immediate reservation can be done by one of the *          following ways: *          <ol> *          <li> <tt>start time = 0 (in long) or null (in Calendar object)</tt> *               <b>and</b> <tt>duration > 0</tt>.<br> *               If successful, expiry time set by a resource would be: *               <tt>expiry time = current time + duration.</tt> *               <br><br> *          <li> <tt>start time = 0 (in long) or null (in Calendar object)</tt> *               <b>and</b> <tt>duration = 0</tt>.<br> *               This means a reservation is running as long as there are empty *               PEs available. <br> *               <b>NOTE:</b> using this approach, a reservation *               is having a risk of being pre-empted or terminated by a *               resource scheduler when new AR requests come. In addition, *               due to complexity, a resource's scheduler might not support *               this method. Finally, <tt>expiry time = 0</tt> since a *               resource's scheduler can not determine it.<br><br> *               Instead of using this approach, you can directly *               submit a job or a Gridlet object using *               {@link gridsim.GridSim#gridletSubmit(Gridlet, int)} method. *          </ol> * </ul> * <p> * The AR methods in this class automatically handle the * communication to/from a destinated grid resource entity. * To use AR functionalities, you need to create a subclass of this object, i.e. * creating a new class that inherits this object. This approach is preferable * because the subclass is responsible for collecting Gridlets back from * a resource entity. * <p> * Important properties regarding to this entity are time and * time zone. Time in this entity, GridResource entities and simulation clock * are all * relative to a given init time. In other words, future time should be greater * than init time given during * {@link gridsim.GridSim#init(int, Calendar, boolean)} method.<br> * <p> * Imagine the following scenarios: * <ul> *      <li> Simulation init time: *            <tt>Calendar cal = Calendar.getInstance();</tt> <br> *           Assume, time and date is <tt>16:00:00 02 May 2004.</tt> *      <li> Simulation clock: <tt>double clock = GridSim.clock();</tt><br> *           Assume the simulation has been run for 45 seconds (according to *           simulation or SimJava clock not the actual running of GridSim). *           So the current simulation time and date is *           <tt>16:00:45 02 May 2004.</tt> *      <li> A user entity reserves a slot at time *           <tt>15:00:00 02 May 2004.</tt><br> *           Result: rejected instantly since the time has expired. *      <li> A user entity reserves a slot at time *          <tt>16:10:00 02 May 2004.</tt><br> *           Result: accepted or rejected depending on a resource availability. *           If it is accepted, assume that expiry period for a reservation is *           5 minutes starting from the current time, then a user must *           commit before time and date <tt>16:05:45 02 May 2004.</tt> * </ul> * <p> * Time can be represented as <tt>Calendar</tt> object or <tt>long</tt> * in milli seconds. * <p> * User and GridResource entities might have different time zone. Therefore, * when communicating between these two entities, upon arrival, the time will * be converted by an individual entity into its local time. * * @author       Anthony Sulistio * @since        GridSim Toolkit 3.0 * @invariant $none * @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[], String) * @see gridsim.GridSim#init(int, Calendar, boolean) */public class AdvanceReservation extends GridSim{    private final int MAX_ID = 4;   // number of variables    private final int SIZE_ARRAY = MAX_ID*5;   // size of int[MAX_ID] array    private long initTime_;        // time in millisec the simulation starts    private double timeZone_;      // this class or user's time zone    private int transactionID_;    // transaction ID for uniqueness    private ArrayList booking_;    // to store a successful reservation    private final int MILLI_SEC = 1000;       // 1 sec = 1,000 milli seconds    /** 1 Hour representation in milliseconds, i.e. 1 hour = 1000*60*60 */    public static final int HOUR = 1000 * 60 * 60;    ////////////////// STATIC METHODS /////////////////////////////////////    /**     * Converts local time from one time zone to another     * @param time    local current time in milliseconds     * @param fromZone   local time zone of range [GMT-12 ... GMT+13]     * @param toZone     destination time zone of range [GMT-12 ... GMT+13]     * @return a converted time in milliseconds or <tt>-1</tt> if a time zone     *         is not within [GMT-12 ... GMT+13]     * @pre $none     * @post $none     */    public static long convertTimeZone(long time,double fromZone,double toZone)    {        int MIN_GMT = -12;        int MAX_GMT = 13;        if (fromZone < MIN_GMT || fromZone > MAX_GMT) {            return -1;        }        if (toZone < MIN_GMT || toZone > MAX_GMT) {            return -1;        }        double diff = toZone - fromZone;        return time + (int) (diff * HOUR);    }    /**     * Checks whether a given time zone is valid or not     * @param timeZone  a time zone     * @return <tt>true</tt> if a time zone is valid, or <tt>false</tt>     *         otherwise     * @pre $none     * @post $none     */    public static boolean validateTimeZone(double timeZone)    {        int MIN_GMT = -12;        int MAX_GMT = 13;        if (timeZone < MIN_GMT || timeZone > MAX_GMT) {            return false;        }        double decimal = timeZone - (int) timeZone;        if (decimal >= 0.60) {            return false;        }        return true;    }    /**     * Converts a reservation result from integer into a String.     * The result is one of GridSimTags.AR_CREATE_XXXX tags,     * where XXXX = specific tag name.     * @param result   a result for a new reservation request     * @return a String representation of the reservation result     *  or <tt>null</tt> if invalid result.     * @pre $none     * @post $none     * @see gridsim.GridSimTags     */    public static String getCreateResult(int result)    {        String str = null;        switch (result)        {            case GridSimTags.AR_CREATE_ERROR:                str = "AR_CREATE_ERROR";                break;            case GridSimTags.AR_CREATE_ERROR_INVALID_START_TIME:                str = "AR_CREATE_ERROR_INVALID_START_TIME";                break;            case GridSimTags.AR_CREATE_ERROR_INVALID_END_TIME:                str = "AR_CREATE_ERROR_INVALID_END_TIME";                break;            case GridSimTags.AR_CREATE_ERROR_INVALID_DURATION_TIME:                str = "AR_CREATE_ERROR_INVALID_DURATION_TIME";                break;            case GridSimTags.AR_CREATE_ERROR_INVALID_NUM_PE:                str = "AR_CREATE_ERROR_INVALID_NUM_PE";                break;            case GridSimTags.AR_CREATE_ERROR_INVALID_RESOURCE_ID:                str = "AR_CREATE_ERROR_INVALID_RESOURCE_ID";                break;            case GridSimTags.AR_CREATE_ERROR_INVALID_RESOURCE_NAME:                str = "AR_CREATE_ERROR_INVALID_RESOURCE_NAME";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_CANT_SUPPORT:                str = "AR_CREATE_FAIL_RESOURCE_CANT_SUPPORT";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_NOT_ENOUGH_PE:                str = "AR_CREATE_FAIL_RESOURCE_NOT_ENOUGH_PE";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_1_SEC:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_1_SEC";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_5_SECS:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_5_SECS";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_10_SECS:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_10_SECS";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_15_SECS:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_15_SECS";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_30_SECS:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_30_SECS";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_45_SECS:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_45_SECS";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_1_MIN:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_1_MIN";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_5_MINS:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_5_MINS";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_10_MINS:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_10_MINS";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_15_MINS:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_15_MINS";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_30_MINS:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_30_MINS";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_45_MINS:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_45_MINS";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_1_HOUR:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_1_HOUR";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_5_HOURS:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_5_HOURS";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_10_HOURS:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_10_HOURS";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_15_HOURS:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_15_HOURS";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_30_HOURS:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_30_HOURS";                break;            case GridSimTags.AR_CREATE_FAIL_RESOURCE_FULL_IN_45_HOURS:                str = "AR_CREATE_FAIL_RESOURCE_FULL_IN_45_HOURS";                break;            default:                break;        }        return str;    }    /**     * Converts a reservation result from integer into a String.     * The result is one of GridSimTags.AR_CANCEL_XXXX tags,     * where XXXX = specific tag name.     * @param result   a result for cancel a reservation     * @return a String representation of the reservation result     *  or <tt>null</tt> if invalid result.     * @pre $none     * @post $none     * @see gridsim.GridSimTags     */    public static String getCancelResult(int result)    {        String str = null;        switch (result)        {            case GridSimTags.AR_CANCEL_ERROR:                str = "AR_CANCEL_ERROR";                break;            case GridSimTags.AR_CANCEL_FAIL:                str = "AR_CANCEL_FAIL";                break;            case GridSimTags.AR_CANCEL_FAIL_INVALID_BOOKING_ID:                str = "AR_CANCEL_FAIL_INVALID_BOOKING_ID";                break;            case GridSimTags.AR_CANCEL_FAIL_GRIDLET_FINISHED:                str = "AR_CANCEL_FAIL_GRIDLET_FINISHED";                break;            case GridSimTags.AR_CANCEL_SUCCESS:                str = "AR_CANCEL_SUCCESS";                break;            case GridSimTags.AR_CANCEL_ERROR_RESOURCE_CANT_SUPPORT:                str = "AR_CANCEL_ERROR_RESOURCE_CANT_SUPPORT";                break;            default:                break;        }        return str;    }    /**     * Converts a reservation result from integer into a String.     * The result is one of GridSimTags.AR_STATUS_XXXX tags,     * where XXXX = specific tag name.     * @param result   a result for query a reservation     * @return a String representation of the reservation result     *  or <tt>null</tt> if invalid result.     * @pre $none     * @post $none     * @see gridsim.GridSimTags     */    public static String getQueryResult(int result)    {        String str = null;        switch (result)        {            case GridSimTags.AR_STATUS_NOT_STARTED:                str = "AR_STATUS_NOT_STARTED";                break;            case GridSimTags.AR_STATUS_ACTIVE:

⌨️ 快捷键说明

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