mirror of
https://gitlab.com/klmp200/LO41.git
synced 2024-11-22 08:43:22 +00:00
commit
120fd7f0d8
@ -135,6 +135,13 @@ void _free__Building(THIS(Building)){
|
|||||||
free(this);
|
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 get_inside_elevator_Building(THIS(Building), int current_floor, Passenger passenger){
|
||||||
int i;
|
int i;
|
||||||
/* Make assumption that a waiting elevator is not full */
|
/* 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++){
|
for (i=0; i<ELEVATOR_NB; i++){
|
||||||
if (this->elevators[i]->can_get_inside(this->elevators[i], current_floor)){
|
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 */
|
/* 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);
|
this->elevators[i]->add_passenger(this->elevators[i], passenger);
|
||||||
/* Il faut faire des trucs ici */
|
/* Il faut faire des trucs ici */
|
||||||
pthread_mutex_unlock(this->mutex_func_get_inside_elevator);
|
pthread_mutex_unlock(this->mutex_func_get_inside_elevator);
|
||||||
return i;
|
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);
|
pthread_cond_wait(this->condition_floors[origin], 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 (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 (elevator_number != -1){ //passenger accepted in elevator
|
||||||
if (passenger.type == RESIDENT)
|
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 rentre dans l'ascenseur %s depuis l'étage %d\n", passenger.resident->name,
|
||||||
this->elevators[elevator_number]->name, destination);
|
this->elevators[elevator_number]->name, origin);
|
||||||
else if (passenger.type == VISITOR)
|
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 rentre dans l'ascenseur %s depuis l'étage %d\n", passenger.visitor->name,
|
||||||
this->elevators[elevator_number]->name, destination);
|
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){
|
int use_call_box_Building(THIS(Building), char * resident_name){
|
||||||
|
@ -69,7 +69,7 @@ int get_next_floor_Elevator(THIS(Elevator)){
|
|||||||
temp_element = this->passengers->get_element(this->passengers, i);
|
temp_element = this->passengers->get_element(this->passengers, i);
|
||||||
temp_passenger = (Passenger*) temp_element->get_data(temp_element);
|
temp_passenger = (Passenger*) temp_element->get_data(temp_element);
|
||||||
temp_floor = temp_passenger->type == RESIDENT ? temp_passenger->resident->destination : temp_passenger->visitor->destination;
|
temp_floor = temp_passenger->type == RESIDENT ? temp_passenger->resident->destination : temp_passenger->visitor->destination;
|
||||||
if(abs(this->floor - temp_floor) < min_diff){
|
if(abs(this->floor - temp_floor) < min_diff && temp_floor != this->floor){
|
||||||
min_diff = abs(this->floor - temp_floor);
|
min_diff = abs(this->floor - temp_floor);
|
||||||
next_floor = temp_floor;
|
next_floor = temp_floor;
|
||||||
}
|
}
|
||||||
@ -109,8 +109,11 @@ void *runnable_Elevator(void * void_this){
|
|||||||
AGENT_OPTIONS
|
AGENT_OPTIONS
|
||||||
|
|
||||||
printf("Je suis l'ascenseur %s\n", this->name);
|
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));
|
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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
main.c
11
main.c
@ -14,11 +14,18 @@ void clean_exit(int error_code){
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main(int argc, char* argv[]) {
|
||||||
SharedData * shared_data = GET_INSTANCE(SharedData);
|
SharedData * shared_data = GET_INSTANCE(SharedData);
|
||||||
|
|
||||||
signal(SIGINT, clean_exit);
|
signal(SIGINT, clean_exit);
|
||||||
shared_data->set_main_building(shared_data, NEW(Building, "../residents.txt", "../visitors.txt"));
|
if(argc == 3){
|
||||||
|
shared_data->set_main_building(shared_data, NEW(Building, argv[1], argv[2]));
|
||||||
|
} else if (argc == 1){
|
||||||
|
shared_data->set_main_building(shared_data, NEW(Building, "../residents.txt", "../visitors.txt"));
|
||||||
|
} else{
|
||||||
|
CRASH("Arguments invalides\nUsage : ./LO41 [residents_file visitors_file]\n");
|
||||||
|
}
|
||||||
|
|
||||||
shared_data->start_all_threads(shared_data);
|
shared_data->start_all_threads(shared_data);
|
||||||
shared_data->wait_all_threads(shared_data);
|
shared_data->wait_all_threads(shared_data);
|
||||||
DELETE(shared_data);
|
DELETE(shared_data);
|
||||||
|
Loading…
Reference in New Issue
Block a user