-
Notifications
You must be signed in to change notification settings - Fork 0
/
DispatcherPackage.cpp
107 lines (99 loc) · 4.08 KB
/
DispatcherPackage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//----------------------------------------------------------------------------
// Filename: DispatcherPackage.cpp
// Date: 12 November 2013
// Description: implementation for Dispatcher class.
//----------------------------------------------------------------------------
#include "Dispatcher.h"
//----------------------------------------------------------------------------
// Dispatcher(): Initializes the Dispatcher.
// Input: array of Assembly lines, an their size.
// Output: None
//----------------------------------------------------------------------------
Dispatcher::Dispatcher(AssemblyLine *list, int size){
this->aLineList = list;
this->length_aLineList = size;
}
//----------------------------------------------------------------------------
// dispatch(): processes a the list of incoming Packages.
// Input: A queue of packages and a count of packages.
// Output: None
//----------------------------------------------------------------------------
void Dispatcher::dispatch(PackageQueue * PkgBuffer, int pkgCount){
this->arrivingPkgBuffer = PkgBuffer;
timeUnit = 0;
int completedPkgCount=0;
while(completedPkgCount != pkgCount){
for (int i = 0; i < length_aLineList; i++){
completedPkgCount += al_do_work(i); //step one
al_ship(i, timeUnit); //step two
}
if (!arrivingPkgBuffer->isEmpty()){
distributeArrivingPkgs(timeUnit); //step three (critical)
}
timeUnit++;
}
}
//----------------------------------------------------------------------------
// al_do_work(int id): works on a specified assmebly line
// Input: an id. (the assembly line's id. )
// Output: int, returns 1 if a package was completed, 0 if not.
//----------------------------------------------------------------------------
int Dispatcher::al_do_work(int id){
int completedPkgCount = 0;
AssemblyLine * aL = &aLineList[id]; // easier to work with.
if (!( aL -> processingPkgBuffer.isEmpty()) ) {
if (! aL->isCurrentPkgLoaded()){
aL->loadCurrentPkg();
completedPkgCount = aL->do_work();
}else{
completedPkgCount = aL->do_work();
}
}
return completedPkgCount;
}
//----------------------------------------------------------------------------
// al_ship(int id, int timeUnit): ships in a specified assemby line.
// Input: an id (int) , the time (int)
// Output: None
//----------------------------------------------------------------------------
void Dispatcher::al_ship(int id, int timeUnit){
AssemblyLine * aL = &aLineList[id];
if (aL->isCurrentPkgLoaded()){
if(aL->isPkgCompleted(aL->currentPkg)){
aL->numUnitsProcessing -= aL->currentPkg->unit_number;
aL->shipPkg(timeUnit); // puts package in a cmpleted Pkg Buffer.
}
}
}
//----------------------------------------------------------------------------
// distributeArrivingPkgs(int timeUnit): distribute Arriving Packages into a
// an assembly line.
// Input: the time (int)
// Output: None
//----------------------------------------------------------------------------
void Dispatcher::distributeArrivingPkgs(int timeUnit){
Package *frontPkg = this->arrivingPkgBuffer->getFront();
if (frontPkg ->time_Arrived == timeUnit) {
Package newPkg = *frontPkg;
dispatch_to_assembly(newPkg); /// this is like the enqueue.
this->arrivingPkgBuffer->dequeue();
}
}
//----------------------------------------------------------------------------
// dispatch_to_assembly(Pkg): Algortithm that decides which assembly to
// dispatch the incoming Pkg.
// Input: Package newPkg.
// Output: None
//----------------------------------------------------------------------------
void Dispatcher::dispatch_to_assembly(Package newPkg){
AssemblyLine *minLine;
minLine = &aLineList[0];
for (int i = 0; i<length_aLineList; i++){
//find lowest
if ( aLineList[i].numPkgsProcessing < minLine->numPkgsProcessing) {
minLine = &aLineList[i];
}
}
minLine->processingPkgBuffer.enqueue(newPkg);
minLine->numPkgsProcessing++;
}