From 12c4e1af19c9ccf466c492efd14b56a0fa371361 Mon Sep 17 00:00:00 2001 From: Aethor Date: Thu, 21 Jun 2018 18:22:02 +0200 Subject: [PATCH] =?UTF-8?q?Les=20ascenseurs=20sont=20intelligents...=20mai?= =?UTF-8?q?s=20ils=20finissent=20pas=20trop=20leur=20travail=20quand=20m?= =?UTF-8?q?=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); }