mirror of
https://gitlab.com/klmp200/LO41.git
synced 2024-11-22 08:43:22 +00:00
commit
b0bf4dba93
@ -199,12 +199,16 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger
|
|||||||
if (destination < 0 || destination >= FLOORS) {CRASH("You are trying to reach a non existing floor\n");}
|
if (destination < 0 || destination >= FLOORS) {CRASH("You are trying to reach a non existing floor\n");}
|
||||||
|
|
||||||
this->waiting_floors[origin]++;//on ajoute à la liste des attentes
|
this->waiting_floors[origin]++;//on ajoute à la liste des attentes
|
||||||
|
pthread_mutex_lock(this->mutex_cond_get_inside_elevator);
|
||||||
pthread_cond_wait(this->condition_floors[origin], this->mutex_cond_get_inside_elevator);
|
pthread_cond_wait(this->condition_floors[origin], this->mutex_cond_get_inside_elevator);
|
||||||
|
pthread_mutex_unlock(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 (elevator_number != -1){ //passenger accepted in elevator
|
||||||
this->waiting_floors[origin]--;//on retire de la liste des attentes
|
this->waiting_floors[origin]--;//on retire de la liste des attentes
|
||||||
|
pthread_mutex_lock(this->mutex_cond_get_outside_elevator);
|
||||||
pthread_cond_wait(this->condition_floors[destination], this->mutex_cond_get_outside_elevator);
|
pthread_cond_wait(this->condition_floors[destination], this->mutex_cond_get_outside_elevator);
|
||||||
|
pthread_mutex_unlock(this->mutex_cond_get_outside_elevator);
|
||||||
this->elevators[elevator_number]->remove_passenger(this->elevators[elevator_number], passenger);
|
this->elevators[elevator_number]->remove_passenger(this->elevators[elevator_number], passenger);
|
||||||
}else{
|
}else{
|
||||||
if (passenger->type == RESIDENT)
|
if (passenger->type == RESIDENT)
|
||||||
@ -212,6 +216,7 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger
|
|||||||
else if (passenger->type == VISITOR)
|
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);
|
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
|
//reloading fire
|
||||||
|
this->waiting_floors[origin]--;
|
||||||
this->go_to_floor(this, origin, destination, passenger);
|
this->go_to_floor(this, origin, destination, passenger);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,12 +33,12 @@ void _free__Elevator(THIS(Elevator)){
|
|||||||
void add_passenger_Elevator(THIS(Elevator), Passenger * passenger){
|
void add_passenger_Elevator(THIS(Elevator), Passenger * passenger){
|
||||||
pthread_mutex_lock(&this->mutex_passengers);
|
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 à l'étage %d\nIl y a maintenant %d passagers dans l'ascenseur %s\n", this->name,
|
printf("L'ascenseur %s recoit le passager %s à l'étage %d\nIl y a maintenant %d passagers dans l'ascenseur %s\n", this->name,
|
||||||
passenger->get_name(passenger),
|
passenger->get_name(passenger),
|
||||||
this->get_floor(this),
|
this->get_floor(this),
|
||||||
this->passengers->get_size(this->passengers), this->name);
|
this->passengers->get_size(this->passengers), this->name);
|
||||||
if (this->passengers->get_size(this->passengers) >= MAX_ELEVATOR_CAPACITY)
|
/*if (this->passengers->get_size(this->passengers) >= MAX_ELEVATOR_CAPACITY)
|
||||||
this->set_state(this, SLEEPING);
|
this->set_state(this, SLEEPING);*/
|
||||||
pthread_mutex_unlock(&this->mutex_passengers);
|
pthread_mutex_unlock(&this->mutex_passengers);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,8 +107,10 @@ int can_get_inside_Elevator(THIS(Elevator), int floor){
|
|||||||
pthread_mutex_lock(&this->mutex_floor);
|
pthread_mutex_lock(&this->mutex_floor);
|
||||||
|
|
||||||
permission = (this->passengers->get_size(this->passengers) < MAX_ELEVATOR_CAPACITY &&
|
permission = (this->passengers->get_size(this->passengers) < MAX_ELEVATOR_CAPACITY &&
|
||||||
this->state == WAITING && this->floor == floor);
|
/*this->state == WAITING &&*/ this->floor == floor);
|
||||||
|
|
||||||
|
if(this->floor != floor)
|
||||||
|
printf("DEBUG : Cause de la préemption : l'ascenseur %s n'est pas là !\n", this->name);
|
||||||
pthread_mutex_unlock(&this->mutex_floor);
|
pthread_mutex_unlock(&this->mutex_floor);
|
||||||
pthread_mutex_unlock(&this->mutex_state);
|
pthread_mutex_unlock(&this->mutex_state);
|
||||||
pthread_mutex_unlock(&this->mutex_passengers);
|
pthread_mutex_unlock(&this->mutex_passengers);
|
||||||
@ -134,20 +136,24 @@ void *runnable_Elevator(void * void_this){
|
|||||||
printf("Initialisation de l'ascenseur %s\n", this->name);
|
printf("Initialisation de l'ascenseur %s\n", this->name);
|
||||||
for (;;){
|
for (;;){
|
||||||
building->signal_elevator_at_floor(data->main_building, this->get_floor(this));
|
building->signal_elevator_at_floor(data->main_building, this->get_floor(this));
|
||||||
|
usleep(500000);
|
||||||
if(this->target_floor == this->get_floor(this)){
|
if(this->target_floor == this->get_floor(this)){
|
||||||
if((next_passenger_stop = this->get_next_passenger_stop(this)) != -1){
|
next_passenger_stop = this->get_next_passenger_stop(this);
|
||||||
|
next_call = building->get_next_call(building, this->floor);
|
||||||
|
if (next_passenger_stop == this->get_floor(this) || next_call == this->get_floor(this)){
|
||||||
|
printf("Ascenseur %s attends pour déposer ou prendre un passager supplémentaire\n", this->name); //while pour attendre ?
|
||||||
|
}else{
|
||||||
|
if(next_passenger_stop != -1){
|
||||||
this->target_floor = next_passenger_stop;
|
this->target_floor = next_passenger_stop;
|
||||||
}else if((next_call = building->get_next_call(building, this->floor)) != -1){
|
}else if(next_call != -1){
|
||||||
this->target_floor = next_call;
|
this->target_floor = next_call;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
if(this->get_floor(this) != this->target_floor){
|
if(this->get_floor(this) != this->target_floor){
|
||||||
this->set_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("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");*/
|
|
||||||
fflush(stdout);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
#include "../Passenger/Passenger.h"
|
#include "../Passenger/Passenger.h"
|
||||||
|
|
||||||
//#define MAX_ELEVATOR_CAPACITY 10
|
//#define MAX_ELEVATOR_CAPACITY 10
|
||||||
#define MAX_ELEVATOR_CAPACITY 3
|
#define MAX_ELEVATOR_CAPACITY 10
|
||||||
|
|
||||||
typedef enum {RUNNING, WAITING, SLEEPING, BROKEN} ELEVATOR_STATE;
|
typedef enum {RUNNING, WAITING, SLEEPING, BROKEN} ELEVATOR_STATE;
|
||||||
|
|
||||||
|
@ -25,6 +25,9 @@ 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",
|
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);
|
this->name, this->apartment_floor, this->destination);
|
||||||
|
if(this->position == this->destination)
|
||||||
|
printf("Résident %s : je reste chez moi\n", this->name);
|
||||||
|
else
|
||||||
data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger);
|
data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user