-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaintainableClosableFacility.cpp
59 lines (48 loc) · 1.81 KB
/
MaintainableClosableFacility.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
//
// Created by david on 26.11.16.
//
#include "MaintainableClosableFacility.h"
#include "FacilityInvalidStateException.h"
void MaintainableClosableFacility::open(Process *proc) {
if (isOpen()) {
throw FacilityInvalidStateException("Cannot open already opened facility");
}
this->_directingProcess = proc;
this->_isShuttingDown = false;
ClosableFacility::open();
this->_directingProcess->Passivate();
}
void MaintainableClosableFacility::close() {
if (!isOpen())
throw FacilityInvalidStateException("Cannot close a facility that is not open");
ClosableFacility::close();
//lastly, wake the process who was maintaining the facility
this->_directingProcess->Activate();
this->_directingProcess = nullptr;
}
// overriding because default param was not working
void MaintainableClosableFacility::Seize(Entity *e, ServicePriority_t sp) {
if(isShuttingDown()){
throw FacilityInvalidStateException("Cannot enter a queue when the facility is shutting down");
}
ClosableFacility::Seize(e,sp);
}
void MaintainableClosableFacility::Seize(Entity *e) {
if(isShuttingDown()){
throw FacilityInvalidStateException("Cannot enter a queue when the facility is shutting down");
}
ClosableFacility::Seize(e);
}
void MaintainableClosableFacility::Release(Entity *e) {
// release the facility
Facility::Release(e);
if (checkClosingCondition() && !isShuttingDown()) {
// if the closing condition is true, facility enters the 'shutting down' state
// it will serve the remaining processes but does not allow new ones to get in the queue
this->_isShuttingDown = true;
}
if (this->QueueLen() == 0 && isShuttingDown() && isOpen()) {
// if there is nobody in the queue, we can shut down
this->close();
}
}