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

📄 regionalgiswithfailure.java

📁 中間件開發详细说明:清华大学J2EE教程讲义(ppt)-Tsinghua University J2EE tutorial lectures (ppt) [上载源码成为会员下载此源码] [成为VIP会
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                do                {                    res_num = random.nextInt(resList_.size());                    res_id_Integer = (Integer) resList_.get(res_num);                    res_id = res_id_Integer.intValue();                    resChar = getResourceCharacteristics(res_id);                    if (resChar == null)                    {                        System.out.println(super.get_name() + " resChar == null");                        isWorking = false;                    }                    else                    {                        isWorking = resChar.isWorking();                    }                } while (isWorking == false);                FailureMsg resFailure = new FailureMsg(failureLength, res_id);                resFailure.setNumMachines(numMachFailed);                // Sends the recovery time for this resource. Sends                // a deferred event to itself for that.                super.send(super.get_id(),                           GridSimTags.SCHEDULE_NOW + failureLength,                           GridSimTags.GRIDRESOURCE_RECOVERY, resFailure);                /*****************/                if (record_ == true) {                    System.out.println(super.get_name() +                       ": sends a GRIDRESOURCE_FAILURE event to the resource " +                       GridSim.getEntityName(res_id) + ". numMachFailed: " +                       numMachFailed + ". Clock: " + GridSim.clock() +                       ". Fail duration: " + (failureLength / 3600) +                       " hours. Some machines may still work or may not.");                }                /*****************/                // Send the GRIDRESOURCE_FAILURE event to the resource.                super.send(super.output, 0.0, ev.get_tag(),                           new IO_data(resFailure, Link.DEFAULT_MTU, res_id));            }        } // else (if begining)    }    /**     * This function processes an incoming event, whose tag is     * GRIDRESOURCE_RECOVERY.     * What the GIS has to do is forward the event to the failed resource,     * whose id comes with the event.     * @param ev  a Sim_event object (or an incoming event or request)     */    private void processGridResource_Recovery(Sim_event ev)    {        Object obj = ev.get_data();        if (obj instanceof FailureMsg)        {            FailureMsg resfail = (FailureMsg) ev.get_data();            int resource_id = resfail.getRes();            /***********/            if (record_ == true) {                 System.out.println(super.get_name() +                       ": sends a GRIDRESOURCE_RECOVERY to the resource " +                       GridSim.getEntityName(resource_id) +                       ". Clock: " + GridSim.clock());            }            /***********/            // Send the GRIDRESOURCE_RECOVERY event to the resource.            super.send(super.output, 0.0, ev.get_tag(),                       new IO_data(resfail, Link.DEFAULT_MTU,                                   resource_id));        } // if (obj instanceof FailureMsg)    }    /**     * This function removes a resource from the list of available resources.     * This is done because an user has detected the failure, and has notified     *  it to the GIS     * @param ev  a Sim_event object (or an incoming event or request)*/    private void processResourceFailed (Sim_event ev)    {        Object obj = ev.get_data();        if (obj instanceof Integer)        {            Integer resID_Int = (Integer) ev.get_data();            int resID = resID_Int.intValue();            if (record_ == true) {                System.out.println(super.get_name() +                   ": receives a resource failure information event. " +                   "Failed resource is " + GridSim.getEntityName(resID) +                   ". resID: " + resID + ". Clock: " + GridSim.clock());            }            removeResource(resID);        }    }    /**     * This function removes a resource from the list of available resources.     * This is done because an user has detected the failure, and has informed     * the GIS about that     * @param resID the id of the resource to be removed     */    private void removeResource (int resID)    {        for (int j = 0; j < resList_.size(); j++)        {            if (((Integer) resList_.get(j)).intValue() == resID)            {                resList_.remove(j);                if (record_ == true) {                    write("Removing", resID, GridSim.clock());                    if (resList_.size() == 0)                    {                        System.out.println(super.get_name() +                            ": No resources available at this moment. Clock: " +                            GridSim.clock());                    }                }            }        }        /***********/        if (record_ == true) {            System.out.println();            System.out.println(super.get_name() + ": Resource list after removal");            for (int j = 0; j < resList_.size(); j++)            {                System.out.println(super.get_name() + ": list["+ j +"] = " +                    GridSim.getEntityName((Integer)resList_.get(j)) );            }            System.out.println();        }        /**********/    }   /**    * Initializes the results files (put headers over each column)    */    private void initializeReportFile()    {        if (record_ == false) {            return;        }        // Initialize the results file        FileWriter fwriter = null;        try {            fwriter = new FileWriter(super.get_name(), true);        }        catch (Exception ex)        {            ex.printStackTrace();            System.out.println("Unwanted errors while opening file " +                super.get_name() + " or " + super.get_name() + "_Fin");        }        try {            fwriter.write("Event \t ResourceID \t Clock\n");        }        catch (Exception ex)        {            ex.printStackTrace();            System.out.println("Unwanted errors while writing on file " +                super.get_name() + " or " + super.get_name() + "_Fin");        }        try {            fwriter.close();        }        catch (Exception ex)        {            ex.printStackTrace();            System.out.println("Unwanted errors while closing file " +                super.get_name() + " or " + super.get_name() + "_Fin");        }    }    /**     * Write an event of this entity into a file.     * If an existing file exists, the new event will be appended at the end.     * The file name is this entity name.     *     * @param event     Values: "Removing" or "Registering" a resource     * @param resID     resource id     * @param clock     Current time     */    protected void write(String event, int resID, double clock)    {        if (record_ == false) {            return;        }        // Write into a results file        FileWriter fwriter = null;        try        {            fwriter = new FileWriter(super.get_name(), true);        }        catch (Exception ex)        {            ex.printStackTrace();            System.out.println(                    "Unwanted errors while opening file " + super.get_name());        }        try        {            fwriter.write(event + "\t" + resID + "\t" + clock + "\n");        }        catch (Exception ex)        {            ex.printStackTrace();            System.out.println(                   "Unwanted errors while writing on file " + super.get_name());        }        try        {            fwriter.close();        }        catch (Exception ex)        {            ex.printStackTrace();            System.out.println(                    "Unwanted errors while closing file " + super.get_name());        }    }    /**     * This function processes an incoming event,     * whose tag is GRIDRESOURCE_POLLING.     * What the GIS has to do is poll the resource in its     * list of available resources,     * and remove those resources which are not available anymore.     * @param ev  a Sim_event object (or an incoming event or request)     */    private void processGridResource_Polling(Sim_event ev)    {        Integer res_id_Integer;        int res_id;        AvailabilityInfo resAv = null;        // Polls the resources that are available right now, as those        // which are out of order were totally removed        for (int i = 0; i < resList_.size(); i++)        {            res_id_Integer = (Integer) resList_.get(i);            res_id = res_id_Integer.intValue();            pollResource(res_id);        }        // receive the polling back        int resListSize = resList_.size();        for (int i = 0; i < resListSize; i++)        {            do            {                super.sim_pause(50);                resAv = pollReturn();            } while (resAv == null);            res_id = resAv.getResID();            /*****************            // NOTE: this keeps printing at every n seconds interval.            if (record_ == true) {                System.out.println(super.get_name() +                           ": receives a poll response from " +                           GridSim.getEntityName(res_id) + ". resID: " + res_id +                           ". Is res available? " + resAv.getAvailability() +                           ". Clock: " + GridSim.clock());            }            ****************/            // Find the AvailabilityInfo object corresponding to the resource            // which has answered this poll request            // and, if the resource is out of order, remove it from the            // list of available resources.            for (int j = 0; j < resList_.size(); j++)            {                if (((Integer) resList_.get(j)).intValue() == res_id)                {                    // Only do anything when the res is out of order                    if (resAv.getAvailability() == false)                    {                        removeResource(res_id);                    }                }            } // for        } // for        /**********/        if (record_ == true) {            if (resList_.size() == 0) {                System.out.println(super.get_name() +                        ": After polling, no resource in the GIS. ");            }        }        /*********/        // Schedule the following polling event.        super.send(super.get_id(), GridSimTags.POLLING_TIME_GIS,                   GridSimTags.GRIDRESOURCE_POLLING);    }    /**     * Process an incoming request that uses a user-defined tag. <br>     * NOTE: This method can be overridden by its subclasses, provided     *       that they call this method first. This is required, just in case     *       this method is not empty.     *     * @param ev  a Sim_event object (or an incoming event or request)     * @pre ev != null     * @post $none     */    protected void processOtherEvent(Sim_event ev)    {        switch (ev.get_tag())        {            // Resource failure event: send a failure event to a resource.            case GridSimTags.GRIDRESOURCE_FAILURE:                processGridResource_Failure(ev);                break;                // Resource recovery event.            case GridSimTags.GRIDRESOURCE_RECOVERY:                processGridResource_Recovery(ev);                break;                // Time for polling resources            case GridSimTags.GRIDRESOURCE_POLLING:                processGridResource_Polling(ev);                break;                // A user tells the GIS that a resource is out of order            case AbstractGIS.NOTIFY_GIS_RESOURCE_FAILURE:                processResourceFailed(ev);                break;        } // switch ( ev.get_tag() )    }    /**     * Process an incoming request from other GIS entities about getting     * a list of resource IDs, that are registered to this regional GIS entity.     *     * @param ev  a Sim_event object (or an incoming event or request)     * @pre ev != null     * @post $none     */    protected void processGISResourceList(Sim_event ev)    {        if (ev == null || ev.get_data() == null) {            return;        }        Integer id = (Integer) ev.get_data();        int tag = AbstractGIS.GIS_INQUIRY_RESOURCE_RESULT;        boolean result = sendListToSender(id.intValue(), tag, resList_);        if (result == false)        {            System.out.println(super.get_name() +                ".processGISResourceList(): Warning - unable to send a list " +                "of resource IDs to sender.");        }    }    /**     * Process an incoming request from other GIS entities about getting     * a list of resource IDs supporting Advanced Reservation,     * that are registered to this regional GIS entity.     *     * @param ev  a Sim_event object (or an incoming event or request)     * @pre ev != null     * @post $none     */    protected void processGISResourceARList(Sim_event ev)    {        if (ev == null || ev.get_data() == null) {            return;        }        Integer id = (Integer) ev.get_data();        int tag = AbstractGIS.GIS_INQUIRY_RESOURCE_AR_RESULT;        boolean result = sendListToSender(id.intValue(), tag, arList_);        if (result == false)        {            System.out.println(super.get_name() +

⌨️ 快捷键说明

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