From 3334f77d2298a2d9baf111f78005332c48bf7614 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Fri, 22 Jun 2018 03:59:36 +0200 Subject: [PATCH] =?UTF-8?q?Le=20r=C3=A9parateur,=20j'ai=20pas=20test=C3=A9?= =?UTF-8?q?=20lol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Building/Building.c | 31 ++++++++++++++------ Building/Building.h | 3 ++ CMakeLists.txt | 2 +- Elevator/Elevator.c | 5 ++++ Elevator/Elevator.h | 1 + ElevatorBreaker/ElevatorBreaker.c | 47 +++++++++++++++++++++++++++++++ ElevatorBreaker/ElevatorBreaker.h | 22 +++++++++++++++ SharedData/SharedData.c | 10 ++++++- SharedData/SharedData.h | 2 ++ main.c | 2 ++ 10 files changed, 115 insertions(+), 10 deletions(-) create mode 100644 ElevatorBreaker/ElevatorBreaker.c create mode 100644 ElevatorBreaker/ElevatorBreaker.h diff --git a/Building/Building.c b/Building/Building.c index 364b441..239006c 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -3,6 +3,7 @@ // #include #include +#include #include "Building.h" #include "../Resident/Resident.h" #include "../Visitor/Visitor.h" @@ -150,6 +151,9 @@ void _free__Building(THIS(Building)){ pthread_mutex_unlock(&this->mutex_waiting_floors); pthread_mutex_destroy(&this->mutex_waiting_floors); + pthread_mutex_unlock(&this->mutex_repair); + pthread_mutex_destroy(&this->mutex_repair); + DELETE(this->residents); DELETE(this->visitors); for (i=0; icondition_floors[floor]); } +void ask_elevator_reparation_Building(THIS(Building), Elevator* elevator){ + printf("Technicien : l'ascenseur %s attend pour se faire réparer\n", elevator->name); + pthread_mutex_lock(&this->mutex_repair); + usleep(10000); + elevator->repair(elevator); + pthread_mutex_unlock(&this->mutex_repair); + printf("Technicien : l'ascenseur %s est maintenant réparé\n", elevator->name); +} + Building *_init_Building(char * residents_file, char * visitors_file){ Building * new_building = malloc_or_die(sizeof(Building)); char elevator_name[] = "@"; @@ -245,6 +258,7 @@ Building *_init_Building(char * residents_file, char * visitors_file){ for (i=0; iwaiting_floors[i] = 0; pthread_mutex_init(&new_building->mutex_waiting_floors, NULL); + pthread_mutex_init(&new_building->mutex_repair, NULL); new_building->elevators = malloc_or_die(sizeof(Elevator*) * ELEVATOR_NB); @@ -273,14 +287,15 @@ Building *_init_Building(char * residents_file, char * visitors_file){ LINK_ALL(Building, new_building, - parse_residents, - parse_visitors, - get_inside_elevator, - use_call_box, - go_to_floor, - get_next_call, - get_waiting_floors, - signal_elevator_at_floor + parse_residents, + parse_visitors, + get_inside_elevator, + use_call_box, + go_to_floor, + get_next_call, + get_waiting_floors, + signal_elevator_at_floor, + ask_elevator_reparation ) if (residents_file != NULL) diff --git a/Building/Building.h b/Building/Building.h index d642d7d..6b56410 100644 --- a/Building/Building.h +++ b/Building/Building.h @@ -22,6 +22,7 @@ typedef struct o_Building { PRIVATE List * visitors; PRIVATE Elevator ** elevators; PRIVATE pthread_mutex_t mutex_waiting_floors; + PRIVATE pthread_mutex_t mutex_repair; PRIVATE pthread_mutex_t * mutex_cond_get_inside_elevator; PRIVATE pthread_mutex_t * mutex_cond_get_outside_elevator; PRIVATE pthread_mutex_t * mutex_func_get_inside_elevator; @@ -37,6 +38,8 @@ typedef struct o_Building { PUBLIC int (*get_next_call)(_THIS(Building), int elevator_floor); PUBLIC int* (*get_waiting_floors)(_THIS(Building)); + SYNCHRONIZE PUBLIC void (*ask_elevator_reparation)(_THIS(Building), Elevator* elevator); + SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger * passenger); DESTRUCTOR(Building); diff --git a/CMakeLists.txt b/CMakeLists.txt index f983266..b7241d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,4 +8,4 @@ if(CMAKE_COMPILER_IS_GNUCC) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread") endif() -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) \ No newline at end of file +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) \ No newline at end of file diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index e5ac460..b202dcf 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -121,6 +121,10 @@ void repair_Elevator(THIS(Elevator)){ this->set_state(this, RUNNING); } +void damage_Elevator(THIS(Elevator)){ + this->set_state(this, BROKEN); +} + void *runnable_Elevator(void * void_this){ /* This is where the thread logic will be implemented */ Elevator * this = (Elevator*) void_this; @@ -196,6 +200,7 @@ Elevator *_init_Elevator(char * name){ get_floor, set_floor, repair, + damage, set_thread_number ); diff --git a/Elevator/Elevator.h b/Elevator/Elevator.h index c9e7571..f4ffd9b 100644 --- a/Elevator/Elevator.h +++ b/Elevator/Elevator.h @@ -34,6 +34,7 @@ typedef struct o_Elevator { SYNCHRONIZE PRIVATE int (*get_next_passenger_stop)(_THIS(Elevator)); SYNCHRONIZE PUBLIC void (*repair)(_THIS(Elevator)); + SYNCHRONIZE PUBLIC void (*damage)(_THIS(Elevator)); SYNCHRONIZE PUBLIC int (*get_number_of_passengers)(_THIS(Elevator)); SYNCHRONIZE PUBLIC void (*remove_passenger) (_THIS(Elevator), Passenger * passenger); SYNCHRONIZE PUBLIC void (*add_passenger)(_THIS(Elevator), Passenger * passenger); diff --git a/ElevatorBreaker/ElevatorBreaker.c b/ElevatorBreaker/ElevatorBreaker.c new file mode 100644 index 0000000..97013c0 --- /dev/null +++ b/ElevatorBreaker/ElevatorBreaker.c @@ -0,0 +1,47 @@ +// +// Created by Antoine Bartuccio on 22/06/2018. +// + +#include + +#include "ElevatorBreaker.h" +#include "../SharedData/SharedData.h" + +SETTER(ElevatorBreaker, int, thread_number); + +void _free__ElevatorBreaker(THIS(ElevatorBreaker)){ + free(this); +} + +void * runnable_ElevatorBreaker(void * void_this){ + ElevatorBreaker *this = (ElevatorBreaker*) void_this; + Elevator *selected_elevator = NULL; + SharedData *data = GET_INSTANCE(SharedData); + + AGENT_OPTIONS + + while (data->is_active_passengers_left(data)){ + usleep(900000); + // One chance out of two to break something + if ((rand() % 101) > 50) { + selected_elevator = data->get_main_building(data)->elevators[rand() % ELEVATOR_NB]; + selected_elevator->damage(selected_elevator); + printf("ALERTE : L'ascenseur %s est endommagé\n", selected_elevator->name); + } + } + + data->unregister_thread(data, this->thread_number); + return NULL; +} + +ElevatorBreaker *_init_ElevatorBreaker(){ + ElevatorBreaker *new_elevator_breaker = malloc_or_die(sizeof(ElevatorBreaker)); + new_elevator_breaker->thread_number = -1; + + LINK_ALL(ElevatorBreaker, new_elevator_breaker, + set_thread_number, + runnable + ) + + return new_elevator_breaker; +} \ No newline at end of file diff --git a/ElevatorBreaker/ElevatorBreaker.h b/ElevatorBreaker/ElevatorBreaker.h new file mode 100644 index 0000000..392c032 --- /dev/null +++ b/ElevatorBreaker/ElevatorBreaker.h @@ -0,0 +1,22 @@ +// +// Created by Antoine Bartuccio on 22/06/2018. +// + +#ifndef LO41_ELEVATORBREAKER_H +#define LO41_ELEVATORBREAKER_H + +#include "../Objects.h" + +typedef struct o_ElevatorBreaker { + PRIVATE int thread_number; + + PUBLIC void * (*runnable)(void * void_this); + PUBLIC void (*set_thread_number)(_THIS(ElevatorBreaker), int data); + + DESTRUCTOR(ElevatorBreaker); + +} ElevatorBreaker; + +ElevatorBreaker *_init_ElevatorBreaker(); + +#endif //LO41_ELEVATORBREAKER_H diff --git a/SharedData/SharedData.c b/SharedData/SharedData.c index 7dc6757..ee90945 100644 --- a/SharedData/SharedData.c +++ b/SharedData/SharedData.c @@ -46,8 +46,13 @@ void start_all_threads_SharedData(THIS(SharedData)){ pthread_mutex_lock(&this->mutex_active_passengers); pthread_mutex_lock(&this->mutex_threads); + + /* starting the elevator breaker */ + this->start_thread(this, this->elevator_breaker->runnable, this->elevator_breaker, 0); + this->elevator_breaker->set_thread_number(this->elevator_breaker, 0); + /* starts threading elevators */ - for (i=0; istart_thread(this, this->main_building->elevators[i]->runnable, this->main_building->elevators[i], i); this->main_building->elevators[i]->set_thread_number(this->main_building->elevators[i], i); @@ -105,6 +110,8 @@ void _free__SharedData(THIS(SharedData)){ if (this->main_building != NULL) DELETE(this->main_building); + DELETE(this->elevator_breaker); + pthread_mutex_unlock(&this->mutex_active_passengers); pthread_mutex_destroy(&this->mutex_active_passengers); pthread_mutex_unlock(&this->mutex_threads); @@ -143,6 +150,7 @@ SharedData *_get_instance_SharedData(){ new_shared_data->main_building = NULL; /* Should be set to NULL if freed before */ new_shared_data->threads_nb = 0; new_shared_data->active_passengers = 0; + new_shared_data->elevator_breaker = NEW(ElevatorBreaker); pthread_mutex_init(&new_shared_data->mutex_active_passengers, NULL); pthread_mutex_init(&new_shared_data->mutex_threads, NULL); diff --git a/SharedData/SharedData.h b/SharedData/SharedData.h index ffdbd34..af58997 100644 --- a/SharedData/SharedData.h +++ b/SharedData/SharedData.h @@ -8,10 +8,12 @@ #include #include "../Objects.h" #include "../Building/Building.h" +#include "../ElevatorBreaker/ElevatorBreaker.h" typedef struct o_SharedData { PRIVATE int threads_nb; PRIVATE int active_passengers; + PRIVATE ElevatorBreaker * elevator_breaker; PRIVATE pthread_t ** threads; PRIVATE Building * main_building; PRIVATE pthread_mutex_t mutex_active_passengers; diff --git a/main.c b/main.c index 3a80ea4..9adbb4d 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,6 @@ #include #include +#include #include "Objects.h" #include "List/List.h" @@ -17,6 +18,7 @@ void clean_exit(int error_code){ int main(int argc, char* argv[]) { SharedData * shared_data = GET_INSTANCE(SharedData); + srand((unsigned int) time(NULL)); signal(SIGINT, clean_exit); if(argc == 3){ shared_data->set_main_building(shared_data, NEW(Building, argv[1], argv[2]));