-
Notifications
You must be signed in to change notification settings - Fork 0
/
plates.c
255 lines (204 loc) · 8.45 KB
/
plates.c
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "simulator.h"
#include "plates.h"
#include "lpr.h"
#include "shm.h"
#include "cars.h"
#include "cpmss.h"
// Global mutex for our plates.c
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t plateGenerationMutex;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
shared_memory_t shm;
// Intialising flags to be used
int flag = 0;
bool found = false;
bool flagPlateFound = false;
bool flagTempGenerated = false;
int full = 0;
// Check the plate against the hash table made from plates.txt
bool checkPlate( char *plate ) {
if (htab_find(&h, plate) == NULL) {
return false;
}
else {
return true;
}
return false;
}
// Thread function to generate the plates to be checked
void *generatePlate(char *plate) {
// Mutex locks to protect the global random
pthread_mutex_lock(&plateGenerationMutex);
int allowCarCheck = generateInRange(0,1);
if (allowCarCheck == 0)
{
randLine(plate);
found = true;
}
else
{
// Uses current time as the random seed
for (int i = 0; i < 3; i++) {
int randIntNumber = rand();
int randIntLetter = rand();
// Generate random integers as a part of the plate
randIntNumber %= 10;
randIntNumber += 48;
char randNumberConverted = randIntNumber;
plate[i] = randNumberConverted;
// Generate random letters as a part of the plate
randIntLetter %= 26;
randIntLetter += 65;
char randLetterConverted = randIntLetter;
plate[i+3] = randLetterConverted;
}
}
// Unlock mutex
pthread_mutex_unlock(&plateGenerationMutex);
return 0;
}
// Thread function to generate the random time between cars being generated
void *generatePlateTime() {
// Lock mutex to protect variables
pthread_mutex_lock(&mutex);
// Signal that work is being done
pthread_cond_signal(&cond);
// Generate time
int plateGenerateTime = generateInRange(1, 100);
int plateGenerateMS;
// Calculate sleep for ms
plateGenerateMS = msSleep(plateGenerateTime);
sleep(plateGenerateMS);
// Flip while condition for generateCars()
flag = 1;
// Unlock mutex protection
pthread_mutex_unlock(&mutex);
return 0;
}
// Generating a random temperature between a range on each floor for simulator
void *generateTemp() {
int tempSleepTime = generateInRange(1,5);
sleep(msSleep(tempSleepTime));
for (int i = 0; i < NUM_LEVELS; i++) {
int temperatureRand = generateInRange(LOWER_TEMP,HIGHER_TEMP);
shm.data->levels[i].temperature_sensor = temperatureRand;
}
flagTempGenerated = true;
return 0;
}
// Function to check cars and generate a car thread
void *generateCars() {
char *plate = (char *)calloc(6, sizeof(char));
// Lock mutex to protect variables
pthread_mutex_lock(&mutex);
// Wait for the time till new license plate to be generated
while (flag == 0) {
pthread_cond_wait(&cond, &mutex);
}
// Generate the plate and assign
generatePlate(plate);
// Randomly generating the entrance check the car will go to (1-5)
int entranceRand = generateInRange(0, NUM_LEVELS-1);
// Boom Gate status being set as 'C' closed at the start
pthread_mutex_lock(&shm.data->entrances[entranceRand].gate.lock);
shm.data->entrances[entranceRand].gate.status = 'C';
pthread_mutex_unlock(&shm.data->entrances[entranceRand].gate.lock);
pthread_mutex_lock(&shm.data->exits[entranceRand].gate.lock);
shm.data->exits[entranceRand].gate.status = 'C';
pthread_mutex_unlock(&shm.data->exits[entranceRand].gate.lock);
// Obtain plate from shared memory
pthread_mutex_lock(&shm.data->entrances[entranceRand].sensor.lock);
strcpy(shm.data->entrances[entranceRand].sensor.plate, plate);
pthread_mutex_unlock(&shm.data->entrances[entranceRand].sensor.lock);
// Once a car reaches the front of the queue, it will wait 2ms before triggering the entrance LPR.
sleep(msSleep(CHECK_TIME));
char infoStatusChar = entranceRand + 1 + '0';
// Update the information sign's status
pthread_mutex_lock(&shm.data->entrances[entranceRand].sign.lock);
shm.data->entrances[entranceRand].sign.status = infoStatusChar;
pthread_mutex_unlock(&shm.data->entrances[entranceRand].sign.lock);
if ((level[0] == MAX_LEVEL_CAPACITY) && (level[1] == MAX_LEVEL_CAPACITY) && (level[2] == MAX_LEVEL_CAPACITY) && (level[3] == MAX_LEVEL_CAPACITY) && (level[4] == MAX_LEVEL_CAPACITY)) {
full = 5;
flagPlateFound = true;
}
// If true save to shared memory entrance LPR
if (found && (full != 5)) {
// Flipping the found bool once a car enters
found = false;
// Initalising variable to be used
int entranceDiff = 0;
// Unusual behaviour of car to move to a different level than originally assigned: 5% chance
int unusualBehaviour = generateInRange(0,20);
if (unusualBehaviour == 0)
{
entranceRand = generateInRange(0, NUM_LEVELS-1);
}
// Checking if the assign level is full
for (int i = 0; i < NUM_LEVELS; i++) {
if (i == entranceRand) {
if (level[i] < MAX_LEVEL_CAPACITY) {
level[i]++; // Incrementing the level capacity
}
// Reassigning a level which is not full
else {
shm.data->entrances[entranceRand].sign.status = 'F';
do {
entranceDiff = generateInRange(0,4);
if (level[i] < MAX_LEVEL_CAPACITY) {
level[entranceDiff]++;
entranceRand = entranceDiff;
}
}
while (entranceDiff == entranceRand);
}
}
}
// Copies the car plate over to shared memory
pthread_mutex_lock(&shm.data->levels[entranceRand].sensor.lock);
strcpy(shm.data->levels[entranceRand].sensor.plate, plate);
pthread_mutex_unlock(&shm.data->levels[entranceRand].sensor.lock);
// Signal to manager.c that a plate has been found and that it is safe to print
flagPlateFound = true;
// Boom Gate status being set and slept for raising
pthread_mutex_lock(&shm.data->entrances[entranceRand].gate.lock);
shm.data->entrances[entranceRand].gate.status = 'R';
sleep(msSleep(RAISE_TIME));
pthread_mutex_unlock(&shm.data->entrances[entranceRand].gate.lock);
// Boom Gate status being set and slept for opening
pthread_mutex_lock(&shm.data->entrances[entranceRand].gate.lock);
shm.data->entrances[entranceRand].gate.status = 'O';
sleep(msSleep(OPEN_TIME));
pthread_mutex_unlock(&shm.data->entrances[entranceRand].gate.lock);
// Initialise the car thread, sending the appropriate plate and its entrance
pthread_mutex_lock(&shm.data->entrances[entranceRand].sensor.lock);
initCars(plate, entranceRand);
pthread_mutex_unlock(&shm.data->entrances[entranceRand].sensor.lock);
// Boom Gate status being set and slept for lowering after the car enters
pthread_mutex_lock(&shm.data->entrances[entranceRand].gate.lock);
shm.data->entrances[entranceRand].gate.status = 'L';
sleep(msSleep(LOWER_TIME));
pthread_mutex_unlock(&shm.data->entrances[entranceRand].gate.lock);
// Boom Gate status being set to closed
pthread_mutex_lock(&shm.data->entrances[entranceRand].gate.lock);
shm.data->entrances[entranceRand].gate.status = 'C';
pthread_mutex_unlock(&shm.data->entrances[entranceRand].gate.lock);
}
else {
// Signal to manager.c that a plate has not been found and that it is not safe to print
flagPlateFound = false;
if (full == 5) {
shm.data->entrances[entranceRand].sign.status = 'F';
}
else {
// Information Sign status being set to 'X' for unapproved cars
pthread_mutex_lock(&shm.data->entrances[entranceRand].sign.lock);
shm.data->entrances[entranceRand].sign.status = 'X';
pthread_mutex_unlock(&shm.data->entrances[entranceRand].sign.lock);
}
}
// Unlock the mutex protection
pthread_mutex_unlock(&mutex);
// Freeing plate memory allocation
free(plate);
return 0;
}