From df740634add657a474c3966ef8043bc55c956fa1 Mon Sep 17 00:00:00 2001 From: Aethor Date: Wed, 20 Jun 2018 17:49:18 +0200 Subject: [PATCH] =?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; }