-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpmaster.c.old
269 lines (245 loc) · 6.21 KB
/
udpmaster.c.old
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
/*
udp master node,reads settings from file,
reads keys from file, sends them to however many nodes set
and waits for sorted keys and then the statistical packet
--------DATA FILE structure---------
<hostname> <port>
---------KEYS------------
<key> --- on in each line
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#define CONF_FILE "data.txt" /*contains node hostname and port, rename as needed */
#define KEYS "keys.txt" /* contains keys to be sorted */
#define OUT_FILE "out.txt"
#define num_of_procs 1 /*number of processors to send data to*/
#define MAX 10 /*max number of keys */
#define min_num 350 /*starting number */
#define max_num 51000
#define receive_port 4001
typedef struct
{
int nums[MAX];
char hostname[100];
int numofkeys;
int numofkeysrecvd ;
struct sockaddr_in pin;
struct hostent *hp;
time_t timeused;
int minrange;
int maxrange;
} Bucket;
typedef struct
{
int numOfKeys;
time_t sorttime;
} KeyData;
int main()
{
Bucket buckets[num_of_procs];
int sd;
int sd2;
FILE *inFP;
char hostname[20];
int portnum;
int i;
time_t totaltime;
int n = min_num;
for ( i = 0;i<num_of_procs;i++) /* initialize data */
{
buckets[i].numofkeys = 0;
buckets[i].numofkeysrecvd =0;
memset(buckets[i].hostname,0,sizeof(buckets[i].hostname));
buckets[i].minrange = n;
n = ((max_num-min_num)/num_of_procs*(i+1))+min_num;
if ( i == num_of_procs -1 )
buckets[i].maxrange = n+1;
else
buckets[i].maxrange = n;
}
/* grab a socket */
if (( sd = socket(AF_INET, SOCK_DGRAM,0)) == -1 )
{
perror("socket");
exit(1);
}
/* read in and set data for each node */
inFP = fopen("data.txt","r");
printf("Reading %s for configuration details.\n",CONF_FILE);
for ( i = 0;i < num_of_procs; i++)
{
fscanf(inFP,"%s %d", hostname, &portnum);
strcpy(buckets[i].hostname,hostname);
if ((buckets[i].hp = gethostbyname(hostname)) == 0)
{
perror("gethostname");
exit(1);
}
memset(&buckets[i].pin,0,sizeof(buckets[i].pin));
buckets[i].pin.sin_family = AF_INET;
buckets[i].pin.sin_addr.s_addr = ((struct in_addr *) ( buckets[i].hp->h_addr)) -> s_addr;
buckets[i].pin.sin_port = htons(receive_port);
}
/****** read numbers ******/
FILE *keysFP;
int keynum;
keysFP = fopen("keys.txt","r");
while ( fscanf(keysFP,"%d",&keynum) != EOF)
{
/* separate numbers into its buckets */
for ( i = 0; i<= num_of_procs;i++)
{
if ( buckets[i].minrange <= keynum && keynum < buckets[i].maxrange )
{
buckets[i].nums[buckets[i].numofkeys]= keynum;
buckets[i].numofkeys++;
}
}
}
int j;
if (( sd2 = socket(AF_INET, SOCK_DGRAM,0)) == -1 )
{
perror("socket");
exit(1);
}
for ( i = 0;i < num_of_procs; i++)
{
for( j = 0; i < buckets[i].numofkeys-1; i++) /* send data in each bucket to perspective node */
{
if (sendto(sd2,&buckets[i].nums[j],sizeof(int),0,(struct sockaddr *) &buckets[i].pin, sizeof(buckets[i].pin)) < 0 )
{
perror("sendto");
exit(1);
}
int tempint = -1;
sendto(sd2,&tempint,sizeof(int),0,(struct sockaddr *) &buckets[i].pin, sizeof(buckets[i].pin));
}
}
struct sockaddr_in sin;
struct timeval tv;
fd_set rfds;
memset(&sin,0,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(receive_port);
if (bind(sd,(struct sockaddr *) &sin,sizeof(sin)) == -1)
{
perror("bind");
exit(1);
}
int structlength = sizeof(sin);
int rc,recvd;
j = 0;
int j2 = 0;
while(1)
{
FD_ZERO(&rfds);
FD_SET(sd,&rfds);
tv.tv_sec = 0;
tv.tv_usec = 500000;
rc = select(FD_SETSIZE, &rfds, (fd_set *) NULL, (fd_set *) NULL, &tv);
if (rc == -1)
{
perror("select");
exit(1);
}
if (rc == 0)
{
printf("... Nothing Yet [%05d]...\n",j2++);
continue;
}
recvd = recvfrom(sd, &keynum, sizeof(int), 0, (struct sockaddr*) &sin, &structlength);
if ( recvd < 0 )
{
perror("recvfrom");
exit(1);
}
if ( recvd > 0 )
{
printf("Received something.");
if ( keynum == -1 ) /* if it is -1 wait for packet statistical packet */
{
struct sockaddr_in tempaddr;
tempaddr.sin_addr.s_addr = sin.sin_addr.s_addr;
int donerec = 1;
KeyData stats;
while(donerec) /* keep looping until it receives statistical packet */
{
int recvdstat;
recvdstat = recvfrom(sd, &stats, sizeof(KeyData), 0, (struct sockaddr*) &sin, &structlength);
if ( recvdstat < 0 )
{
perror("recvdstat");
exit(1);
}
if ( recvdstat > 0 )
{ /* if received data is from node that is to send statistical packet process it */
if ( sin.sin_addr.s_addr == tempaddr.sin_addr.s_addr )
{
printf("received statistical packet.");
for ( i = 0; i < num_of_procs; i++)
{
if ( sin.sin_addr.s_addr == buckets[i].pin.sin_addr.s_addr )
{
buckets[i].numofkeysrecvd = stats.numOfKeys;
buckets[i].timeused = stats.sorttime;
}
char umsg[3] = "ACK";
if ( sendto(sd2,umsg,sizeof(umsg),0,(struct sockaddr *) &buckets[i].pin, sizeof(buckets[i].pin)) < 0 )
{
perror("sendto");
exit(1);
}
donerec = 0;
}
}
/*else
{
for ( i = 0; i < num_of_procs; i++)
{
if ( sin.sin_addr.s_addr == buckets[i].pin.sin_addr.s_addr )
{
buckets[i].nums[buckets[i].numofkeys] = keynum;
buckets[i].numofkeys++;
}
}
memset(&keynum,0,sizeof(int));
}*/
}
}
}
else /* otherise just insert key in correct spot */
{
for ( i = 0; i < num_of_procs; i++)
{
if ( sin.sin_addr.s_addr == buckets[i].pin.sin_addr.s_addr )
{
buckets[i].nums[buckets[i].numofkeys] = keynum;
buckets[i].numofkeys++;
}
}
memset(&keynum,0,sizeof(int));
}
}
}
FILE *outFP;
outFP = fopen(OUT_FILE,"w+");
printf("PROC_ID SORT_TIME KEYS_RECEIVED KEYS_SENT");
for ( i = 0; i<num_of_procs; i++) /* print data */
{
for ( j = 0;j < buckets[i].numofkeysrecvd; j++)
{
fprintf(outFP,"%d\n",buckets[i].nums[j]);
printf(" %d %u %d %d\n",i,buckets[i].timeused,buckets[i].numofkeysrecvd,buckets[i].numofkeys);
}
}
close(sd);
sleep(1);
return 0;
}