1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2024-11-24 10:04:21 +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,7 +150,7 @@ 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)
//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);
@ -162,6 +169,8 @@ 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 (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);
@ -171,11 +180,18 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger
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,
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 sors 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.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){

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;
}