From 9b11ae08355961983f427f3099d6f754c4d21277 Mon Sep 17 00:00:00 2001 From: Aethor Date: Wed, 20 Jun 2018 19:36:00 +0200 Subject: [PATCH] huge refactor, pointy pointer --- Building/Building.c | 37 +++++++++++++++++++------------------ Building/Building.h | 4 ++-- Elevator/Elevator.c | 6 +++--- Elevator/Elevator.h | 2 +- Passenger/Passenger.c | 15 +++++++++++---- Passenger/Passenger.h | 4 +++- Resident/Resident.c | 6 +++--- Visitor/Visitor.c | 6 +++--- 8 files changed, 45 insertions(+), 35 deletions(-) diff --git a/Building/Building.c b/Building/Building.c index d859b2d..0f8c639 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -142,13 +142,13 @@ void _free__Building(THIS(Building)){ * @passenger the passenger to put in the elevator * @returns an elevator number ( between 0 and ELEVATOR_NB - 1, inclusive ), -1 if no elevator was found ready to accept the passenger */ -int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger passenger){ +int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger * passenger){ int i; /* Make assumption that a waiting elevator is not full */ pthread_mutex_lock(this->mutex_func_get_inside_elevator); printf("Test d'entrée à l'étage %d de %s : id %d\n", current_floor, - passenger.type == VISITOR ? passenger.visitor->name : passenger.resident->name, - passenger.type == VISITOR ? passenger.visitor->id : passenger.resident->id); + passenger->type == VISITOR ? passenger->visitor->name : passenger->resident->name, + passenger->type == VISITOR ? passenger->visitor->id : passenger->resident->id); for (i=0; ielevators[i]->can_get_inside(this->elevators[i], current_floor)){ this->elevators[i]->add_passenger(this->elevators[i], passenger); @@ -160,37 +160,38 @@ int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger pa return -1; } -void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger passenger){ +void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger * passenger){ int elevator_number; if (origin < 0 || origin >= FLOORS) {CRASH("You are trying to start from a non existing floor\n");} if (destination < 0 || destination >= FLOORS) {CRASH("You are trying to reach a non existing floor\n");} - //if(this->waiting_passengers->compare(this->waiting_passengers, (void*) &passenger, passenger.compare)) - this->waiting_passengers->insert_tail(this->waiting_passengers, (void*) &passenger, sizeof(passenger));//todo : check if inside list + if(!this->waiting_passengers->contains(this->waiting_passengers, (void*) passenger, passenger->compare)){ + this->waiting_passengers->insert_tail(this->waiting_passengers, (void*) passenger, sizeof(Passenger));//todo : check if inside list + } pthread_cond_wait(this->condition_floors[origin], this->mutex_cond_get_inside_elevator); elevator_number = this->get_inside_elevator(this, origin, passenger); if (elevator_number != -1){ //passenger accepted in elevator - if (passenger.type == RESIDENT) - printf("Le résident %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger.resident->name, + if (passenger->type == RESIDENT) + printf("Le résident %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger->resident->name, this->elevators[elevator_number]->name, origin); - else if (passenger.type == VISITOR) - printf("Le visiteur %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger.visitor->name, + else if (passenger->type == VISITOR) + printf("Le visiteur %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger->visitor->name, this->elevators[elevator_number]->name, origin); pthread_cond_wait(this->condition_floors[destination], this->mutex_cond_get_outside_elevator); - if (passenger.type == RESIDENT) - printf("Le résident %s sort de l'ascenseur %s à l'étage %d\n", passenger.resident->name, + if (passenger->type == RESIDENT) + printf("Le résident %s sort de l'ascenseur %s à l'étage %d\n", passenger->resident->name, this->elevators[elevator_number]->name, destination); - else if (passenger.type == VISITOR) - printf("Le visiteur %s sort de l'ascenseur %s à l'étage %d\n", passenger.visitor->name, + else if (passenger->type == VISITOR) + printf("Le visiteur %s sort de l'ascenseur %s à l'étage %d\n", passenger->visitor->name, this->elevators[elevator_number]->name, destination); }else{ - if (passenger.type == RESIDENT) - printf("Le résident %s à l'étage %d n'a pas pu rentrer dans un ascenseur. Préempté !\n", passenger.resident->name, origin); - else if (passenger.type == VISITOR) - printf("Le visiteur %s à l'étage %d n'a pas pu rentrer dans un ascenseur. Préempté !\n", passenger.visitor->name, origin); + if (passenger->type == RESIDENT) + printf("Le résident %s à l'étage %d n'a pas pu rentrer dans un ascenseur. Préempté !\n", passenger->resident->name, origin); + else if (passenger->type == VISITOR) + printf("Le visiteur %s à l'étage %d n'a pas pu rentrer dans un ascenseur. Préempté !\n", passenger->visitor->name, origin); //réarmement this->go_to_floor(this, origin, destination, passenger); } diff --git a/Building/Building.h b/Building/Building.h index 28f23d8..3e2862d 100644 --- a/Building/Building.h +++ b/Building/Building.h @@ -28,12 +28,12 @@ typedef struct o_Building { PRIVATE void (*parse_residents)(_THIS(Building), char * file); PRIVATE void (*parse_visitors)(_THIS(Building), char * file); - PRIVATE int (*get_inside_elevator)(_THIS(Building), int current_floor, Passenger passenger); + PRIVATE int (*get_inside_elevator)(_THIS(Building), int current_floor, Passenger * passenger); PUBLIC int (*use_call_box)(_THIS(Building), char * resident_name); PUBLIC void (*signal_elevator_at_floor)(_THIS(Building), int floor); - SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger passenger); + SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger * passenger); DESTRUCTOR(Building); } Building; diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index cf89e32..2703600 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -29,11 +29,11 @@ void _free__Elevator(THIS(Elevator)){ free(this); } -void add_passenger_Elevator(THIS(Elevator), Passenger passenger){ +void add_passenger_Elevator(THIS(Elevator), Passenger * passenger){ pthread_mutex_lock(&this->mutex_passengers); - this->passengers->insert_tail(this->passengers, ((void *)&passenger), sizeof(Passenger)); + this->passengers->insert_tail(this->passengers, ((void *)passenger), sizeof(Passenger)); printf("L'ascenseur %s recoit le visiteur %s\nIl y a maintenant %d passagers dans l'ascenseur %s\n", this->name, - passenger.type == VISITOR ? passenger.visitor->get_name(passenger.visitor) : passenger.resident->get_name(passenger.resident), + passenger->type == VISITOR ? passenger->visitor->get_name(passenger->visitor) : passenger->resident->get_name(passenger->resident), this->passengers->get_size(this->passengers), this->name); if (this->passengers->get_size(this->passengers) >= MAX_ELEVATOR_CAPACITY) this->set_state(this, SLEEPING); diff --git a/Elevator/Elevator.h b/Elevator/Elevator.h index 186e552..d953315 100644 --- a/Elevator/Elevator.h +++ b/Elevator/Elevator.h @@ -32,7 +32,7 @@ typedef struct o_Elevator { SYNCHRONIZE PUBLIC void (*repair)(_THIS(Elevator)); SYNCHRONIZE PUBLIC int (*get_number_of_passengers)(_THIS(Elevator)); - SYNCHRONIZE PUBLIC void (*add_passenger)(_THIS(Elevator), Passenger passenger); + SYNCHRONIZE PUBLIC void (*add_passenger)(_THIS(Elevator), Passenger * passenger); SYNCHRONIZE PUBLIC ELEVATOR_STATE (*get_state)(_THIS(Elevator)); SYNCHRONIZE PUBLIC int (*get_floor)(_THIS(Elevator)); SYNCHRONIZE PUBLIC int (*can_get_inside)(_THIS(Elevator), int floor); diff --git a/Passenger/Passenger.c b/Passenger/Passenger.c index 2a33e7d..a1eaf4b 100644 --- a/Passenger/Passenger.c +++ b/Passenger/Passenger.c @@ -4,6 +4,11 @@ #include "Passenger.h" +int compare_Passenger(void * passenger1, void * passenger2){ + return (strcmp(((Passenger*) passenger1)->get_name((Passenger*) passenger1), + ((Passenger*) passenger2)->get_name((Passenger*) passenger2)) == 0); +} + int get_id_Passenger(THIS(Passenger)){ if (this->type == RESIDENT) return this->resident->get_id(this->resident); @@ -33,23 +38,25 @@ void _free__Passenger(THIS(Passenger)){ free(this); } -Passenger *_init_Passenger(void *passenger, PASSENGER_TYPE type){ +Passenger *_init_Passenger(void* passenger_data, PASSENGER_TYPE type){ Passenger * new_passenger = malloc_or_die(sizeof(Passenger)); new_passenger->resident = NULL; new_passenger->visitor = NULL; new_passenger->type = type; if (type == RESIDENT) - new_passenger->resident = (Resident*) passenger; + new_passenger->resident = (Resident*) passenger_data; if (type == VISITOR) - new_passenger->visitor = (Visitor*) passenger; + new_passenger->visitor = (Visitor*) passenger_data; + //new_passenger->compare = compare_Passenger; LINK_ALL(Passenger, new_passenger, get_id, get_name, + compare, runnable ) return new_passenger; -} \ No newline at end of file +} diff --git a/Passenger/Passenger.h b/Passenger/Passenger.h index 896b378..0f64456 100644 --- a/Passenger/Passenger.h +++ b/Passenger/Passenger.h @@ -7,6 +7,7 @@ #include "../Resident/Resident.h" #include "../Visitor/Visitor.h" +#include typedef enum {RESIDENT, VISITOR} PASSENGER_TYPE; @@ -20,10 +21,11 @@ typedef struct o_Passenger { PUBLIC char * (*get_name)(_THIS(Passenger)); PUBLIC int (*get_id)(_THIS(Passenger)); PUBLIC void * (*runnable)(void* void_this); + PUBLIC int (*compare)(void * passenger1, void * passenger2);//yeah I know, but i needed int (*) (void*, void*) DESTRUCTOR(Passenger); } Passenger; -Passenger *_init_Passenger(void *passenger, PASSENGER_TYPE type); +Passenger *_init_Passenger(void* passenger_data, PASSENGER_TYPE type); #endif //LO41_PASSENGER_H diff --git a/Resident/Resident.c b/Resident/Resident.c index 84a7468..7423b94 100644 --- a/Resident/Resident.c +++ b/Resident/Resident.c @@ -14,12 +14,12 @@ GETTER(Resident, int, apartment_floor); void * runnable_Resident(void * void_this){ Resident * this = (Resident*) void_this; SharedData* data = GET_INSTANCE(SharedData); - Passenger passenger; + Passenger * passenger = NEW(Passenger, this, RESIDENT); AGENT_OPTIONS; - passenger.resident = this; - passenger.type = RESIDENT; + passenger->resident = this; + passenger->type = RESIDENT; printf("Je suis le resident %s et je suis a l'etage %d en direction de l'etage %d\n", this->name, this->apartment_floor, this->destination); diff --git a/Visitor/Visitor.c b/Visitor/Visitor.c index ce93c42..3f1a534 100644 --- a/Visitor/Visitor.c +++ b/Visitor/Visitor.c @@ -12,16 +12,16 @@ GETTER(Visitor, int, id); void * runnable_Visitor(void * void_this){ Visitor *this = (Visitor*) void_this; SharedData * data = GET_INSTANCE(SharedData); - Passenger passenger; + Passenger * passenger = NEW(Passenger, (void*) this, VISITOR); AGENT_OPTIONS; - passenger.visitor = this; - passenger.type = VISITOR; + passenger->visitor = this; printf("Bonjour, je suis %s et je souhaite rendre visite a %s\n", this->name, this->contact_name); printf("Bip, %s appel a l'interphone\n%s habite a l'etage %d\n", this->name, this->contact_name, (this->destination = data->use_call_box(data, this->contact_name))); data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger); + DELETE(passenger); return NULL; }