-
Notifications
You must be signed in to change notification settings - Fork 1
/
Upload_Environment.c
502 lines (318 loc) · 17.6 KB
/
Upload_Environment.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
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
492
493
494
495
496
497
498
499
500
501
502
/* Client and Server side implementation of RUFT : UPLOAD ENVIRONMENT */
#pragma once
#include "header.h"
#include "Reliable_Data_Transfer.c"
#include "time_toolbox.c"
#define POOLSIZE 10
pthread_mutex_t rcv_window_mutex = PTHREAD_MUTEX_INITIALIZER;
/*
SERVER SIDE implementation of RUFT Upload Environment.
The following structures are used to receive data from clients uploading files on server's directory.
*/
struct upload_block {
int sockfd; // Socket descriptor for this upload block.
int identifier; // Identifier code for this upload block.
char filepath[MAXLINE]; // Current file pathname ( variable for each upload instance).
char ACK[ACK_SIZE]; // Buffer used to write and send acknowledgements.
struct sockaddr_in *clientaddr; // Current client's address.
int addr_len; // Current client address' lenght.
pthread_t uploader; // Uploader thread identifier.
pthread_t writer; // Writer thread identifier.
rw_slot *rcv_wnd; // Pointer to the current block's receiving window.
char uploading; // Flag that represents the current state of this upload block (waiting or uploading).
int sem_id; // Semaphore array identifier. This is used to handle uploader and writer thread's start.
struct upload_block *next; // Pointer to the next block of Upload Environment's pool.
}; struct upload_block *upload_environment;
/*
This is a thread function. The Uploader thread is always coupled with a Writer thread.
Each Uploader has the responsibility to operate an upload of a file from a remote client.
The Uploader receives data packets and transmits acknowledgements, in order to ensure reliable file transfer.
Each Uploader communicates with its Writer, making him transcribing all packet's data in a persistent file on server's directory.
After the Upload is complete, this thread is blocked and waits to be signaled for a following new upload occurrence.
*/
void * uploader( void * upload_block ) {
int ret, num, counter = 0;
struct upload_block *block = (struct upload_block *) upload_block;
char rcv_buffer[MAXLINE];
struct sembuf oper;
start:
counter = 0;
oper.sem_flg = 0;
oper.sem_num = 0;
oper.sem_op = -1;
ret = semop( block -> sem_id, &oper, 1 );
if (ret == -1) {
printf("\n Error in function semop (uploader). errno %d", errno );
goto start;
}
// Upload File.
printf("\n PREPEARING TO UPLOAD...\n Getting informations about the uploading file..."); fflush(stdout);
block -> rcv_wnd = get_rcv_window();
block -> addr_len = sizeof( struct sockaddr_in );
/* Receive the file size and the identifier of server worker matched to this download instance. */
ret = recvfrom( ( block -> sockfd ), (char *) rcv_buffer, MAXLINE, MSG_WAITALL,
(struct sockaddr *) &( block -> clientaddr ), &( block -> addr_len ) );
if (ret <= 0) Error_("Error in function : recvfrom (downloader).", 1);
printf("\n RECEIVED infos : %s", rcv_buffer ); fflush(stdout);
/* Initiate the exit-condition's values for the next cycle. */
char *idtf;
idtf = strtok( rcv_buffer, "/" );
block -> identifier = atoi( idtf );
printf("\n - WORKER ID : %d", block -> identifier ); fflush(stdout);
char *filesz;
filesz = strtok( NULL, "/" );
int filesize = atoi( filesz );
printf("\n - SIZE : %d", filesize ); fflush(stdout);
memset( rcv_buffer, 0, MAXLINE);
/* Extract a seed from current time and set it for the rand function. */
srand((unsigned int)time(0));
do{
printf("\n UPLOAD IN PROGRESS... "); fflush(stdout);
loss:
ret = recvfrom( block -> sockfd, (char *) rcv_buffer, MAXLINE, MSG_WAITALL,
(struct sockaddr *) &( block -> clientaddr ), &( block -> addr_len ) );
if (ret <= 0) Error_("Error in function : recvfrom (uploader).", 1);
char *idtf; idtf = strtok( rcv_buffer, "/");
int identifier = atoi( idtf );
num = ( rand() % 100 ) + 1;
if( num <= LOSS_PROBABILITY ) goto loss;
if ( identifier == ( block -> identifier ) ) {
/* THIS IS A CRITIAL SECTION FOR RECEIVING WINDOWS ACCESS ON WRITING.
DOWNLOADER THREAD TAKES A TOKEN FROM MUTEX TO RUN THIS CODE BLOCK. */
if ( pthread_mutex_lock( &rcv_window_mutex ) == -1 )
Error_("Error in function : pthread_mutex_lock (uploader).", 1);
rw_slot *wnd_tmp = ( block -> rcv_wnd );
char *sn = strtok( NULL, "/" );
int sequence_number = atoi( sn );
printf("\033[01;34m");
printf("\n --> ARRIVED PACKET WITH SEQUENCE NUMBER : %d .", sequence_number); fflush(stdout);
printf("\033[0m");
for (int i = 0; i < WINDOW_SIZE; i++) {
printf("\n wnd_tmp->sequence_number=%d sequence_number=%d",
( wnd_tmp -> sequence_number ), sequence_number); fflush(stdout);
if ( wnd_tmp -> sequence_number == sequence_number ) {
/* Send an ACKNOWLEDGMENT to the RUFT Server Side. */
sprintf( ( block -> ACK ), "%d/%d/", identifier, sequence_number );
printf("\033[01;32m");
printf(" SENDING ACK : %s", block -> ACK);
printf("\033[0m");
ret = sendto( block -> sockfd, (const char *) ( block -> ACK ), MAXLINE, MSG_CONFIRM,
(const struct sockaddr *) &( block -> clientaddr ), block -> addr_len );
if (ret <= 0) {
printf("\n Error in function : sendto (uploader). errno = %d ", errno );
exit(-1);
}
/* Update rcv_window's slot status. */
wnd_tmp -> status = RECEIVED;
wnd_tmp -> packet = malloc( sizeof(char) * MAXLINE );
if (wnd_tmp -> packet == NULL) Error_( "Error in function : malloc (downloader).", 1);
if ( sprintf( ( wnd_tmp -> packet ), "%s", ( rcv_buffer + ( strlen(idtf) + strlen(sn) + 2 ) ) ) == -1 )
Error_( "Error in function : sprintf (uploader).", 1);
counter += strlen( wnd_tmp -> packet);
if ( ( wnd_tmp -> is_first ) == '1' ) {
printf( "\n SENDING SIGNAL TO WRITER FOR PACKET %d", sequence_number ); fflush(stdout);
/* If this is the first slot of the window, then alert the writer about it (SIGUSR2)
so that it could slide the rcv_window on. */
pthread_kill( block -> writer, SIGUSR2 );
if ( pthread_mutex_unlock( &rcv_window_mutex ) == -1 )
Error_("Error in function : pthread_mutex_unlock (uploader).", 1);
}
break;
}
wnd_tmp = wnd_tmp -> next;
}
if ( pthread_mutex_unlock( &rcv_window_mutex ) == -1 )
Error_("Error in function : pthread_mutex_unlock (uploader).", 1);
/* END OF CRITICAL SECTION FOR RECEIVING WINDOW'S ACCESS. */
}
printf("\n counter = %d, filesize = %d", counter, filesize); fflush(stdout);
memset( rcv_buffer, 0, strlen(rcv_buffer) );
} while( counter < filesize );
printf("\033[01;34m");
printf("\n UPLOAD COMPLETE."); fflush(stdout);
printf("\033[0m");
block -> uploading = '0';
free( block -> rcv_wnd);
goto start;
}
/*
This is a thread function. The Writer thread is always coupled with an Uploader thread.
Each Writer has the responsibility of writing data coming from the client in a persistent file on server's directory.
After the Upload is complete, this thread is blocked and waits to be signaled for a following new upload occurrence.
*/
void * writer( void * upload_block ) {
int ret;
struct upload_block *block = (struct upload_block *) upload_block;
sigset_t set; struct sembuf oper;
start:
oper.sem_num = 1;
oper.sem_op = -1;
oper.sem_flg = 0;
ret = semop( block -> sem_id, &oper, 1 );
if (ret == -1) {
printf("\n Error in funciton : semop (writer). errno %d", errno);
goto start;
}
/* Temporarily block SIGUSR2 signal occurrences. */
sigemptyset( &set );
sigaddset( &set, SIGUSR2);
sigprocmask( SIG_BLOCK, &set, NULL );
int file_descriptor, counter = 0;
/* Create the new file in client's directory or truncate an existing one with the same pathname, to start download. */
file_descriptor = open( ( block -> filepath ), O_RDWR | O_CREAT , 0660 );
if (file_descriptor == -1) {
printf("\n Error in function : open (writer). errno = %d", errno);
pthread_exit(NULL);
}
printf("\n WRITER IS ENTERING THE CYCLE."); fflush(stdout);
do {
if ( block -> uploading == '0') {
printf("\n All file has been written on server's directory."); fflush(stdout);
break;
}
printf("\n WRITER IS in THE CYCLE."); fflush(stdout);
/* Be ready to be awaken by SIGUSR2 occurrence. Go on pause. */
sigpending(& set);
if ( sigismember( &set, SIGUSR2 ) ) {
signal( SIGUSR2, wake_up );
sigprocmask( SIG_UNBLOCK, &set, NULL );
printf("\n SIGUSR2 pending on mask! goto action."); fflush(stdout);
goto action;
}
signal( SIGUSR2, wake_up );
sigemptyset( &set );
sigaddset( &set, SIGUSR2);
sigprocmask( SIG_UNBLOCK, &set, NULL );
printf("\n SIGUSR2 UNBLOCKED"); fflush(stdout);
pause();
action:
printf("\n SIGUSR2 BLOCKED"); fflush(stdout);
/* Temporarily block SIGUSR2 signal occurrences. */
sigprocmask( SIG_BLOCK, &set, NULL );
printf( "\n WRITER AWAKED" ); fflush(stdout);
{
/* THIS IS A CRITIAL SECTION FOR RECEIVING WINDOWS ACCESS ON WRITING.
WRITER THREAD TAKES A TOKEN FROM MUTEX TO RUN THIS CODE BLOCK. */
if ( pthread_mutex_lock( &rcv_window_mutex ) == -1 ) Error_("Error in function : pthread_mutex_lock (writer).", 1);
rw_slot *wnd_tmp = ( block -> rcv_wnd );
rw_slot *curr_first = ( block -> rcv_wnd );
while( ( curr_first -> status != RECEIVED ) && ( curr_first -> is_first != '1') ) {
curr_first = ( curr_first -> next );
}
while ( curr_first -> status == RECEIVED ) {
lseek( file_descriptor, 0, SEEK_END );
/* Write the packet within the new file in client's directory. */
ret = write( file_descriptor, ( curr_first -> packet ), strlen( curr_first -> packet ) );
if ( ret == -1) Error_( "Error in function : write (thread writer).", 1);
printf("\033[01;34m");
printf( "\n PACKET %d CONTENT WRITTEN ON FILE : %s. %d BYTES WRITTEN.",
( curr_first -> sequence_number ), ( block -> filepath ), ret ); fflush(stdout);
printf("\033[0m");
/* Slide the receiving window on. */
( curr_first -> sequence_number ) += WINDOW_SIZE;
( curr_first -> status ) = WAITING;
memset( ( curr_first -> packet ), 0, sizeof( curr_first -> packet ) );
curr_first -> is_first = '0';
curr_first -> next -> is_first = '1';
curr_first = ( curr_first -> next );
block -> rcv_wnd = curr_first;
printf("\n WINDOW SLIDED ON"); fflush(stdout);
}
if ( pthread_mutex_unlock( &rcv_window_mutex ) == -1 ) Error_("Error in function : pthread_mutex_unlock (writer).", 1);
printf("\n WRITER HAS COMPLETED ITS TASK AND GOES TO SLEEP."); fflush(stdout);
/* END OF THE CRITICAL SECTION. */
}
} while (1);
block -> uploading = '0';
goto start;
}
/*
By this function, RUFT Server initiates the Upload Environment.
It consists in a thread pool of size POOLSIZE: once initialized, POOLSIZE 'uploader' and 'writer' threads are launched,
and wait for new upload occurrences. A new upload occurrence is signaled by function "start_upload".
*/
int initialize_upload_environment() {
int ret;
upload_environment = malloc( sizeof( struct upload_block ) );
if (upload_environment == NULL ) {
printf("Error in function : malloc (init_upload_environment). errno %d", errno );
exit(1);
}
struct upload_block *tmp;
tmp = upload_environment;
for ( int i = 0; i < POOLSIZE; i++ ) {
if ( ( ( tmp -> sockfd ) = socket( AF_INET, SOCK_DGRAM, 0 ) ) < 0 ) { //creating block's socket file descriptor
perror("\n socket creation failed (init new block).");
return -1;
}
tmp -> sem_id = semget( IPC_PRIVATE, 2, O_CREAT | 0660 );
if (tmp -> sem_id == -1) {
printf("\n Error in function : semget (itinialize_upload_environment). errno %d", errno);
return -1;
}
ret = semctl( tmp -> sem_id, 0, SETVAL, 0 );
if (ret == -1) {
printf("\n Error in function : semctl (initialize_upload_instance). errno %d", errno);
return -1;
}
ret = semctl( tmp -> sem_id, 1, SETVAL, 0 );
if (ret == -1) {
printf("\n Error in function : semctl (initialize_upload_instance). errno %d", errno);
return -1;
}
tmp -> identifier = i;
tmp -> uploading = '0';
ret = pthread_create( &( tmp -> uploader ), NULL, uploader, ( void * ) tmp );
if (ret == -1) {
printf("Error in function : pthread_create (initialize_upload_environment). errno %d", errno );
exit(1);
}
ret = pthread_create( &( tmp -> writer ), NULL, writer, (void *) tmp );
if (ret == -1) {
printf("Error in function : pthread_create (initialize_upload_environment). errno %d", errno );
exit(1);
}
tmp -> next = malloc( sizeof( struct upload_block ) );
tmp = tmp -> next;
}
return 0;
}
/*
This function is called by RUFT Server's receptionist, to serve a new PUT (upload) request.
By this function, a waiting slot of upload environment is updated with the new request's informations, and the respective
couple of waiting uploader&writer threads is signaled to serve the upload request.
*/
int start_upload( char * filepath, struct sockaddr_in *clientaddress, int len ) {
int ret; struct upload_block *tmp;
char buffer[MAXLINE];
struct sembuf oper; oper.sem_flg = 0; oper.sem_op = 1;
redo: tmp = upload_environment;
while( ( tmp -> uploading ) != '0') {
if (tmp -> next == NULL ) goto redo;
tmp = ( tmp -> next );
}
sprintf( tmp -> filepath, "%s", filepath );
tmp -> clientaddr = clientaddress;
tmp -> addr_len = len;
sprintf( buffer, "%d/", ( tmp -> identifier ) );
printf("\n Buffer content : %s", buffer); fflush(stdout);
ret = sendto( ( tmp -> sockfd ), (char *) buffer, MAXLINE, MSG_CONFIRM, (struct sockaddr *) ( tmp -> clientaddr ), ( tmp -> addr_len ) );
if (ret <= 0) {
printf("\n Error in function : sendto (start_upload). errno %d", errno );
return -1;
}
tmp -> uploading = '1';
oper.sem_num = 0;
ret = semop( tmp -> sem_id, &oper, 1 );
if (ret == -1) {
printf("\n Error in function semop (start_upload). errno %d", errno );
return -1;
}
oper.sem_num = 1;
ret = semop( tmp -> sem_id, &oper, 1 );
if (ret == -1) {
printf("\n Error in function semop (start_upload). errno %d", errno );
return -1;
}
return 0;
}