📄 arsimplespaceshared.java
字号:
* @param reservationID a reservation ID * @param senderID a sender ID * @param sendTag a tag to send the result back to user * @pre reservationID > 0 * @pre senderID > 0 * @post $none */ public void handleCancelReservation(int reservationID, int senderID, int sendTag) { // case 1: check whether a reservation exists or not int result = 0; int index = super.searchReservation(reservList_, reservationID); if (index == NOT_FOUND) { // check on expiry list index = super.searchReservation(expiryList_, reservationID); if (index == NOT_FOUND) { result = GridSimTags.AR_CANCEL_FAIL_INVALID_BOOKING_ID; } else { result = GridSimTags.AR_CANCEL_SUCCESS; } super.replyCancelReservation(senderID, sendTag, result); return; } // gets the reservation object ARObject obj = (ARObject) reservList_.get(index); // case 2: if a reservation hasn't been committed int totalGridlet = 0; if (obj.hasCommitted() == false) { totalGridlet = 0; } else { // if a reservation has been committed totalGridlet = obj.getTotalGridlet(); } // case 3: if a reservation has been committed and Gridlets are // submitted then need to cancel all the Gridlets for (int i = 0; i < totalGridlet; i++) { ResGridlet rgl = cancelReservationGridlet(reservationID); // if for some reason, can't find the gridlet if (rgl == null) { result = GridSimTags.AR_CANCEL_FAIL; super.replyCancelReservation(senderID, sendTag, result); return; } rgl.finalizeGridlet(); super.sendCancelGridlet(GridSimTags.GRIDLET_CANCEL,rgl.getGridlet(), rgl.getGridletID(), senderID); } // change the status and remove the reservation obj.setStatus(GridSimTags.AR_STATUS_CANCELED); reservList_.remove(obj); expiryList_.add(obj); // send back the result of this operation result = GridSimTags.AR_CANCEL_SUCCESS; super.replyCancelReservation(senderID, sendTag, result); } /** * Handles a cancel reservation request. This method * cancels only for a given Gridlet ID of a reservation. * @param reservationID a reservation ID * @param senderID a sender ID * @param gridletID a Gridlet ID * @param sendTag a tag to send the result back to user * @pre reservationID > 0 * @pre senderID > 0 * @post $none */ public void handleCancelReservation(int reservationID, int senderID, int gridletID, int sendTag) { // case 1: check whether a reservation exists or not int result = 0; int index = super.searchReservation(reservList_, reservationID); if (index == NOT_FOUND) { // check on expiry list index = super.searchReservation(expiryList_, reservationID); if (index == NOT_FOUND) { result = GridSimTags.AR_CANCEL_FAIL_INVALID_BOOKING_ID; } else { result = GridSimTags.AR_CANCEL_SUCCESS; } super.replyCancelReservation(senderID, sendTag, result); return; } // cancel a particular Gridlet result = cancelReservation(gridletID, senderID); // send the result back super.replyCancelReservation(senderID, sendTag, result); } /** * Cancels a particular Gridlet of a reservation * @param gridletID a gridlet ID * @param userID a Gridlet user ID * @return a cancel reservation tag * @pre gridletID > 0 * @pre userID > 0 * @post $none */ private int cancelReservation(int gridletID, int userID) { int result = 0; // if exist, then cancel a gridlet ResGridlet rgl = cancel(gridletID, userID); // if the Gridlet is not found if (rgl == null) { result = GridSimTags.AR_CANCEL_FAIL; return result; } // if the Gridlet has finished beforehand then prints an error msg if (rgl.getGridletStatus() == Gridlet.SUCCESS) { result = GridSimTags.AR_CANCEL_FAIL_GRIDLET_FINISHED; } else { result = GridSimTags.AR_CANCEL_SUCCESS; } // sends the Gridlet back to sender rgl.finalizeGridlet(); super.sendCancelGridlet(GridSimTags.GRIDLET_CANCEL, rgl.getGridlet(), gridletID, userID); return result; } /** * Cancels a particular Gridlet of a reservation * @param reservationID a reservation ID * @return a ResGridlet object * @pre reservationID > 0 * @post $none */ private ResGridlet cancelReservationGridlet(int reservationID) { ResGridlet rgl = null; // Find in EXEC List first int found = findGridlet(gridletInExecList_, reservationID); if (found >= 0) { // update the gridlets in execution list up to this point in time updateGridletProcessing(); // Get the Gridlet from the execution list rgl = (ResGridlet) gridletInExecList_.remove(found); // if a Gridlet is finished upon cancelling, then set it to success // instead. if (rgl.getRemainingGridletLength() == 0.0) { rgl.setGridletStatus(Gridlet.SUCCESS); } else { rgl.setGridletStatus(Gridlet.CANCELED); } // Set PE on which Gridlet finished to FREE super.resource_.setStatusPE( PE.FREE, rgl.getMachineID(), rgl.getPEID() ); allocateQueueGridlet(); return rgl; } // Find in QUEUE list found = findGridlet(gridletQueueList_, reservationID); if (found >= 0) { rgl = (ResGridlet) gridletQueueList_.remove(found); rgl.setGridletStatus(Gridlet.CANCELED); return rgl; } // if not found, then find in the Paused list found = findGridlet(gridletPausedList_, reservationID); if (found >= 0) { rgl = (ResGridlet) gridletPausedList_.remove(found); rgl.setGridletStatus(Gridlet.CANCELED); return rgl; } // if not found, then find in AR waiting list found = findGridlet(gridletWaitingList_, reservationID); if (found >= 0) { rgl = (ResGridlet) gridletWaitingList_.remove(found); rgl.setGridletStatus(Gridlet.CANCELED); return rgl; } // if not found rgl = null; return rgl; } /** * Search for a particular reservation in a data structure * @param list a data structure * @param reservationID a reservation ID * @return location in the data structure or <tt>-1</tt> if not found * @pre list != null * @post $none */ private int findGridlet(LinkedList list, int reservationID) { ResGridlet rgl = null; int found = -1; // means the Gridlet is not in the list try { // Search through the list to find the given Gridlet object int i = 0; Iterator iter = list.iterator(); while ( iter.hasNext() ) { rgl = (ResGridlet) iter.next(); if (rgl.getReservationID() == reservationID) { found = i; break; } i++; } } catch (Exception e) { System.out.println(super.get_name() + ".findGridlet(): Exception error occurs."); System.out.println( e.getMessage() ); } return found; } /** * Handles a query busy time request (NOTE: <b>NOT YET SUPPORTED</b>). * @param from starting period time * @param to ending period time * @param senderID a sender or user ID * @param sendTag a tag to send to the user * @pre from > 0 * @pre to > 0 * @pre senderID > 0 * @post $none */ public void handleQueryBusyTime(long from, long to, int senderID, int sendTag, double userTimeZone) { System.out.println(super.get_name() + ".handleQueryBusyTime(): not supported at the moment."); super.replyTimeReservation(senderID, sendTag, null, userTimeZone); } /** * Handles a query free time request (NOTE: <b>NOT YET SUPPORTED</b>). * @param from starting period time * @param to ending period time * @param senderID a sender or user ID * @param sendTag a tag to send to the user * @pre from > 0 * @pre to > 0 * @pre senderID > 0 * @post $none */ public void handleQueryFreeTime(long from, long to, int senderID, int sendTag, double userTimeZone) { System.out.println(super.get_name() + ".handleQueryFreeTime(): not supported at the moment."); super.replyTimeReservation(senderID, sendTag, null, userTimeZone); } /** * Handles a commit reservation request. * This method commits a reservation only. Gridlets are submitted using * {@link #handleCommitReservation(int, int, int, Gridlet)} or * {@link #handleCommitReservation(int, int, int, GridletList)} method. * @param reservationID a reservation ID * @param senderID a sender or user ID * @param sendTag a tag to send to the user * @pre reservationID > 0 * @pre senderID > 0 * @post $none */ public void handleCommitOnly(int reservationID, int senderID, int sendTag) { int result = doCommitReservation(reservationID, null); super.replyCommitReservation(senderID, sendTag, result); } /** * Handles a commit reservation request. * This method commits a reservation and submits a Gridlet to be processed * as well. * @param reservationID a reservation ID * @param senderID a sender or user ID * @param sendTag a tag to send to the user * @param list a list of Gridlet object * @pre reservationID > 0 * @pre senderID > 0 * @pre list != null * @post $none */ public void handleCommitReservation(int reservationID, int senderID, int sendTag, GridletList list) { int result = doCommitReservation(reservationID, list); super.replyCommitReservation(senderID, sendTag, result); } /** * Handles a commit reservation request. * This method commits a reservation and submits a list of Gridlets * to be processed as well. * @param reservationID a reservation ID * @param senderID a sender or user ID * @param sendTag a tag to send to the user * @param gridlet a Gridlet object * @pre reservationID > 0 * @pre senderID > 0 * @pre gridlet != null * @post $none */ public void handleCommitReservation(int reservationID, int senderID, int sendTag, Gridlet gridlet) { int result = doCommitReservation(reservationID, gridlet); super.replyCommitReservation(senderID, sendTag, result); } /** * Internal method that handles different type of commit reservation * @param reservationID a reservation ID * @param obj a generic object, might contain Gridlet or * GridletList object. * @return a commit tag * @pre reservationID > 0 * @post $none */ private int doCommitReservation(int reservationID, Object obj) { // search the reservation first int index = super.searchReservation(reservList_, reservationID);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -