-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadbalancer.c
363 lines (285 loc) · 10.2 KB
/
loadbalancer.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
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
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <string.h>
#include <arpa/inet.h>
#include <time.h>
#include <pthread.h>
//#define SIZE sizeof(struct sockaddr_in)
/*typedef struct server_info {
char ipaddr[15];
int port;
pthread_mutex_t th_mutex;
} server_dt;*/
#define MAXBUFSIZE 4096
typedef struct server_database {
int server_id;
char ipaddr[15];
int port;
int count;
int is_alive;
pthread_mutex_t th_mutex;
} server_dt;
server_dt *primary_server;
server_dt *secondary_server;
typedef struct arguments {
int sock;
} args_dt;
typedef struct rqst {
char *command;
int handler;
long int datalength;
char *data;
} tweet_req_dt;
typedef struct rslt {
char command[15];
int st_code;
long int dlength;
char *data;
} tweet_resp_dt;
struct sockaddr_in server;
void server_check();
void tcp();
void create_newconnect();
void session(void *q);
void timer();
char *recv_buf(int sock);
int main (int argc, char **argv)
{
int msock, ssock,clen;
int portno;
struct sockaddr_in server,cli;
struct hostent *cli_details;
char *caddr;
pthread_t tid;
pthread_attr_t tattr;
//Test for correct number of arguments
if (argc != 2)
{
fprintf(stderr, "Usage: <filename> <port>");
exit(0);
}
primary_server = (server_dt *)malloc(sizeof(server_dt));
secondary_server = (server_dt *)malloc(sizeof(server_dt));
primary_server->server_id = 1;
strcpy(primary_server->ipaddr , "127.0.0.1");
primary_server->port = 6001;
primary_server->count = 0;
primary_server->is_alive = 0;
secondary_server->server_id = 2;
strcpy(secondary_server->ipaddr ,"127.0.0.1");
secondary_server->port = 7001;
secondary_server->count = 0;
secondary_server->is_alive = 0;
//server_dt *global_server;
//global_server = (server_dt *)malloc(sizeof(server_dt));
portno = atoi(argv[1]);//convert port # to int
server.sin_family = AF_INET;
server.sin_port = htons(portno);
server.sin_addr.s_addr = htonl(INADDR_ANY);//use client ip address
msock = socket(PF_INET,SOCK_STREAM,0);
if (msock<0) {
printf("Error: Failed to create a socket.\n");
exit(0);
}
if (bind(msock, (struct sockaddr *)&server, sizeof(server)) < 0) {
printf("Error: Failed to bind the socket.\n");
exit(0);
}
if (listen(msock,5) <0) {
printf("Error: Failed to set socket in passive mode.\n");
exit(0);
}
printf("\n\n****************LOAD BALANCER********************\n\n");
while(1) {
clen = sizeof(cli);
if ((ssock = accept(msock, (struct sockaddr *)&cli,&clen)) < 0) {
printf("Error: Failed to connect to remote host.\n");
exit(0);
}
cli_details = gethostbyaddr((const char *)&cli.sin_addr.s_addr, sizeof(cli.sin_addr.s_addr), PF_INET);
caddr = inet_ntoa(cli.sin_addr);
// printf("Connection established to %s(%s) \n",(char *)cli_details->h_name,caddr);
/*args_dt *args = (args_dt *)malloc(sizeof(args_dt));
args->sock = ssock;
args->server_list[0] = primary_server;
args->server_list[1] = secondary_server;*/
pthread_attr_init(&tattr);
pthread_attr_setdetachstate(&tattr,PTHREAD_CREATE_DETACHED);
pthread_mutex_init(&primary_server->th_mutex,0);
pthread_mutex_init(&secondary_server->th_mutex,0);
if (pthread_create(&tid,&tattr,(void*(*)(void *))session,(void *)ssock) < 0) {
printf("Error: Failed to create a new thread.\n");
exit(0);
}
}
return 0;
}
void session(void *q)
{
int sock = (int)q;
char *request, *reply, *format;
char *tmp1, *data1, *data2;
// th_args_dt *args = (th_args_dt *)q;
pthread_t tid;
pthread_attr_t tattr;
tweet_req_dt *tweet_req = (tweet_req_dt *)malloc(sizeof(tweet_req_dt));
tweet_resp_dt *result= (tweet_resp_dt *)malloc(sizeof(tweet_resp_dt));
/*receive buffer*/
request = recv_buf(sock);
//printf("Request received: %s\n",request);
char *tmp;
tmp = strtok(request," ");
tweet_req->command = tmp;
if (strcmp(tweet_req->command,"CLIENT") == 0) {
printf("CLIENT Requested the server address...\n");
char ipaddr[15];
int port;
pthread_mutex_lock(&primary_server->th_mutex);
if (primary_server->is_alive == 1) {
strcpy(ipaddr,primary_server->ipaddr);
port = primary_server->port;
}
else {
pthread_mutex_lock(&secondary_server->th_mutex);
strcpy(ipaddr,secondary_server->ipaddr);
port = secondary_server->port;
pthread_mutex_unlock(&secondary_server->th_mutex);
}
pthread_mutex_unlock(&primary_server->th_mutex);
pthread_mutex_lock(&primary_server->th_mutex);
if (primary_server->is_alive == 1) {
result->st_code = 200;
}
else {
pthread_mutex_lock(&secondary_server->th_mutex);
result->st_code = 400;
pthread_mutex_unlock(&secondary_server->th_mutex);
}
pthread_mutex_unlock(&primary_server->th_mutex);
strcpy(result->command,tmp);
result->dlength = (strlen(ipaddr) + 3);
format = "%s %d %ld\r\n\r\n%s %d\0";
reply = (char *)malloc(strlen(format)+strlen(result->command)+sizeof(result->st_code)+sizeof(result->dlength)+strlen(ipaddr)+sizeof(port));
sprintf(reply,format,result->command,result->st_code, result->dlength,ipaddr,port);
printf("\nData Sent:%s\n",reply);
send_buf(sock,reply);
free(reply);
free(request);
free(result);
free(tweet_req);
close(sock);
pthread_exit(0);
}
else if (strcmp(tweet_req->command,"SERVER") == 0) {
int count;
tmp = strtok(NULL,"\0");
int server_id = atoi(tmp);
printf("Keep Alive message received from SERVER%d\n",server_id);
time_t ltime;
ltime=time(NULL); /* get current cal time */
printf("Message Timestamp: %s\n",asctime(localtime(<ime)));
if (server_id == 1) {
pthread_mutex_lock(&primary_server->th_mutex);
primary_server->count++;
count = primary_server->count;
pthread_mutex_unlock(&primary_server->th_mutex);
}
else if(server_id == 2) {
pthread_mutex_lock(&secondary_server->th_mutex);
secondary_server->count++;
count = secondary_server->count;
pthread_mutex_unlock(&secondary_server->th_mutex);
}
/*
if (server_id == 1) {
pthread_mutex_lock(&primary_server->th_mutex);
printf("TIMER START: Server:>%d< & Count:>%d< & Primary Count:>%d<\n",server_id,count,primary_server->count);
pthread_mutex_unlock(&primary_server->th_mutex);
}
else {
pthread_mutex_lock(&secondary_server->th_mutex);
printf("TIMER START: Server:>%d< & Count:>%d< & Secondary Count:>%d<\n",server_id,count,secondary_server->count);
pthread_mutex_unlock(&secondary_server->th_mutex);
}*/
sleep(70);
if (server_id == 1) {
pthread_mutex_lock(&primary_server->th_mutex);
//printf("TIMER END: Server:>%d< & Primary Count:>%d<\n",server_id,primary_server->count);
if (count <= primary_server->count) {
primary_server->is_alive = 1;
printf("Server%d is ALIVE!!!\n\n",server_id);
}
else {
primary_server->is_alive = 0;
printf("Server%d is DOWN!!!\n\n",server_id);
}
primary_server->count--;
pthread_mutex_unlock(&primary_server->th_mutex);
}
else if (server_id == 2){
pthread_mutex_lock(&secondary_server->th_mutex);
//printf("TIMER END: Server:>%d< & Secondary Count:>%d<\n",server_id,secondary_server->count);
if (count <= secondary_server->count) {
secondary_server->is_alive = 1;
printf("Server%d is ALIVE!!!\n\n",server_id);
}
else {
secondary_server->is_alive = 0;
printf("Server%d is DOWN!!!\n\n",server_id);
}
secondary_server->count--;
pthread_mutex_unlock(&secondary_server->th_mutex);
}
/*pthread_mutex_unlock(&primary_server->th_mutex);
pthread_attr_init(&tattr);
pthread_attr_setdetachstate(&tattr,PTHREAD_CREATE_DETACHED);
if (pthread_create(&tid,&tattr,(void*(*)(void *))timer,NULL) < 0) {
printf("Error: Failed to start the timer thread.\n");
exit(0);
}*/
pthread_exit(0);
}
}
/*void timer()
{
pthread_mutex_lock(&primary_server->th_mutex);
primary_server->count = 1;
pthread_mutex_unlock(&primary_server->th_mutex);
sleep(350);
pthread_mutex_lock(&primary_server->th_mutex);
if (primary_server->count == 0) primary_server->is_alive = 1;
else primary_server->is_alive = 0;
pthread_mutex_unlock(&primary_server->th_mutex);
pthread_exit(0);
}*/
void send_buf(int sock, char *buffer)
{
int sdata, sbits = 0;
while(sbits < strlen(buffer))
{
if ((sdata = send(sock, buffer, strlen(buffer)-sbits, 0)) <0){
printf("Error: Failed to send data to remote server.\n");
exit(0);
}
else sbits += sdata;
}
}
char *recv_buf(int sock)
{
int size_buf=MAXBUFSIZE;
int sbits;
char *buffer = (char *)malloc(MAXBUFSIZE);
memset(buffer, NULL, sizeof(buffer));
while ((sbits=recv(sock, buffer, size_buf-1, 0)) > 0) {
//("Write buffer: %s\n", buffer);
if (strchr(buffer, '\0') != NULL )
break;
size_buf-=sbits;
}
return buffer;
}