-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverT.c
288 lines (242 loc) · 7.46 KB
/
serverT.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
/*
file name: serverT.c
coder: Junyu Huang
USC-ID: 8109163913
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#define ServerT_PORT "21913" //The port number used for serverT for udp connection
#define LOCAL_HOST "127.0.0.1" //The local IP
#define MAX_DADAZIE 1024
#define MAXBUFLEN 4000 //MAX datalen for udp transimission
#define IFIN 20000
#define MAX_VERT 100
#define MAX_LEN 100
#define MAXQUEUE 10
#define FILEROUTE "/home/student/Desktop/source/edgelist.txt" //file route
//Global variables
int get_textline();
void *get_in_addr(struct sockaddr *sa);
void get_network_graph(char *username, char *network_graph);
void split_argvs(char *argv[],char *data);
int get_index(char *argv[],int vertnum, char buf[MAX_LEN][MAX_LEN]);
void array_transfer(int vertnum, int number_buf[vertnum][vertnum], char letter_buf[100]);
int main()
{
int socketfd,rv,bytesnum; // listen on sock_fd, new connection on new_fd
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr; // connector's address information
socklen_t addr_len = sizeof their_addr;
char recv_buf[MAXBUFLEN],send_buf[MAXBUFLEN];
char *argv[3];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
rv = getaddrinfo(LOCAL_HOST, ServerT_PORT, &hints, &servinfo);
if (rv != 0)
{
printf("getaddrinfo failed\n");
return 1;
}
// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((socketfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1)
{
perror("ServerT: socket");
continue;
}
if (bind(socketfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(socketfd);
perror("ServerT: bind");
continue;
}
break;
}
if (p == NULL)
{
fprintf(stderr, "Central server: failed to bind\n");
return 2;
}
freeaddrinfo(servinfo); //release the space
printf("The ServerT is up and running using UDP on port<%s>: \n",ServerT_PORT);
fflush(stdout);
while(1)
{
if((bytesnum = recvfrom(socketfd,recv_buf,MAXBUFLEN-1,0,
(struct sockaddr*)&their_addr,&addr_len)) == -1)
{
perror("recvfrom");
exit(1);
}
recv_buf[bytesnum] = '\0';
split_argvs(argv,recv_buf);
//printf("1st input is: %s, 2nd input is: %s\n", argv[0],argv[1]);
//get_index(argv,vernum,host_buf);
printf("The ServerT received a request from Central to get the topology\n");
//open the edgelist.txt file to get the relationship
//realted to differnet hostname
FILE *file = fopen(FILEROUTE, "r");
if(file == NULL)
{
printf("File %s not found\n", FILEROUTE);
}
char str[100],str1[100];
char *data_buf[100];
char host_buf[MAX_LEN][MAX_LEN] = {"\0"};
int linenum, hostnum = 0, edgenum, vernum;
int temp;
while(fgets(str,sizeof(str),file))
{
str[strlen(str) - 1] = '\0';
split_argvs(data_buf,str);
strcpy(host_buf[hostnum], data_buf[0]);
strcpy(host_buf[++hostnum], data_buf[1]);
hostnum++;
}
//printf("hostname: %d\n",hostnum);
// create matrix
//int matrix[hostnum][hostnum];
int times = 0;
int colum,line;
for(int i=0;i<hostnum;++i)
{
for(int j =i+1;j < hostnum;++j)
{
if(strcmp(host_buf[i] ,host_buf[j]) == 0)
{
for(int k = j;k< hostnum-1;k++)
strcpy(host_buf[k] , host_buf[k+1]);
j = i;
--hostnum;
}
}
}
//printf("hostname: %d\n",hostnum);
int matrix[hostnum][hostnum];
for(int i = 0; i < hostnum; i++)
{
for(int j =0; j < hostnum; j++)
{
matrix[i][j] = 0;
}
}
//printf("\n");
memset(&data_buf,0,sizeof(data_buf));
/*
for(int i =0;i<hostnum;i++)
{
printf("host %d is %s\n",i,host_buf[i]);
}
*/
//open file again to get the matrix
FILE *file1 = fopen(FILEROUTE, "r");
while(fgets(str,sizeof(str),file1))
{
str[strlen(str) - 1] = '\0';
split_argvs(data_buf,str);
//printf("GET IN data1 %s \n",*data_buf);
//printf("GET IN data1 %s \n",*(data_buf+1));
colum = get_index(data_buf,hostnum,host_buf);
line = get_index((data_buf+1),hostnum,host_buf);
if((colum <= hostnum)&&(line <= hostnum))
{
matrix[colum][line]= matrix[line][colum] = 1;
}
}
fclose(file);
fclose(file1);
//transfer the matrix to another format before sending
memset(&send_buf,0,sizeof send_buf);
array_transfer(hostnum,matrix,send_buf);
//printf("send_buf : %s \n",send_buf);
char temp_send[MAX_LEN];
for(int i =0;i<hostnum;i++)
{
sprintf(temp_send, "%s/", host_buf[i]);
strcat(send_buf,temp_send);
}
strcat(send_buf,"\n");
//printf("send_buf : %s \n",send_buf);
if((bytesnum = sendto(socketfd,send_buf,strlen(send_buf),0,
(struct sockaddr*)&their_addr,addr_len)) == -1)
{
perror("ServerT: sendto");
exit(1);
}
printf("The ServerT finished sending the topology to the Central\n");
}
return 0;
}
//To get the line number of a txt file
int get_textline(const char *filename)
{
FILE *file = fopen(filename, "r");
int count = 0;
if(file != NULL)
{
while(!feof(file))
{
if(fgetc(file) == '\n')
count++;
}
}
fclose(file);
return count;
}
void split_argvs(char *argv[],char *data)
{
char *s = strtok(data, " ");
int i =0;
while(s != NULL)
{
argv[i++] = s;
s = strtok (NULL," ");
}
}
//Function to get the index according to hostname
int get_index(char *argv[],int vertnum, char buf[MAX_LEN][MAX_LEN])
{
for(int i = 0; i < vertnum; i++)
{
if(strcmp(*argv,buf[i])==0)
{
return i;
}
}
}
//Functions to pack up the matrix before sending
void array_transfer(int vertnum, int number_buf[vertnum][vertnum], char letter_buf[MAXBUFLEN])
{
int j,i;
//printf("send_buf : %s \n",letter_buf);printf("The Central server sent the results to client A\n");
for(i=0 ; i < vertnum; i++)
{
for(j=0 ; j < vertnum; j++)
{
if(number_buf[i][j] == 0)
//letter_buf[(i*vertnum) + j] = "0";
strcat(letter_buf,"0");
if(number_buf[i][j] == 1)
//letter_buf[(i*vertnum) + j] = "1";
strcat(letter_buf,"1");
}
strcat(letter_buf," ");
//printf("j = %d\n",j);
}
strcat(letter_buf,"\n");
//printf("send_buf : %s \n",letter_buf);
}