-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
492 lines (384 loc) · 13.9 KB
/
main.cpp
File metadata and controls
492 lines (384 loc) · 13.9 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <cstdlib>
#include <algorithm>
#include <initializer_list>
#include <ctime>
#include <chrono>
#include <vector>
#include <time.h>
#include <thread>
#include <mutex>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#define SERV1 "192.168.0.101"
#define SERV2 "192.168.0.102"
#define SERV3 "192.168.0.103"
#define PORT 80
#define BUFFER_LEN 256
#define MUSIC 'M'
#define VIDEO 'V'
#define PICTURE 'P'
using std::cout;
using std::cerr;
using std::endl;
using std::cin;
using std::vector;
using std::localtime;
using std::time_t;
using std::mutex;
bool availableThreads[5] = {true, true, true, true, true};
std::mutex mtx;
const std::string servers_ip[3] = {"192.168.0.101", "192.168.0.102", "192.168.0.103"};
sem_t threads_sem;
int count = 0;
class ThreadFuncVars {
public:
int sockfd;
vector<time_t> servers_empty_times;
vector<int> servers_fds;
int thread_idx;
ThreadFuncVars(int sockfd, vector<time_t>& servers_empty_times, vector<int>& servers_fds, int thread_idx) : sockfd(sockfd), servers_empty_times(servers_empty_times), servers_fds(servers_fds), thread_idx(thread_idx) {}
};
int getAvailableThread() {
for (int i = 0; i < 5; i++) {
if (availableThreads[i]) {
return i;
}
}
return -1;
}
void error(const char *msg) {
cerr << msg << endl;
exit(0);
}
int findBestServer(double server1_expect_time, double server2_expect_time, double server3_except_time) {
if (server1_expect_time < server2_expect_time) {
if (server1_expect_time < server3_except_time) {
return 0;
}
else {
return 2;
}
}
else {
if (server2_expect_time < server3_except_time) {
return 1;
}
else {
return 2;
}
}
}
void* message_handler(void* abs_params) {
sem_wait(&threads_sem);
count++;
sem_post(&threads_sem);
ThreadFuncVars* params = (ThreadFuncVars*) abs_params;
int message_time, video_server_execution_time, music_server_execution_time;
char message_type;
time_t curr_time;
char buf[2];
char answer[64];
struct sockaddr_in addr;
socklen_t addr_size = sizeof(struct sockaddr_in);
int res = getpeername(params->sockfd, (struct sockaddr *)&addr, &addr_size);
// TODO: add recv from client, find out which server to send to, send to server, recv answer from server, send to client
// TODO: add the recv
sem_wait(&threads_sem);
if (recv(params->sockfd, buf, sizeof(buf), 0) < 0) {
error("Could not receive message");
}
sem_post(&threads_sem);
message_type = buf[0];
message_time = (int)(buf[1] - '0'); //To convert from ascii to int
// AFTER THE PARSING OF THE MESSAGE
switch(message_type) {
case VIDEO:
video_server_execution_time = message_time;
music_server_execution_time = 3 * message_time;
break;
case MUSIC:
music_server_execution_time = message_time;
video_server_execution_time = 2 * message_time;
break;
case PICTURE:
video_server_execution_time = message_time;
music_server_execution_time = 2 * message_time;
break;
default:
// should not get here
error("UNDEFINED MESSAGE");
break;
}
// TODO: lock
//mtx.lock();
sem_wait(&threads_sem);
time(&curr_time);
params->servers_empty_times[0] = std::max(params->servers_empty_times[0],curr_time);
params->servers_empty_times[1] = std::max(params->servers_empty_times[1],curr_time);
params->servers_empty_times[2] = std::max(params->servers_empty_times[2],curr_time);
int index = findBestServer(params->servers_empty_times[0] + video_server_execution_time, params->servers_empty_times[1] + video_server_execution_time, params->servers_empty_times[2] + music_server_execution_time);
if(index == 2) {
params->servers_empty_times[index] += music_server_execution_time;
} else {
params->servers_empty_times[index] += video_server_execution_time;
}
cout << " " << curr_time << ": recieved request " << message_type << message_time;
cout << " from " << inet_ntoa(addr.sin_addr) << ", sending to " << servers_ip[index] << "-----" << endl;
// TODO: unlock
//mtx.unlock();
sem_post(&threads_sem);
cout << "A" << endl;
sem_wait(&threads_sem);
cout << " " << curr_time << endl;
// TODO: send the message to the best server using server_fds[index]
if (send(params->servers_fds[index], buf, sizeof(buf), 0) < 0) {
error("Can't send the message to the server");
}
sem_post(&threads_sem);
cout << "B" << endl;
sem_wait(&threads_sem);
// TODO: recv the answer from the server
if (recv(params->servers_fds[index], answer, sizeof(answer), 0) < 0) {
error("Can't receive the message from the server");
}
cout << "GOT ANSWER FROM SERVER:" << endl;
cout << answer << endl;
sem_post(&threads_sem);
sem_wait(&threads_sem);
// TODO: send the answer to the client
if (send(params->sockfd, answer, sizeof(answer), 0) < 0) {
error("Can't send the message to the client");
}
sem_post(&threads_sem);
cout << "C" << endl;
/*
// lock
mtx.lock();
availableThreads[params->thread_idx] = true;
// unlock
mtx.unlock();
*/
sem_wait(&threads_sem);
close(params->sockfd);
cout << "D" << endl;
cout << "ENDED THE THREAD CODE" << endl;
//mtx.unlock();
count--;
sem_post(&threads_sem);
pthread_exit(NULL);
}
int main() {
int serv1_sock, serv2_sock, serv3_sock;
int video_server_execution_time;
int music_server_execution_time;
struct sockaddr_in serv1_addr, serv2_addr, serv3_addr;
time_t server1_empty_time;
time(&server1_empty_time);
time_t server2_empty_time = server1_empty_time;
time_t server3_empty_time = server1_empty_time;
sem_init(&threads_sem, 0, 1);
time_t curr_time;
time(&curr_time);
cout << curr_time << ": LB started-----lb1#" << endl;
int message_time;
char message_type;
vector<int> servers_fds;
vector<time_t> servers_empty_times = {server1_empty_time, server2_empty_time, server3_empty_time};
cout << curr_time << ": Connecting to servers-----lb1#" << endl;
serv1_sock = socket(AF_INET, SOCK_STREAM, 0);
if (serv1_sock < 0) {
error("ERROR opening socket");
}
serv2_sock = socket(AF_INET, SOCK_STREAM, 0);
if (serv2_sock < 0) {
error("ERROR opening socket");
}
serv3_sock = socket(AF_INET, SOCK_STREAM, 0);
if (serv3_sock < 0) {
error("ERROR opening socket");
}
serv1_addr.sin_family = AF_INET;
serv2_addr.sin_family = AF_INET;
serv3_addr.sin_family = AF_INET;
serv1_addr.sin_port = htons(PORT);
serv2_addr.sin_port = htons(PORT);
serv3_addr.sin_port = htons(PORT);
//inet_aton(SERV1, &serv1_addr.sin_addr.s_addr);
//inet_aton(SERV2, &serv2_addr.sin_addr.s_addr);
//inet_aton(SERV3, &serv3_addr.sin_addr.s_addr);
inet_aton(SERV1, &serv1_addr.sin_addr);
inet_aton(SERV2, &serv2_addr.sin_addr);
inet_aton(SERV3, &serv3_addr.sin_addr);
if(connect(serv1_sock, (struct sockaddr*) &serv1_addr, sizeof(serv1_addr)) < 0) {
error("ERROR connecting");
}
if(connect(serv2_sock, (struct sockaddr*) &serv2_addr, sizeof(serv2_addr)) < 0) {
error("ERROR connecting");
}
if(connect(serv3_sock, (struct sockaddr*) &serv3_addr, sizeof(serv3_addr)) < 0) {
error("ERROR connecting");
}
servers_fds.push_back(serv1_sock);
servers_fds.push_back(serv2_sock);
servers_fds.push_back(serv3_sock);
// now we are connected to the 3 servers
int listen_sock_fd, newsockfd;
socklen_t client_len;
char buffer[BUFFER_LEN];
struct sockaddr_in listen_addr, client_addr;
listen_sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_sock_fd < 0) {
error("ERROR opening socket");
}
listen_addr.sin_family = AF_INET;
listen_addr.sin_addr.s_addr = INADDR_ANY;
listen_addr.sin_port = htons(PORT);
if(bind(listen_sock_fd, (struct sockaddr*) &listen_addr, sizeof(listen_addr)) < 0) {
error("ERROR on binding");
}
//listen(listen_sock_fd, 5);
listen(listen_sock_fd, 32767);
//std::thread myThreads[5];
//std::vector<std::thread> myThreads;
std::vector<pthread_t> myThreads;
for (int i = 0; i < 5; i++) {
pthread_t curr_thread;
myThreads.push_back(curr_thread);
}
int i = 0;
while(true) {
cout << "lb1#";
client_len = sizeof(client_addr);
newsockfd = accept(listen_sock_fd, (struct sockaddr*) &client_addr, (socklen_t*) &client_len);
if(newsockfd < 0) {
error("ERROR on accept");
}
/*
// TODO: lock
mtx.lock();
int t_index = getAvailableThread();
if(t_index < 0) {
error("No available threads");
}
availableThreads[t_index] = false;
// TODO: unlock
ThreadFuncVars params(newsockfd, servers_empty_times, servers_fds, t_index);
*/
/*
params.sockfd = newsockfd;
params.servers_empty_times = servers_empty_times;
params.servers_fds = servers_fds;
params.thread_idx = t_index;
*/
//mtx.unlock();
//std::thread th(message_handler, params);
//myThreads.push_back(th);
//std::thread th(message_handler, newsockfd, servers_empty_times, servers_fds, t_index);
//myThreads.push_back(th);
/*
ThreadFuncVars params(newsockfd, servers_empty_times, servers_fds, 0);
//pthread_create(&myThreads[t_index], NULL, message_handler, ¶ms);
pthread_create(&myThreads[i++], NULL, message_handler, ¶ms);
if (i >= 5) {
i = 0;
while(i < 5) {
pthread_join(myThreads[i++], NULL);
}
i = 0;
}
*/
// TODO: maybe add join
//i++;
ThreadFuncVars params(newsockfd, servers_empty_times, servers_fds, 0);
int message_time, video_server_execution_time, music_server_execution_time;
char message_type;
time_t curr_time;
char buf[2];
char answer[64];
struct sockaddr_in addr;
socklen_t addr_size = sizeof(struct sockaddr_in);
int res = getpeername(params.sockfd, (struct sockaddr *)&addr, &addr_size);
// TODO: add recv from client, find out which server to send to, send to server, recv answer from server, send to client
// TODO: add the recv
if (recv(params.sockfd, buf, sizeof(buf), 0) < 0) {
error("Could not receive message");
}
message_type = buf[0];
message_time = (int)(buf[1] - '0'); //To convert from ascii to int
// AFTER THE PARSING OF THE MESSAGE
switch(message_type) {
case VIDEO:
video_server_execution_time = message_time;
music_server_execution_time = 3 * message_time;
break;
case MUSIC:
music_server_execution_time = message_time;
video_server_execution_time = 2 * message_time;
break;
case PICTURE:
video_server_execution_time = message_time;
music_server_execution_time = 2 * message_time;
break;
default:
// should not get here
error("UNDEFINED MESSAGE");
break;
}
// TODO: lock
//mtx.lock();
time(&curr_time);
params.servers_empty_times[0] = std::max(params.servers_empty_times[0],curr_time);
params.servers_empty_times[1] = std::max(params.servers_empty_times[1],curr_time);
params.servers_empty_times[2] = std::max(params.servers_empty_times[2],curr_time);
int index = findBestServer(params.servers_empty_times[0] + video_server_execution_time, params.servers_empty_times[1] + video_server_execution_time, params.servers_empty_times[2] + music_server_execution_time);
if(index == 2) {
params.servers_empty_times[index] += music_server_execution_time;
} else {
params.servers_empty_times[index] += video_server_execution_time;
}
cout << " " << curr_time << ": recieved request " << message_type << message_time;
cout << " from " << inet_ntoa(addr.sin_addr) << ", sending to " << servers_ip[index] << "-----" << endl;
// TODO: unlock
//mtx.unlock();
cout << "A" << endl;
cout << " " << curr_time << endl;
// TODO: send the message to the best server using server_fds[index]
if (send(params.servers_fds[index], buf, sizeof(buf), 0) < 0) {
error("Can't send the message to the server");
}
cout << "B" << endl;
// TODO: recv the answer from the server
if (recv(params.servers_fds[index], answer, sizeof(answer), 0) < 0) {
error("Can't receive the message from the server");
}
cout << "GOT ANSWER FROM SERVER:" << endl;
cout << answer << endl;
// TODO: send the answer to the client
if (send(params.sockfd, answer, sizeof(answer), 0) < 0) {
error("Can't send the message to the client");
}
cout << "C" << endl;
/*
// lock
mtx.lock();
availableThreads[params->thread_idx] = true;
// unlock
mtx.unlock();
*/
close(params.sockfd);
cout << "D" << endl;
cout << "ENDED THE THREAD CODE" << endl;
//mtx.unlock();
}
for(int i = 0; i < servers_fds.size(); i++) {
close(servers_fds[i]);
}
return 0;
}