From 5b0b83e4352bdb2a9c0fcb78e43eddfd78ea168a Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 20 Jun 2018 17:46:14 +0200 Subject: [PATCH 01/15] =?UTF-8?q?Fonction=20contains,=20j'ai=20pas=20test?= =?UTF-8?q?=C3=A9=20lol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- List/List.c | 23 ++++++++++++++++++++++- List/List.h | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/List/List.c b/List/List.c index b4553da..2f22f1d 100644 --- a/List/List.c +++ b/List/List.c @@ -140,6 +140,26 @@ void insert_inside_List(THIS(List), void * data, size_t data_size, int index){ } +/** + * Check if data_to_find exist in list + * @param this THIS(List) + * @param data_to_find pointer to data you want to find inside + * @param compare a compare function, return true or false and takes two void * pointers + * @return true or false if the element is within the list or not + */ +int contains_List(THIS(List), void * data_to_find, int (*compare)(void *, void *)){ + Element * current = this->head; + + while (current != NULL){ + if (compare(data_to_find, current->data)){ + return 1; + } + current = current->next; + } + + return 0; +} + void remove_head_List(THIS(List)){ DELETE(this->head); } @@ -170,7 +190,8 @@ List *_init_List(){ clear_custom, clear, remove_head, - remove_tail + remove_tail, + contains ) return l; } diff --git a/List/List.h b/List/List.h index 6af0f0d..0c9511b 100644 --- a/List/List.h +++ b/List/List.h @@ -29,6 +29,7 @@ struct o_List { PUBLIC void* (*get_element_data)(_THIS(List), int index); PUBLIC int (*get_size)(_THIS(List)); + PUBLIC int (*contains)(_THIS(List), void * data_to_find, int (*compare)(void *, void *)); PUBLIC void (*set_custom_free)(_THIS(List), void (*custom_free)(void *)); PUBLIC void (*insert_inside)(_THIS(List), void * data, size_t data_size, int index); From df740634add657a474c3966ef8043bc55c956fa1 Mon Sep 17 00:00:00 2001 From: Aethor Date: Wed, 20 Jun 2018 17:49:18 +0200 Subject: [PATCH 02/15] =?UTF-8?q?pr=C3=A9empt=C3=A9=20=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Building/Building.c | 13 ++++++++----- Building/Building.h | 2 ++ Resident/Resident.c | 9 ++++++++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Building/Building.c b/Building/Building.c index fcabb1a..b2b3735 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -146,13 +146,12 @@ int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger pa 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.visitor->name, passenger.visitor->id); + 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); for (i=0; ielevators[i]->can_get_inside(this->elevators[i], current_floor)){ - /* pour faire taire le compilateur le temps que je revienne sur cette fonction */ - //if (passenger.type == VISITOR) TODO : ??? this->elevators[i]->add_passenger(this->elevators[i], passenger); - /* Il faut faire des trucs ici */ pthread_mutex_unlock(this->mutex_func_get_inside_elevator); return i; } @@ -167,6 +166,7 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger 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");} + this->waiting_passengers->insert_tail(this->waiting_passengers, (void*) &passenger, sizeof(passenger)); pthread_cond_wait(this->condition_floors[origin], this->mutex_cond_get_inside_elevator); elevator_number = this->get_inside_elevator(this, origin, passenger); @@ -190,7 +190,9 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger 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); - }//todo : else, remettre en attente ? + //réarmement + this->go_to_floor(this, origin, destination, passenger); + } } @@ -228,6 +230,7 @@ Building *_init_Building(char * residents_file, char * visitors_file){ new_building->condition_floors = malloc_or_die(sizeof(pthread_cond_t*) * FLOORS); new_building->residents = NEW(List); new_building->visitors = NEW(List); + new_building->waiting_passengers = NEW(List); for (i=0; ielevators[i] = NEW(Elevator, elevator_name); diff --git a/Building/Building.h b/Building/Building.h index 42711cf..28f23d8 100644 --- a/Building/Building.h +++ b/Building/Building.h @@ -19,6 +19,7 @@ typedef struct o_Building { PRIVATE int floors; PRIVATE List * residents; PRIVATE List * visitors; + PRIVATE List * waiting_passengers; PRIVATE Elevator ** elevators; PRIVATE pthread_mutex_t * mutex_cond_get_inside_elevator; PRIVATE pthread_mutex_t * mutex_cond_get_outside_elevator; @@ -40,6 +41,7 @@ typedef struct o_Building { FRIENDLY(residents, SharedData) FRIENDLY(visitors, SharedData) FRIENDLY(elevators, SharedData) +FRIENDLY(waiting_passengers, SharedData) Building *_init_Building(char * residents_file, char * visitors_file); diff --git a/Resident/Resident.c b/Resident/Resident.c index cccf30c..84a7468 100644 --- a/Resident/Resident.c +++ b/Resident/Resident.c @@ -5,6 +5,7 @@ #include #include #include "Resident.h" +#include "../SharedData/SharedData.h" GETTER(Resident, char *, name); GETTER(Resident, int, id); @@ -12,11 +13,17 @@ GETTER(Resident, int, apartment_floor); void * runnable_Resident(void * void_this){ Resident * this = (Resident*) void_this; + SharedData* data = GET_INSTANCE(SharedData); + Passenger passenger; - AGENT_OPTIONS + AGENT_OPTIONS; + + 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); + data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger); return NULL; } From 4f1179bcbebc80e7d23ea1c87aa9a9f1d26d5fdb Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 20 Jun 2018 17:46:14 +0200 Subject: [PATCH 03/15] =?UTF-8?q?Fonction=20contains,=20j'ai=20pas=20test?= =?UTF-8?q?=C3=A9=20lol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- List/List.c | 23 ++++++++++++++++++++++- List/List.h | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/List/List.c b/List/List.c index b4553da..2f22f1d 100644 --- a/List/List.c +++ b/List/List.c @@ -140,6 +140,26 @@ void insert_inside_List(THIS(List), void * data, size_t data_size, int index){ } +/** + * Check if data_to_find exist in list + * @param this THIS(List) + * @param data_to_find pointer to data you want to find inside + * @param compare a compare function, return true or false and takes two void * pointers + * @return true or false if the element is within the list or not + */ +int contains_List(THIS(List), void * data_to_find, int (*compare)(void *, void *)){ + Element * current = this->head; + + while (current != NULL){ + if (compare(data_to_find, current->data)){ + return 1; + } + current = current->next; + } + + return 0; +} + void remove_head_List(THIS(List)){ DELETE(this->head); } @@ -170,7 +190,8 @@ List *_init_List(){ clear_custom, clear, remove_head, - remove_tail + remove_tail, + contains ) return l; } diff --git a/List/List.h b/List/List.h index 6af0f0d..0c9511b 100644 --- a/List/List.h +++ b/List/List.h @@ -29,6 +29,7 @@ struct o_List { PUBLIC void* (*get_element_data)(_THIS(List), int index); PUBLIC int (*get_size)(_THIS(List)); + PUBLIC int (*contains)(_THIS(List), void * data_to_find, int (*compare)(void *, void *)); PUBLIC void (*set_custom_free)(_THIS(List), void (*custom_free)(void *)); PUBLIC void (*insert_inside)(_THIS(List), void * data, size_t data_size, int index); From 3dbdc47452fcbbead61cb041b90d9b735cc08fc8 Mon Sep 17 00:00:00 2001 From: Aethor Date: Wed, 20 Jun 2018 18:05:37 +0200 Subject: [PATCH 04/15] ~~ --- Building/Building.c | 3 ++- Visitor/Visitor.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Building/Building.c b/Building/Building.c index b2b3735..d859b2d 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -166,7 +166,8 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger 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");} - this->waiting_passengers->insert_tail(this->waiting_passengers, (void*) &passenger, sizeof(passenger)); + //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 pthread_cond_wait(this->condition_floors[origin], this->mutex_cond_get_inside_elevator); elevator_number = this->get_inside_elevator(this, origin, passenger); diff --git a/Visitor/Visitor.c b/Visitor/Visitor.c index 0cf4ccd..ce93c42 100644 --- a/Visitor/Visitor.c +++ b/Visitor/Visitor.c @@ -14,7 +14,7 @@ void * runnable_Visitor(void * void_this){ SharedData * data = GET_INSTANCE(SharedData); Passenger passenger; - AGENT_OPTIONS + AGENT_OPTIONS; passenger.visitor = this; passenger.type = VISITOR; From d1576231356623353f2d5a90f41cdac7b40812ab Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 20 Jun 2018 17:46:14 +0200 Subject: [PATCH 05/15] =?UTF-8?q?Fonction=20contains,=20j'ai=20pas=20test?= =?UTF-8?q?=C3=A9=20lol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- List/List.c | 23 ++++++++++++++++++++++- List/List.h | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/List/List.c b/List/List.c index b4553da..2f22f1d 100644 --- a/List/List.c +++ b/List/List.c @@ -140,6 +140,26 @@ void insert_inside_List(THIS(List), void * data, size_t data_size, int index){ } +/** + * Check if data_to_find exist in list + * @param this THIS(List) + * @param data_to_find pointer to data you want to find inside + * @param compare a compare function, return true or false and takes two void * pointers + * @return true or false if the element is within the list or not + */ +int contains_List(THIS(List), void * data_to_find, int (*compare)(void *, void *)){ + Element * current = this->head; + + while (current != NULL){ + if (compare(data_to_find, current->data)){ + return 1; + } + current = current->next; + } + + return 0; +} + void remove_head_List(THIS(List)){ DELETE(this->head); } @@ -170,7 +190,8 @@ List *_init_List(){ clear_custom, clear, remove_head, - remove_tail + remove_tail, + contains ) return l; } diff --git a/List/List.h b/List/List.h index 6af0f0d..0c9511b 100644 --- a/List/List.h +++ b/List/List.h @@ -29,6 +29,7 @@ struct o_List { PUBLIC void* (*get_element_data)(_THIS(List), int index); PUBLIC int (*get_size)(_THIS(List)); + PUBLIC int (*contains)(_THIS(List), void * data_to_find, int (*compare)(void *, void *)); PUBLIC void (*set_custom_free)(_THIS(List), void (*custom_free)(void *)); PUBLIC void (*insert_inside)(_THIS(List), void * data, size_t data_size, int index); From f6f12b398d5436f7a21a85d8b73786de3f18dbc6 Mon Sep 17 00:00:00 2001 From: Aethor Date: Wed, 20 Jun 2018 18:05:37 +0200 Subject: [PATCH 06/15] ~~ --- Building/Building.c | 3 ++- Visitor/Visitor.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Building/Building.c b/Building/Building.c index b2b3735..d859b2d 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -166,7 +166,8 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger 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");} - this->waiting_passengers->insert_tail(this->waiting_passengers, (void*) &passenger, sizeof(passenger)); + //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 pthread_cond_wait(this->condition_floors[origin], this->mutex_cond_get_inside_elevator); elevator_number = this->get_inside_elevator(this, origin, passenger); diff --git a/Visitor/Visitor.c b/Visitor/Visitor.c index 0cf4ccd..ce93c42 100644 --- a/Visitor/Visitor.c +++ b/Visitor/Visitor.c @@ -14,7 +14,7 @@ void * runnable_Visitor(void * void_this){ SharedData * data = GET_INSTANCE(SharedData); Passenger passenger; - AGENT_OPTIONS + AGENT_OPTIONS; passenger.visitor = this; passenger.type = VISITOR; From 9b11ae08355961983f427f3099d6f754c4d21277 Mon Sep 17 00:00:00 2001 From: Aethor Date: Wed, 20 Jun 2018 19:36:00 +0200 Subject: [PATCH 07/15] 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; } From 7315e1595eb05268fa1a9330c3e8112d948ff75f Mon Sep 17 00:00:00 2001 From: Aethor Date: Wed, 20 Jun 2018 22:06:45 +0200 Subject: [PATCH 08/15] c'est plus beau --- Building/Building.c | 18 ++++++++---------- Elevator/Elevator.c | 4 ++-- Passenger/Passenger.c | 17 +++++++++++++---- Passenger/Passenger.h | 1 + Resident/Resident.c | 13 ++++++++----- Resident/Resident.h | 1 + Visitor/Visitor.c | 8 +++++--- Visitor/Visitor.h | 1 + 8 files changed, 39 insertions(+), 24 deletions(-) diff --git a/Building/Building.c b/Building/Building.c index 0f8c639..8b14c9d 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -146,9 +146,7 @@ int get_inside_elevator_Building(THIS(Building), int current_floor, 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); + printf("Test d'entrée à l'étage %d de %s : id %d\n", current_floor, passenger->get_name(passenger), passenger->get_id(passenger)); for (i=0; ielevators[i]->can_get_inside(this->elevators[i], current_floor)){ this->elevators[i]->add_passenger(this->elevators[i], passenger); @@ -174,25 +172,25 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, 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, + printf("Le résident %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger->get_name(passenger), 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, + printf("Le visiteur %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger->get_name(passenger), 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, + printf("Le résident %s sort de l'ascenseur %s à l'étage %d\n", passenger->get_name(passenger), 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, + printf("Le visiteur %s sort de l'ascenseur %s à l'étage %d\n", passenger->get_name(passenger), 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); + printf("Le résident %s à l'étage %d n'a pas pu rentrer dans un ascenseur. Préempté !\n", passenger->get_name(passenger), 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 + printf("Le visiteur %s à l'étage %d n'a pas pu rentrer dans un ascenseur. Préempté !\n", passenger->get_name(passenger), origin); + //reloading fire this->go_to_floor(this, origin, destination, passenger); } diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index 2703600..b791bc4 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -33,8 +33,8 @@ void add_passenger_Elevator(THIS(Elevator), Passenger * passenger){ pthread_mutex_lock(&this->mutex_passengers); 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), - this->passengers->get_size(this->passengers), this->name); + passenger->get_name(passenger), + this->passengers->get_size(this->passengers), this->name); if (this->passengers->get_size(this->passengers) >= MAX_ELEVATOR_CAPACITY) this->set_state(this, SLEEPING); pthread_mutex_unlock(&this->mutex_passengers); diff --git a/Passenger/Passenger.c b/Passenger/Passenger.c index a1eaf4b..3a6f7b5 100644 --- a/Passenger/Passenger.c +++ b/Passenger/Passenger.c @@ -9,6 +9,14 @@ int compare_Passenger(void * passenger1, void * passenger2){ ((Passenger*) passenger2)->get_name((Passenger*) passenger2)) == 0); } +int get_destination_Passenger(THIS(Passenger)){ + if (this->type == RESIDENT) + return this->resident->get_destination(this->resident); + if (this->type == VISITOR) + return this->visitor->get_destination(this->visitor); + return -1; +} + int get_id_Passenger(THIS(Passenger)){ if (this->type == RESIDENT) return this->resident->get_id(this->resident); @@ -52,10 +60,11 @@ Passenger *_init_Passenger(void* passenger_data, PASSENGER_TYPE type){ //new_passenger->compare = compare_Passenger; LINK_ALL(Passenger, new_passenger, - get_id, - get_name, - compare, - runnable + get_id, + get_name, + get_destination, + compare, + runnable ) return new_passenger; diff --git a/Passenger/Passenger.h b/Passenger/Passenger.h index 0f64456..b29f0d0 100644 --- a/Passenger/Passenger.h +++ b/Passenger/Passenger.h @@ -20,6 +20,7 @@ typedef struct o_Passenger { PUBLIC char * (*get_name)(_THIS(Passenger)); PUBLIC int (*get_id)(_THIS(Passenger)); + PUBLIC int (*get_destination)(_THIS(Passenger)); PUBLIC void * (*runnable)(void* void_this); PUBLIC int (*compare)(void * passenger1, void * passenger2);//yeah I know, but i needed int (*) (void*, void*) diff --git a/Resident/Resident.c b/Resident/Resident.c index 7423b94..6e4aac4 100644 --- a/Resident/Resident.c +++ b/Resident/Resident.c @@ -8,13 +8,14 @@ #include "../SharedData/SharedData.h" GETTER(Resident, char *, name); +GETTER(Resident, int, destination); GETTER(Resident, int, id); GETTER(Resident, int, apartment_floor); void * runnable_Resident(void * void_this){ Resident * this = (Resident*) void_this; SharedData* data = GET_INSTANCE(SharedData); - Passenger * passenger = NEW(Passenger, this, RESIDENT); + Passenger * passenger = NEW(Passenger, (void*) this, RESIDENT); AGENT_OPTIONS; @@ -24,6 +25,7 @@ void * runnable_Resident(void * void_this){ 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); data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger); + DELETE(passenger); return NULL; } @@ -43,10 +45,11 @@ Resident *_init_Resident(int id, char* name, int apartment_floor, int destinatio new_resident->destination = destination; LINK_ALL(Resident, new_resident, - get_name, - get_id, - runnable, - get_apartment_floor + get_name, + get_destination, + get_id, + runnable, + get_apartment_floor ) return new_resident; diff --git a/Resident/Resident.h b/Resident/Resident.h index 2dfbaa1..969b584 100644 --- a/Resident/Resident.h +++ b/Resident/Resident.h @@ -18,6 +18,7 @@ typedef struct o_Resident { PUBLIC char * (*get_name)(_THIS(Resident)); PUBLIC int (*get_id)(_THIS(Resident)); PUBLIC int (*get_apartment_floor)(_THIS(Resident)); + PUBLIC int (*get_destination)(_THIS(Resident)); DESTRUCTOR(Resident); } Resident; diff --git a/Visitor/Visitor.c b/Visitor/Visitor.c index 3f1a534..56fe7f1 100644 --- a/Visitor/Visitor.c +++ b/Visitor/Visitor.c @@ -7,6 +7,7 @@ #include GETTER(Visitor, char*, name); +GETTER(Visitor, int, destination); GETTER(Visitor, int, id); void * runnable_Visitor(void * void_this){ @@ -46,9 +47,10 @@ Visitor *_init_Visitor(int id, char* name, char * contact_name){ new_visitor->contact_name = NULL; LINK_ALL(Visitor, new_visitor, - get_name, - get_id, - runnable + get_name, + get_destination, + get_id, + runnable ); return new_visitor; diff --git a/Visitor/Visitor.h b/Visitor/Visitor.h index 6164c4f..4ad33ed 100644 --- a/Visitor/Visitor.h +++ b/Visitor/Visitor.h @@ -17,6 +17,7 @@ typedef struct o_Visitor { PUBLIC void * (*runnable)(void* void_this); PUBLIC char * (*get_name)(_THIS(Visitor)); PUBLIC int (*get_id)(_THIS(Visitor)); + PUBLIC int (*get_destination)(_THIS(Visitor)); DESTRUCTOR(Visitor); } Visitor; From f066e224196de6a3ee12e2877a4beac12e8717ef Mon Sep 17 00:00:00 2001 From: Aethor Date: Wed, 20 Jun 2018 22:11:48 +0200 Subject: [PATCH 09/15] PREEMPTE --- Building/Building.c | 4 ++-- Elevator/Elevator.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Building/Building.c b/Building/Building.c index 8b14c9d..411c161 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -168,8 +168,8 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger 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); + 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->get_name(passenger), @@ -191,7 +191,7 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger else if (passenger->type == VISITOR) printf("Le visiteur %s à l'étage %d n'a pas pu rentrer dans un ascenseur. Préempté !\n", passenger->get_name(passenger), origin); //reloading fire - this->go_to_floor(this, origin, destination, passenger); + //this->go_to_floor(this, origin, destination, passenger); } } diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index b791bc4..b1e2143 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -68,7 +68,7 @@ int get_next_floor_Elevator(THIS(Elevator)){ for(i=0;ipassengers->get_size(this->passengers);i++){ temp_element = this->passengers->get_element(this->passengers, i); temp_passenger = (Passenger*) temp_element->get_data(temp_element); - temp_floor = temp_passenger->type == RESIDENT ? temp_passenger->resident->destination : temp_passenger->visitor->destination; + temp_floor = temp_passenger->get_destination(temp_passenger); if(abs(this->floor - temp_floor) < min_diff && temp_floor != this->floor){ min_diff = abs(this->floor - temp_floor); next_floor = temp_floor; From 92c784a76b999df760f5663ed7718014ab301c6c Mon Sep 17 00:00:00 2001 From: Amalvy Arthur Date: Thu, 21 Jun 2018 16:15:33 +0200 Subject: [PATCH 10/15] du refactor --- Building/Building.c | 36 ++++++++++++++++++++++++++---------- Building/Building.h | 5 ++++- Elevator/Elevator.c | 1 + Resident/Resident.c | 5 ++++- Resident/Resident.h | 1 + Visitor/Visitor.c | 5 ++++- Visitor/Visitor.h | 1 + 7 files changed, 41 insertions(+), 13 deletions(-) diff --git a/Building/Building.c b/Building/Building.c index 411c161..9e75a78 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -8,12 +8,19 @@ #define LINE_BUFFER 256 +SYNCHRONIZED_GETTER(Building, int*, waiting_floors) + void remove_end_char(char * string, char character){ size_t string_size = strlen(string); if (string[string_size - 1] == character) string[string_size - 1] = '\0'; } +int get_target_Building(THIS(Building)){ + int* waiting_floors = this->get_waiting_floors(this); + return waiting_floors[1]; +} + List * split(char * line, char * separator){ List * split = NEW(List); char * to_delete = strdup(line); @@ -118,6 +125,9 @@ void _free__Building(THIS(Building)){ pthread_mutex_unlock(this->mutex_func_get_inside_elevator); pthread_mutex_destroy(this->mutex_func_get_inside_elevator); + pthread_mutex_unlock(&this->mutex_waiting_floors); + pthread_mutex_destroy(&this->mutex_waiting_floors); + DELETE(this->residents); DELETE(this->visitors); for (i=0; imutex_cond_get_inside_elevator); free(this->mutex_cond_get_outside_elevator); free(this->mutex_func_get_inside_elevator); + //free(&this->mutex_waiting_floors); free(this->elevators); free(this); @@ -164,9 +175,9 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger 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->contains(this->waiting_passengers, (void*) passenger, passenger->compare)){ + /*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); @@ -215,8 +226,12 @@ Building *_init_Building(char * residents_file, char * visitors_file){ Building * new_building = malloc_or_die(sizeof(Building)); char elevator_name[] = "@"; int i; - new_building->floors = FLOORS; + + for (i=0; iwaiting_floors[i] = 0; + pthread_mutex_init(&new_building->mutex_waiting_floors, NULL); + new_building->elevators = malloc_or_die(sizeof(Elevator*) * ELEVATOR_NB); new_building->mutex_cond_get_inside_elevator = malloc_or_die(sizeof(pthread_mutex_t)); @@ -230,7 +245,6 @@ Building *_init_Building(char * residents_file, char * visitors_file){ new_building->condition_floors = malloc_or_die(sizeof(pthread_cond_t*) * FLOORS); new_building->residents = NEW(List); new_building->visitors = NEW(List); - new_building->waiting_passengers = NEW(List); for (i=0; ielevators[i] = NEW(Elevator, elevator_name); @@ -243,12 +257,14 @@ 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, - signal_elevator_at_floor + parse_residents, + parse_visitors, + get_inside_elevator, + use_call_box, + go_to_floor, + get_target, + get_waiting_floors, + signal_elevator_at_floor ) if (residents_file != NULL) diff --git a/Building/Building.h b/Building/Building.h index 3e2862d..3879d8c 100644 --- a/Building/Building.h +++ b/Building/Building.h @@ -17,10 +17,11 @@ typedef struct o_Building { PRIVATE int floors; + PRIVATE int waiting_floors[FLOORS]; PRIVATE List * residents; PRIVATE List * visitors; - PRIVATE List * waiting_passengers; PRIVATE Elevator ** elevators; + PRIVATE pthread_mutex_t mutex_waiting_floors; 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; @@ -32,6 +33,8 @@ typedef struct o_Building { PUBLIC int (*use_call_box)(_THIS(Building), char * resident_name); PUBLIC void (*signal_elevator_at_floor)(_THIS(Building), int floor); + PUBLIC int (*get_target)(_THIS(Building)); + PUBLIC int* (*get_waiting_floors)(_THIS(Building)); SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger * passenger); diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index b1e2143..af010f8 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -110,6 +110,7 @@ void *runnable_Elevator(void * void_this){ printf("Je suis l'ascenseur %s\n", this->name); for (;;){ + data->main_building->get_waiting_floors(data->main_building); data->main_building->signal_elevator_at_floor(data->main_building, this->get_floor(this)); this->set_floor(this, this->get_next_floor(this)); } diff --git a/Resident/Resident.c b/Resident/Resident.c index 6e4aac4..ede284f 100644 --- a/Resident/Resident.c +++ b/Resident/Resident.c @@ -19,19 +19,21 @@ void * runnable_Resident(void * void_this){ AGENT_OPTIONS; + this->passenger = passenger; 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); data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger); - DELETE(passenger); return NULL; } void _free__Resident(THIS(Resident)){ if (this->name != NULL) free(this->name); + if(this->passenger != NULL) + free(this->passenger); free(this); } @@ -43,6 +45,7 @@ Resident *_init_Resident(int id, char* name, int apartment_floor, int destinatio new_resident->apartment_floor = apartment_floor; new_resident->position = new_resident->apartment_floor; new_resident->destination = destination; + new_resident->passenger = NULL; LINK_ALL(Resident, new_resident, get_name, diff --git a/Resident/Resident.h b/Resident/Resident.h index 969b584..3641713 100644 --- a/Resident/Resident.h +++ b/Resident/Resident.h @@ -13,6 +13,7 @@ typedef struct o_Resident { PRIVATE int destination; PRIVATE int position; PRIVATE char* name; + PRIVATE void* passenger; PUBLIC void * (*runnable)(void * void_this); PUBLIC char * (*get_name)(_THIS(Resident)); diff --git a/Visitor/Visitor.c b/Visitor/Visitor.c index 56fe7f1..e59f4dc 100644 --- a/Visitor/Visitor.c +++ b/Visitor/Visitor.c @@ -16,13 +16,13 @@ void * runnable_Visitor(void * void_this){ Passenger * passenger = NEW(Passenger, (void*) this, VISITOR); AGENT_OPTIONS; + this->passenger = (void*) passenger; 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; } @@ -31,6 +31,8 @@ void _free__Visitor(THIS(Visitor)){ free(this->name); if (this->contact_name != NULL) free(this->contact_name); + if (this->passenger != NULL) + free(this->passenger); free(this); } @@ -40,6 +42,7 @@ Visitor *_init_Visitor(int id, char* name, char * contact_name){ new_visitor->id = id; new_visitor->position = 0; new_visitor->destination = -1; + new_visitor->passenger = NULL; if (contact_name != NULL) new_visitor->contact_name = strdup(contact_name); diff --git a/Visitor/Visitor.h b/Visitor/Visitor.h index 4ad33ed..734c144 100644 --- a/Visitor/Visitor.h +++ b/Visitor/Visitor.h @@ -13,6 +13,7 @@ typedef struct o_Visitor { PRIVATE char * contact_name; PRIVATE int position; PRIVATE int destination; + PRIVATE void * passenger; PUBLIC void * (*runnable)(void* void_this); PUBLIC char * (*get_name)(_THIS(Visitor)); From 479cbb68462614376fc89180b629f3f2c44eca47 Mon Sep 17 00:00:00 2001 From: Aethor Date: Thu, 21 Jun 2018 17:39:30 +0200 Subject: [PATCH 11/15] Added useful functions for elevator IA --- Building/Building.c | 33 +++++++++++++++++++++++++++++---- Building/Building.h | 4 ++-- Elevator/Elevator.c | 41 +++++++++++++++++++++++++---------------- Elevator/Elevator.h | 3 ++- 4 files changed, 58 insertions(+), 23 deletions(-) diff --git a/Building/Building.c b/Building/Building.c index 9e75a78..47d7636 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -2,6 +2,7 @@ // Created by Antoine Bartuccio on 05/06/2018. // #include +#include #include "Building.h" #include "../Resident/Resident.h" #include "../Visitor/Visitor.h" @@ -16,9 +17,28 @@ void remove_end_char(char * string, char character){ string[string_size - 1] = '\0'; } -int get_target_Building(THIS(Building)){ +/** + * Get the best call for the elevator + *@param THIS(Building) : this + *@param elevator_floor : actual floor of the requesting elevator + *@returns the closest floor where a client is calling as an int. If no client is calling, returns -1. + */ +int get_next_call_Building(THIS(Building), int elevator_floor){ + pthread_mutex_lock(this->mutex_func_get_next_call); int* waiting_floors = this->get_waiting_floors(this); - return waiting_floors[1]; + int i; + float best_diff = INFINITY; + int next_target = -1; + for(i=0; i 0){ + if(abs(elevator_floor - waiting_floors[i]) < best_diff){ + best_diff = abs(elevator_floor - waiting_floors[i]); + next_target = waiting_floors[i]; + } + } + } + pthread_mutex_unlock(this->mutex_func_get_next_call); + return next_target; } List * split(char * line, char * separator){ @@ -125,6 +145,9 @@ void _free__Building(THIS(Building)){ pthread_mutex_unlock(this->mutex_func_get_inside_elevator); pthread_mutex_destroy(this->mutex_func_get_inside_elevator); + pthread_mutex_unlock(this->mutex_func_get_next_call); + pthread_mutex_destroy(this->mutex_func_get_next_call); + pthread_mutex_unlock(&this->mutex_waiting_floors); pthread_mutex_destroy(&this->mutex_waiting_floors); @@ -140,7 +163,7 @@ void _free__Building(THIS(Building)){ free(this->mutex_cond_get_inside_elevator); free(this->mutex_cond_get_outside_elevator); free(this->mutex_func_get_inside_elevator); - //free(&this->mutex_waiting_floors); + free(this->mutex_func_get_next_call); free(this->elevators); free(this); @@ -237,10 +260,12 @@ Building *_init_Building(char * residents_file, char * visitors_file){ new_building->mutex_cond_get_inside_elevator = malloc_or_die(sizeof(pthread_mutex_t)); new_building->mutex_cond_get_outside_elevator = malloc_or_die(sizeof(pthread_mutex_t)); new_building->mutex_func_get_inside_elevator = malloc_or_die(sizeof(pthread_mutex_t)); + new_building->mutex_func_get_next_call = malloc_or_die(sizeof(pthread_mutex_t)); pthread_mutex_init(new_building->mutex_cond_get_inside_elevator, NULL); pthread_mutex_init(new_building->mutex_cond_get_outside_elevator, NULL); pthread_mutex_init(new_building->mutex_func_get_inside_elevator, NULL); + pthread_mutex_init(new_building->mutex_func_get_next_call, NULL); new_building->condition_floors = malloc_or_die(sizeof(pthread_cond_t*) * FLOORS); new_building->residents = NEW(List); @@ -262,7 +287,7 @@ Building *_init_Building(char * residents_file, char * visitors_file){ get_inside_elevator, use_call_box, go_to_floor, - get_target, + get_next_call, get_waiting_floors, signal_elevator_at_floor ) diff --git a/Building/Building.h b/Building/Building.h index 3879d8c..d642d7d 100644 --- a/Building/Building.h +++ b/Building/Building.h @@ -25,6 +25,7 @@ typedef struct o_Building { 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; + PRIVATE pthread_mutex_t * mutex_func_get_next_call; PRIVATE pthread_cond_t ** condition_floors; PRIVATE void (*parse_residents)(_THIS(Building), char * file); @@ -33,7 +34,7 @@ typedef struct o_Building { PUBLIC int (*use_call_box)(_THIS(Building), char * resident_name); PUBLIC void (*signal_elevator_at_floor)(_THIS(Building), int floor); - PUBLIC int (*get_target)(_THIS(Building)); + PUBLIC int (*get_next_call)(_THIS(Building), int elevator_floor); PUBLIC int* (*get_waiting_floors)(_THIS(Building)); SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger * passenger); @@ -44,7 +45,6 @@ typedef struct o_Building { FRIENDLY(residents, SharedData) FRIENDLY(visitors, SharedData) FRIENDLY(elevators, SharedData) -FRIENDLY(waiting_passengers, SharedData) Building *_init_Building(char * residents_file, char * visitors_file); diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index af010f8..6f679dc 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -51,10 +51,10 @@ int get_number_of_passengers_Elevator(THIS(Elevator)){ /** * Search the closest floor where the elevator should stop. In practice ( for now ), it means that this function returns the closest floor amongs it's passengers destinations. * @THIS(Elevator) a pointer the current Elevator - * @return the found floor as an int. If no passengers are in the elevator, returns 0 ( as the elevator should come back to floor 0 ). + * @return the found floor as an int. If no passengers are in the elevator, returns -1. * @todo should consider passenger calling the elevator */ -int get_next_floor_Elevator(THIS(Elevator)){ +int get_next_passenger_stop_Elevator(THIS(Elevator)){ int i, next_floor, temp_floor; float min_diff; Element* temp_element; @@ -63,7 +63,7 @@ int get_next_floor_Elevator(THIS(Elevator)){ pthread_mutex_lock(&this->mutex_floor); min_diff = INFINITY; - next_floor = 0; + next_floor = -1; for(i=0;ipassengers->get_size(this->passengers);i++){ temp_element = this->passengers->get_element(this->passengers, i); @@ -105,14 +105,22 @@ void *runnable_Elevator(void * void_this){ /* This is where the thread logic will be implemented */ Elevator * this = (Elevator*) void_this; SharedData * data = GET_INSTANCE(SharedData); + Building * building = data->main_building; AGENT_OPTIONS printf("Je suis l'ascenseur %s\n", this->name); for (;;){ - data->main_building->get_waiting_floors(data->main_building); - data->main_building->signal_elevator_at_floor(data->main_building, this->get_floor(this)); - this->set_floor(this, this->get_next_floor(this)); + building->signal_elevator_at_floor(data->main_building, this->get_floor(this)); + if(this->target_floor == this->get_floor(this)){ + if(this->get_next_passenger_stop(this) != -1){ + this->target_floor = this->get_next_passenger_stop(this); + }else if(building->get_next_call(building, this->floor) != -1){ + this->target_floor = building->get_next_call(building, this->floor); + } + + } + this->set_floor(this, this->target_floor); } return NULL; @@ -122,21 +130,22 @@ Elevator *_init_Elevator(char * name){ Elevator * new_elevator = malloc_or_die(sizeof(Elevator)); new_elevator->name = strdup(name); new_elevator->passengers = NEW(List); + new_elevator->target_floor = 0; pthread_mutex_init(&new_elevator->mutex_passengers, NULL); pthread_mutex_init(&new_elevator->mutex_state, NULL); pthread_mutex_init(&new_elevator->mutex_floor, NULL); LINK_ALL(Elevator, new_elevator, - runnable, - get_number_of_passengers, - get_next_floor, - can_get_inside, - add_passenger, - get_state, - set_state, - get_floor, - set_floor, - repair + runnable, + get_number_of_passengers, + get_next_passenger_stop, + can_get_inside, + add_passenger, + get_state, + set_state, + get_floor, + set_floor, + repair ); new_elevator->set_floor(new_elevator, 0); diff --git a/Elevator/Elevator.h b/Elevator/Elevator.h index d953315..766b6e1 100644 --- a/Elevator/Elevator.h +++ b/Elevator/Elevator.h @@ -20,6 +20,7 @@ typedef struct o_Elevator { PRIVATE List * passengers; PRIVATE char * name; PRIVATE int floor; + PRIVATE int target_floor; PRIVATE pthread_mutex_t mutex_passengers; PRIVATE pthread_mutex_t mutex_state; PRIVATE pthread_mutex_t mutex_floor; @@ -28,7 +29,7 @@ typedef struct o_Elevator { SYNCHRONIZE PRIVATE void (*set_state)(_THIS(Elevator), ELEVATOR_STATE var); SYNCHRONIZE PRIVATE void (*set_floor)(_THIS(Elevator), int var); - SYNCHRONIZE PRIVATE int (*get_next_floor)(_THIS(Elevator)); + SYNCHRONIZE PRIVATE int (*get_next_passenger_stop)(_THIS(Elevator)); SYNCHRONIZE PUBLIC void (*repair)(_THIS(Elevator)); SYNCHRONIZE PUBLIC int (*get_number_of_passengers)(_THIS(Elevator)); From 12c4e1af19c9ccf466c492efd14b56a0fa371361 Mon Sep 17 00:00:00 2001 From: Aethor Date: Thu, 21 Jun 2018 18:22:02 +0200 Subject: [PATCH 12/15] =?UTF-8?q?Les=20ascenseurs=20sont=20intelligents...?= =?UTF-8?q?=20mais=20ils=20finissent=20pas=20trop=20leur=20travail=20quand?= =?UTF-8?q?=20m=C3=AAme.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Building/Building.c | 11 +++++------ Elevator/Elevator.c | 20 ++++++++++++++------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Building/Building.c b/Building/Building.c index 47d7636..c941e76 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -31,9 +31,9 @@ int get_next_call_Building(THIS(Building), int elevator_floor){ int next_target = -1; for(i=0; i 0){ - if(abs(elevator_floor - waiting_floors[i]) < best_diff){ - best_diff = abs(elevator_floor - waiting_floors[i]); - next_target = waiting_floors[i]; + if(abs(elevator_floor - i) < best_diff){ + best_diff = abs(elevator_floor - i); + next_target = i; } } } @@ -198,13 +198,12 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger 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->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 - }*/ + this->waiting_floors[origin]++;//on ajoute à la liste des attentes 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 + this->waiting_floors[origin]--;//on retire de la liste des attentes if (passenger->type == RESIDENT) printf("Le résident %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger->get_name(passenger), this->elevators[elevator_number]->name, origin); diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index 6f679dc..3e133de 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -107,18 +107,26 @@ void *runnable_Elevator(void * void_this){ SharedData * data = GET_INSTANCE(SharedData); Building * building = data->main_building; + int next_call; + int next_passenger_stop; + AGENT_OPTIONS - printf("Je suis l'ascenseur %s\n", this->name); + printf("Initialisation de l'ascenseur %s\n", this->name); for (;;){ building->signal_elevator_at_floor(data->main_building, this->get_floor(this)); if(this->target_floor == this->get_floor(this)){ - if(this->get_next_passenger_stop(this) != -1){ - this->target_floor = this->get_next_passenger_stop(this); - }else if(building->get_next_call(building, this->floor) != -1){ - this->target_floor = building->get_next_call(building, this->floor); + if((next_passenger_stop = this->get_next_passenger_stop(this)) != -1){ + this->target_floor = next_passenger_stop; + }else if((next_call = building->get_next_call(building, this->floor)) != -1){ + this->target_floor = next_call; } - + } + if(this->get_floor(this) != this->target_floor){ + printf("Ascenseur %s en route vers l'étage %d\n", this->name, this->target_floor); + printf("DEBUG : Next passenger stop : %d\n", next_passenger_stop); + printf("DEBUG : Next call from user : %d\n", next_call); + printf("\n\n"); } this->set_floor(this, this->target_floor); } From 55126c9ca64300810b02674805d10b0c4d961e74 Mon Sep 17 00:00:00 2001 From: Aethor Date: Thu, 21 Jun 2018 19:21:17 +0200 Subject: [PATCH 13/15] Added remove_inside_List function. Experimental user removal. --- Elevator/Elevator.c | 7 +++++ List/List.c | 63 +++++++++++++++++++++++++++++++++------------ List/List.h | 1 + 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index 3e133de..23f0d2f 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -40,6 +40,12 @@ void add_passenger_Elevator(THIS(Elevator), Passenger * passenger){ pthread_mutex_unlock(&this->mutex_passengers); } +void remove_passenger_Elevator(THIS(Elevator), Passenger * passenger){ + pthread_mutex_lock(&this->mutex_passengers); + this->passengers->remove_inside(this->passengers, passenger, passenger->compare); + pthread_mutex_unlock(&this->mutex_passengers); +} + int get_number_of_passengers_Elevator(THIS(Elevator)){ int num; pthread_mutex_lock(&this->mutex_passengers); @@ -127,6 +133,7 @@ void *runnable_Elevator(void * void_this){ printf("DEBUG : Next passenger stop : %d\n", next_passenger_stop); printf("DEBUG : Next call from user : %d\n", next_call); printf("\n\n"); + fflush(stdout); } this->set_floor(this, this->target_floor); } diff --git a/List/List.c b/List/List.c index 2f22f1d..5cc52e3 100644 --- a/List/List.c +++ b/List/List.c @@ -140,6 +140,36 @@ void insert_inside_List(THIS(List), void * data, size_t data_size, int index){ } +/** + * Remove element with specified data in the list + * @param this THIS(List) + * @param data_to_remove pointer to data you want to remove + * @param compare a compare function, return true or false and takes two void * pointers + * @return true or false, depending on if the element was found in the list + */ +int remove_inside_List(THIS(List), void * data_to_remove, int (*compare)(void*, void*)){ + Element* temp_element = this->head; + + while(temp_element != NULL){ + if(compare(data_to_remove, temp_element->data)){ + if(temp_element == this->head){ + this->remove_head(this); + }else if (temp_element == this->tail){ + this->remove_tail(this); + }else{ + if(temp_element->previous != NULL) + temp_element->previous->next = temp_element->next; + if(temp_element->next != NULL) + temp_element->next->previous = temp_element->previous; + DELETE(temp_element); + } + return 1; + } + temp_element = temp_element->next; + } + return 0; +} + /** * Check if data_to_find exist in list * @param this THIS(List) @@ -176,22 +206,23 @@ List *_init_List(){ l->custom_free = free; LINK_ALL(List, l, - get_head, - get_tail, - get_size, - get_head_data, - get_tail_data, - get_element, - get_element_data, - set_custom_free, - insert_inside, - insert_tail, - insert_head, - clear_custom, - clear, - remove_head, - remove_tail, - contains + get_head, + get_tail, + get_size, + get_head_data, + get_tail_data, + get_element, + get_element_data, + set_custom_free, + remove_inside, + insert_inside, + insert_tail, + insert_head, + clear_custom, + clear, + remove_head, + remove_tail, + contains ) return l; } diff --git a/List/List.h b/List/List.h index 0c9511b..8906d62 100644 --- a/List/List.h +++ b/List/List.h @@ -30,6 +30,7 @@ struct o_List { PUBLIC int (*get_size)(_THIS(List)); PUBLIC int (*contains)(_THIS(List), void * data_to_find, int (*compare)(void *, void *)); + PUBLIC int (*remove_inside)(_THIS(List), void * data_to_remove, int (*compare)(void*, void*)); PUBLIC void (*set_custom_free)(_THIS(List), void (*custom_free)(void *)); PUBLIC void (*insert_inside)(_THIS(List), void * data, size_t data_size, int index); From 653734ec1f38ac90d82740792aed334a851ead60 Mon Sep 17 00:00:00 2001 From: Aethor Date: Thu, 21 Jun 2018 20:26:35 +0200 Subject: [PATCH 14/15] To the moooo.... oh wait, not yet. --- Building/Building.c | 7 +------ Elevator/Elevator.c | 13 +++++++++++++ Elevator/Elevator.h | 1 + 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Building/Building.c b/Building/Building.c index c941e76..09f6c08 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -212,12 +212,7 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger 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->get_name(passenger), - 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->get_name(passenger), - this->elevators[elevator_number]->name, destination); + this->elevators[elevator_number]->remove_passenger(this->elevators[elevator_number], passenger); }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->get_name(passenger), origin); diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index 23f0d2f..9ce3c47 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -4,6 +4,7 @@ #include #include +#include #include "Elevator.h" #include "../SharedData/SharedData.h" @@ -40,9 +41,20 @@ void add_passenger_Elevator(THIS(Elevator), Passenger * passenger){ pthread_mutex_unlock(&this->mutex_passengers); } +/** + * Remove a passenger from an elevator. Efectively remove him from the elevator's passenger list. + * @SYNCHRONIZED passengers : this elevator's list of passenger is accessed via mutex + * @param THIS(Elevator) : this + * @param passenger : the passenger to remove + */ void remove_passenger_Elevator(THIS(Elevator), Passenger * passenger){ pthread_mutex_lock(&this->mutex_passengers); + if (passenger->type == RESIDENT) + printf("Le résident %s sort de l'ascenseur %s à l'étage %d\n", passenger->get_name(passenger), this->name, this->get_floor(this)); + else if (passenger->type == VISITOR) + printf("Le visiteur %s sort de l'ascenseur %s à l'étage %d\n", passenger->get_name(passenger), this->name, this->get_floor(this)); this->passengers->remove_inside(this->passengers, passenger, passenger->compare); + printf("Ascenseur %s : j'ai encore %d passagers\n", this->name, this->passengers->get_size(this->passengers)); pthread_mutex_unlock(&this->mutex_passengers); } @@ -155,6 +167,7 @@ Elevator *_init_Elevator(char * name){ get_number_of_passengers, get_next_passenger_stop, can_get_inside, + remove_passenger, add_passenger, get_state, set_state, diff --git a/Elevator/Elevator.h b/Elevator/Elevator.h index 766b6e1..a5f847b 100644 --- a/Elevator/Elevator.h +++ b/Elevator/Elevator.h @@ -33,6 +33,7 @@ typedef struct o_Elevator { SYNCHRONIZE PUBLIC void (*repair)(_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); SYNCHRONIZE PUBLIC ELEVATOR_STATE (*get_state)(_THIS(Elevator)); SYNCHRONIZE PUBLIC int (*get_floor)(_THIS(Elevator)); From 036d4b55b78bbc2645fb0a829b62aff45e0ef7f0 Mon Sep 17 00:00:00 2001 From: Aethor Date: Thu, 21 Jun 2018 21:34:34 +0200 Subject: [PATCH 15/15] fixed a bug. I think ??? --- Building/Building.c | 9 +-------- Elevator/Elevator.c | 11 ++++++----- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/Building/Building.c b/Building/Building.c index 09f6c08..406bdd0 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -204,13 +204,6 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger elevator_number = this->get_inside_elevator(this, origin, passenger); if (elevator_number != -1){ //passenger accepted in elevator this->waiting_floors[origin]--;//on retire de la liste des attentes - if (passenger->type == RESIDENT) - printf("Le résident %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger->get_name(passenger), - 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->get_name(passenger), - this->elevators[elevator_number]->name, origin); - pthread_cond_wait(this->condition_floors[destination], this->mutex_cond_get_outside_elevator); this->elevators[elevator_number]->remove_passenger(this->elevators[elevator_number], passenger); }else{ @@ -219,7 +212,7 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger else if (passenger->type == VISITOR) printf("Le visiteur %s à l'étage %d n'a pas pu rentrer dans un ascenseur. Préempté !\n", passenger->get_name(passenger), origin); //reloading fire - //this->go_to_floor(this, origin, destination, passenger); + this->go_to_floor(this, origin, destination, passenger); } } diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index 9ce3c47..9f5779b 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -33,8 +33,9 @@ void _free__Elevator(THIS(Elevator)){ void add_passenger_Elevator(THIS(Elevator), Passenger * passenger){ pthread_mutex_lock(&this->mutex_passengers); 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, + printf("L'ascenseur %s recoit le visiteur %s à l'étage %d\nIl y a maintenant %d passagers dans l'ascenseur %s\n", this->name, passenger->get_name(passenger), + this->get_floor(this), this->passengers->get_size(this->passengers), this->name); if (this->passengers->get_size(this->passengers) >= MAX_ELEVATOR_CAPACITY) this->set_state(this, SLEEPING); @@ -87,7 +88,7 @@ int get_next_passenger_stop_Elevator(THIS(Elevator)){ temp_element = this->passengers->get_element(this->passengers, i); temp_passenger = (Passenger*) temp_element->get_data(temp_element); temp_floor = temp_passenger->get_destination(temp_passenger); - if(abs(this->floor - temp_floor) < min_diff && temp_floor != this->floor){ + if(abs(this->floor - temp_floor) < min_diff /*&& temp_floor != this->floor*/){ min_diff = abs(this->floor - temp_floor); next_floor = temp_floor; } @@ -141,13 +142,13 @@ void *runnable_Elevator(void * void_this){ } } if(this->get_floor(this) != this->target_floor){ + this->set_floor(this, this->target_floor); printf("Ascenseur %s en route vers l'étage %d\n", this->name, this->target_floor); - printf("DEBUG : Next passenger stop : %d\n", next_passenger_stop); + /*printf("DEBUG : Next passenger stop : %d\n", next_passenger_stop); printf("DEBUG : Next call from user : %d\n", next_call); - printf("\n\n"); + printf("\n\n");*/ fflush(stdout); } - this->set_floor(this, this->target_floor); } return NULL;