mirror of
https://gitlab.com/klmp200/LO41.git
synced 2025-07-12 04:39:23 +00:00
Added useful functions for elevator IA
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
// Created by Antoine Bartuccio on 05/06/2018.
|
||||
//
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include "Building.h"
|
||||
#include "../Resident/Resident.h"
|
||||
#include "../Visitor/Visitor.h"
|
||||
@ -16,9 +17,28 @@ void remove_end_char(char * string, char character){
|
||||
string[string_size - 1] = '\0';
|
||||
}
|
||||
|
||||
int get_target_Building(THIS(Building)){
|
||||
/**
|
||||
* Get the best call for the elevator
|
||||
*@param THIS(Building) : this
|
||||
*@param elevator_floor : actual floor of the requesting elevator
|
||||
*@returns the closest floor where a client is calling as an int. If no client is calling, returns -1.
|
||||
*/
|
||||
int get_next_call_Building(THIS(Building), int elevator_floor){
|
||||
pthread_mutex_lock(this->mutex_func_get_next_call);
|
||||
int* waiting_floors = this->get_waiting_floors(this);
|
||||
return waiting_floors[1];
|
||||
int i;
|
||||
float best_diff = INFINITY;
|
||||
int next_target = -1;
|
||||
for(i=0; i<FLOORS; i++){
|
||||
if(waiting_floors[i] > 0){
|
||||
if(abs(elevator_floor - waiting_floors[i]) < best_diff){
|
||||
best_diff = abs(elevator_floor - waiting_floors[i]);
|
||||
next_target = waiting_floors[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(this->mutex_func_get_next_call);
|
||||
return next_target;
|
||||
}
|
||||
|
||||
List * split(char * line, char * separator){
|
||||
@ -125,6 +145,9 @@ void _free__Building(THIS(Building)){
|
||||
pthread_mutex_unlock(this->mutex_func_get_inside_elevator);
|
||||
pthread_mutex_destroy(this->mutex_func_get_inside_elevator);
|
||||
|
||||
pthread_mutex_unlock(this->mutex_func_get_next_call);
|
||||
pthread_mutex_destroy(this->mutex_func_get_next_call);
|
||||
|
||||
pthread_mutex_unlock(&this->mutex_waiting_floors);
|
||||
pthread_mutex_destroy(&this->mutex_waiting_floors);
|
||||
|
||||
@ -140,7 +163,7 @@ void _free__Building(THIS(Building)){
|
||||
free(this->mutex_cond_get_inside_elevator);
|
||||
free(this->mutex_cond_get_outside_elevator);
|
||||
free(this->mutex_func_get_inside_elevator);
|
||||
//free(&this->mutex_waiting_floors);
|
||||
free(this->mutex_func_get_next_call);
|
||||
free(this->elevators);
|
||||
|
||||
free(this);
|
||||
@ -237,10 +260,12 @@ Building *_init_Building(char * residents_file, char * visitors_file){
|
||||
new_building->mutex_cond_get_inside_elevator = malloc_or_die(sizeof(pthread_mutex_t));
|
||||
new_building->mutex_cond_get_outside_elevator = malloc_or_die(sizeof(pthread_mutex_t));
|
||||
new_building->mutex_func_get_inside_elevator = malloc_or_die(sizeof(pthread_mutex_t));
|
||||
new_building->mutex_func_get_next_call = malloc_or_die(sizeof(pthread_mutex_t));
|
||||
|
||||
pthread_mutex_init(new_building->mutex_cond_get_inside_elevator, NULL);
|
||||
pthread_mutex_init(new_building->mutex_cond_get_outside_elevator, NULL);
|
||||
pthread_mutex_init(new_building->mutex_func_get_inside_elevator, NULL);
|
||||
pthread_mutex_init(new_building->mutex_func_get_next_call, NULL);
|
||||
|
||||
new_building->condition_floors = malloc_or_die(sizeof(pthread_cond_t*) * FLOORS);
|
||||
new_building->residents = NEW(List);
|
||||
@ -262,7 +287,7 @@ Building *_init_Building(char * residents_file, char * visitors_file){
|
||||
get_inside_elevator,
|
||||
use_call_box,
|
||||
go_to_floor,
|
||||
get_target,
|
||||
get_next_call,
|
||||
get_waiting_floors,
|
||||
signal_elevator_at_floor
|
||||
)
|
||||
|
@ -25,6 +25,7 @@ typedef struct o_Building {
|
||||
PRIVATE pthread_mutex_t * mutex_cond_get_inside_elevator;
|
||||
PRIVATE pthread_mutex_t * mutex_cond_get_outside_elevator;
|
||||
PRIVATE pthread_mutex_t * mutex_func_get_inside_elevator;
|
||||
PRIVATE pthread_mutex_t * mutex_func_get_next_call;
|
||||
PRIVATE pthread_cond_t ** condition_floors;
|
||||
|
||||
PRIVATE void (*parse_residents)(_THIS(Building), char * file);
|
||||
@ -33,7 +34,7 @@ typedef struct o_Building {
|
||||
|
||||
PUBLIC int (*use_call_box)(_THIS(Building), char * resident_name);
|
||||
PUBLIC void (*signal_elevator_at_floor)(_THIS(Building), int floor);
|
||||
PUBLIC int (*get_target)(_THIS(Building));
|
||||
PUBLIC int (*get_next_call)(_THIS(Building), int elevator_floor);
|
||||
PUBLIC int* (*get_waiting_floors)(_THIS(Building));
|
||||
|
||||
SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger * passenger);
|
||||
@ -44,7 +45,6 @@ typedef struct o_Building {
|
||||
FRIENDLY(residents, SharedData)
|
||||
FRIENDLY(visitors, SharedData)
|
||||
FRIENDLY(elevators, SharedData)
|
||||
FRIENDLY(waiting_passengers, SharedData)
|
||||
|
||||
Building *_init_Building(char * residents_file, char * visitors_file);
|
||||
|
||||
|
Reference in New Issue
Block a user