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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/select.h>
#define BUFFER_SIZE 1024
typedef struct number_val {
int val;
struct timeval tv;
} num;
int sock_init(argv)
char **argv;
{
struct sockaddr_in address, serv_addr;
int sock = 0, valread;
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket creation error\n");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(atoi(argv[2]));
if (inet_pton(AF_INET, argv[1], &serv_addr.sin_addr) <= 0) {
perror("Invalid address/Address not supported\n");
return -1;
}
if (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("Connection Failed");
return -1;
}
return sock;
}
int main(argc, argv)
int argc;
char **argv;
{
int sock = 0, valread, start_ptr = -1, end_ptr = 0, qset, i;
double avg = 0, cnt;
char buffer[1024] = {0};
num num_buffer[BUFFER_SIZE];
fd_set rs, ws, es;
struct timeval now, *ww, tmp;
sock = sock_init(argv);
ww = NULL;
printf("Connected, waiting for numbers...\n");
while (1) {
FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
FD_SET(sock, &rs);
qset = select(sock + 1, &rs, (fd_set *) 0, (fd_set *) 0, ww);
gettimeofday(&now, NULL);
now.tv_sec = now.tv_sec - 60;
ww = &tmp;
tmp.tv_sec = 0;
tmp.tv_usec = 500000;
if (FD_ISSET(sock, &rs)) {
valread = read(sock, buffer, 1024);
for (i = 0; i < valread; i+=2) {
if (end_ptr == BUFFER_SIZE)
end_ptr = 0;
if (end_ptr != start_ptr) {
gettimeofday(&(num_buffer[end_ptr].tv), NULL);
num_buffer[end_ptr].val = atoi(buffer + i);
end_ptr++;
} else {
printf("dropping number...\n");
}
}
}
if (start_ptr == -1)
start_ptr = 0;
if (start_ptr < end_ptr) {
for (i = start_ptr; i < end_ptr; i++) {
if (num_buffer[i].tv.tv_sec <= now.tv_sec)
start_ptr = i + 1;
}
} else {
for (i = start_ptr; i < BUFFER_SIZE; i++) {
num_buffer[end_ptr].val = atoi(buffer);
end_ptr++;
num_buffer[end_ptr].val = atoi(buffer);
end_ptr++;
num_buffer[end_ptr].val = atoi(buffer);
end_ptr++;
if (num_buffer[i].tv.tv_sec <= now.tv_sec)
start_ptr = i + 1;
}
for (i = 0; i < end_ptr; i++) {
if (num_buffer[i].tv.tv_sec <= now.tv_sec)
start_ptr = i + 1;
}
start_ptr %= BUFFER_SIZE;
}
avg = 0;
cnt = start_ptr > end_ptr ? (BUFFER_SIZE - start_ptr + end_ptr) : (end_ptr - start_ptr);
cnt = cnt == 0 ? 10 : cnt;
if (start_ptr < end_ptr) {
for (i = start_ptr; i < end_ptr; i++)
avg += num_buffer[i].val / cnt;
} else {
for (i = start_ptr; i < BUFFER_SIZE; i++)
avg += num_buffer[i].val / cnt;
for (i = 0; i < end_ptr; i++)
avg += num_buffer[i].val /cnt;
}
printf("avg: %10.5f\n", avg);
}
}
|