-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaxiCenter.cpp
More file actions
330 lines (285 loc) · 9.26 KB
/
TaxiCenter.cpp
File metadata and controls
330 lines (285 loc) · 9.26 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "TaxiCenter.h"
#include "Serialization.h"
/*
//Get taxis.
const vector<Taxi *> &TaxiCenter::getTaxis() const {
return taxis;
}
//Get passengers.
const vector<Passenger *> &TaxiCenter::getPassengers() const {
return passengers;
}
//Set taxis.
void TaxiCenter::setTaxis(const vector<Taxi *> &taxis) {
TaxiCenter::taxis = taxis;
}
//Set passngers.
void TaxiCenter::setPassengers(const vector<Passenger *> &passengers) {
TaxiCenter::passengers = passengers;
}
//Getting the trips.
const vector<Trip *> &TaxiCenter::getTrips() const {
return trips;
}
//Setting the trips.
void TaxiCenter::setTrips(const vector<Trip *> &trips) {
TaxiCenter::trips = trips;
}
//Adding passenger.
void TaxiCenter::addPassenger(Passenger *passenger) {
TaxiCenter::passengers.push_back(passenger);
}
*/
//Adding taxi.
void TaxiCenter::addTaxi(Taxi *taxi) {
TaxiCenter::taxis.push_back(taxi);
}
//Adding trip.
void TaxiCenter::addTrip(Trip *trip) {
int size = trips.size();
for (int i = 0; i < size; i++) {
if (trip->getTimeToStart() < trips.at(i)->getTimeToStart()) {
trips.insert(trips.begin() + i, trip);
break;
}
}
if (size == 0 || trips.size() == size) {
trips.push_back(trip);
}
}
//Print the location of the driver.
bool TaxiCenter::printDriverLocation(int id) {
for (int i = 0; i < detailsDrivers.size(); i++) {
if (detailsDrivers.at(i)->getDriverId() == id) {
detailsDrivers.at(i)->pushMission(4);
//Waiting for the thread to print the location of driver.
while (detailsDrivers.at(i)->getMissionNoDelete() != -1) {
usleep(10000);
}
//the driver was found.
return true;
}
}
//the driver was not found.
return false;
}
//Get the location of the driver.
Node* TaxiCenter::getDriverLocation(int id) {
for (int i = 0; i < detailsDrivers.size(); i++) {
if (detailsDrivers.at(i)->getDriverId() == id) {
return getLocationFromDriver(detailsDrivers.at(i));
}
}
return NULL;
}
//Setting taxis and trips to the drivers.
void TaxiCenter::setAll() {
/*
//Assigning taxis to drivers.
for (int i = 0, j = 0; i < taxis.size() && j < detailsDrivers.size(); i++, j++) {
for (int k = 0; k < detailsDrivers.size(); k++) {
if (detailsDrivers.at(k)->getTaxiId() == -1) {
detailsDrivers.at(k)->setTaxiId(taxis.at(i)->getTaxiId());
//to serialize the taxi and sendTo it to the driver.
sendTaxi(detailsDrivers.at(k), taxis.at(i));
taxis.erase(taxis.begin() + i);/////////check if really delete.
i--;
break;
}
}
}
*/
//Assigning trips to drivers
for (int i = 0; i < trips.size(); ++i) {
if (trips.at(i)->getTimeToStart() > clock->getTime()) {
continue;
}
//if we are here there is a trip to assign and we wait for all the drivers to
//finish the mission.
for (int j = 0; j < detailsDrivers.size(); ++j) {
//Waiting for the thread to finsh all his missions.
while (detailsDrivers.at(j)->getMissionNoDelete() != -1) {
usleep(10000);
}
}
//now all the drivers arrived to their destination.
//waiting for the trip to be calculated
while (!trips.at(i)->isDoneCalculatePath()) {
usleep(10000);
}
//only here delete the trip if there time has passed(they have been transfared to
// the client alreasy) or there is no path to them - there path is empty.
if (trips.at(i)->getTimeToStart() < clock->getTime() || trips.at(i)->isPathEmpty()) {
trips.erase(trips.begin() + i);
i--;
continue;
}
//getting a new vector with the detailsDrivers sort by the arrived time of the trip.
vector<DetailsDriver*> detailsSortedByArrivedTime = getDetailsSortedByArrivedTime();
for (int k = 0; k < detailsSortedByArrivedTime.size(); k++) {
set();
//pass on the ones which are in the middle of trip.
if (!detailsSortedByArrivedTime.at(k)->isAvailable()) {
continue;
}
detailsSortedByArrivedTime.at(k)->pushTripId(trips.at(i)->getTripId());
detailsSortedByArrivedTime.at(k)->pushMission(3);
set();
//if the driver is in position of the start trip - he got the trip and not available.
if (detailsSortedByArrivedTime.at(k)->isAvailable()) {
continue;
}
//not available - means he got the trip.
break;
/*
Node* location = this->getDriverLocation(detailsDrivers.at(k)->getDriverId());
if (*trips.at(i)->getStart() == location) {
if (location != NULL) {
delete (location);
}
sendTrip(detailsDrivers.at(k), trips.at(i));
trips.erase(trips.begin() + i);/////////check if really delete.
//availableDrivers.erase(availableDrivers.begin() + k);
detailsDrivers.at(k)->setAvailable(false);
i--;
break;//////////check if exit the for and not only the if.
}
*/
}
}
}
//Distructor to taxi center.
TaxiCenter::~TaxiCenter() {
for (int j = 0; j < taxis.size(); ++j) {
if (taxis[j] != NULL) {
delete taxis[j];
}
}
taxis.clear();
for (int j = 0; j < trips.size(); ++j) {
if (trips[j] != NULL) {
delete trips[j];
}
}
trips.clear();
for (int i = 0; i < detailsDrivers.size() ; ++i) {
if (detailsDrivers[i] != NULL) {
delete detailsDrivers[i];
}
}
detailsDrivers.clear();
passengers.clear();
}
/*
//add Details Drivers.
void TaxiCenter::addDetailsDrivers(int driverId) {
DetailsDriver* detailsDriver = new DetailsDriver(driverId);
detailsDrivers.push_back(detailsDriver);
}*/
//add Details Drivers.
void TaxiCenter::addDetailsDrivers(int driverId, Tcp *tcp, int socketId) {
DetailsDriver* detailsDriver = new DetailsDriver(driverId, tcp);
detailsDriver->setSocketId(socketId);
//inserting the new details driver by sorting according to the driver id.
for (int i = 0; i < detailsDrivers.size(); ++i) {
if (driverId < detailsDrivers[i]->getDriverId()) {
detailsDrivers.insert(detailsDrivers.begin() + i, detailsDriver);
return;
}
}
detailsDrivers.push_back(detailsDriver);
}
//get details driver By Id
DetailsDriver* TaxiCenter::getDetailsDriversById(int driverId) {
for (int i = 0; i < detailsDrivers.size() ; ++i) {
if (detailsDrivers[i]->getDriverId() == driverId) {
return detailsDrivers[i];
}
}
return NULL;
}
//update the Time.
void TaxiCenter::updateTime() {
clock->updateTime();
//cout << "time: " << clock->getTime() << endl;
set();
for (int i = 0; i < detailsDrivers.size(); i++) {
if (!detailsDrivers.at(i)->isAvailable()) {
detailsDrivers.at(i)->pushMission(9);
}
}
}
//set clock.
void TaxiCenter::setClock(Time* clock) {
TaxiCenter::clock = clock;
}
//add Taxi To Driver
void TaxiCenter::addTaxiToDriver(int id) {
for (int i = 0; i < detailsDrivers.size(); ++i) {
if (detailsDrivers[i]->getDriverId() == id) {
detailsDrivers[i]->setTaxiId(id);
break;
}
}
}
//get Taxi By Id
Taxi *TaxiCenter::getTaxiById(int id) {
for (int i = 0; i < taxis.size(); ++i) {
if (taxis[i]->getTaxiId() == id) {
return taxis[i];
}
}
return NULL;
}
//delete Taxi By Id
void TaxiCenter::deleteTaxiById(int id) {
for (int i = 0; i < taxis.size(); ++i) {
if (taxis[i]->getTaxiId() == id) {
taxis.erase(taxis.begin() + i);
}
}
}
//get trip By Id
Trip *TaxiCenter::getTripByid(int id) {
for (int i = 0; i < trips.size(); ++i) {
if (trips[i]->getTripId() == id) {
return trips[i];
}
}
return NULL;
}
//delete Trip By Id
void TaxiCenter::deleteTripById(int id) {
for (int i = 0; i < trips.size(); ++i) {
if (trips[i]->getTripId() == id) {
trips.erase(trips.begin() + i);
}
}
}
//get Details Sorted By Arrived Time
vector<DetailsDriver *> TaxiCenter::getDetailsSortedByArrivedTime() {
vector<DetailsDriver *> sortedDetails;
for (int i = 0; i < detailsDrivers.size(); ++i) {
sortedDetails.push_back(detailsDrivers[i]);
}
sort(sortedDetails.begin(), sortedDetails.end(), compareDetailsDriver);
return sortedDetails;
}
//compare Details Driver
bool TaxiCenter::compareDetailsDriver(DetailsDriver *d1, DetailsDriver *d2) {
return (d1->getTimeArrivedLastTrip() < d2->getTimeArrivedLastTrip());
}
//close Client Threads
void TaxiCenter::closeClientThreads() {
for (int i = 0; i < detailsDrivers.size(); ++i) {
detailsDrivers[i]->pushMission(7);
}
}
//set
void TaxiCenter::set() {
for (int j = 0; j < detailsDrivers.size(); ++j) {
while (detailsDrivers.at(j)->getMissionNoDelete() != -1) {
usleep(10000);
}
}
}