mirror of
https://gitlab.com/klmp200/LO41.git
synced 2025-07-12 04:39:23 +00:00
du refactor
This commit is contained in:
@ -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)
|
||||
|
@ -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);
|
||||
|
||||
|
Reference in New Issue
Block a user