1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2024-12-22 07:41:10 +00:00

moving elevators, fixed a crash, more documentation

This commit is contained in:
Aethor 2018-06-18 23:03:32 +02:00
parent c4048afe3b
commit 7fcb2a8b5a
2 changed files with 36 additions and 17 deletions

View File

@ -135,6 +135,13 @@ void _free__Building(THIS(Building)){
free(this);
}
/**
* Tries to make @passenger enter an elevator, passenger being at @current_floor
* @THIS(Building)
* @current_floor the floor where the passenger is trying to enter the elevator
* @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 i;
/* Make assumption that a waiting elevator is not full */
@ -143,8 +150,8 @@ int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger pa
for (i=0; i<ELEVATOR_NB; i++){
if (this->elevators[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)
this->elevators[i]->add_passenger(this->elevators[i], passenger);
//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;
@ -162,20 +169,29 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger
pthread_cond_wait(this->condition_floors[origin], this->mutex_cond_get_inside_elevator);
elevator_number = this->get_inside_elevator(this, origin, passenger);
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,
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 sors 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 sors de l'ascenseur %s à l'étage %d\n", passenger.visitor->name,
this->elevators[elevator_number]->name, destination);
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,
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,
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,
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,
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);
}//todo : else, remettre en attente ?
}
int use_call_box_Building(THIS(Building), char * resident_name){
@ -239,4 +255,4 @@ Building *_init_Building(char * residents_file, char * visitors_file){
new_building->parse_visitors(new_building, visitors_file);
return new_building;
}
}

View File

@ -109,8 +109,11 @@ void *runnable_Elevator(void * void_this){
AGENT_OPTIONS
printf("Je suis l'ascenseur %s\n", this->name);
for (;;)
for (;;){
data->main_building->signal_elevator_at_floor(data->main_building, this->get_floor(this));
this->set_floor(this, this->get_next_floor(this));
}
return NULL;
}