-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
190 lines (165 loc) · 5.41 KB
/
main.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* main.c
*
* Copyright 2017 christian <christian@coolermaster>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <sys/un.h>
#include <unistd.h>
#include "circularBuffer.h"
#include "historic.h"
#define SEPARATOR_JSON_FIELD ",\n"
typedef enum {
QUEUE_MINUTE,
QUEUE_HOUR,
QUEUE_DAYS,
QUEUE_NBELEMENTS
} tEQueues;
int initSocket();
void treatSocket(int sockfd, tHistoricItem historics[], int nbHistorics);
void writeAllHistoricToSocket(int newsockfd, tHistoricItem historics[], int nbHistorics);
int main(int argc, char *argv[]) {
tData dataMinute[32];
tData dataHour[128];
tData dataDays[9192];
tHistoricItem historicQueues[QUEUE_NBELEMENTS] = {{.limit = 24},{.limit = 120},{.limit = 9192}};
int sockfd = initSocket(argc >=2 ? argv[1] : NULL);
queue_init(&historicQueues[QUEUE_MINUTE].circularBuffer, dataMinute, sizeof(dataMinute)/sizeof(dataMinute[0]));
queue_init(&historicQueues[QUEUE_HOUR ].circularBuffer, dataHour , sizeof(dataHour) /sizeof(dataHour[0]));
queue_init(&historicQueues[QUEUE_DAYS ].circularBuffer, dataDays , sizeof(dataDays) /sizeof(dataDays[0]));
printf("Enter loop\n");
while(true) {
tData data;
queue_putItem(&historicQueues[QUEUE_MINUTE].circularBuffer, retrieveDynamicData(&data));
//printf("nbElements (MINUTE) = %d\tnbElements (HOUR) = %d\tnbElements (DAYS) = %d\n",
// queue_getNbItems(&historicQueues[QUEUE_MINUTE].circularBuffer),
// queue_getNbItems(&historicQueues[QUEUE_HOUR ].circularBuffer),
// queue_getNbItems(&historicQueues[QUEUE_DAYS ].circularBuffer));
reduceHistoric(historicQueues, QUEUE_NBELEMENTS);
treatSocket(sockfd, historicQueues, QUEUE_NBELEMENTS);
}
return 0;
}
int initSocket(char socketPath[]) {
int sockfd;
/* Initialize socket structure */
struct sockaddr_un serv_addr = {
.sun_family = AF_UNIX,
.sun_path = "socket"
};
if(socketPath != NULL) {
strncpy(serv_addr.sun_path, socketPath, sizeof(serv_addr.sun_path)-1);
}
unlink(serv_addr.sun_path);
/* First call to socket() function */
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
/* Now bind the host address using bind() call.*/
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(1);
}
/* Now start listening for the clients, here process will
* go in sleep mode and will wait for the incoming connection
*/
listen(sockfd,5);
return sockfd;
}
void treatSocket(int sockfd, tHistoricItem historics[], int nbHistorics) {
fd_set rfds;
struct timeval tv = {
.tv_sec = 5,
.tv_usec = 0
};
int retval;
FD_ZERO(&rfds);
FD_SET(sockfd, &rfds);
retval = select(sockfd+1, &rfds, NULL, NULL, &tv);
/* Don’t rely on the value of tv now! */
if (retval == -1)
perror("select()");
else if (retval)
{
//printf("Data is available now.\n");
if(FD_ISSET(sockfd, &rfds))
{
//int n;
//char buffer[256] = {0};
/* Accept actual connection from the client */
int newsockfd = accept(sockfd, NULL, 0);
if (newsockfd < 0) {
perror("ERROR on accept");
exit(1);
}
/* If connection is established then start communicating */
//n = read(newsockfd, buffer, sizeof(buffer)-1);
//if (n < 0) {
// perror("ERROR reading from socket");
// exit(1);
//}
//printf("Here is the message: %s\n",buffer);
/* Write all json to the client */
writeAllHistoricToSocket(newsockfd, historics, nbHistorics);
/* No more information to send, so we can close the client socket*/
close(newsockfd);
}
}
//else
// printf("No data within five seconds.\n");
}
void writeAllHistoricToSocket(int newsockfd, tHistoricItem historics[], int nbHistorics) {
bool firstSeparator = true;
// Write only '['
write(newsockfd,&"[",1);
for(int i = nbHistorics-1; i >= 0; i--) {
int nbElements = queue_getNbItems(&historics[i].circularBuffer);
for(int index = 0; index < nbElements; index++) {
tData itemValue;
char str[240];
char *pStr = str;
if(queue_peekItem(&historics[i].circularBuffer, index, &itemValue)) {
int remainingSize = sizeof(str);
if(tData_json(&itemValue, &pStr, &remainingSize) != NULL) {
if(!firstSeparator) {
write(newsockfd, SEPARATOR_JSON_FIELD, sizeof(SEPARATOR_JSON_FIELD)-1);
}
// Minus one is for '\0' (we mustn't send it!)
int nbChars = sizeof(str) - remainingSize - 1;
printf("Write size = %d\n", nbChars);
int n = write(newsockfd, str, nbChars);
if (n < 0) {
perror("ERROR writing to socket");
return;
}
firstSeparator = false;
}
}
}
}
// Write only ']'! Don't write "\0" !!!!
write(newsockfd,&"]",1);
}