Browse Source

Le réparateur, j'ai pas testé lol

master
Antoine Bartuccio 1 year ago
parent
commit
3334f77d22
Signed by: klmp200 <klmp200@klmp200.net> GPG Key ID: E7245548C53F904B

+ 23
- 8
Building/Building.c View File

@@ -3,6 +3,7 @@
3 3
 //
4 4
 #include <string.h>
5 5
 #include <math.h>
6
+#include <unistd.h>
6 7
 #include "Building.h"
7 8
 #include "../Resident/Resident.h"
8 9
 #include "../Visitor/Visitor.h"
@@ -150,6 +151,9 @@ void _free__Building(THIS(Building)){
150 151
 	pthread_mutex_unlock(&this->mutex_waiting_floors);
151 152
 	pthread_mutex_destroy(&this->mutex_waiting_floors);
152 153
 
154
+	pthread_mutex_unlock(&this->mutex_repair);
155
+	pthread_mutex_destroy(&this->mutex_repair);
156
+
153 157
 	DELETE(this->residents);
154 158
 	DELETE(this->visitors);
155 159
 	for (i=0; i<ELEVATOR_NB; i++)
@@ -236,6 +240,15 @@ void signal_elevator_at_floor_Building(THIS(Building), int floor){
236 240
 	pthread_cond_signal(this->condition_floors[floor]);
237 241
 }
238 242
 
243
+void ask_elevator_reparation_Building(THIS(Building), Elevator* elevator){
244
+	printf("Technicien : l'ascenseur %s attend pour se faire réparer\n", elevator->name);
245
+	pthread_mutex_lock(&this->mutex_repair);
246
+	usleep(10000);
247
+	elevator->repair(elevator);
248
+	pthread_mutex_unlock(&this->mutex_repair);
249
+	printf("Technicien : l'ascenseur %s est maintenant réparé\n", elevator->name);
250
+}
251
+
239 252
 Building *_init_Building(char * residents_file, char * visitors_file){
240 253
 	Building * new_building = malloc_or_die(sizeof(Building));
241 254
 	char elevator_name[] = "@";
@@ -245,6 +258,7 @@ Building *_init_Building(char * residents_file, char * visitors_file){
245 258
 	for (i=0; i<FLOORS; i++)
246 259
 		new_building->waiting_floors[i] = 0;
247 260
 	pthread_mutex_init(&new_building->mutex_waiting_floors, NULL);
261
+	pthread_mutex_init(&new_building->mutex_repair, NULL);
248 262
 	
249 263
 	new_building->elevators = malloc_or_die(sizeof(Elevator*) * ELEVATOR_NB);
250 264
 
@@ -273,14 +287,15 @@ Building *_init_Building(char * residents_file, char * visitors_file){
273 287
 
274 288
 
275 289
 	LINK_ALL(Building, new_building,
276
-		 parse_residents,
277
-		 parse_visitors,
278
-		 get_inside_elevator,
279
-		 use_call_box,
280
-		 go_to_floor,
281
-		 get_next_call,
282
-		 get_waiting_floors,
283
-		 signal_elevator_at_floor
290
+			 parse_residents,
291
+			 parse_visitors,
292
+			 get_inside_elevator,
293
+			 use_call_box,
294
+			 go_to_floor,
295
+			 get_next_call,
296
+			 get_waiting_floors,
297
+			 signal_elevator_at_floor,
298
+			 ask_elevator_reparation
284 299
 	)
285 300
 
286 301
 	if (residents_file != NULL)

+ 3
- 0
Building/Building.h View File

@@ -22,6 +22,7 @@ typedef struct o_Building {
22 22
 	PRIVATE List * visitors;
23 23
 	PRIVATE Elevator ** elevators;
24 24
 	PRIVATE pthread_mutex_t mutex_waiting_floors;
25
+	PRIVATE pthread_mutex_t mutex_repair;
25 26
 	PRIVATE pthread_mutex_t * mutex_cond_get_inside_elevator;
26 27
 	PRIVATE pthread_mutex_t * mutex_cond_get_outside_elevator;
27 28
 	PRIVATE pthread_mutex_t * mutex_func_get_inside_elevator;
@@ -37,6 +38,8 @@ typedef struct o_Building {
37 38
 	PUBLIC int (*get_next_call)(_THIS(Building), int elevator_floor);
38 39
 	PUBLIC int* (*get_waiting_floors)(_THIS(Building));
39 40
 
41
+	SYNCHRONIZE PUBLIC void (*ask_elevator_reparation)(_THIS(Building), Elevator* elevator);
42
+
40 43
 	SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger * passenger);
41 44
 
42 45
 	DESTRUCTOR(Building);

+ 1
- 1
CMakeLists.txt View File

@@ -8,4 +8,4 @@ if(CMAKE_COMPILER_IS_GNUCC)
8 8
     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
9 9
 endif()
10 10
 
11
-add_executable(LO41 main.c List/List.h List/List.c Objects.h List/Element.c List/Element.h Objects.c Building/Building.c Building/Building.h Elevator/Elevator.c Elevator/Elevator.h SharedData/SharedData.c SharedData/SharedData.h Visitor/Visitor.c Visitor/Visitor.h Resident/Resident.c Resident/Resident.h Passenger/Passenger.h Passenger/Passenger.c)
11
+add_executable(LO41 main.c List/List.h List/List.c Objects.h List/Element.c List/Element.h Objects.c Building/Building.c Building/Building.h Elevator/Elevator.c Elevator/Elevator.h SharedData/SharedData.c SharedData/SharedData.h Visitor/Visitor.c Visitor/Visitor.h Resident/Resident.c Resident/Resident.h Passenger/Passenger.h Passenger/Passenger.c ElevatorBreaker/ElevatorBreaker.c ElevatorBreaker/ElevatorBreaker.h)

+ 5
- 0
Elevator/Elevator.c View File

@@ -121,6 +121,10 @@ void repair_Elevator(THIS(Elevator)){
121 121
 	this->set_state(this, RUNNING);
122 122
 }
123 123
 
124
+void damage_Elevator(THIS(Elevator)){
125
+	this->set_state(this, BROKEN);
126
+}
127
+
124 128
 void *runnable_Elevator(void * void_this){
125 129
 	/* This is where the thread logic will be implemented */
126 130
 	Elevator * this = (Elevator*) void_this;
@@ -196,6 +200,7 @@ Elevator *_init_Elevator(char * name){
196 200
 			 get_floor,
197 201
 			 set_floor,
198 202
 			 repair,
203
+			 damage,
199 204
 			 set_thread_number
200 205
 	);
201 206
 

+ 1
- 0
Elevator/Elevator.h View File

@@ -34,6 +34,7 @@ typedef struct o_Elevator {
34 34
 	SYNCHRONIZE PRIVATE int (*get_next_passenger_stop)(_THIS(Elevator));
35 35
 
36 36
 	SYNCHRONIZE PUBLIC void (*repair)(_THIS(Elevator));
37
+	SYNCHRONIZE PUBLIC void (*damage)(_THIS(Elevator));
37 38
 	SYNCHRONIZE PUBLIC int (*get_number_of_passengers)(_THIS(Elevator));
38 39
 	SYNCHRONIZE PUBLIC void (*remove_passenger) (_THIS(Elevator), Passenger * passenger);
39 40
 	SYNCHRONIZE PUBLIC void (*add_passenger)(_THIS(Elevator), Passenger * passenger);

+ 47
- 0
ElevatorBreaker/ElevatorBreaker.c View File

@@ -0,0 +1,47 @@
1
+//
2
+// Created by Antoine Bartuccio on 22/06/2018.
3
+//
4
+
5
+#include <unistd.h>
6
+
7
+#include "ElevatorBreaker.h"
8
+#include "../SharedData/SharedData.h"
9
+
10
+SETTER(ElevatorBreaker, int, thread_number);
11
+
12
+void _free__ElevatorBreaker(THIS(ElevatorBreaker)){
13
+	free(this);
14
+}
15
+
16
+void * runnable_ElevatorBreaker(void * void_this){
17
+	ElevatorBreaker *this = (ElevatorBreaker*) void_this;
18
+	Elevator *selected_elevator = NULL;
19
+	SharedData *data = GET_INSTANCE(SharedData);
20
+
21
+	AGENT_OPTIONS
22
+
23
+	while (data->is_active_passengers_left(data)){
24
+		usleep(900000);
25
+		// One chance out of two to break something
26
+		if ((rand() % 101) > 50) {
27
+			selected_elevator = data->get_main_building(data)->elevators[rand() % ELEVATOR_NB];
28
+			selected_elevator->damage(selected_elevator);
29
+			printf("ALERTE : L'ascenseur %s est endommagé\n", selected_elevator->name);
30
+		}
31
+	}
32
+
33
+	data->unregister_thread(data, this->thread_number);
34
+	return NULL;
35
+}
36
+
37
+ElevatorBreaker *_init_ElevatorBreaker(){
38
+	ElevatorBreaker *new_elevator_breaker = malloc_or_die(sizeof(ElevatorBreaker));
39
+	new_elevator_breaker->thread_number = -1;
40
+
41
+	LINK_ALL(ElevatorBreaker, new_elevator_breaker,
42
+			 set_thread_number,
43
+			 runnable
44
+	)
45
+
46
+	return new_elevator_breaker;
47
+}

+ 22
- 0
ElevatorBreaker/ElevatorBreaker.h View File

@@ -0,0 +1,22 @@
1
+//
2
+// Created by Antoine Bartuccio on 22/06/2018.
3
+//
4
+
5
+#ifndef LO41_ELEVATORBREAKER_H
6
+#define LO41_ELEVATORBREAKER_H
7
+
8
+#include "../Objects.h"
9
+
10
+typedef struct o_ElevatorBreaker {
11
+	PRIVATE int thread_number;
12
+
13
+	PUBLIC void * (*runnable)(void * void_this);
14
+	PUBLIC void (*set_thread_number)(_THIS(ElevatorBreaker), int data);
15
+
16
+	DESTRUCTOR(ElevatorBreaker);
17
+
18
+} ElevatorBreaker;
19
+
20
+ElevatorBreaker *_init_ElevatorBreaker();
21
+
22
+#endif //LO41_ELEVATORBREAKER_H

+ 9
- 1
SharedData/SharedData.c View File

@@ -46,8 +46,13 @@ void start_all_threads_SharedData(THIS(SharedData)){
46 46
 
47 47
 			pthread_mutex_lock(&this->mutex_active_passengers);
48 48
 			pthread_mutex_lock(&this->mutex_threads);
49
+
50
+			/* starting the elevator breaker */
51
+			this->start_thread(this, this->elevator_breaker->runnable, this->elevator_breaker, 0);
52
+			this->elevator_breaker->set_thread_number(this->elevator_breaker, 0);
53
+
49 54
 			/* starts threading elevators */
50
-			for (i=0; i<ELEVATOR_NB; i++) {
55
+			for (i=1; i<ELEVATOR_NB; i++) {
51 56
 				this->start_thread(this, this->main_building->elevators[i]->runnable,
52 57
 								   this->main_building->elevators[i], i);
53 58
 				this->main_building->elevators[i]->set_thread_number(this->main_building->elevators[i], i);
@@ -105,6 +110,8 @@ void _free__SharedData(THIS(SharedData)){
105 110
 	if (this->main_building != NULL)
106 111
 		DELETE(this->main_building);
107 112
 
113
+	DELETE(this->elevator_breaker);
114
+
108 115
 	pthread_mutex_unlock(&this->mutex_active_passengers);
109 116
 	pthread_mutex_destroy(&this->mutex_active_passengers);
110 117
 	pthread_mutex_unlock(&this->mutex_threads);
@@ -143,6 +150,7 @@ SharedData *_get_instance_SharedData(){
143 150
 		new_shared_data->main_building = NULL; /* Should be set to NULL if freed before */
144 151
 		new_shared_data->threads_nb = 0;
145 152
 		new_shared_data->active_passengers = 0;
153
+		new_shared_data->elevator_breaker = NEW(ElevatorBreaker);
146 154
 
147 155
 		pthread_mutex_init(&new_shared_data->mutex_active_passengers, NULL);
148 156
 		pthread_mutex_init(&new_shared_data->mutex_threads, NULL);

+ 2
- 0
SharedData/SharedData.h View File

@@ -8,10 +8,12 @@
8 8
 #include <pthread.h>
9 9
 #include "../Objects.h"
10 10
 #include "../Building/Building.h"
11
+#include "../ElevatorBreaker/ElevatorBreaker.h"
11 12
 
12 13
 typedef struct o_SharedData {
13 14
 	PRIVATE int threads_nb;
14 15
 	PRIVATE int active_passengers;
16
+	PRIVATE ElevatorBreaker * elevator_breaker;
15 17
 	PRIVATE pthread_t ** threads;
16 18
 	PRIVATE Building * main_building;
17 19
 	PRIVATE pthread_mutex_t mutex_active_passengers;

+ 2
- 0
main.c View File

@@ -1,5 +1,6 @@
1 1
 #include <stdio.h>
2 2
 #include <unistd.h>
3
+#include <time.h>
3 4
 
4 5
 #include "Objects.h"
5 6
 #include "List/List.h"
@@ -17,6 +18,7 @@ void clean_exit(int error_code){
17 18
 int main(int argc, char* argv[]) {
18 19
 	SharedData * shared_data = GET_INSTANCE(SharedData);
19 20
 
21
+	srand((unsigned int) time(NULL));
20 22
 	signal(SIGINT, clean_exit);
21 23
 	if(argc == 3){
22 24
 		shared_data->set_main_building(shared_data, NEW(Building, argv[1], argv[2]));

Loading…
Cancel
Save