-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdef.h
More file actions
151 lines (130 loc) · 4.72 KB
/
def.h
File metadata and controls
151 lines (130 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// 2019 ARP assignment V2.0
// Andrea Pitto - S3942710
// 10 - 07 - 2020
// This header stores all the definitions and settings needed by the processes
#ifndef DEF_H
#define DEF_H
// Inclusion of POSIX libraries
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <netdb.h>
#include <signal.h>
#include <fcntl.h>
#include <math.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#define LOCAL_IP "localhost" // localhost name (equivalent to 127.0.0.1 and to machine's own name, i.e. Linux's "hostname")
#define LOCAL_PORT 5000 // chosen local port for the communication
#ifndef M_PI
#define M_PI 3.14159265358979323846 // pi value definition
#endif
#define MAX_REQS 5 // maximum number of allowed requests on sockets
struct log_message
{
struct timeval timestamp; // timestamp of the message
int status; // status = 0: pause; status = 1: computing; status = 3: open log file;
// status = 8 new token data value from G; status = 9 new token data value from P
float value; // if status == 8 || satus == 9 this stores token.value, irrelevant otherwise
};
typedef struct token_struct
{
struct timeval timestamp; // timestamp of the token
float value; // actual token value
} token; // struct alias
struct configuration
{
int run_mode; // set to 0 to go in Debug mode (= Single-machine mode), to 1 for Multi-machine mode [default: 0]
int chain_starter; // in Multi-machine mode, set to 1 to flag this machine as the one starting the P-G communication.
// Only a single PC in the chain should have this set to 1 [default: 0]
double rf; // sine wave frequency [default: 1.0]
int waiting_time_microsecs; // waiting time, in microseconds, applied by process P before sending the updated token [default: 1000]
char *next_ip; // IP address of the next machine in the chain ("hostname -I" to get your current IP) [default: 192.168.1.12]
int next_port; // chosen remote port for the communication [default: 5000]
char *fifo; // name of the fifo (i.e. named pipe) [default: npipe]
};
void error(const char *msg) // display a message about the error on stderr and then abort the program
{
perror(msg);
exit(0);
}
// Load the values inside the config file and store them into constants
void configLoader(char *path, struct configuration *conf)
{
FILE *config_file = fopen(path, "r"); // open the config file in read mode
int line_out;
char *line = NULL;
size_t len = 0;
if (config_file == NULL)
{
perror("Could not open config file");
}
// Read 1st line of the config file (run_mode)
if ((line_out = getline(&line, &len, config_file)) != -1)
{
conf->run_mode = atoi(line);
}
else
perror("Error reading 1st line of config file");
// Read 2nd line of the config file (chain_starter)
if ((line_out = getline(&line, &len, config_file)) != -1)
{
conf->chain_starter = atoi(line);
}
else
perror("Error reading 2nd line of config file");
// Read 3rd line of the config file (rf)
if ((line_out = getline(&line, &len, config_file)) != -1)
{
conf->rf = atof(line);
}
else
perror("Error reading 3rd line of config file");
// Read 4th line of the config file (waiting_time_microsecs)
if ((line_out = getline(&line, &len, config_file)) != -1)
{
conf->waiting_time_microsecs = atoi(line);
}
else
perror("Error reading 4th line of config file");
// Read 5th line of the config file (next_ip)
if ((line_out = getline(&line, &len, config_file)) != -1)
{
if (line_out > 0 && line[line_out - 1] == '\n')
{
line[line_out - 1] = '\0';
}
conf->next_ip = strdup(line);
}
else
perror("Error reading 5th line of config file");
// Read 6th line of the config file (next_port)
if ((line_out = getline(&line, &len, config_file)) != -1)
{
conf->next_port = atoi(line);
}
else
perror("Error reading 6th line of config file");
// Read 7th line of the config file (fifo)
if ((line_out = getline(&line, &len, config_file)) != -1)
{
if (line_out > 0 && line[line_out - 1] == '\n')
{
line[line_out - 1] = '\0';
}
conf->fifo = strdup(line);
}
else
perror("Error reading 7th line of config file");
// Close the config file
free(line);
fclose(config_file);
}
#endif