root / trunk / code / projects / warehouse / Scheduler.cpp @ 2002
History | View | Annotate | Download (2.54 KB)
1 | 2002 | jmcarrol | #include "Scheduler.h" |
---|---|---|---|
2 | |||
3 | using namespace std; |
||
4 | |||
5 | /** @Brief: Initialize data structures for the scheduler. */
|
||
6 | Scheduler::Scheduler() |
||
7 | { |
||
8 | WH_Robot rob1(/*params*/);
|
||
9 | WH_Robot rob2(/*params*/);
|
||
10 | WH_Robot rob3(/*params*/);
|
||
11 | |||
12 | robots.insert(rob1); |
||
13 | robots.insert(rob2); |
||
14 | robots.insert(rob3); |
||
15 | |||
16 | create_orders(); |
||
17 | } |
||
18 | |||
19 | /** @Brief: Free allocatetd memory. */
|
||
20 | Scheduler::~Scheduler() |
||
21 | { |
||
22 | delete rob1;
|
||
23 | delete rob2;
|
||
24 | delete rob3;
|
||
25 | |||
26 | delete robots;
|
||
27 | delete unassignedOrders;
|
||
28 | delete assigned Orders;
|
||
29 | delete waitingRobots;
|
||
30 | } |
||
31 | |||
32 | /** @Brief: Add robot to the waiting queue.
|
||
33 | * A robot calls this function with itself
|
||
34 | * and gets pushed on a list of waiting robots.
|
||
35 | * When a task is availaible the scheduler, removes
|
||
36 | * the robot of the waiting queue, and gives it a
|
||
37 | * task.
|
||
38 | */
|
||
39 | void Scheduler::get_task(WH_Robot r)
|
||
40 | { |
||
41 | waitingRobots.push(r); |
||
42 | } |
||
43 | |||
44 | /** @Brief: Statically add orders to the Priority Queue Wrapper.*/
|
||
45 | void Scheduler::create_orders()
|
||
46 | { |
||
47 | Order a(1,0,0,101,0,0,0); |
||
48 | Order b(2,0,0,11,0,0,0); |
||
49 | Order c(3,0,0,91,0,0,0); |
||
50 | Order d(4,0,0,21,0,0,0); |
||
51 | Order e(5,0,0,41,0,0,0); |
||
52 | |||
53 | unassignedOrders.insert(a); |
||
54 | unassignedOrders.insert(b); |
||
55 | unassignedOrders.insert(c); |
||
56 | unassignedOrders.insert(d); |
||
57 | unassignedOrders.insert(e); |
||
58 | } |
||
59 | |||
60 | /** @Brief: This is a confirmation that the task is complete.
|
||
61 | This function removes the order from assignedOrders. */
|
||
62 | void Scheduler::task_complete(Order o)
|
||
63 | { |
||
64 | for (int i=0; i< assignedOrders.size(); i++) |
||
65 | { |
||
66 | if (assignedOrders[i].getid()==o.getid())
|
||
67 | { |
||
68 | assignedOrders.erase(assignedOrders.begin()+i); |
||
69 | } |
||
70 | } |
||
71 | } |
||
72 | |||
73 | /** @Brief: This is a confirmation that the task failed.
|
||
74 | This function places the order back on the PQWrapper. */
|
||
75 | void Scheduler::task_failed(Order o)
|
||
76 | { |
||
77 | task_complete(o); |
||
78 | unassignedOrders.insert(o); |
||
79 | } |
||
80 | |||
81 | /** @Brief: Do a waiting dance. */
|
||
82 | void Scheduler::waiting_dance()
|
||
83 | { |
||
84 | |||
85 | } |
||
86 | |||
87 | /** @Brief: The scheduling algorithm. Picks a task and pops from the PQWrapper. */
|
||
88 | Order Scheduler::get_next_item() |
||
89 | { |
||
90 | Time time = ros::TIME_MAX; |
||
91 | |||
92 | Order best; |
||
93 | for(int i=0; i<unassigned_orders.arraySize(); i++) |
||
94 | { |
||
95 | Order order = unassigned_orders.peek(i); |
||
96 | |||
97 | if(order.orderDeadline < time) //TODO: use another function |
||
98 | { |
||
99 | best = order; |
||
100 | time = order.orderDeadline; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | } |
||
105 | |||
106 | /** @Brief: Continuously checks for waiting robots. If no robots are waiting,
|
||
107 | this function calls the waiting_dance() function. */
|
||
108 | void Scheduler::run()
|
||
109 | { |
||
110 | while (ok())
|
||
111 | { |
||
112 | while(waitingRobots.empty() || unassignedOrders.arraySize()==0) |
||
113 | waiting_dance(); |
||
114 | |||
115 | waitingRobots.front().set_task(get_next_item()); |
||
116 | waitingRobots.pop(); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | |||
121 |