1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2024-11-21 16:23:22 +00:00

du refactor

This commit is contained in:
Amalvy Arthur 2018-06-21 16:15:33 +02:00
parent f066e22419
commit 92c784a76b
7 changed files with 41 additions and 13 deletions

View File

@ -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; i<ELEVATOR_NB; i++)
@ -130,6 +140,7 @@ void _free__Building(THIS(Building)){
free(this->mutex_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; i<FLOORS; i++)
new_building->waiting_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; i<ELEVATOR_NB; i++) {
elevator_name[0]++;
new_building->elevators[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)

View File

@ -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);

View File

@ -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));
}

View File

@ -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,

View File

@ -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));

View File

@ -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);

View File

@ -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));