?? gridsim.java
字號:
int[] array = (int[]) ev.get_data(); result = array[RESULT]; } catch (Exception e) { result = -1; } return result; } /** * Gets a Gridlet belong to the first event <b>CURRENTLY</b> waiting in this * entity's deferred queue (incoming buffer). * If there are no events, then wait indefinitely for an event to arrive. * @return A Gridlet object or <tt>null</tt> if an error occurs. * @deprecated As of GridSim 2.1, replaced by {@link #gridletReceive()} * @pre $none * @post $none */ protected Gridlet GridletReceive() { return gridletReceive(); } /** * Gets a Gridlet belong to the first event <b>CURRENTLY</b> waiting in this * entity's deferred queue (incoming buffer). * If there are no events, then wait indefinitely for an event to arrive. * @return A Gridlet object or <tt>null</tt> if an error occurs. * @pre $none * @post $none */ protected Gridlet gridletReceive() { Sim_event ev = new Sim_event(); // waiting for a response from the GridResource entity Sim_type_p tag = new Sim_type_p(GridSimTags.GRIDLET_RETURN); super.sim_get_next(tag, ev); // wait for the correct event type Gridlet gl = null; try { gl = (Gridlet) ev.get_data(); } catch (ClassCastException c) { gl = null; } catch (Exception e) { gl = null; } return gl; } /** * Gets a Gridlet belong to the first event <b>CURRENTLY</b> waiting in this * entity's deferred queue (incoming buffer). * If there are no events, then wait indefinitely for an event to arrive. * @param gridletId a Gridlet ID * @param userId a user ID * @param resId a grid resource ID * @return A Gridlet object or <tt>null</tt> if an error occurs. * @pre gridletId >= 0 * @pre userId > 0 * @pre resId > 0 * @post $none */ protected Gridlet gridletReceive(int gridletId, int userId, int resId) { String errorMsg = super.get_name() + ".gridletReceive(): "; boolean valid = validateValue(errorMsg, gridletId, userId, resId); if (valid == false) { return null; } // waiting for a response from the GridResource entity FilterGridlet tag = new FilterGridlet(gridletId, userId, resId); // only look for this type of ack for same Gridlet ID Sim_event ev = new Sim_event(); super.sim_get_next(tag, ev); Gridlet gl = null; try { gl = (Gridlet) ev.get_data(); } catch (ClassCastException c) { gl = null; } catch (Exception e) { gl = null; } return gl; } /** * Gets a Gridlet belong to the first event <b>CURRENTLY</b> waiting in this * entity's deferred queue (incoming buffer). * If there are no events, then wait indefinitely for an event to arrive. * @param gridletId a Gridlet ID * @param resId a grid resource ID * @return A Gridlet object or <tt>null</tt> if an error occurs. * @pre gridletId >= 0 * @pre resId > 0 * @post $none */ protected Gridlet gridletReceive(int gridletId, int resId) { String errorMsg = super.get_name() + ".gridletReceive(): "; boolean valid = validateValue(errorMsg,gridletId,super.get_id(),resId); if (valid == false) { return null; } // waiting for a response from the GridResource entity FilterGridlet tag = new FilterGridlet(gridletId, resId); // only look for this type of ack for same Gridlet ID Sim_event ev = new Sim_event(); super.sim_get_next(tag, ev); Gridlet gl = null; try { gl = (Gridlet) ev.get_data(); } catch (ClassCastException c) { gl = null; } catch (Exception e) { gl = null; } return gl; } /** * Sends a Gridlet based on their event tag to the destination resource ID. * @param errorMsg a message containing which operation it belongs to * @param gridletId a Gridlet ID * @param userId a Gridlet's user or owner ID * @param resourceId a GridResource ID * @param delay sending delay time * @param tag event tag (such as GridSimTags.GRIDLET_PAUSE, etc...) * @param ack denotes whether want to have an acknowledgment or not * @return <tt>true</tt> if the Gridlet has been sent successfully, * <tt>false</tt> otherwise * @pre errorMsg != null * @pre gridletId > 0 * @pre userId > 0 * @pre resourceId > 0 * @pre delay >= 0.0 * @pre tag > 0 * @post $result = true || false */ private boolean sendGridlet(String errorMsg, int gridletId, int userId, int resourceId, double delay, int tag, boolean ack) { boolean valid = validateValue(errorMsg, gridletId, userId, resourceId); if (valid == false || delay < 0.0) { return false; } int size = 14; // size of having 3 ints + 2 bytes overhead int[] array = new int[ARRAY_SIZE]; array[0] = gridletId; array[1] = userId; array[2] = NOT_FOUND; // this index is only used by gridletMove() // if an ack is required, then change the tag int newTag = tag; if (ack == true) { switch (tag) { case GridSimTags.GRIDLET_PAUSE: newTag = GridSimTags.GRIDLET_PAUSE_ACK; break; case GridSimTags.GRIDLET_RESUME: newTag = GridSimTags.GRIDLET_RESUME_ACK; break; default: break; } } // send this Gridlet send(super.output, delay, newTag, new IO_data(array, size, resourceId)); return true; } /** * Performs validation of the given parameters before sending a Gridlet * to a GridResource entity * @param msg a message containing which operation it belongs to * @param gridletId a Gridlet ID * @param userId a Gridlet's user or owner ID * @param resourceId a GridResource ID * @return <tt>true</tt> if the validation has passed successfully, * <tt>false</tt> otherwise * @pre msg != null * @pre gridletId > 0 * @pre userId > 0 * @pre resourceId > 0 * @post $result = true || false */ private boolean validateValue(String msg, int gridletId, int userId, int resourceId) { boolean valid = true; // Gridlet ID must be 0 or positive if (gridletId < 0) { valid = false; System.out.println(msg + "Error - Gridlet ID must be >= 0, not " + gridletId); } // User ID must be 0 or positive if (userId < 0) { valid = false; System.out.println(msg + "Error - User ID must be >= 0, not " + userId); } // GridResource ID must be 0 or positive if (resourceId < 0) { valid = false; System.out.println(msg + "Error - GridResource ID must be >= 0, not " + resourceId); } // if a grid resource ID doesn't exist in GIS list if (gis_.isResourceExist(resourceId) == false) { valid = false; System.out.println(msg + "Error - GridResource ID #" + resourceId + " doesn't exist"); } return valid; } /** * Cancels a Gridlet that is currently executing in a given GridResource * ID <tt>with</tt> a delay. <br> * <b>NOTE:</b> Canceling a Gridlet operation can take a long time over a * slow network if the Gridlet size is big. * @param gl a Gridlet object to be canceled * @param resourceId an unique resource ID * @param delay delay time or <tt>0.0</tt> if want to cancel NOW * @return the canceled Gridlet or <tt>null</tt if this operation fails. * If a Gridlet has <tt>finished</tt> in time of cancellation, then * this method will return the finished Gridlet. * Canceling a Gridlet can be failed for the one or more * following reasons: * <ul> * <li> if a GridResource ID doesn't exist * <li> if a Gridlet ID doesn't exist * <li> if a Gridlet's user ID doesn't exist * <li> if the delay time is negative * <li> if a Gridlet object is <tt>null</tt> or empty; * </ul> * @pre gl != null * @pre resourceId >= 0 * @pre delay >= 0.0 * @post $none */ protected Gridlet gridletCancel(Gridlet gl, int resourceId, double delay) { if (gl == null || delay < 0.0) { return null; } Gridlet obj = gridletCancel( gl.getGridletID(), gl.getUserID(), resourceId, delay ); return obj; } /** * Cancels a Gridlet that is currently executing in a given GridResource * ID <tt>with</tt> a delay. <br> * <b>NOTE:</b> Canceling a Gridlet operation can be slow over a slow * network if the Gridlet size is big. * @param gridletId a Gridlet ID * @param userId the user or owner ID of this Gridlet * @param resourceId an unique resource ID to which this Gridlet was * previously sent to * @param delay delay time or <tt>0.0</tt> if want to cancel NOW * @return the canceled Gridlet or <tt>null</tt if this operation fails. * If a Gridlet has <tt>finished</tt> in time of cancellation, then * this method will return the finished Gridlet. * Canceling a Gridlet can be failed for the one or more * following reasons: * <ul> * <li> if a GridResource ID doesn't exist * <li> if a Gridlet ID doesn't exist * <li> if a Gridlet's user ID doesn't exist * <li> if the delay time is negative * </ul> * @pre gridletId >= 0 * @pre userId >= 0 * @pre resourceId >= 0 * @pre delay >= 0.0 * @post $none */ protected Gridlet gridletCancel(int gridletId, int userId, int resourceId, double delay) { Gridlet gl = null; String errorMsg = super.get_name() + ".gridletCancel(): "; try { boolean valid = sendGridlet(errorMsg, gridletId, userId, resourceId, delay, GridSimTags.GRIDLET_CANCEL, false); if (valid == true) { // waiting for a response from the GridResource entity FilterGridlet tag = new FilterGridlet(gridletId, resourceId); tag.setTag(GridSimTags.GRIDLET_CANCEL); // only look for this type of ack for same Gridlet ID Sim_event ev = new Sim_event(); super.sim_get_next(tag, ev); gl = (Gridlet) ev.get_data(); // if a gridlet comes with a failed status, it means that // a resource could not find the gridlet if (gl.getGridletStatus() == Gridlet.FAILED) { gl = null; } } } catch (Sim_exception e) { gl = null; System.out.println(errorMsg + "Error occurs.");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -