mirror of
https://gitlab.com/klmp200/LO41.git
synced 2024-11-21 16:23:22 +00:00
Petit refactor avec des signaux
This commit is contained in:
parent
741a98f926
commit
ab17d4fa0e
@ -38,8 +38,9 @@ void parse_residents_Building(THIS(Building), char * file){
|
||||
char * trash;
|
||||
int i = 0;
|
||||
|
||||
if (f == NULL)
|
||||
if (f == NULL) {
|
||||
CRASH("File for residents does not exist");
|
||||
}
|
||||
|
||||
while (getline(&line, &len, f) > 0) {
|
||||
remove_end_char(line, '\n');
|
||||
@ -68,8 +69,9 @@ void parse_visitors_Building(THIS(Building), char * file){
|
||||
char * line = NULL;
|
||||
int i = 0;
|
||||
|
||||
if (f == NULL)
|
||||
if (f == NULL) {
|
||||
CRASH("File for visitors does not exist");
|
||||
}
|
||||
|
||||
while (getline(&line, &len, f) > 0) {
|
||||
remove_end_char(line, '\n');
|
||||
@ -131,9 +133,10 @@ int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger pa
|
||||
/* Make assumption that a waiting elevator is not full */
|
||||
for (i=0; i<ELEVATOR_NB; i++){
|
||||
if (this->elevators[i]->get_floor(this->elevators[i]) == current_floor &&
|
||||
this->elevators[i]->get_state(this->elevators[i]) == waiting){
|
||||
this->elevators[i]->get_state(this->elevators[i]) == WAITING){
|
||||
/* pour faire taire le compilateur le temps que je revienne sur cette fonction */
|
||||
printf("visiteur %s, de type %d\n", passenger.visitor->name, type);
|
||||
if (type == VISITOR)
|
||||
this->elevators[i]->add_passenger(this->elevators[i], passenger.visitor->get_id(passenger.visitor));
|
||||
/* Il faut faire des trucs ici */
|
||||
return i;
|
||||
}
|
||||
@ -144,8 +147,8 @@ int get_inside_elevator_Building(THIS(Building), int current_floor, Passenger pa
|
||||
void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger passenger, PASSENGER_TYPE type){
|
||||
int elevator_number;
|
||||
|
||||
if (origin < 0 || origin >= FLOORS) CRASH("You are trying to start from a non existing floor\n");
|
||||
if (destination < 0 || origin >= FLOORS) CRASH("You are trying to reach 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 || origin >= FLOORS) {CRASH("You are trying to reach a non existing floor\n");}
|
||||
|
||||
pthread_cond_wait(this->condition_floors[origin], this->mutex_get_inside_elevator);
|
||||
elevator_number = this->get_inside_elevator(this, origin, passenger, type);
|
||||
|
@ -27,6 +27,16 @@ void _free__Elevator(THIS(Elevator)){
|
||||
free(this);
|
||||
}
|
||||
|
||||
void add_passenger_Elevator(THIS(Elevator), int passenger_id){
|
||||
pthread_mutex_lock(&this->mutex_passenger);
|
||||
this->passenger_ids->insert_tail(this->passenger_ids, ((void *)&passenger_id), sizeof(int));
|
||||
printf("Le passager avec l'id %d est entre dans l'ascenseur %s\nIl y a maintenant %d passagers\n",
|
||||
passenger_id, this->name, this->passenger_ids->get_size(this->passenger_ids));
|
||||
if (this->passenger_ids->get_size(this->passenger_ids) >= MAX_ELEVATOR_CAPACITY)
|
||||
this->set_state(this, SLEEPING);
|
||||
pthread_mutex_unlock(&this->mutex_passenger);
|
||||
}
|
||||
|
||||
int get_number_of_passengers_Elevator(THIS(Elevator)){
|
||||
int num;
|
||||
pthread_mutex_lock(&this->mutex_passenger);
|
||||
@ -40,7 +50,7 @@ int can_get_more_passengers_Elevator(THIS(Elevator)){
|
||||
}
|
||||
|
||||
void repair_Elevator(THIS(Elevator)){
|
||||
this->set_state(this, running);
|
||||
this->set_state(this, RUNNING);
|
||||
}
|
||||
|
||||
void *runnable_Elevator(void * void_this){
|
||||
@ -52,7 +62,7 @@ void *runnable_Elevator(void * void_this){
|
||||
|
||||
Elevator *_init_Elevator(char * name){
|
||||
Elevator * new_elevator = malloc_or_die(sizeof(Elevator));
|
||||
new_elevator->state = waiting;
|
||||
new_elevator->state = WAITING;
|
||||
new_elevator->name = strdup(name);
|
||||
new_elevator->passenger_ids = NEW(List);
|
||||
pthread_mutex_init(&new_elevator->mutex_passenger, NULL);
|
||||
@ -63,6 +73,7 @@ Elevator *_init_Elevator(char * name){
|
||||
runnable,
|
||||
get_number_of_passengers,
|
||||
can_get_more_passengers,
|
||||
add_passenger,
|
||||
get_state,
|
||||
set_state,
|
||||
get_floor,
|
||||
|
@ -9,9 +9,10 @@
|
||||
#include "../Objects.h"
|
||||
#include "../List/List.h"
|
||||
|
||||
#define MAX_ELEVATOR_CAPACITY 10
|
||||
//#define MAX_ELEVATOR_CAPACITY 10
|
||||
#define MAX_ELEVATOR_CAPACITY 3
|
||||
|
||||
typedef enum {running, waiting, sleeping, broken} ELEVATOR_STATE;
|
||||
typedef enum {RUNNING, WAITING, SLEEPING, BROKEN} ELEVATOR_STATE;
|
||||
|
||||
typedef struct o_Elevator {
|
||||
PRIVATE ELEVATOR_STATE state;
|
||||
@ -29,6 +30,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), int passenger_id);
|
||||
SYNCHRONIZE PUBLIC ELEVATOR_STATE (*get_state)(_THIS(Elevator));
|
||||
SYNCHRONIZE PUBLIC int (*get_floor)(_THIS(Elevator));
|
||||
SYNCHRONIZE PUBLIC int (*can_get_more_passengers)(_THIS(Elevator));
|
||||
|
16
List/List.c
16
List/List.c
@ -7,6 +7,10 @@ GETTER(List, Element*, head)
|
||||
GETTER(List, Element*, tail)
|
||||
GETTER(List, int, size)
|
||||
|
||||
void set_custom_free_List(THIS(List), void (*custom_free)(void *)){
|
||||
this->custom_free = custom_free;
|
||||
}
|
||||
|
||||
Element * get_element_List(THIS(List), int index){
|
||||
Element * current = NULL;
|
||||
int i;
|
||||
@ -31,7 +35,9 @@ Element * get_element_List(THIS(List), int index){
|
||||
i--;
|
||||
}
|
||||
}
|
||||
} else OUTSIDE_BOUNDS;
|
||||
} else {
|
||||
OUTSIDE_BOUNDS;
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
@ -81,6 +87,8 @@ void clear_List(THIS(List)){
|
||||
void insert_tail_List(THIS(List), void * data, size_t data_size){
|
||||
/* Create a new element */
|
||||
Element *new_element = NEW(Element, data, data_size, this);
|
||||
new_element->data_free = this->custom_free;
|
||||
|
||||
if (this->tail == NULL){
|
||||
this->head = new_element;
|
||||
this->tail = new_element;
|
||||
@ -94,6 +102,8 @@ void insert_tail_List(THIS(List), void * data, size_t data_size){
|
||||
void insert_head_List(THIS(List), void * data, size_t data_size){
|
||||
/* Create a new element */
|
||||
Element *new_element = NEW(Element, data, data_size, this);
|
||||
new_element->data_free = this->custom_free;
|
||||
|
||||
if (this->head != NULL)
|
||||
this->head->previous = new_element;
|
||||
else
|
||||
@ -113,6 +123,8 @@ void insert_inside_List(THIS(List), void * data, size_t data_size, int index){
|
||||
this->insert_tail(this, data, data_size);
|
||||
else if (index < this->size){
|
||||
new_element = NEW(Element, data, data_size, this);
|
||||
new_element->data_free = this->custom_free;
|
||||
|
||||
old_element = this->get_element(this, index);
|
||||
|
||||
new_element->previous = old_element->previous;
|
||||
@ -139,6 +151,7 @@ List *_init_List(){
|
||||
l->size = 0;
|
||||
l->head = NULL;
|
||||
l->tail = NULL;
|
||||
l->custom_free = free;
|
||||
|
||||
LINK_ALL(List, l,
|
||||
get_head,
|
||||
@ -148,6 +161,7 @@ List *_init_List(){
|
||||
get_tail_data,
|
||||
get_element,
|
||||
get_element_data,
|
||||
set_custom_free,
|
||||
insert_inside,
|
||||
insert_tail,
|
||||
insert_head,
|
||||
|
@ -17,6 +17,7 @@ struct o_List {
|
||||
PRIVATE Element *head;
|
||||
PRIVATE Element *tail;
|
||||
PRIVATE int size;
|
||||
PRIVATE void (*custom_free)(void *);
|
||||
|
||||
PUBLIC Element *(*get_head)(_THIS(List));
|
||||
PUBLIC Element *(*get_tail)(_THIS(List));
|
||||
@ -29,6 +30,7 @@ struct o_List {
|
||||
|
||||
PUBLIC int (*get_size)(_THIS(List));
|
||||
|
||||
PUBLIC void (*set_custom_free)(_THIS(List), void (*custom_free)(void *));
|
||||
PUBLIC void (*insert_inside)(_THIS(List), void * data, size_t data_size, int index);
|
||||
PUBLIC void (*insert_tail)(_THIS(List), void * data, size_t data_size);
|
||||
PUBLIC void (*insert_head)(_THIS(List), void * data, size_t data_size);
|
||||
|
@ -6,7 +6,8 @@
|
||||
|
||||
void *malloc_or_die(size_t size){
|
||||
void *ptr = malloc(size);
|
||||
if (ptr == NULL)
|
||||
perror("Error allocating object");
|
||||
if (ptr == NULL){
|
||||
CRASH("Error allocating object");
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
|
||||
#define NEW(type, ...) _init_##type(__VA_ARGS__)
|
||||
#define GET_INSTANCE(type, ...) _get_instance_##type(__VA_ARGS__)
|
||||
@ -14,7 +15,7 @@
|
||||
#define DESTRUCTOR(type) void (*_free_)(struct o_##type *this)
|
||||
#define THIS(type) type *this
|
||||
#define _THIS(type) struct o_##type *this
|
||||
#define CRASH(message) perror(message)
|
||||
#define CRASH(message) perror(message); raise(SIGINT)
|
||||
|
||||
#define SYNCHRONIZE
|
||||
#define PRIVATE
|
||||
|
@ -35,8 +35,9 @@ void start_all_threads_SharedData(THIS(SharedData)){
|
||||
List * visitors = NULL;
|
||||
|
||||
if (this->threads == NULL){
|
||||
if (this->main_building == NULL)
|
||||
if (this->main_building == NULL) {
|
||||
CRASH("No building attached to SharedData and thus no thread can be created\n");
|
||||
}
|
||||
else {
|
||||
residents = this->main_building->residents;
|
||||
visitors = this->main_building->visitors;
|
||||
|
@ -12,8 +12,12 @@ GETTER(Visitor, int, id);
|
||||
void * runnable_Visitor(void * void_this){
|
||||
Visitor *this = (Visitor*) void_this;
|
||||
SharedData * data = GET_INSTANCE(SharedData);
|
||||
Passenger 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, 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);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
2
main.c
2
main.c
@ -1,5 +1,4 @@
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "Objects.h"
|
||||
@ -18,6 +17,7 @@ void clean_exit(int error_code){
|
||||
int main() {
|
||||
SharedData * shared_data = GET_INSTANCE(SharedData);
|
||||
|
||||
signal(SIGINT, clean_exit);
|
||||
shared_data->set_main_building(shared_data, NEW(Building, "../residents.txt", "../visitors.txt"));
|
||||
shared_data->start_all_threads(shared_data);
|
||||
shared_data->wait_all_threads(shared_data);
|
||||
|
Loading…
Reference in New Issue
Block a user