1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2024-12-22 15:51:11 +00:00

c'est plus beau

This commit is contained in:
Aethor 2018-06-20 22:06:45 +02:00
parent f6dc3e5f3a
commit 7315e1595e
8 changed files with 39 additions and 24 deletions

View File

@ -146,9 +146,7 @@ int get_inside_elevator_Building(THIS(Building), int current_floor, 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);
printf("Test d'entrée à l'étage %d de %s : id %d\n", current_floor, passenger->get_name(passenger), passenger->get_id(passenger));
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);
@ -174,25 +172,25 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, 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,
printf("Le résident %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger->get_name(passenger),
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,
printf("Le visiteur %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger->get_name(passenger),
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,
printf("Le résident %s sort de l'ascenseur %s à l'étage %d\n", passenger->get_name(passenger),
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,
printf("Le visiteur %s sort de l'ascenseur %s à l'étage %d\n", passenger->get_name(passenger),
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);
printf("Le résident %s à l'étage %d n'a pas pu rentrer dans un ascenseur. Préempté !\n", passenger->get_name(passenger), 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
printf("Le visiteur %s à l'étage %d n'a pas pu rentrer dans un ascenseur. Préempté !\n", passenger->get_name(passenger), origin);
//reloading fire
this->go_to_floor(this, origin, destination, passenger);
}

View File

@ -33,8 +33,8 @@ void add_passenger_Elevator(THIS(Elevator), Passenger * passenger){
pthread_mutex_lock(&this->mutex_passengers);
this->passengers->insert_tail(this->passengers, ((void *)passenger), sizeof(Passenger));
printf("L'ascenseur %s recoit le visiteur %s\nIl y a maintenant %d passagers dans l'ascenseur %s\n", this->name,
passenger->type == VISITOR ? passenger->visitor->get_name(passenger->visitor) : passenger->resident->get_name(passenger->resident),
this->passengers->get_size(this->passengers), this->name);
passenger->get_name(passenger),
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_passengers);

View File

@ -9,6 +9,14 @@ int compare_Passenger(void * passenger1, void * passenger2){
((Passenger*) passenger2)->get_name((Passenger*) passenger2)) == 0);
}
int get_destination_Passenger(THIS(Passenger)){
if (this->type == RESIDENT)
return this->resident->get_destination(this->resident);
if (this->type == VISITOR)
return this->visitor->get_destination(this->visitor);
return -1;
}
int get_id_Passenger(THIS(Passenger)){
if (this->type == RESIDENT)
return this->resident->get_id(this->resident);
@ -52,10 +60,11 @@ Passenger *_init_Passenger(void* passenger_data, PASSENGER_TYPE type){
//new_passenger->compare = compare_Passenger;
LINK_ALL(Passenger, new_passenger,
get_id,
get_name,
compare,
runnable
get_id,
get_name,
get_destination,
compare,
runnable
)
return new_passenger;

View File

@ -20,6 +20,7 @@ typedef struct o_Passenger {
PUBLIC char * (*get_name)(_THIS(Passenger));
PUBLIC int (*get_id)(_THIS(Passenger));
PUBLIC int (*get_destination)(_THIS(Passenger));
PUBLIC void * (*runnable)(void* void_this);
PUBLIC int (*compare)(void * passenger1, void * passenger2);//yeah I know, but i needed int (*) (void*, void*)

View File

@ -8,13 +8,14 @@
#include "../SharedData/SharedData.h"
GETTER(Resident, char *, name);
GETTER(Resident, int, destination);
GETTER(Resident, int, id);
GETTER(Resident, int, apartment_floor);
void * runnable_Resident(void * void_this){
Resident * this = (Resident*) void_this;
SharedData* data = GET_INSTANCE(SharedData);
Passenger * passenger = NEW(Passenger, this, RESIDENT);
Passenger * passenger = NEW(Passenger, (void*) this, RESIDENT);
AGENT_OPTIONS;
@ -24,6 +25,7 @@ void * runnable_Resident(void * void_this){
printf("Je suis le resident %s et je suis a l'etage %d en direction de l'etage %d\n",
this->name, this->apartment_floor, this->destination);
data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger);
DELETE(passenger);
return NULL;
}
@ -43,10 +45,11 @@ Resident *_init_Resident(int id, char* name, int apartment_floor, int destinatio
new_resident->destination = destination;
LINK_ALL(Resident, new_resident,
get_name,
get_id,
runnable,
get_apartment_floor
get_name,
get_destination,
get_id,
runnable,
get_apartment_floor
)
return new_resident;

View File

@ -18,6 +18,7 @@ typedef struct o_Resident {
PUBLIC char * (*get_name)(_THIS(Resident));
PUBLIC int (*get_id)(_THIS(Resident));
PUBLIC int (*get_apartment_floor)(_THIS(Resident));
PUBLIC int (*get_destination)(_THIS(Resident));
DESTRUCTOR(Resident);
} Resident;

View File

@ -7,6 +7,7 @@
#include <string.h>
GETTER(Visitor, char*, name);
GETTER(Visitor, int, destination);
GETTER(Visitor, int, id);
void * runnable_Visitor(void * void_this){
@ -46,9 +47,10 @@ Visitor *_init_Visitor(int id, char* name, char * contact_name){
new_visitor->contact_name = NULL;
LINK_ALL(Visitor, new_visitor,
get_name,
get_id,
runnable
get_name,
get_destination,
get_id,
runnable
);
return new_visitor;

View File

@ -17,6 +17,7 @@ typedef struct o_Visitor {
PUBLIC void * (*runnable)(void* void_this);
PUBLIC char * (*get_name)(_THIS(Visitor));
PUBLIC int (*get_id)(_THIS(Visitor));
PUBLIC int (*get_destination)(_THIS(Visitor));
DESTRUCTOR(Visitor);
} Visitor;