1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2024-11-24 18:14:19 +00:00

Added experimental "get_next_floor_Elevator" function

This commit is contained in:
Aethor 2018-06-18 19:24:35 +02:00
parent ef4e29f7c2
commit c4048afe3b
2 changed files with 46 additions and 10 deletions

View File

@ -3,6 +3,7 @@
// //
#include <string.h> #include <string.h>
#include <math.h>
#include "Elevator.h" #include "Elevator.h"
#include "../SharedData/SharedData.h" #include "../SharedData/SharedData.h"
@ -16,8 +17,8 @@ void _free__Elevator(THIS(Elevator)){
if (this->name != NULL) if (this->name != NULL)
free(this->name); free(this->name);
pthread_mutex_unlock(&this->mutex_passenger); pthread_mutex_unlock(&this->mutex_passengers);
pthread_mutex_destroy(&this->mutex_passenger); pthread_mutex_destroy(&this->mutex_passengers);
pthread_mutex_unlock(&this->mutex_state); pthread_mutex_unlock(&this->mutex_state);
pthread_mutex_destroy(&this->mutex_state); pthread_mutex_destroy(&this->mutex_state);
@ -29,27 +30,60 @@ void _free__Elevator(THIS(Elevator)){
} }
void add_passenger_Elevator(THIS(Elevator), Passenger passenger){ void add_passenger_Elevator(THIS(Elevator), Passenger passenger){
pthread_mutex_lock(&this->mutex_passenger); pthread_mutex_lock(&this->mutex_passengers);
this->passengers->insert_tail(this->passengers, ((void *)&passenger), sizeof(Passenger)); this->passengers->insert_tail(this->passengers, ((void *)&passenger), sizeof(Passenger));
printf("L'ascenseur %s recoit le visiteur %s\nIl y a maintenant %d passagers dans l'ascenseur %s\n", this->name, 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), passenger.type == VISITOR ? passenger.visitor->get_name(passenger.visitor) : passenger.resident->get_name(passenger.resident),
this->passengers->get_size(this->passengers), this->name); this->passengers->get_size(this->passengers), this->name);
if (this->passengers->get_size(this->passengers) >= 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_passengers);
} }
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_passengers);
num = this->passengers->get_size(this->passengers); num = this->passengers->get_size(this->passengers);
pthread_mutex_unlock(&this->mutex_passenger); pthread_mutex_unlock(&this->mutex_passengers);
return num; return num;
} }
/**
* 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;
}
int can_get_inside_Elevator(THIS(Elevator), int floor){ int can_get_inside_Elevator(THIS(Elevator), int floor){
int permission; int permission;
pthread_mutex_lock(&this->mutex_passenger); pthread_mutex_lock(&this->mutex_passengers);
pthread_mutex_lock(&this->mutex_state); pthread_mutex_lock(&this->mutex_state);
pthread_mutex_lock(&this->mutex_floor); pthread_mutex_lock(&this->mutex_floor);
@ -58,7 +92,7 @@ int can_get_inside_Elevator(THIS(Elevator), int floor){
pthread_mutex_unlock(&this->mutex_floor); pthread_mutex_unlock(&this->mutex_floor);
pthread_mutex_unlock(&this->mutex_state); pthread_mutex_unlock(&this->mutex_state);
pthread_mutex_unlock(&this->mutex_passenger); pthread_mutex_unlock(&this->mutex_passengers);
return permission; return permission;
} }
@ -84,13 +118,14 @@ 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->passengers = NEW(List); new_elevator->passengers = NEW(List);
pthread_mutex_init(&new_elevator->mutex_passenger, NULL); pthread_mutex_init(&new_elevator->mutex_passengers, 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);
LINK_ALL(Elevator, new_elevator, LINK_ALL(Elevator, new_elevator,
runnable, runnable,
get_number_of_passengers, get_number_of_passengers,
get_next_floor,
can_get_inside, can_get_inside,
add_passenger, add_passenger,
get_state, get_state,

View File

@ -20,7 +20,7 @@ typedef struct o_Elevator {
PRIVATE List * passengers; 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_passengers;
PRIVATE pthread_mutex_t mutex_state; PRIVATE pthread_mutex_t mutex_state;
PRIVATE pthread_mutex_t mutex_floor; PRIVATE pthread_mutex_t mutex_floor;
@ -28,6 +28,7 @@ typedef struct o_Elevator {
SYNCHRONIZE PRIVATE void (*set_state)(_THIS(Elevator), ELEVATOR_STATE var); SYNCHRONIZE PRIVATE void (*set_state)(_THIS(Elevator), ELEVATOR_STATE var);
SYNCHRONIZE PRIVATE void (*set_floor)(_THIS(Elevator), int var); SYNCHRONIZE PRIVATE void (*set_floor)(_THIS(Elevator), int var);
SYNCHRONIZE PRIVATE int (*get_next_floor)(_THIS(Elevator));
SYNCHRONIZE PUBLIC void (*repair)(_THIS(Elevator)); SYNCHRONIZE PUBLIC void (*repair)(_THIS(Elevator));
SYNCHRONIZE PUBLIC int (*get_number_of_passengers)(_THIS(Elevator)); SYNCHRONIZE PUBLIC int (*get_number_of_passengers)(_THIS(Elevator));