[104] | 1 | package example.localplugin; |
---|
| 2 | |
---|
| 3 | import gridsim.Gridlet; |
---|
| 4 | |
---|
| 5 | import java.util.ArrayList; |
---|
| 6 | import java.util.Arrays; |
---|
| 7 | import java.util.List; |
---|
| 8 | import java.util.Properties; |
---|
| 9 | |
---|
| 10 | import schedframe.scheduling.Queue; |
---|
| 11 | import schedframe.scheduling.TaskInterface; |
---|
| 12 | import schedframe.scheduling.events.SchedulingEvent; |
---|
| 13 | import schedframe.scheduling.plugin.local.ResourceUnitsManagerInterface; |
---|
| 14 | import simulator.utils.MergeSort; |
---|
| 15 | |
---|
| 16 | public class SJF_EBFLocalPlugin extends BaseLocalPlugin { |
---|
| 17 | |
---|
| 18 | public SJF_EBFLocalPlugin(){ |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | public void schedule(SchedulingEvent event, |
---|
| 22 | List<? extends TaskInterface<?>> inExecution, |
---|
| 23 | List<? extends Queue<? extends TaskInterface<?>>> queues, |
---|
| 24 | ResourceUnitsManagerInterface unitsManagerInterface) { |
---|
| 25 | |
---|
| 26 | // do this trick to make add() method available |
---|
| 27 | List <TaskInterface<?>> execute = (List<TaskInterface<?>>) inExecution; |
---|
| 28 | |
---|
| 29 | // chose the events types to serve. |
---|
| 30 | // Different actions for different events are possible. |
---|
| 31 | switch(event.getType()){ |
---|
| 32 | //case START_TASK_EXECUTION: |
---|
| 33 | //case TASK_FINISHED: |
---|
| 34 | case TIMER: |
---|
| 35 | // our tasks are placed only in first queue (see BaseLocalPlugin.placeTasksInQueues() method) |
---|
| 36 | Queue<? extends TaskInterface<?>> q = queues.get(0); |
---|
| 37 | |
---|
| 38 | sortTasksInQueue(q); |
---|
| 39 | |
---|
| 40 | // check all tasks in queue |
---|
| 41 | for(int i = 0; i < q.size(); i++){ |
---|
| 42 | TaskInterface<?> task = q.get(i); |
---|
| 43 | // if status of the tasks in READY |
---|
| 44 | if(task.getStatus() == Gridlet.READY){ |
---|
| 45 | // then try to execute this task. Add it the execute list. |
---|
| 46 | if(execute.add(task)){ |
---|
| 47 | // if task started successfully, then remove it from the queue. |
---|
| 48 | q.remove(i); |
---|
| 49 | i--; |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | } |
---|
| 53 | break; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | public String getName() { |
---|
| 59 | return getClass().getName(); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | public void init(Properties properties) { |
---|
| 63 | // no extra initialization is expected. |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | public void sortTasksInQueue(Queue<? extends TaskInterface<?>> q) { |
---|
| 67 | |
---|
| 68 | TaskInterface<?>[] arrayQueue = new TaskInterface<?>[q.size()]; |
---|
| 69 | int i = 0; |
---|
| 70 | for(TaskInterface<?> task: q){ |
---|
| 71 | arrayQueue[i] = task; |
---|
| 72 | i++; |
---|
| 73 | } |
---|
| 74 | MergeSort mergeSort = new MergeSort(); |
---|
| 75 | mergeSort.initSort(arrayQueue, "getLength", true); |
---|
| 76 | q.clear(); |
---|
| 77 | q.addAll(new ArrayList(Arrays.asList(arrayQueue))); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | } |
---|