diff --git a/Building/Building.c b/Building/Building.c index 411c161..9e75a78 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -8,12 +8,19 @@ #define LINE_BUFFER 256 +SYNCHRONIZED_GETTER(Building, int*, waiting_floors) + void remove_end_char(char * string, char character){ size_t string_size = strlen(string); if (string[string_size - 1] == character) string[string_size - 1] = '\0'; } +int get_target_Building(THIS(Building)){ + int* waiting_floors = this->get_waiting_floors(this); + return waiting_floors[1]; +} + List * split(char * line, char * separator){ List * split = NEW(List); char * to_delete = strdup(line); @@ -118,6 +125,9 @@ void _free__Building(THIS(Building)){ pthread_mutex_unlock(this->mutex_func_get_inside_elevator); pthread_mutex_destroy(this->mutex_func_get_inside_elevator); + pthread_mutex_unlock(&this->mutex_waiting_floors); + pthread_mutex_destroy(&this->mutex_waiting_floors); + DELETE(this->residents); DELETE(this->visitors); for (i=0; imutex_cond_get_inside_elevator); free(this->mutex_cond_get_outside_elevator); free(this->mutex_func_get_inside_elevator); + //free(&this->mutex_waiting_floors); free(this->elevators); free(this); @@ -164,9 +175,9 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger 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->contains(this->waiting_passengers, (void*) passenger, passenger->compare)){ + /*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); @@ -215,8 +226,12 @@ Building *_init_Building(char * residents_file, char * visitors_file){ Building * new_building = malloc_or_die(sizeof(Building)); char elevator_name[] = "@"; int i; - new_building->floors = FLOORS; + + for (i=0; iwaiting_floors[i] = 0; + pthread_mutex_init(&new_building->mutex_waiting_floors, NULL); + new_building->elevators = malloc_or_die(sizeof(Elevator*) * ELEVATOR_NB); new_building->mutex_cond_get_inside_elevator = malloc_or_die(sizeof(pthread_mutex_t)); @@ -230,7 +245,6 @@ Building *_init_Building(char * residents_file, char * visitors_file){ new_building->condition_floors = malloc_or_die(sizeof(pthread_cond_t*) * FLOORS); new_building->residents = NEW(List); new_building->visitors = NEW(List); - new_building->waiting_passengers = NEW(List); for (i=0; ielevators[i] = NEW(Elevator, elevator_name); @@ -243,12 +257,14 @@ Building *_init_Building(char * residents_file, char * visitors_file){ LINK_ALL(Building, new_building, - parse_residents, - parse_visitors, - get_inside_elevator, - use_call_box, - go_to_floor, - signal_elevator_at_floor + parse_residents, + parse_visitors, + get_inside_elevator, + use_call_box, + go_to_floor, + get_target, + get_waiting_floors, + signal_elevator_at_floor ) if (residents_file != NULL) diff --git a/Building/Building.h b/Building/Building.h index 3e2862d..3879d8c 100644 --- a/Building/Building.h +++ b/Building/Building.h @@ -17,10 +17,11 @@ typedef struct o_Building { PRIVATE int floors; + PRIVATE int waiting_floors[FLOORS]; PRIVATE List * residents; PRIVATE List * visitors; - PRIVATE List * waiting_passengers; PRIVATE Elevator ** elevators; + PRIVATE pthread_mutex_t mutex_waiting_floors; PRIVATE pthread_mutex_t * mutex_cond_get_inside_elevator; PRIVATE pthread_mutex_t * mutex_cond_get_outside_elevator; PRIVATE pthread_mutex_t * mutex_func_get_inside_elevator; @@ -32,6 +33,8 @@ typedef struct o_Building { PUBLIC int (*use_call_box)(_THIS(Building), char * resident_name); PUBLIC void (*signal_elevator_at_floor)(_THIS(Building), int floor); + PUBLIC int (*get_target)(_THIS(Building)); + PUBLIC int* (*get_waiting_floors)(_THIS(Building)); SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger * passenger); diff --git a/Elevator/Elevator.c b/Elevator/Elevator.c index b1e2143..af010f8 100644 --- a/Elevator/Elevator.c +++ b/Elevator/Elevator.c @@ -110,6 +110,7 @@ void *runnable_Elevator(void * void_this){ printf("Je suis l'ascenseur %s\n", this->name); for (;;){ + data->main_building->get_waiting_floors(data->main_building); data->main_building->signal_elevator_at_floor(data->main_building, this->get_floor(this)); this->set_floor(this, this->get_next_floor(this)); } diff --git a/Resident/Resident.c b/Resident/Resident.c index 6e4aac4..ede284f 100644 --- a/Resident/Resident.c +++ b/Resident/Resident.c @@ -19,19 +19,21 @@ void * runnable_Resident(void * void_this){ AGENT_OPTIONS; + this->passenger = passenger; passenger->resident = this; passenger->type = RESIDENT; 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; } void _free__Resident(THIS(Resident)){ if (this->name != NULL) free(this->name); + if(this->passenger != NULL) + free(this->passenger); free(this); } @@ -43,6 +45,7 @@ Resident *_init_Resident(int id, char* name, int apartment_floor, int destinatio new_resident->apartment_floor = apartment_floor; new_resident->position = new_resident->apartment_floor; new_resident->destination = destination; + new_resident->passenger = NULL; LINK_ALL(Resident, new_resident, get_name, diff --git a/Resident/Resident.h b/Resident/Resident.h index 969b584..3641713 100644 --- a/Resident/Resident.h +++ b/Resident/Resident.h @@ -13,6 +13,7 @@ typedef struct o_Resident { PRIVATE int destination; PRIVATE int position; PRIVATE char* name; + PRIVATE void* passenger; PUBLIC void * (*runnable)(void * void_this); PUBLIC char * (*get_name)(_THIS(Resident)); diff --git a/Visitor/Visitor.c b/Visitor/Visitor.c index 56fe7f1..e59f4dc 100644 --- a/Visitor/Visitor.c +++ b/Visitor/Visitor.c @@ -16,13 +16,13 @@ void * runnable_Visitor(void * void_this){ Passenger * passenger = NEW(Passenger, (void*) this, VISITOR); AGENT_OPTIONS; + this->passenger = (void*) passenger; passenger->visitor = this; 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, passenger); - DELETE(passenger); return NULL; } @@ -31,6 +31,8 @@ void _free__Visitor(THIS(Visitor)){ free(this->name); if (this->contact_name != NULL) free(this->contact_name); + if (this->passenger != NULL) + free(this->passenger); free(this); } @@ -40,6 +42,7 @@ Visitor *_init_Visitor(int id, char* name, char * contact_name){ new_visitor->id = id; new_visitor->position = 0; new_visitor->destination = -1; + new_visitor->passenger = NULL; if (contact_name != NULL) new_visitor->contact_name = strdup(contact_name); diff --git a/Visitor/Visitor.h b/Visitor/Visitor.h index 4ad33ed..734c144 100644 --- a/Visitor/Visitor.h +++ b/Visitor/Visitor.h @@ -13,6 +13,7 @@ typedef struct o_Visitor { PRIVATE char * contact_name; PRIVATE int position; PRIVATE int destination; + PRIVATE void * passenger; PUBLIC void * (*runnable)(void* void_this); PUBLIC char * (*get_name)(_THIS(Visitor));