diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8e9be42..9c6f865 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,4 +1,4 @@ -image: ubuntu:bionic +image: ubuntu:artful build: stage: build diff --git a/Building/Building.c b/Building/Building.c index 7ac3691..1136fe4 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -114,7 +114,6 @@ void _free__Building(THIS(Building)){ DELETE(this->residents); DELETE(this->visitors); - DELETE(this->box); for (i=0; ielevators[i]); for (i=0; ielevators[elevator_number]->name, destination); } +int use_call_box_Building(THIS(Building), char * resident_name){ + /* Return negative number if not found */ + Element * current = this->residents->get_head(this->residents); + while (current != NULL){ + if (!strcmp(((Resident *) current->get_data(current))->name, resident_name)) + return ((Resident *) current->get_data(current))->apartment_floor; + current = current->get_next(current); + } + return -1; +} + Building *_init_Building(char * residents_file, char * visitors_file){ Building * new_building = malloc_or_die(sizeof(Building)); char elevator_name[] = "@"; @@ -178,7 +188,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->box = NEW(CommunicationBox); for (i=0; ielevators[i] = NEW(Elevator, elevator_name); @@ -194,6 +203,7 @@ Building *_init_Building(char * residents_file, char * visitors_file){ parse_residents, parse_visitors, get_inside_elevator, + use_call_box, go_to_floor ) diff --git a/Building/Building.h b/Building/Building.h index 6e70fb6..265ac5b 100644 --- a/Building/Building.h +++ b/Building/Building.h @@ -11,7 +11,6 @@ #include "../Objects.h" #include "../List/List.h" #include "../Elevator/Elevator.h" -#include "../CommunicationBox/CommunicationBox.h" #include "../Resident/Resident.h" #include "../Visitor/Visitor.h" @@ -26,7 +25,6 @@ typedef struct o_Building { PRIVATE int floors; PRIVATE List * residents; PRIVATE List * visitors; - PRIVATE CommunicationBox * box; PRIVATE Elevator ** elevators; PRIVATE pthread_mutex_t * mutex_get_inside_elevator; PRIVATE pthread_mutex_t * mutex_get_outside_elevator; @@ -36,6 +34,8 @@ typedef struct o_Building { PRIVATE void (*parse_visitors)(_THIS(Building), char * file); PRIVATE int (*get_inside_elevator)(_THIS(Building), int current_floor, Passenger passenger, PASSENGER_TYPE type); + PUBLIC int (*use_call_box)(_THIS(Building), char * resident_name); + SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger passenger, PASSENGER_TYPE type); DESTRUCTOR(Building); diff --git a/CMakeLists.txt b/CMakeLists.txt index 16e6c41..70ecd1a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,4 +8,4 @@ if(CMAKE_COMPILER_IS_GNUCC) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread") endif() -add_executable(LO41 main.c List/List.h List/List.c Objects.h List/Element.c List/Element.h Objects.c Building/Building.c Building/Building.h Elevator/Elevator.c Elevator/Elevator.h CommunicationBox/CommunicationBox.c CommunicationBox/CommunicationBox.h SharedData/SharedData.c SharedData/SharedData.h Visitor/Visitor.c Visitor/Visitor.h Resident/Resident.c Resident/Resident.h) \ No newline at end of file +add_executable(LO41 main.c List/List.h List/List.c Objects.h List/Element.c List/Element.h Objects.c Building/Building.c Building/Building.h Elevator/Elevator.c Elevator/Elevator.h SharedData/SharedData.c SharedData/SharedData.h Visitor/Visitor.c Visitor/Visitor.h Resident/Resident.c Resident/Resident.h) \ No newline at end of file diff --git a/CommunicationBox/CommunicationBox.c b/CommunicationBox/CommunicationBox.c deleted file mode 100644 index e712b2f..0000000 --- a/CommunicationBox/CommunicationBox.c +++ /dev/null @@ -1,17 +0,0 @@ -// -// Created by Antoine Bartuccio on 05/06/2018. -// - -#include "CommunicationBox.h" - -void _free__CommunicationBox(THIS(CommunicationBox)){ - free(this); -} - -CommunicationBox *_init_CommunicationBox(){ - CommunicationBox * new_box = malloc_or_die(sizeof(CommunicationBox)); - - LINK(CommunicationBox, new_box, _free_); - - return new_box; -} \ No newline at end of file diff --git a/CommunicationBox/CommunicationBox.h b/CommunicationBox/CommunicationBox.h deleted file mode 100644 index dbc378b..0000000 --- a/CommunicationBox/CommunicationBox.h +++ /dev/null @@ -1,17 +0,0 @@ -// -// Created by Antoine Bartuccio on 05/06/2018. -// - -#ifndef LO41_COMMUNICATIONBOX_H -#define LO41_COMMUNICATIONBOX_H - -#include "../Objects.h" - -typedef struct o_CommunicationBox { - - DESTRUCTOR(CommunicationBox); -} CommunicationBox; - -CommunicationBox *_init_CommunicationBox(); - -#endif //LO41_COMMUNICATIONBOX_H diff --git a/SharedData/SharedData.c b/SharedData/SharedData.c index 0e3c0ce..5a2df17 100644 --- a/SharedData/SharedData.c +++ b/SharedData/SharedData.c @@ -5,12 +5,7 @@ #include "SharedData.h" GETTER(SharedData, Building *, main_building) -GETTER(SharedData, CommunicationBox *, box) - -void set_main_building_SharedData(THIS(SharedData), Building * building){ - this->main_building = building; - this->box = (building != NULL ) ? building->box : NULL; -} +SETTER(SharedData, Building *, main_building) void wait_threads_SharedData(THIS(SharedData)){ int i; @@ -97,8 +92,7 @@ SharedData *_get_instance_SharedData(){ new_shared_data = malloc_or_die(sizeof(SharedData)); new_shared_data->threads = NULL; - new_shared_data->main_building = NULL; /* Should be set to NULL if freed */ - new_shared_data->box = NULL; /* freed inside main_building */ + new_shared_data->main_building = NULL; /* Should be set to NULL if freed before */ new_shared_data->threads_nb = 0; LINK_ALL(SharedData, new_shared_data, @@ -108,7 +102,6 @@ SharedData *_get_instance_SharedData(){ wait_all_threads, set_main_building, get_main_building, - get_box, call_elevator ); } diff --git a/SharedData/SharedData.h b/SharedData/SharedData.h index 54c660d..953721c 100644 --- a/SharedData/SharedData.h +++ b/SharedData/SharedData.h @@ -19,7 +19,6 @@ typedef struct o_SharedData { PRIVATE pthread_mutex_t mutex_array[MUTEX_NB]; PRIVATE Building * main_building; - PRIVATE CommunicationBox * box; PRIVATE void (*start_thread)(_THIS(SharedData), void * (*thread)(void *), void * data, int thread_number); @@ -27,7 +26,6 @@ typedef struct o_SharedData { PUBLIC void (*start_all_threads)(_THIS(SharedData)); PUBLIC void (*set_main_building)(_THIS(SharedData), Building * building); PUBLIC Building * (*get_main_building)(_THIS(SharedData)); - PUBLIC CommunicationBox * (*get_box)(_THIS(SharedData)); PUBLIC void (*wait_threads)(_THIS(SharedData)); PUBLIC int (*call_elevator)(_THIS(SharedData), int starting_floor, int destination_floor); diff --git a/Visitor/Visitor.c b/Visitor/Visitor.c index b18f420..16eae0b 100644 --- a/Visitor/Visitor.c +++ b/Visitor/Visitor.c @@ -3,6 +3,7 @@ // #include "Visitor.h" +#include "../SharedData/SharedData.h" #include GETTER(Visitor, char*, name); @@ -10,7 +11,9 @@ GETTER(Visitor, int, id); void * runnable_Visitor(void * void_this){ Visitor *this = (Visitor*) void_this; + SharedData * data = GET_INSTANCE(SharedData); 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, data->main_building->use_call_box(data->main_building, this->contact_name)); return NULL; }