mirror of
https://gitlab.com/klmp200/LO41.git
synced 2024-11-22 08:43:22 +00:00
106 lines
3.1 KiB
C
106 lines
3.1 KiB
C
//
|
|
// Created by Antoine Bartuccio on 05/06/2018.
|
|
//
|
|
|
|
#include <string.h>
|
|
#include "Elevator.h"
|
|
#include "../SharedData/SharedData.h"
|
|
|
|
SYNCHRONIZED_GETTER(Elevator, ELEVATOR_STATE, state)
|
|
SYNCHRONIZED_SETTER(Elevator, ELEVATOR_STATE, state)
|
|
SYNCHRONIZED_GETTER(Elevator, int, floor)
|
|
SYNCHRONIZED_SETTER(Elevator, int, floor)
|
|
|
|
void _free__Elevator(THIS(Elevator)){
|
|
DELETE(this->passenger_ids);
|
|
if (this->name != NULL)
|
|
free(this->name);
|
|
|
|
pthread_mutex_unlock(&this->mutex_passenger);
|
|
pthread_mutex_destroy(&this->mutex_passenger);
|
|
|
|
pthread_mutex_unlock(&this->mutex_state);
|
|
pthread_mutex_destroy(&this->mutex_state);
|
|
|
|
pthread_mutex_unlock(&this->mutex_floor);
|
|
pthread_mutex_destroy(&this->mutex_floor);
|
|
|
|
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 dans l'ascenseur %s\n",
|
|
passenger_id, this->name, this->passenger_ids->get_size(this->passenger_ids), this->name);
|
|
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);
|
|
num = this->passenger_ids->get_size(this->passenger_ids);
|
|
pthread_mutex_lock(&this->mutex_passenger);
|
|
return num;
|
|
}
|
|
|
|
int can_get_inside_Elevator(THIS(Elevator), int floor){
|
|
int permission;
|
|
pthread_mutex_lock(&this->mutex_passenger);
|
|
pthread_mutex_lock(&this->mutex_state);
|
|
pthread_mutex_lock(&this->mutex_floor);
|
|
|
|
permission = (this->passenger_ids->get_size(this->passenger_ids) < MAX_ELEVATOR_CAPACITY &&
|
|
this->state == WAITING && this->floor == floor);
|
|
|
|
pthread_mutex_unlock(&this->mutex_floor);
|
|
pthread_mutex_unlock(&this->mutex_state);
|
|
pthread_mutex_unlock(&this->mutex_passenger);
|
|
|
|
return permission;
|
|
}
|
|
|
|
void repair_Elevator(THIS(Elevator)){
|
|
this->set_state(this, RUNNING);
|
|
}
|
|
|
|
void *runnable_Elevator(void * void_this){
|
|
/* This is where the thread logic will be implemented */
|
|
Elevator * this = (Elevator*) void_this;
|
|
SharedData * data = GET_INSTANCE(SharedData);
|
|
|
|
AGENT_OPTIONS
|
|
|
|
printf("Je suis l'ascenseur %s\n", this->name);
|
|
for (;;)
|
|
data->main_building->signal_elevator_at_floor(data->main_building, this->get_floor(this));
|
|
return NULL;
|
|
}
|
|
|
|
Elevator *_init_Elevator(char * name){
|
|
Elevator * new_elevator = malloc_or_die(sizeof(Elevator));
|
|
new_elevator->name = strdup(name);
|
|
new_elevator->passenger_ids = NEW(List);
|
|
pthread_mutex_init(&new_elevator->mutex_passenger, NULL);
|
|
pthread_mutex_init(&new_elevator->mutex_state, NULL);
|
|
pthread_mutex_init(&new_elevator->mutex_floor, NULL);
|
|
|
|
LINK_ALL(Elevator, new_elevator,
|
|
runnable,
|
|
get_number_of_passengers,
|
|
can_get_inside,
|
|
add_passenger,
|
|
get_state,
|
|
set_state,
|
|
get_floor,
|
|
set_floor,
|
|
repair
|
|
);
|
|
|
|
new_elevator->set_floor(new_elevator, 0);
|
|
new_elevator->set_state(new_elevator, WAITING);
|
|
|
|
return new_elevator;
|
|
} |