2018-06-05 23:15:23 +00:00
|
|
|
//
|
|
|
|
// Created by Antoine Bartuccio on 05/06/2018.
|
|
|
|
//
|
|
|
|
|
2018-06-07 20:04:30 +00:00
|
|
|
#include <string.h>
|
2018-06-05 23:15:23 +00:00
|
|
|
#include "Elevator.h"
|
2018-06-06 01:00:35 +00:00
|
|
|
|
2018-06-07 22:08:18 +00:00
|
|
|
SYNCHRONIZED_GETTER(Elevator, ELEVATOR_STATE, state)
|
|
|
|
SYNCHRONIZED_SETTER(Elevator, ELEVATOR_STATE, state)
|
2018-06-10 10:47:07 +00:00
|
|
|
SYNCHRONIZED_GETTER(Elevator, int, floor)
|
|
|
|
SYNCHRONIZED_SETTER(Elevator, int, floor)
|
2018-06-07 22:08:18 +00:00
|
|
|
|
2018-06-06 01:00:35 +00:00
|
|
|
void _free__Elevator(THIS(Elevator)){
|
2018-06-07 20:04:30 +00:00
|
|
|
DELETE(this->passenger_ids);
|
|
|
|
if (this->name != NULL)
|
|
|
|
free(this->name);
|
2018-06-07 22:08:18 +00:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&this->mutex_passenger);
|
|
|
|
pthread_mutex_destroy(&this->mutex_passenger);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&this->mutex_state);
|
|
|
|
pthread_mutex_destroy(&this->mutex_state);
|
|
|
|
|
2018-06-10 02:00:01 +00:00
|
|
|
pthread_mutex_unlock(&this->mutex_floor);
|
|
|
|
pthread_mutex_destroy(&this->mutex_floor);
|
|
|
|
|
2018-06-06 01:00:35 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2018-06-15 09:50:38 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-06-07 20:04:30 +00:00
|
|
|
int get_number_of_passengers_Elevator(THIS(Elevator)){
|
|
|
|
int num;
|
2018-06-07 22:08:18 +00:00
|
|
|
pthread_mutex_lock(&this->mutex_passenger);
|
2018-06-07 20:04:30 +00:00
|
|
|
num = this->passenger_ids->get_size(this->passenger_ids);
|
2018-06-07 22:08:18 +00:00
|
|
|
pthread_mutex_lock(&this->mutex_passenger);
|
2018-06-07 20:04:30 +00:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2018-06-07 22:08:18 +00:00
|
|
|
int can_get_more_passengers_Elevator(THIS(Elevator)){
|
2018-06-07 20:04:30 +00:00
|
|
|
return (this->get_number_of_passengers(this) < MAX_ELEVATOR_CAPACITY);
|
|
|
|
}
|
|
|
|
|
2018-06-07 22:08:18 +00:00
|
|
|
void repair_Elevator(THIS(Elevator)){
|
2018-06-15 09:50:38 +00:00
|
|
|
this->set_state(this, RUNNING);
|
2018-06-07 22:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *runnable_Elevator(void * void_this){
|
|
|
|
/* This is where the thread logic will be implemented */
|
|
|
|
Elevator * this = (Elevator*) void_this;
|
2018-06-10 23:58:52 +00:00
|
|
|
printf("Je suis l'ascenseur %s\n", this->name);
|
|
|
|
return NULL;
|
2018-06-07 22:08:18 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 20:04:30 +00:00
|
|
|
Elevator *_init_Elevator(char * name){
|
2018-06-06 01:00:35 +00:00
|
|
|
Elevator * new_elevator = malloc_or_die(sizeof(Elevator));
|
2018-06-15 09:50:38 +00:00
|
|
|
new_elevator->state = WAITING;
|
2018-06-07 20:04:30 +00:00
|
|
|
new_elevator->name = strdup(name);
|
|
|
|
new_elevator->passenger_ids = NEW(List);
|
2018-06-07 22:08:18 +00:00
|
|
|
pthread_mutex_init(&new_elevator->mutex_passenger, NULL);
|
|
|
|
pthread_mutex_init(&new_elevator->mutex_state, NULL);
|
2018-06-10 02:00:01 +00:00
|
|
|
pthread_mutex_init(&new_elevator->mutex_floor, NULL);
|
2018-06-06 01:00:35 +00:00
|
|
|
|
2018-06-07 20:04:30 +00:00
|
|
|
LINK_ALL(Elevator, new_elevator,
|
2018-06-07 22:08:18 +00:00
|
|
|
runnable,
|
2018-06-07 20:04:30 +00:00
|
|
|
get_number_of_passengers,
|
2018-06-07 22:08:18 +00:00
|
|
|
can_get_more_passengers,
|
2018-06-15 09:50:38 +00:00
|
|
|
add_passenger,
|
2018-06-07 22:08:18 +00:00
|
|
|
get_state,
|
|
|
|
set_state,
|
2018-06-10 02:00:01 +00:00
|
|
|
get_floor,
|
|
|
|
set_floor,
|
2018-06-07 22:08:18 +00:00
|
|
|
repair
|
2018-06-07 20:04:30 +00:00
|
|
|
);
|
2018-06-06 01:00:35 +00:00
|
|
|
|
|
|
|
return new_elevator;
|
|
|
|
}
|