-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.c
More file actions
260 lines (224 loc) · 8.06 KB
/
task1.c
File metadata and controls
260 lines (224 loc) · 8.06 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
# Copyright 2025 University of Kentucky
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
*/
/*
Please specify the group members here
# Student #1: Charles Schmidt
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <pthread.h>
#include <limits.h>
#define MAX_EVENTS 64
#define MESSAGE_SIZE 16
#define DEFAULT_CLIENT_THREADS 4
#define EPOLL_SAMPLING_RATE 100
#define TIMEOUT 1000
#define NUM_PIPE_PACKETS 5
char *server_ip = "127.0.0.1";
int server_port = 12345;
char *client_ip;
int client_port = 8080;
int num_client_threads = DEFAULT_CLIENT_THREADS;
int num_requests = 1000000;
socklen_t addr_len = sizeof(struct sockaddr_in);
typedef struct {
int epoll_fd;
int socket_fd;
int tx;
int rx;
char thread_num;
long long total_rtt;
long total_messages;
float request_rate;
struct sockaddr_in server_addr;
} client_thread_data_t;
void *client_thread_func(void *arg) {
client_thread_data_t *data = (client_thread_data_t *)arg;
struct epoll_event event, events[MAX_EVENTS];
char send_buf[MESSAGE_SIZE] = "ABCDEFGHIJKMLNOP";
char recv_buf[MESSAGE_SIZE];
struct timeval start, end, cur;
struct timeval timer_array[NUM_PIPE_PACKETS];
int sent_array[NUM_PIPE_PACKETS];
data->request_rate = 0;
data->total_rtt = 0;
data->total_messages = 0;
// Register the socket in the epoll instance
event.events = EPOLLIN;
event.data.fd = data->socket_fd;
epoll_ctl(data->epoll_fd, EPOLL_CTL_ADD, data->socket_fd, &event);
for(char i=0;i<NUM_PIPE_PACKETS;i++){
send_buf[0] = i;
send_buf[1] = data->thread_num;
data->tx++;
send(data->socket_fd, send_buf, MESSAGE_SIZE, 0);
sent_array[i] = 1;
gettimeofday(&timer_array[i], NULL);
}
while (1) {
if(data->tx < num_requests){
for(char i=0;i<NUM_PIPE_PACKETS;i++){
gettimeofday(&cur,NULL);
long long micro = (cur.tv_sec - timer_array[i].tv_sec) * 1000000LL + (cur.tv_usec - timer_array[i].tv_usec);
if((micro/1000 > TIMEOUT||!sent_array[i])&&data->tx < num_requests){
gettimeofday(&timer_array[i],NULL);
send_buf[0] = i;
send_buf[1] = data->thread_num;
if(send(data->socket_fd, send_buf, MESSAGE_SIZE, 0)<0){
perror("timeout sendto failure");
}
sent_array[i] = 1;
data->tx++;
}
}
}
// use a low timeout for the epoll wait so that the function can keep checking the individual
// packets timers like a sampling rate
int n_events = epoll_wait(data->epoll_fd, events, MAX_EVENTS, EPOLL_SAMPLING_RATE);
for (int i = 0; i < n_events; i++) {
if (events[i].data.fd == data->socket_fd) {
int n = recv(data->socket_fd, recv_buf, MESSAGE_SIZE,0);
if(n<0){
perror("ack recieve failed");
}
char index = recv_buf[0];
sent_array[index] = 0;
gettimeofday(&timer_array[index],NULL);
data->rx++;
// Calculate RTT
}
}
gettimeofday(&end, NULL);
long long rtt = (end.tv_sec - cur.tv_sec) * 1000000LL + (end.tv_usec - cur.tv_usec);
if(data->tx >= num_requests && rtt/1000 >= TIMEOUT) break;
}
// Update request rate
// printf("Total RTT: %lld s\n", data->total_rtt / 1000000);
// printf("Total messages: %ld\n", data->total_messages);
// printf("RPS: %f\n", data->request_rate);
close(data->socket_fd);
close(data->epoll_fd);
return NULL;
}
void run_client() {
pthread_t threads[num_client_threads];
client_thread_data_t thread_data[num_client_threads];
struct sockaddr_in server_addr,client_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(server_port);
if(inet_pton(AF_INET, server_ip, &server_addr.sin_addr)<0){
perror("bad server ip");
}
// Create client threads
for (int i = 0; i < num_client_threads; i++) {
thread_data[i].epoll_fd = epoll_create1(0);
thread_data[i].socket_fd = socket(AF_INET,SOCK_DGRAM,0);
thread_data[i].server_addr = server_addr;
thread_data[i].tx = 0;
thread_data[i].rx = 0;
thread_data[i].thread_num = i;
if(connect(thread_data[i].socket_fd,(struct sockaddr *)&server_addr,addr_len)<0){
perror("client socket failed to connect");
}
}
for (int i = 0; i < num_client_threads; i++) {
pthread_create(&threads[i], NULL, client_thread_func, &thread_data[i]);
}
// Wait for threads to complete
long long total_rtt = 0;
long total_messages = 0;
float total_request_rate = 0;
double tx = 0.0;
double rx = 0.0;
for (int i = 0; i < num_client_threads; i++) {
pthread_join(threads[i], NULL);
total_rtt += thread_data[i].total_rtt;
total_messages += thread_data[i].total_messages;
total_request_rate += thread_data[i].request_rate;
tx += thread_data[i].tx;
rx += thread_data[i].rx;
}
printf("tx: %i\n",(int)tx);
printf("rx: %i\n",(int)rx);
printf("percentage packets loss: %f\n",1-(rx/tx));
}
void run_server() {
int server_fd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in server_addr,client_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(server_port);
if(bind(server_fd,(struct sockaddr *)&server_addr,addr_len)<0){
perror("server port failed to bind");
}
int epoll_fd = epoll_create1(0);
if(epoll_fd<0){
perror("epoll create failed");
}
struct epoll_event event, events[MAX_EVENTS];
event.events = EPOLLIN;
event.data.fd = server_fd;
if(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, server_fd, &event)<0){
perror("epoll ctl failed");
}
while (1) {
int n_events = epoll_wait(epoll_fd, events, MAX_EVENTS, TIMEOUT);
if(n_events<0){
perror("epoll wait failed");
}
for (int i = 0; i < n_events; i++) {
if (events[i].data.fd == server_fd) {
// Handle client data
char buffer[MESSAGE_SIZE];
int n = recvfrom(server_fd, &buffer, MESSAGE_SIZE, 0,(struct sockaddr *)&client_addr,&addr_len);
if(n == -1){
perror("packet error");
}
if(sendto(server_fd, &buffer, MESSAGE_SIZE, 0,(struct sockaddr *)&client_addr,addr_len)<0){
perror("server send failed");
}
}
}
}
close(server_fd);
close(epoll_fd);
}
int main(int argc, char *argv[]) {
if (argc > 1 && strcmp(argv[1], "server") == 0) {
if (argc > 2) server_ip = argv[2];
if (argc > 3) server_port = atoi(argv[3]);
run_server();
} else if (argc > 1 && strcmp(argv[1], "client") == 0) {
if (argc > 2) server_ip = argv[2];
if (argc > 3) server_port = atoi(argv[3]);
if (argc > 4) num_client_threads = atoi(argv[4]);
if (argc > 5) num_requests = atoi(argv[5]);
run_client();
} else {
printf("Usage: %s <server|client> [server_ip server_port num_client_threads num_requests]\n", argv[0]);
}
return 0;
}