-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
93 lines (82 loc) · 1.85 KB
/
main.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
/* Rinnakkaisuus 2014
pussuw, r00pe, xzr
*/
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include "genericthread.h"
#include "syncthread.h"
#include "genericsignal.h"
#include "trafficlight.h"
#include "routemanager.h"
#include "ferry.h"
#include "car.h"
#include "randomizer.h"
//delay between every tick in milliseconds
const unsigned int TICK_DELAY = 100;
const unsigned int LIMIT_CARS_PASSED = 100;
const unsigned int MAX_SPAWN_DELAY = 20;
volatile unsigned int g_cars_passed = 0;
volatile unsigned int g_cars_created = 0;
void server_loop()
{
std::vector<Car*> children;
//initialize ferry
Ferry ferry1;
ferry1.Start();
TrafficLight traffic_light;
Randomizer randomizer;
RouteManager routemanager(&randomizer);
unsigned int spawn_delay = randomizer.Randomize(MAX_SPAWN_DELAY);
unsigned int car_id = 0;
Car* tmp;
do
{
usleep(TICK_DELAY);
//send signal to move
ferry1.GetHeartBeat()->Signal();
if( spawn_delay == 0 )
{
if(g_cars_created < LIMIT_CARS_PASSED)
{
tmp = new Car(car_id, &ferry1, &traffic_light, &routemanager);
tmp->Start();
children.push_back(tmp);
tmp = 0;
spawn_delay = randomizer.Randomize(MAX_SPAWN_DELAY);
++car_id;
g_cars_created++;
}
}
else
{
spawn_delay--;
}
for( std::vector<Car*>::iterator it = children.begin(); it != children.end(); )
{
if( (*it)->IsRunning() )
{
(*it)->GetHeartBeat()->Signal();
it++;
}
else
{
delete *it;
it = children.erase(it);
g_cars_passed++;
}
}
} while( g_cars_passed < LIMIT_CARS_PASSED );
}
int main(void)
{
server_loop();
return true;
}