-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.h
152 lines (130 loc) · 3.49 KB
/
functions.h
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
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/select.h>
#include <string.h>
#include <math.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
const int BufferSize = 50;
double ComputeToken(double receivedToken, float DT, double RF)
{
if(receivedToken >= 1)
receivedToken = -1;
double newToken;
newToken = receivedToken + DT * (1 - (receivedToken * receivedToken) / 2 )* (2 * M_PI * RF);
printf("New Token:%f\n", newToken);
return newToken;
}
// Returns the current time (mu_sec)
long getTime(){
struct timeval currentTime;
gettimeofday(¤tTime, NULL);
return currentTime.tv_sec * (int)1e6 + currentTime.tv_usec;
}
// Saves the current process PID to an external file
int save_pid_to_file_fnct(const char * pid_fileName, pid_t pid){
FILE *pid_file = fopen(pid_fileName, "w");
if (pid_file == NULL)
{
perror("File opening");
exit(-1);
}
if (fprintf(pid_file, "%i", pid) == -1)
{
perror("File writing");
exit(-1);
}
fclose(pid_file);
}
// Reads the necessary processes IDs from an external file
int read_pid_from_file_fnct(const char * pid_fileName){
char Mystr[5];
int pid_;
FILE *pid_f = fopen(pid_fileName, "r");
if (pid_f == NULL)
{
perror("file opening");
exit(-1);
}
while (fgets(Mystr, 10, pid_f) == NULL)
{
}
pid_ = atoi(Mystr);
return pid_;
}
// Dumps log file
int dump_log_fnct(char* logfile, FILE * logname, int dump_size, char buffer_LP[50]){
logname = fopen(logfile, "r");
if (logname == NULL)
{
printf("There is not Log File yet\n");
}
else
{
fseek(logname, 0, SEEK_END);
int size = ftell(logname);
fseek(logname, size - dump_size, SEEK_SET);
if (size >= dump_size)
{
char *buffer_LP = malloc(dump_size + 1);
// Read the file and save
if (fread(buffer_LP, 1, dump_size, logname) != dump_size)
{
perror("Reading the file");
exit(-1);
}
fclose(logname);
// Write log on stdout
if (write(STDOUT_FILENO, buffer_LP, dump_size) == -1)
{
perror("write log on stdout");
exit(-1);
}
fflush(stdout);
return 1;
}
else
{
printf("The log file is empty.\n");
return 0;
}
}
}
// Initializes a socket connection
int create_socket_fnct(int sockfd, int W, struct sockaddr_in serv_addr, struct sockaddr_in cli_addr, int clilen, int portno, char server_buf[256]){
int newsockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("ERROR opening socket");
exit(-1);
}
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &W, sizeof(W));
fflush(stdout);
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *)&serv_addr,
sizeof(serv_addr)) < 0)
{
perror("ERROR on binding");
exit(-1);
}
listen(sockfd, 5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
if (newsockfd < 0)
{
perror("ERROR on accept");
exit(-1);
}
bzero(server_buf, 256);
return newsockfd;
}