1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2024-12-26 09:41:11 +00:00

huge refactor, pointy pointer

This commit is contained in:
Aethor 2018-06-20 19:36:00 +02:00
parent f6f12b398d
commit 9b11ae0835
8 changed files with 45 additions and 35 deletions

View File

@ -142,13 +142,13 @@ void _free__Building(THIS(Building)){
* @passenger the passenger to put in the elevator
* @returns an elevator number ( between 0 and ELEVATOR_NB - 1, inclusive ), -1 if no elevator was found ready to accept the passenger
*/
int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger passenger){
int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger * 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);
passenger->type == VISITOR ? passenger->visitor->name : passenger->resident->name,
passenger->type == VISITOR ? passenger->visitor->id : passenger->resident->id);
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);
@ -160,37 +160,38 @@ int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger pa
return -1;
}
void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger passenger){
void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger * passenger){
int elevator_number;
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->compare(this->waiting_passengers, (void*) &passenger, passenger.compare))
this->waiting_passengers->insert_tail(this->waiting_passengers, (void*) &passenger, sizeof(passenger));//todo : check if inside list
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);
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,
if (passenger->type == RESIDENT)
printf("Le résident %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger->resident->name,
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,
else if (passenger->type == VISITOR)
printf("Le visiteur %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger->visitor->name,
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,
if (passenger->type == RESIDENT)
printf("Le résident %s sort de l'ascenseur %s à l'étage %d\n", passenger->resident->name,
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,
else if (passenger->type == VISITOR)
printf("Le visiteur %s sort de l'ascenseur %s à l'étage %d\n", passenger->visitor->name,
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);
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);
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);
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
this->go_to_floor(this, origin, destination, passenger);
}

View File

@ -28,12 +28,12 @@ typedef struct o_Building {
PRIVATE void (*parse_residents)(_THIS(Building), char * file);
PRIVATE void (*parse_visitors)(_THIS(Building), char * file);
PRIVATE int (*get_inside_elevator)(_THIS(Building), int current_floor, Passenger passenger);
PRIVATE int (*get_inside_elevator)(_THIS(Building), int current_floor, Passenger * passenger);
PUBLIC int (*use_call_box)(_THIS(Building), char * resident_name);
PUBLIC void (*signal_elevator_at_floor)(_THIS(Building), int floor);
SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger passenger);
SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger * passenger);
DESTRUCTOR(Building);
} Building;

View File

@ -29,11 +29,11 @@ void _free__Elevator(THIS(Elevator)){
free(this);
}
void add_passenger_Elevator(THIS(Elevator), Passenger passenger){
void add_passenger_Elevator(THIS(Elevator), Passenger * passenger){
pthread_mutex_lock(&this->mutex_passengers);
this->passengers->insert_tail(this->passengers, ((void *)&passenger), sizeof(Passenger));
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),
passenger->type == VISITOR ? passenger->visitor->get_name(passenger->visitor) : passenger->resident->get_name(passenger->resident),
this->passengers->get_size(this->passengers), this->name);
if (this->passengers->get_size(this->passengers) >= MAX_ELEVATOR_CAPACITY)
this->set_state(this, SLEEPING);

View File

@ -32,7 +32,7 @@ typedef struct o_Elevator {
SYNCHRONIZE PUBLIC void (*repair)(_THIS(Elevator));
SYNCHRONIZE PUBLIC int (*get_number_of_passengers)(_THIS(Elevator));
SYNCHRONIZE PUBLIC void (*add_passenger)(_THIS(Elevator), Passenger passenger);
SYNCHRONIZE PUBLIC void (*add_passenger)(_THIS(Elevator), Passenger * passenger);
SYNCHRONIZE PUBLIC ELEVATOR_STATE (*get_state)(_THIS(Elevator));
SYNCHRONIZE PUBLIC int (*get_floor)(_THIS(Elevator));
SYNCHRONIZE PUBLIC int (*can_get_inside)(_THIS(Elevator), int floor);

View File

@ -4,6 +4,11 @@
#include "Passenger.h"
int compare_Passenger(void * passenger1, void * passenger2){
return (strcmp(((Passenger*) passenger1)->get_name((Passenger*) passenger1),
((Passenger*) passenger2)->get_name((Passenger*) passenger2)) == 0);
}
int get_id_Passenger(THIS(Passenger)){
if (this->type == RESIDENT)
return this->resident->get_id(this->resident);
@ -33,23 +38,25 @@ void _free__Passenger(THIS(Passenger)){
free(this);
}
Passenger *_init_Passenger(void *passenger, PASSENGER_TYPE type){
Passenger *_init_Passenger(void* passenger_data, PASSENGER_TYPE type){
Passenger * new_passenger = malloc_or_die(sizeof(Passenger));
new_passenger->resident = NULL;
new_passenger->visitor = NULL;
new_passenger->type = type;
if (type == RESIDENT)
new_passenger->resident = (Resident*) passenger;
new_passenger->resident = (Resident*) passenger_data;
if (type == VISITOR)
new_passenger->visitor = (Visitor*) passenger;
new_passenger->visitor = (Visitor*) passenger_data;
//new_passenger->compare = compare_Passenger;
LINK_ALL(Passenger, new_passenger,
get_id,
get_name,
compare,
runnable
)
return new_passenger;
}
}

View File

@ -7,6 +7,7 @@
#include "../Resident/Resident.h"
#include "../Visitor/Visitor.h"
#include <string.h>
typedef enum {RESIDENT, VISITOR} PASSENGER_TYPE;
@ -20,10 +21,11 @@ typedef struct o_Passenger {
PUBLIC char * (*get_name)(_THIS(Passenger));
PUBLIC int (*get_id)(_THIS(Passenger));
PUBLIC void * (*runnable)(void* void_this);
PUBLIC int (*compare)(void * passenger1, void * passenger2);//yeah I know, but i needed int (*) (void*, void*)
DESTRUCTOR(Passenger);
} Passenger;
Passenger *_init_Passenger(void *passenger, PASSENGER_TYPE type);
Passenger *_init_Passenger(void* passenger_data, PASSENGER_TYPE type);
#endif //LO41_PASSENGER_H

View File

@ -14,12 +14,12 @@ GETTER(Resident, int, apartment_floor);
void * runnable_Resident(void * void_this){
Resident * this = (Resident*) void_this;
SharedData* data = GET_INSTANCE(SharedData);
Passenger passenger;
Passenger * passenger = NEW(Passenger, this, RESIDENT);
AGENT_OPTIONS;
passenger.resident = this;
passenger.type = RESIDENT;
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);

View File

@ -12,16 +12,16 @@ GETTER(Visitor, int, id);
void * runnable_Visitor(void * void_this){
Visitor *this = (Visitor*) void_this;
SharedData * data = GET_INSTANCE(SharedData);
Passenger passenger;
Passenger * passenger = NEW(Passenger, (void*) this, VISITOR);
AGENT_OPTIONS;
passenger.visitor = this;
passenger.type = VISITOR;
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;
}