mirror of
https://gitlab.com/klmp200/LO41.git
synced 2024-11-22 08:43:22 +00:00
ElevatorPassenger c'est plus propre, c'est ça le vrai polymorphisme du C++ en C
This commit is contained in:
parent
575defc5fc
commit
b001cb1a91
@ -135,16 +135,16 @@ void _free__Building(THIS(Building)){
|
|||||||
free(this);
|
free(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger passenger, PASSENGER_TYPE type){
|
int get_inside_elevator_Building(THIS(Building), int current_floor, ElevatorPassenger elevator_passenger){
|
||||||
int i;
|
int i;
|
||||||
/* Make assumption that a waiting elevator is not full */
|
/* Make assumption that a waiting elevator is not full */
|
||||||
pthread_mutex_lock(this->mutex_func_get_inside_elevator);
|
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.visitor->name, passenger.visitor->id);
|
printf("Test d'entrée à l'étage %d de %s : id %d\n", current_floor, elevator_passenger.passenger.visitor->name, elevator_passenger.passenger.visitor->id);
|
||||||
for (i=0; i<ELEVATOR_NB; i++){
|
for (i=0; i<ELEVATOR_NB; i++){
|
||||||
if (this->elevators[i]->can_get_inside(this->elevators[i], current_floor)){
|
if (this->elevators[i]->can_get_inside(this->elevators[i], current_floor)){
|
||||||
/* pour faire taire le compilateur le temps que je revienne sur cette fonction */
|
/* pour faire taire le compilateur le temps que je revienne sur cette fonction */
|
||||||
if (type == VISITOR)
|
if (elevator_passenger.type == VISITOR)
|
||||||
this->elevators[i]->add_passenger(this->elevators[i], passenger.visitor->get_id(passenger.visitor));
|
this->elevators[i]->add_passenger(this->elevators[i], elevator_passenger.passenger.visitor->get_id(elevator_passenger.passenger.visitor));
|
||||||
/* Il faut faire des trucs ici */
|
/* Il faut faire des trucs ici */
|
||||||
pthread_mutex_unlock(this->mutex_func_get_inside_elevator);
|
pthread_mutex_unlock(this->mutex_func_get_inside_elevator);
|
||||||
return i;
|
return i;
|
||||||
@ -154,27 +154,27 @@ int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger pa
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger passenger, PASSENGER_TYPE type){
|
void go_to_floor_Building(THIS(Building), int origin, int destination, ElevatorPassenger elevator_passenger){
|
||||||
int elevator_number;
|
int elevator_number;
|
||||||
|
|
||||||
if (origin < 0 || origin >= FLOORS) {CRASH("You are trying to start from a non existing floor\n");}
|
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 (destination < 0 || destination >= FLOORS) {CRASH("You are trying to reach a non existing floor\n");}
|
||||||
|
|
||||||
pthread_cond_wait(this->condition_floors[origin], this->mutex_cond_get_inside_elevator);
|
pthread_cond_wait(this->condition_floors[origin], this->mutex_cond_get_inside_elevator);
|
||||||
elevator_number = this->get_inside_elevator(this, origin, passenger, type);
|
elevator_number = this->get_inside_elevator(this, origin, elevator_passenger);
|
||||||
if (type == RESIDENT)
|
if (elevator_passenger.type == RESIDENT)
|
||||||
printf("Le résident %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger.resident->name,
|
printf("Le résident %s rentre dans l'ascenseur %s depuis l'étage %d\n", elevator_passenger.passenger.resident->name,
|
||||||
this->elevators[elevator_number]->name, origin);
|
this->elevators[elevator_number]->name, origin);
|
||||||
else if (type == VISITOR)
|
else if (elevator_passenger.type == VISITOR)
|
||||||
printf("Le visiteur %s rentre dans l'ascenseur %s depuis l'étage %d\n", passenger.visitor->name,
|
printf("Le visiteur %s rentre dans l'ascenseur %s depuis l'étage %d\n", elevator_passenger.passenger.visitor->name,
|
||||||
this->elevators[elevator_number]->name, origin);
|
this->elevators[elevator_number]->name, origin);
|
||||||
|
|
||||||
pthread_cond_wait(this->condition_floors[destination], this->mutex_cond_get_outside_elevator);
|
pthread_cond_wait(this->condition_floors[destination], this->mutex_cond_get_outside_elevator);
|
||||||
if (type == RESIDENT)
|
if (elevator_passenger.type == RESIDENT)
|
||||||
printf("Le résident %s sors de l'ascenseur %s à l'étage %d\n", passenger.resident->name,
|
printf("Le résident %s sors de l'ascenseur %s à l'étage %d\n", elevator_passenger.passenger.resident->name,
|
||||||
this->elevators[elevator_number]->name, destination);
|
this->elevators[elevator_number]->name, destination);
|
||||||
else if (type == VISITOR)
|
else if (elevator_passenger.type == VISITOR)
|
||||||
printf("Le visiteur %s sors de l'ascenseur %s à l'étage %d\n", passenger.visitor->name,
|
printf("Le visiteur %s sors de l'ascenseur %s à l'étage %d\n", elevator_passenger.passenger.visitor->name,
|
||||||
this->elevators[elevator_number]->name, destination);
|
this->elevators[elevator_number]->name, destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,13 +13,7 @@
|
|||||||
#include "../Elevator/Elevator.h"
|
#include "../Elevator/Elevator.h"
|
||||||
#include "../Resident/Resident.h"
|
#include "../Resident/Resident.h"
|
||||||
#include "../Visitor/Visitor.h"
|
#include "../Visitor/Visitor.h"
|
||||||
|
#include "../Passenger/Passenger.h"
|
||||||
typedef enum {RESIDENT, VISITOR} PASSENGER_TYPE;
|
|
||||||
|
|
||||||
typedef union u_Passenger {
|
|
||||||
Resident * resident;
|
|
||||||
Visitor * visitor;
|
|
||||||
} Passenger;
|
|
||||||
|
|
||||||
typedef struct o_Building {
|
typedef struct o_Building {
|
||||||
PRIVATE int floors;
|
PRIVATE int floors;
|
||||||
@ -33,12 +27,12 @@ typedef struct o_Building {
|
|||||||
|
|
||||||
PRIVATE void (*parse_residents)(_THIS(Building), char * file);
|
PRIVATE void (*parse_residents)(_THIS(Building), char * file);
|
||||||
PRIVATE void (*parse_visitors)(_THIS(Building), char * file);
|
PRIVATE void (*parse_visitors)(_THIS(Building), char * file);
|
||||||
PRIVATE int (*get_inside_elevator)(_THIS(Building), int current_floor, Passenger passenger, PASSENGER_TYPE type);
|
PRIVATE int (*get_inside_elevator)(_THIS(Building), int current_floor, ElevatorPassenger elevator_passenger);
|
||||||
|
|
||||||
PUBLIC int (*use_call_box)(_THIS(Building), char * resident_name);
|
PUBLIC int (*use_call_box)(_THIS(Building), char * resident_name);
|
||||||
PUBLIC void (*signal_elevator_at_floor)(_THIS(Building), int floor);
|
PUBLIC void (*signal_elevator_at_floor)(_THIS(Building), int floor);
|
||||||
|
|
||||||
SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger passenger, PASSENGER_TYPE type);
|
SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, ElevatorPassenger elevator_passenger);
|
||||||
|
|
||||||
DESTRUCTOR(Building);
|
DESTRUCTOR(Building);
|
||||||
} Building;
|
} Building;
|
||||||
|
@ -8,4 +8,4 @@ if(CMAKE_COMPILER_IS_GNUCC)
|
|||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
|
||||||
endif()
|
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 SharedData/SharedData.c SharedData/SharedData.h Visitor/Visitor.c Visitor/Visitor.h Resident/Resident.c Resident/Resident.h)
|
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 Passenger/Passenger.h)
|
@ -12,7 +12,7 @@ SYNCHRONIZED_GETTER(Elevator, int, floor)
|
|||||||
SYNCHRONIZED_SETTER(Elevator, int, floor)
|
SYNCHRONIZED_SETTER(Elevator, int, floor)
|
||||||
|
|
||||||
void _free__Elevator(THIS(Elevator)){
|
void _free__Elevator(THIS(Elevator)){
|
||||||
DELETE(this->passenger_ids);
|
DELETE(this->passengers);
|
||||||
if (this->name != NULL)
|
if (this->name != NULL)
|
||||||
free(this->name);
|
free(this->name);
|
||||||
|
|
||||||
@ -30,10 +30,10 @@ void _free__Elevator(THIS(Elevator)){
|
|||||||
|
|
||||||
void add_passenger_Elevator(THIS(Elevator), int passenger_id){
|
void add_passenger_Elevator(THIS(Elevator), int passenger_id){
|
||||||
pthread_mutex_lock(&this->mutex_passenger);
|
pthread_mutex_lock(&this->mutex_passenger);
|
||||||
this->passenger_ids->insert_tail(this->passenger_ids, ((void *)&passenger_id), sizeof(int));
|
this->passengers->insert_tail(this->passengers, ((void *)&passenger_id), sizeof(int));
|
||||||
printf("Le passager avec l'id %d est entre dans l'ascenseur %s\nIl y a maintenant %d passagers dans l'ascenseur %s\n",
|
printf("Le passager avec l'id %d est entre dans l'ascenseur %s\nIl y a maintenant %d passagers dans l'ascenseur %s\n",
|
||||||
passenger_id, this->name, this->passenger_ids->get_size(this->passenger_ids), this->name);
|
passenger_id, this->name, this->passengers->get_size(this->passengers), this->name);
|
||||||
if (this->passenger_ids->get_size(this->passenger_ids) >= MAX_ELEVATOR_CAPACITY)
|
if (this->passengers->get_size(this->passengers) >= MAX_ELEVATOR_CAPACITY)
|
||||||
this->set_state(this, SLEEPING);
|
this->set_state(this, SLEEPING);
|
||||||
pthread_mutex_unlock(&this->mutex_passenger);
|
pthread_mutex_unlock(&this->mutex_passenger);
|
||||||
}
|
}
|
||||||
@ -41,7 +41,7 @@ void add_passenger_Elevator(THIS(Elevator), int passenger_id){
|
|||||||
int get_number_of_passengers_Elevator(THIS(Elevator)){
|
int get_number_of_passengers_Elevator(THIS(Elevator)){
|
||||||
int num;
|
int num;
|
||||||
pthread_mutex_lock(&this->mutex_passenger);
|
pthread_mutex_lock(&this->mutex_passenger);
|
||||||
num = this->passenger_ids->get_size(this->passenger_ids);
|
num = this->passengers->get_size(this->passengers);
|
||||||
pthread_mutex_unlock(&this->mutex_passenger);
|
pthread_mutex_unlock(&this->mutex_passenger);
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
@ -52,7 +52,7 @@ int can_get_inside_Elevator(THIS(Elevator), int floor){
|
|||||||
pthread_mutex_lock(&this->mutex_state);
|
pthread_mutex_lock(&this->mutex_state);
|
||||||
pthread_mutex_lock(&this->mutex_floor);
|
pthread_mutex_lock(&this->mutex_floor);
|
||||||
|
|
||||||
permission = (this->passenger_ids->get_size(this->passenger_ids) < MAX_ELEVATOR_CAPACITY &&
|
permission = (this->passengers->get_size(this->passengers) < MAX_ELEVATOR_CAPACITY &&
|
||||||
this->state == WAITING && this->floor == floor);
|
this->state == WAITING && this->floor == floor);
|
||||||
|
|
||||||
pthread_mutex_unlock(&this->mutex_floor);
|
pthread_mutex_unlock(&this->mutex_floor);
|
||||||
@ -82,7 +82,7 @@ void *runnable_Elevator(void * void_this){
|
|||||||
Elevator *_init_Elevator(char * name){
|
Elevator *_init_Elevator(char * name){
|
||||||
Elevator * new_elevator = malloc_or_die(sizeof(Elevator));
|
Elevator * new_elevator = malloc_or_die(sizeof(Elevator));
|
||||||
new_elevator->name = strdup(name);
|
new_elevator->name = strdup(name);
|
||||||
new_elevator->passenger_ids = NEW(List);
|
new_elevator->passengers = NEW(List);
|
||||||
pthread_mutex_init(&new_elevator->mutex_passenger, NULL);
|
pthread_mutex_init(&new_elevator->mutex_passenger, NULL);
|
||||||
pthread_mutex_init(&new_elevator->mutex_state, NULL);
|
pthread_mutex_init(&new_elevator->mutex_state, NULL);
|
||||||
pthread_mutex_init(&new_elevator->mutex_floor, NULL);
|
pthread_mutex_init(&new_elevator->mutex_floor, NULL);
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include "../Objects.h"
|
#include "../Objects.h"
|
||||||
#include "../List/List.h"
|
#include "../List/List.h"
|
||||||
|
#include "../Passenger/Passenger.h"
|
||||||
|
|
||||||
//#define MAX_ELEVATOR_CAPACITY 10
|
//#define MAX_ELEVATOR_CAPACITY 10
|
||||||
#define MAX_ELEVATOR_CAPACITY 3
|
#define MAX_ELEVATOR_CAPACITY 3
|
||||||
@ -16,7 +17,7 @@ typedef enum {RUNNING, WAITING, SLEEPING, BROKEN} ELEVATOR_STATE;
|
|||||||
|
|
||||||
typedef struct o_Elevator {
|
typedef struct o_Elevator {
|
||||||
PRIVATE ELEVATOR_STATE state;
|
PRIVATE ELEVATOR_STATE state;
|
||||||
PRIVATE List * passenger_ids;
|
PRIVATE List * passengers;
|
||||||
PRIVATE char * name;
|
PRIVATE char * name;
|
||||||
PRIVATE int floor;
|
PRIVATE int floor;
|
||||||
PRIVATE pthread_mutex_t mutex_passenger;
|
PRIVATE pthread_mutex_t mutex_passenger;
|
||||||
|
23
Passenger/Passenger.h
Normal file
23
Passenger/Passenger.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
//
|
||||||
|
// Created by Antoine Bartuccio on 18/06/2018.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef LO41_PASSENGER_H
|
||||||
|
#define LO41_PASSENGER_H
|
||||||
|
|
||||||
|
#include "../Resident/Resident.h"
|
||||||
|
#include "../Visitor/Visitor.h"
|
||||||
|
|
||||||
|
typedef enum {RESIDENT, VISITOR} PASSENGER_TYPE;
|
||||||
|
|
||||||
|
typedef union u_Passenger {
|
||||||
|
Resident * resident;
|
||||||
|
Visitor * visitor;
|
||||||
|
} Passenger;
|
||||||
|
|
||||||
|
typedef struct s_ElevatorPassenger {
|
||||||
|
PASSENGER_TYPE type;
|
||||||
|
Passenger passenger;
|
||||||
|
} ElevatorPassenger;
|
||||||
|
|
||||||
|
#endif //LO41_PASSENGER_H
|
@ -13,14 +13,17 @@ void * runnable_Visitor(void * void_this){
|
|||||||
Visitor *this = (Visitor*) void_this;
|
Visitor *this = (Visitor*) void_this;
|
||||||
SharedData * data = GET_INSTANCE(SharedData);
|
SharedData * data = GET_INSTANCE(SharedData);
|
||||||
Passenger passenger;
|
Passenger passenger;
|
||||||
|
ElevatorPassenger elevator_passenger;
|
||||||
|
|
||||||
AGENT_OPTIONS
|
AGENT_OPTIONS
|
||||||
|
|
||||||
passenger.visitor = this;
|
passenger.visitor = this;
|
||||||
|
elevator_passenger.passenger = passenger;
|
||||||
|
elevator_passenger.type = VISITOR;
|
||||||
|
|
||||||
printf("Bonjour, je suis %s et je souhaite rendre visite a %s\n", this->name, this->contact_name);
|
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)));
|
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, VISITOR);
|
data->main_building->go_to_floor(data->main_building, this->position, this->destination, elevator_passenger);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user