diff --git a/Building/Building.c b/Building/Building.c index 85c1351..1075032 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -135,16 +135,16 @@ void _free__Building(THIS(Building)){ free(this); } -int get_inside_elevator_Building(THIS(Building), int current_floor, ElevatorPassenger elevator_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, elevator_passenger.passenger.visitor->name, elevator_passenger.passenger.visitor->id); + printf("Test d'entrée à l'étage %d de %s : id %d\n", current_floor, passenger.visitor->name, passenger.visitor->id); for (i=0; ielevators[i]->can_get_inside(this->elevators[i], current_floor)){ /* pour faire taire le compilateur le temps que je revienne sur cette fonction */ - if (elevator_passenger.type == VISITOR) - this->elevators[i]->add_passenger(this->elevators[i], elevator_passenger); + if (passenger.type == VISITOR) + 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; @@ -154,27 +154,27 @@ int get_inside_elevator_Building(THIS(Building), int current_floor, ElevatorPass return -1; } -void go_to_floor_Building(THIS(Building), int origin, int destination, ElevatorPassenger elevator_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");} pthread_cond_wait(this->condition_floors[origin], this->mutex_cond_get_inside_elevator); - elevator_number = this->get_inside_elevator(this, origin, elevator_passenger); - if (elevator_passenger.type == RESIDENT) - printf("Le résident %s rentre dans l'ascenseur %s depuis l'étage %d\n", elevator_passenger.passenger.resident->name, + 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 (elevator_passenger.type == VISITOR) - printf("Le visiteur %s rentre dans l'ascenseur %s depuis l'étage %d\n", elevator_passenger.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 (elevator_passenger.type == RESIDENT) - printf("Le résident %s sors de l'ascenseur %s à l'étage %d\n", elevator_passenger.passenger.resident->name, + 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 (elevator_passenger.type == VISITOR) - printf("Le visiteur %s sors de l'ascenseur %s à l'étage %d\n", elevator_passenger.passenger.visitor->name, + 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); } diff --git a/Building/Building.h b/Building/Building.h index 3287c0a..42711cf 100644 --- a/Building/Building.h +++ b/Building/Building.h @@ -27,12 +27,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, ElevatorPassenger elevator_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, ElevatorPassenger elevator_passenger); + SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger passenger); DESTRUCTOR(Building); } Building; diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index 6080c70..ad2e033 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -28,11 +28,11 @@ void _free__Elevator(THIS(Elevator)){ free(this); } -void add_passenger_Elevator(THIS(Elevator), ElevatorPassenger passenger){ +void add_passenger_Elevator(THIS(Elevator), Passenger passenger){ pthread_mutex_lock(&this->mutex_passenger); - this->passengers->insert_tail(this->passengers, ((void *)&passenger), sizeof(ElevatorPassenger)); + this->passengers->insert_tail(this->passengers, ((void *)&passenger), sizeof(Passenger)); printf("Le passager avec l'id %d est entre dans l'ascenseur %s\nIl y a maintenant %d passagers dans l'ascenseur %s\n", - passenger.passenger.visitor->get_id(passenger.passenger.visitor), this->name, this->passengers->get_size(this->passengers), this->name); + passenger.visitor->get_id(passenger.visitor), this->name, this->passengers->get_size(this->passengers), this->name); if (this->passengers->get_size(this->passengers) >= MAX_ELEVATOR_CAPACITY) this->set_state(this, SLEEPING); pthread_mutex_unlock(&this->mutex_passenger); diff --git a/Elevator/Elevator.h b/Elevator/Elevator.h index 24a52b1..3a9c6dd 100644 --- a/Elevator/Elevator.h +++ b/Elevator/Elevator.h @@ -31,7 +31,7 @@ typedef struct o_Elevator { SYNCHRONIZE PUBLIC void (*repair)(_THIS(Elevator)); SYNCHRONIZE PUBLIC int (*get_number_of_passengers)(_THIS(Elevator)); - SYNCHRONIZE PUBLIC void (*add_passenger)(_THIS(Elevator), ElevatorPassenger passenger); + SYNCHRONIZE PUBLIC void (*add_passenger)(_THIS(Elevator), Passenger passenger); SYNCHRONIZE PUBLIC ELEVATOR_STATE (*get_state)(_THIS(Elevator)); SYNCHRONIZE PUBLIC int (*get_floor)(_THIS(Elevator)); SYNCHRONIZE PUBLIC int (*can_get_inside)(_THIS(Elevator), int floor); diff --git a/Passenger/Passenger.h b/Passenger/Passenger.h index 3c83ed5..8142e6c 100644 --- a/Passenger/Passenger.h +++ b/Passenger/Passenger.h @@ -10,14 +10,12 @@ typedef enum {RESIDENT, VISITOR} PASSENGER_TYPE; -typedef union u_Passenger { - Resident * resident; - Visitor * visitor; +typedef struct s_Passenger { + PASSENGER_TYPE type; + union { + Resident * resident; + Visitor * visitor; + }; } Passenger; -typedef struct s_ElevatorPassenger { - PASSENGER_TYPE type; - Passenger passenger; -} ElevatorPassenger; - #endif //LO41_PASSENGER_H diff --git a/Visitor/Visitor.c b/Visitor/Visitor.c index 8239d72..0cf4ccd 100644 --- a/Visitor/Visitor.c +++ b/Visitor/Visitor.c @@ -13,17 +13,15 @@ void * runnable_Visitor(void * void_this){ Visitor *this = (Visitor*) void_this; SharedData * data = GET_INSTANCE(SharedData); Passenger passenger; - ElevatorPassenger elevator_passenger; AGENT_OPTIONS passenger.visitor = this; - elevator_passenger.passenger = passenger; - elevator_passenger.type = VISITOR; + passenger.type = VISITOR; printf("Bonjour, je suis %s et je souhaite rendre visite a %s\n", this->name, this->contact_name); printf("Bip, %s appel a l'interphone\n%s habite a l'etage %d\n", this->name, this->contact_name, (this->destination = data->use_call_box(data, this->contact_name))); - data->main_building->go_to_floor(data->main_building, this->position, this->destination, elevator_passenger); + data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger); return NULL; }