1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2025-07-12 04:39:23 +00:00

huge refactor, pointy pointer

This commit is contained in:
Aethor
2018-06-20 19:36:00 +02:00
parent f6f12b398d
commit 9b11ae0835
8 changed files with 45 additions and 35 deletions

View File

@ -142,13 +142,13 @@ void _free__Building(THIS(Building)){
* @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;
/* Make assumption that a waiting elevator is not full */
pthread_mutex_lock(this->mutex_func_get_inside_elevator);
printf("Test d'entrée à l'étage %d de %s : id %d\n", current_floor,
passenger.type == VISITOR ? passenger.visitor->name : passenger.resident->name,
passenger.type == VISITOR ? passenger.visitor->id : passenger.resident->id);
passenger->type == VISITOR ? passenger->visitor->name : passenger->resident->name,
passenger->type == VISITOR ? passenger->visitor->id : passenger->resident->id);
for (i=0; i<ELEVATOR_NB; i++){
if (this->elevators[i]->can_get_inside(this->elevators[i], current_floor)){
this->elevators[i]->add_passenger(this->elevators[i], passenger);
@ -160,37 +160,38 @@ int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger pa
return -1;
}
void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger passenger){
void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger * passenger){
int elevator_number;
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->compare(this->waiting_passengers, (void*) &passenger, passenger.compare))
this->waiting_passengers->insert_tail(this->waiting_passengers, (void*) &passenger, sizeof(passenger));//todo : check if inside list
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
}
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,
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,
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,
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,
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);
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);
//réarmement
this->go_to_floor(this, origin, destination, passenger);
}

View File

@ -28,12 +28,12 @@ typedef struct o_Building {
PRIVATE void (*parse_residents)(_THIS(Building), char * file);
PRIVATE void (*parse_visitors)(_THIS(Building), char * file);
PRIVATE int (*get_inside_elevator)(_THIS(Building), int current_floor, Passenger passenger);
PRIVATE int (*get_inside_elevator)(_THIS(Building), int current_floor, Passenger * passenger);
PUBLIC int (*use_call_box)(_THIS(Building), char * resident_name);
PUBLIC void (*signal_elevator_at_floor)(_THIS(Building), int floor);
SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger passenger);
SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger * passenger);
DESTRUCTOR(Building);
} Building;