mirror of
https://gitlab.com/klmp200/LO41.git
synced 2024-12-21 23:31:12 +00:00
Le réparateur, j'ai pas testé lol
This commit is contained in:
parent
6b7bb7e119
commit
3334f77d22
@ -3,6 +3,7 @@
|
||||
//
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <unistd.h>
|
||||
#include "Building.h"
|
||||
#include "../Resident/Resident.h"
|
||||
#include "../Visitor/Visitor.h"
|
||||
@ -150,6 +151,9 @@ void _free__Building(THIS(Building)){
|
||||
pthread_mutex_unlock(&this->mutex_waiting_floors);
|
||||
pthread_mutex_destroy(&this->mutex_waiting_floors);
|
||||
|
||||
pthread_mutex_unlock(&this->mutex_repair);
|
||||
pthread_mutex_destroy(&this->mutex_repair);
|
||||
|
||||
DELETE(this->residents);
|
||||
DELETE(this->visitors);
|
||||
for (i=0; i<ELEVATOR_NB; i++)
|
||||
@ -236,6 +240,15 @@ void signal_elevator_at_floor_Building(THIS(Building), int floor){
|
||||
pthread_cond_signal(this->condition_floors[floor]);
|
||||
}
|
||||
|
||||
void ask_elevator_reparation_Building(THIS(Building), Elevator* elevator){
|
||||
printf("Technicien : l'ascenseur %s attend pour se faire réparer\n", elevator->name);
|
||||
pthread_mutex_lock(&this->mutex_repair);
|
||||
usleep(10000);
|
||||
elevator->repair(elevator);
|
||||
pthread_mutex_unlock(&this->mutex_repair);
|
||||
printf("Technicien : l'ascenseur %s est maintenant réparé\n", elevator->name);
|
||||
}
|
||||
|
||||
Building *_init_Building(char * residents_file, char * visitors_file){
|
||||
Building * new_building = malloc_or_die(sizeof(Building));
|
||||
char elevator_name[] = "@";
|
||||
@ -245,6 +258,7 @@ Building *_init_Building(char * residents_file, char * visitors_file){
|
||||
for (i=0; i<FLOORS; i++)
|
||||
new_building->waiting_floors[i] = 0;
|
||||
pthread_mutex_init(&new_building->mutex_waiting_floors, NULL);
|
||||
pthread_mutex_init(&new_building->mutex_repair, NULL);
|
||||
|
||||
new_building->elevators = malloc_or_die(sizeof(Elevator*) * ELEVATOR_NB);
|
||||
|
||||
@ -273,14 +287,15 @@ Building *_init_Building(char * residents_file, char * visitors_file){
|
||||
|
||||
|
||||
LINK_ALL(Building, new_building,
|
||||
parse_residents,
|
||||
parse_visitors,
|
||||
get_inside_elevator,
|
||||
use_call_box,
|
||||
go_to_floor,
|
||||
get_next_call,
|
||||
get_waiting_floors,
|
||||
signal_elevator_at_floor
|
||||
parse_residents,
|
||||
parse_visitors,
|
||||
get_inside_elevator,
|
||||
use_call_box,
|
||||
go_to_floor,
|
||||
get_next_call,
|
||||
get_waiting_floors,
|
||||
signal_elevator_at_floor,
|
||||
ask_elevator_reparation
|
||||
)
|
||||
|
||||
if (residents_file != NULL)
|
||||
|
@ -22,6 +22,7 @@ typedef struct o_Building {
|
||||
PRIVATE List * visitors;
|
||||
PRIVATE Elevator ** elevators;
|
||||
PRIVATE pthread_mutex_t mutex_waiting_floors;
|
||||
PRIVATE pthread_mutex_t mutex_repair;
|
||||
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;
|
||||
@ -37,6 +38,8 @@ typedef struct o_Building {
|
||||
PUBLIC int (*get_next_call)(_THIS(Building), int elevator_floor);
|
||||
PUBLIC int* (*get_waiting_floors)(_THIS(Building));
|
||||
|
||||
SYNCHRONIZE PUBLIC void (*ask_elevator_reparation)(_THIS(Building), Elevator* elevator);
|
||||
|
||||
SYNCHRONIZE PUBLIC void (*go_to_floor)(_THIS(Building), int origin, int destination, Passenger * passenger);
|
||||
|
||||
DESTRUCTOR(Building);
|
||||
|
@ -8,4 +8,4 @@ if(CMAKE_COMPILER_IS_GNUCC)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
|
||||
endif()
|
||||
|
||||
add_executable(LO41 main.c List/List.h List/List.c Objects.h List/Element.c List/Element.h Objects.c Building/Building.c Building/Building.h Elevator/Elevator.c Elevator/Elevator.h SharedData/SharedData.c SharedData/SharedData.h Visitor/Visitor.c Visitor/Visitor.h Resident/Resident.c Resident/Resident.h Passenger/Passenger.h Passenger/Passenger.c)
|
||||
add_executable(LO41 main.c List/List.h List/List.c Objects.h List/Element.c List/Element.h Objects.c Building/Building.c Building/Building.h Elevator/Elevator.c Elevator/Elevator.h SharedData/SharedData.c SharedData/SharedData.h Visitor/Visitor.c Visitor/Visitor.h Resident/Resident.c Resident/Resident.h Passenger/Passenger.h Passenger/Passenger.c ElevatorBreaker/ElevatorBreaker.c ElevatorBreaker/ElevatorBreaker.h)
|
@ -121,6 +121,10 @@ void repair_Elevator(THIS(Elevator)){
|
||||
this->set_state(this, RUNNING);
|
||||
}
|
||||
|
||||
void damage_Elevator(THIS(Elevator)){
|
||||
this->set_state(this, BROKEN);
|
||||
}
|
||||
|
||||
void *runnable_Elevator(void * void_this){
|
||||
/* This is where the thread logic will be implemented */
|
||||
Elevator * this = (Elevator*) void_this;
|
||||
@ -196,6 +200,7 @@ Elevator *_init_Elevator(char * name){
|
||||
get_floor,
|
||||
set_floor,
|
||||
repair,
|
||||
damage,
|
||||
set_thread_number
|
||||
);
|
||||
|
||||
|
@ -34,6 +34,7 @@ typedef struct o_Elevator {
|
||||
SYNCHRONIZE PRIVATE int (*get_next_passenger_stop)(_THIS(Elevator));
|
||||
|
||||
SYNCHRONIZE PUBLIC void (*repair)(_THIS(Elevator));
|
||||
SYNCHRONIZE PUBLIC void (*damage)(_THIS(Elevator));
|
||||
SYNCHRONIZE PUBLIC int (*get_number_of_passengers)(_THIS(Elevator));
|
||||
SYNCHRONIZE PUBLIC void (*remove_passenger) (_THIS(Elevator), Passenger * passenger);
|
||||
SYNCHRONIZE PUBLIC void (*add_passenger)(_THIS(Elevator), Passenger * passenger);
|
||||
|
47
ElevatorBreaker/ElevatorBreaker.c
Normal file
47
ElevatorBreaker/ElevatorBreaker.c
Normal file
@ -0,0 +1,47 @@
|
||||
//
|
||||
// Created by Antoine Bartuccio on 22/06/2018.
|
||||
//
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "ElevatorBreaker.h"
|
||||
#include "../SharedData/SharedData.h"
|
||||
|
||||
SETTER(ElevatorBreaker, int, thread_number);
|
||||
|
||||
void _free__ElevatorBreaker(THIS(ElevatorBreaker)){
|
||||
free(this);
|
||||
}
|
||||
|
||||
void * runnable_ElevatorBreaker(void * void_this){
|
||||
ElevatorBreaker *this = (ElevatorBreaker*) void_this;
|
||||
Elevator *selected_elevator = NULL;
|
||||
SharedData *data = GET_INSTANCE(SharedData);
|
||||
|
||||
AGENT_OPTIONS
|
||||
|
||||
while (data->is_active_passengers_left(data)){
|
||||
usleep(900000);
|
||||
// One chance out of two to break something
|
||||
if ((rand() % 101) > 50) {
|
||||
selected_elevator = data->get_main_building(data)->elevators[rand() % ELEVATOR_NB];
|
||||
selected_elevator->damage(selected_elevator);
|
||||
printf("ALERTE : L'ascenseur %s est endommagé\n", selected_elevator->name);
|
||||
}
|
||||
}
|
||||
|
||||
data->unregister_thread(data, this->thread_number);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ElevatorBreaker *_init_ElevatorBreaker(){
|
||||
ElevatorBreaker *new_elevator_breaker = malloc_or_die(sizeof(ElevatorBreaker));
|
||||
new_elevator_breaker->thread_number = -1;
|
||||
|
||||
LINK_ALL(ElevatorBreaker, new_elevator_breaker,
|
||||
set_thread_number,
|
||||
runnable
|
||||
)
|
||||
|
||||
return new_elevator_breaker;
|
||||
}
|
22
ElevatorBreaker/ElevatorBreaker.h
Normal file
22
ElevatorBreaker/ElevatorBreaker.h
Normal file
@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by Antoine Bartuccio on 22/06/2018.
|
||||
//
|
||||
|
||||
#ifndef LO41_ELEVATORBREAKER_H
|
||||
#define LO41_ELEVATORBREAKER_H
|
||||
|
||||
#include "../Objects.h"
|
||||
|
||||
typedef struct o_ElevatorBreaker {
|
||||
PRIVATE int thread_number;
|
||||
|
||||
PUBLIC void * (*runnable)(void * void_this);
|
||||
PUBLIC void (*set_thread_number)(_THIS(ElevatorBreaker), int data);
|
||||
|
||||
DESTRUCTOR(ElevatorBreaker);
|
||||
|
||||
} ElevatorBreaker;
|
||||
|
||||
ElevatorBreaker *_init_ElevatorBreaker();
|
||||
|
||||
#endif //LO41_ELEVATORBREAKER_H
|
@ -46,8 +46,13 @@ void start_all_threads_SharedData(THIS(SharedData)){
|
||||
|
||||
pthread_mutex_lock(&this->mutex_active_passengers);
|
||||
pthread_mutex_lock(&this->mutex_threads);
|
||||
|
||||
/* starting the elevator breaker */
|
||||
this->start_thread(this, this->elevator_breaker->runnable, this->elevator_breaker, 0);
|
||||
this->elevator_breaker->set_thread_number(this->elevator_breaker, 0);
|
||||
|
||||
/* starts threading elevators */
|
||||
for (i=0; i<ELEVATOR_NB; i++) {
|
||||
for (i=1; i<ELEVATOR_NB; i++) {
|
||||
this->start_thread(this, this->main_building->elevators[i]->runnable,
|
||||
this->main_building->elevators[i], i);
|
||||
this->main_building->elevators[i]->set_thread_number(this->main_building->elevators[i], i);
|
||||
@ -105,6 +110,8 @@ void _free__SharedData(THIS(SharedData)){
|
||||
if (this->main_building != NULL)
|
||||
DELETE(this->main_building);
|
||||
|
||||
DELETE(this->elevator_breaker);
|
||||
|
||||
pthread_mutex_unlock(&this->mutex_active_passengers);
|
||||
pthread_mutex_destroy(&this->mutex_active_passengers);
|
||||
pthread_mutex_unlock(&this->mutex_threads);
|
||||
@ -143,6 +150,7 @@ SharedData *_get_instance_SharedData(){
|
||||
new_shared_data->main_building = NULL; /* Should be set to NULL if freed before */
|
||||
new_shared_data->threads_nb = 0;
|
||||
new_shared_data->active_passengers = 0;
|
||||
new_shared_data->elevator_breaker = NEW(ElevatorBreaker);
|
||||
|
||||
pthread_mutex_init(&new_shared_data->mutex_active_passengers, NULL);
|
||||
pthread_mutex_init(&new_shared_data->mutex_threads, NULL);
|
||||
|
@ -8,10 +8,12 @@
|
||||
#include <pthread.h>
|
||||
#include "../Objects.h"
|
||||
#include "../Building/Building.h"
|
||||
#include "../ElevatorBreaker/ElevatorBreaker.h"
|
||||
|
||||
typedef struct o_SharedData {
|
||||
PRIVATE int threads_nb;
|
||||
PRIVATE int active_passengers;
|
||||
PRIVATE ElevatorBreaker * elevator_breaker;
|
||||
PRIVATE pthread_t ** threads;
|
||||
PRIVATE Building * main_building;
|
||||
PRIVATE pthread_mutex_t mutex_active_passengers;
|
||||
|
2
main.c
2
main.c
@ -1,5 +1,6 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "Objects.h"
|
||||
#include "List/List.h"
|
||||
@ -17,6 +18,7 @@ void clean_exit(int error_code){
|
||||
int main(int argc, char* argv[]) {
|
||||
SharedData * shared_data = GET_INSTANCE(SharedData);
|
||||
|
||||
srand((unsigned int) time(NULL));
|
||||
signal(SIGINT, clean_exit);
|
||||
if(argc == 3){
|
||||
shared_data->set_main_building(shared_data, NEW(Building, argv[1], argv[2]));
|
||||
|
Loading…
Reference in New Issue
Block a user