202 lines
3.4 KiB
C
Executable File
202 lines
3.4 KiB
C
Executable File
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <pthread.h>
|
|
#include <time.h>
|
|
|
|
#include "localsys.h"
|
|
#include "mastermod.h"
|
|
#include "elev.h"
|
|
#include "globals.h"
|
|
#include "netmod.h"
|
|
|
|
|
|
role_t role = MASTER;
|
|
int master_sock;
|
|
int master_cand_sock = -1;
|
|
status_t *lhead = NULL;
|
|
status_t *llast = NULL;
|
|
|
|
|
|
void print_status(){
|
|
status_t *it = lhead;
|
|
printf("Online Elevators: ");
|
|
while(it != NULL){
|
|
printf("socket %d, role %d | ",it->sockid, it->role);
|
|
it = it->next;
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
void add_to_list(int sock){
|
|
status_t *new_elev;
|
|
new_elev = malloc(sizeof(status_t));
|
|
new_elev->sockid = sock;
|
|
new_elev->next = NULL;
|
|
|
|
if (lhead == NULL){
|
|
lhead = new_elev;
|
|
}
|
|
else {
|
|
llast->next = new_elev;
|
|
}
|
|
llast = new_elev;
|
|
print_status();
|
|
}
|
|
|
|
int get_master_cand_sock() {
|
|
return master_cand_sock;
|
|
}
|
|
|
|
void set_alive(int sock, int b) {
|
|
status_t *it = lhead;
|
|
while(it != NULL){
|
|
if (it->sockid == sock) {
|
|
it->alive = b;
|
|
return;
|
|
}
|
|
it = it->next;
|
|
}
|
|
}
|
|
|
|
role_t get_role() {
|
|
return role;
|
|
}
|
|
void set_role(role_t r) {
|
|
role = r;
|
|
}
|
|
|
|
int get_master_sock() {
|
|
return master_sock;
|
|
}
|
|
|
|
void set_master_flag() {
|
|
lhead->sockid = -1;
|
|
}
|
|
|
|
void set_master_sock(int sock) {
|
|
master_sock = sock;
|
|
}
|
|
|
|
void print_elevs() {
|
|
status_t *it = lhead;
|
|
printf("Elevator list: ");
|
|
while(it != NULL){
|
|
printf("Socket %d, alive %d\n",it->sockid, it->alive);
|
|
it = it->next;
|
|
}
|
|
}
|
|
|
|
void add_elev(int sock){
|
|
status_t *new_elev;
|
|
new_elev = malloc(sizeof(status_t));
|
|
new_elev->sockid = sock;
|
|
new_elev->floor = 0;
|
|
new_elev->end_dest = 0;
|
|
new_elev->state = STOPPED;
|
|
new_elev->alive = 1;
|
|
new_elev->next = NULL;
|
|
|
|
if (lhead == NULL){
|
|
lhead = new_elev;
|
|
new_elev->role = MASTER;
|
|
}
|
|
else {
|
|
llast->next = new_elev;
|
|
new_elev->role = SLAVE;
|
|
}
|
|
llast = new_elev;
|
|
print_elevs();
|
|
}
|
|
|
|
void master_init(int s) {
|
|
add_elev(s);
|
|
lhead->role = MASTER;
|
|
lhead->alive = 1;
|
|
}
|
|
|
|
void add_slave(int sock) {
|
|
add_to_list(sock);
|
|
|
|
llast->floor = 0;
|
|
llast->state = STOPPED;
|
|
llast->alive = 1;
|
|
llast->end_dest = 0;
|
|
|
|
if (master_cand_sock == -1) {
|
|
master_cand_sock = sock;
|
|
llast->role = MASTER_CAND;
|
|
}
|
|
else llast->role = SLAVE;
|
|
}
|
|
|
|
void set_elev_state(int sock, char str[]) {
|
|
status_t *it = lhead;
|
|
while(it != NULL){
|
|
if (it->sockid == sock) {
|
|
it->state = (int) str[1];
|
|
it->floor = (int) str[2];
|
|
it->end_dest = (int) str[3];
|
|
return;
|
|
}
|
|
|
|
|
|
it = it->next;
|
|
}
|
|
}
|
|
|
|
int find_opt_elev(int call, int floor) {
|
|
int best = abs(floor - lhead->floor);
|
|
status_t *res = lhead;
|
|
status_t *it = lhead->next;
|
|
int dir = lhead->end_dest-lhead->floor;
|
|
|
|
int passing = 0;
|
|
int idle = 0;
|
|
|
|
while (it != NULL) {
|
|
if (((!it->alive) || (it->end_dest == -1))) {
|
|
it = it->next;
|
|
continue;
|
|
}
|
|
dir = it->end_dest-it->floor;
|
|
|
|
if (((dir > 0) && (floor > it->floor) && (floor <= it->end_dest) && (call != ELEV_DIR_DOWN)) ||
|
|
((dir < 0) && (floor < it->floor) && (floor >= it->end_dest) && (call != ELEV_DIR_UP))) {
|
|
if (abs(floor - it->floor) < best) {
|
|
res = it;
|
|
best = abs(floor - it->floor);
|
|
passing = 1;
|
|
}
|
|
}
|
|
it = it->next;
|
|
}
|
|
|
|
if (!passing) {
|
|
it = lhead;
|
|
best = N_FLOORS+1;
|
|
while (it != NULL) {
|
|
if (!it->alive) {
|
|
it = it->next;
|
|
continue;
|
|
}
|
|
if ((it->state == IDLE) && (abs(floor - it->floor) < best)) {
|
|
best = abs(floor - it->floor);
|
|
res = it;
|
|
idle = 1;
|
|
}
|
|
else if ((!idle) && (abs(floor - it->floor) < best)){
|
|
best = abs(floor - it->floor);
|
|
res = it;
|
|
}
|
|
it = it->next;
|
|
}
|
|
|
|
}
|
|
return res->sockid;
|
|
}
|
|
|
|
|
|
|