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-18 17:24:35 +00:00
|
|
|
#include <math.h>
|
2018-06-05 23:15:23 +00:00
|
|
|
#include "Elevator.h"
|
2018-06-15 13:38:26 +00:00
|
|
|
#include "../SharedData/SharedData.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-18 13:51:48 +00:00
|
|
|
DELETE(this->passengers);
|
2018-06-07 20:04:30 +00:00
|
|
|
if (this->name != NULL)
|
|
|
|
free(this->name);
|
2018-06-07 22:08:18 +00:00
|
|
|
|
2018-06-18 17:24:35 +00:00
|
|
|
pthread_mutex_unlock(&this->mutex_passengers);
|
|
|
|
pthread_mutex_destroy(&this->mutex_passengers);
|
2018-06-07 22:08:18 +00:00
|
|
|
|
|
|
|
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-18 14:09:28 +00:00
|
|
|
void add_passenger_Elevator(THIS(Elevator), Passenger passenger){
|
2018-06-18 17:24:35 +00:00
|
|
|
pthread_mutex_lock(&this->mutex_passengers);
|
2018-06-18 14:09:28 +00:00
|
|
|
this->passengers->insert_tail(this->passengers, ((void *)&passenger), sizeof(Passenger));
|
2018-06-18 14:14:01 +00:00
|
|
|
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),
|
|
|
|
this->passengers->get_size(this->passengers), this->name);
|
2018-06-18 13:51:48 +00:00
|
|
|
if (this->passengers->get_size(this->passengers) >= MAX_ELEVATOR_CAPACITY)
|
2018-06-15 09:50:38 +00:00
|
|
|
this->set_state(this, SLEEPING);
|
2018-06-18 17:24:35 +00:00
|
|
|
pthread_mutex_unlock(&this->mutex_passengers);
|
2018-06-15 09:50:38 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 20:04:30 +00:00
|
|
|
int get_number_of_passengers_Elevator(THIS(Elevator)){
|
|
|
|
int num;
|
2018-06-18 17:24:35 +00:00
|
|
|
pthread_mutex_lock(&this->mutex_passengers);
|
2018-06-18 13:51:48 +00:00
|
|
|
num = this->passengers->get_size(this->passengers);
|
2018-06-18 17:24:35 +00:00
|
|
|
pthread_mutex_unlock(&this->mutex_passengers);
|
2018-06-07 20:04:30 +00:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2018-06-18 17:24:35 +00:00
|
|
|
/**
|
|
|
|
* Search the closest floor where the elevator should stop. In practice ( for now ), it means that this function returns the closest floor amongs it's passengers destinations.
|
|
|
|
* @THIS(Elevator) a pointer the current Elevator
|
|
|
|
* @return the found floor as an int. If no passengers are in the elevator, returns 0 ( as the elevator should come back to floor 0 ).
|
|
|
|
* @todo should consider passenger calling the elevator
|
|
|
|
*/
|
|
|
|
int get_next_floor_Elevator(THIS(Elevator)){
|
|
|
|
int i, next_floor, temp_floor;
|
|
|
|
float min_diff;
|
|
|
|
Element* temp_element;
|
|
|
|
Passenger* temp_passenger;
|
|
|
|
pthread_mutex_lock(&this->mutex_passengers);
|
|
|
|
pthread_mutex_lock(&this->mutex_floor);
|
|
|
|
|
|
|
|
min_diff = INFINITY;
|
|
|
|
next_floor = 0;
|
|
|
|
|
|
|
|
for(i=0;i<this->passengers->get_size(this->passengers);i++){
|
|
|
|
temp_element = this->passengers->get_element(this->passengers, i);
|
|
|
|
temp_passenger = (Passenger*) temp_element->get_data(temp_element);
|
|
|
|
temp_floor = temp_passenger->type == RESIDENT ? temp_passenger->resident->destination : temp_passenger->visitor->destination;
|
|
|
|
if(abs(this->floor - temp_floor) < min_diff){
|
|
|
|
min_diff = abs(this->floor - temp_floor);
|
|
|
|
next_floor = temp_floor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&this->mutex_passengers);
|
|
|
|
pthread_mutex_unlock(&this->mutex_floor);
|
|
|
|
|
|
|
|
return next_floor;
|
|
|
|
}
|
|
|
|
|
2018-06-15 13:38:26 +00:00
|
|
|
int can_get_inside_Elevator(THIS(Elevator), int floor){
|
|
|
|
int permission;
|
2018-06-18 17:24:35 +00:00
|
|
|
pthread_mutex_lock(&this->mutex_passengers);
|
2018-06-15 13:38:26 +00:00
|
|
|
pthread_mutex_lock(&this->mutex_state);
|
|
|
|
pthread_mutex_lock(&this->mutex_floor);
|
|
|
|
|
2018-06-18 13:51:48 +00:00
|
|
|
permission = (this->passengers->get_size(this->passengers) < MAX_ELEVATOR_CAPACITY &&
|
2018-06-15 13:38:26 +00:00
|
|
|
this->state == WAITING && this->floor == floor);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&this->mutex_floor);
|
|
|
|
pthread_mutex_unlock(&this->mutex_state);
|
2018-06-18 17:24:35 +00:00
|
|
|
pthread_mutex_unlock(&this->mutex_passengers);
|
2018-06-15 13:38:26 +00:00
|
|
|
|
|
|
|
return permission;
|
2018-06-07 20:04:30 +00:00
|
|
|
}
|
|
|
|
|
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-15 13:38:26 +00:00
|
|
|
SharedData * data = GET_INSTANCE(SharedData);
|
|
|
|
|
2018-06-15 17:14:30 +00:00
|
|
|
AGENT_OPTIONS
|
|
|
|
|
2018-06-10 23:58:52 +00:00
|
|
|
printf("Je suis l'ascenseur %s\n", this->name);
|
2018-06-18 21:03:32 +00:00
|
|
|
for (;;){
|
2018-06-15 13:38:26 +00:00
|
|
|
data->main_building->signal_elevator_at_floor(data->main_building, this->get_floor(this));
|
2018-06-18 21:03:32 +00:00
|
|
|
this->set_floor(this, this->get_next_floor(this));
|
|
|
|
}
|
|
|
|
|
2018-06-10 23:58:52 +00:00
|
|
|
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-07 20:04:30 +00:00
|
|
|
new_elevator->name = strdup(name);
|
2018-06-18 13:51:48 +00:00
|
|
|
new_elevator->passengers = NEW(List);
|
2018-06-18 17:24:35 +00:00
|
|
|
pthread_mutex_init(&new_elevator->mutex_passengers, NULL);
|
2018-06-07 22:08:18 +00:00
|
|
|
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-18 17:24:35 +00:00
|
|
|
get_next_floor,
|
2018-06-15 13:38:26 +00:00
|
|
|
can_get_inside,
|
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
|
|
|
|
2018-06-15 17:14:30 +00:00
|
|
|
new_elevator->set_floor(new_elevator, 0);
|
|
|
|
new_elevator->set_state(new_elevator, WAITING);
|
|
|
|
|
2018-06-06 01:00:35 +00:00
|
|
|
return new_elevator;
|
2018-06-18 13:20:08 +00:00
|
|
|
}
|