Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void * acknowledgment_keeper( void * _block ){
- signal( SIGUSR1, erase );
- int ret, len = sizeof( struct sockaddr_in );
- char *id, *seq_num;
- char *buffer = malloc( sizeof( char ) * MAXLINE );
- block *myblock = ( block * ) _block;
- worker *w_tmp = ( myblock -> workers );
- sw_slot *sw_tmp;
- printf("\n ACKNOWLEDGMENT KEEPER RUNNING FOR BLOCK MATCHED TO FILE : %s\n ", myblock -> filename ); fflush(stdout);
- do {
- /* Receive a packet (acknowledgments) from related block's socket. */
- struct sockaddr_in client_address;
- memset( buffer, 0, sizeof( buffer ) );
- ret = recvfrom( myblock -> server_sock_desc, (char *) buffer, MAXLINE , MSG_WAITALL, ( struct sockaddr *) &client_address, &len);
- if (ret <= 0) {
- printf("\n ACK KEEPER EXITS...");
- pthread_exit( NULL );
- }
- printf("\n ACK received : ");
- /* Parse the packet to keep separated the identifier and sequence number fields. */
- id = strtok( buffer, "/" );
- if (ret == -1) Error_("Error in function sprintf (acknowledgment_demultiplexer).", 1);
- seq_num = strtok( NULL, "/" ) ;
- if (ret == -1) Error_("Error in function sprintf (acknowledgment_demultiplexer).", 1);
- /* Find the block's worker with identifier as specified on ACK. */
- while ( ( w_tmp -> identifier ) != atoi(id) ) {
- w_tmp = ( w_tmp -> next );
- }
- /* Once the worker is found, find the worker's window's slot with sequence number as specified on ACK */
- sw_tmp = ( w_tmp -> sliding_window_slot_ );
- while ( ( sw_tmp -> sequence_number ) != atoi(seq_num) ) {
- sw_tmp = ( sw_tmp -> next );
- }
- {
- pthread_mutex_lock( &( w_tmp -> s_window_mutex) );
- /* THIS IS A CRITICAL SECTION FOR ACCESS ON THE SLIDING WINDOW (shared by ack-keeper thread and the relative worker).
- Update worker window's slot's status from SENT to ACKED.
- If the slot is the first of the sliding window, forward a SIGUSR2 signal to worker-thread to get the window sliding on. */
- if ( ( sw_tmp -> status ) != SENT ) {
- printf("\n Error in acknowledgemnt keeper : unexpected window status = %d", sw_tmp ->status);
- }
- sw_tmp -> status = ACKED;
- printf(" %d", sw_tmp -> sequence_number ); fflush(stdout);
- //current_timestamp( sw_tmp -> acked_timestamp );
- if ( ( sw_tmp -> is_first ) == '1' ) {
- pthread_kill( ( w_tmp -> tid ), SIGUSR2 );
- printf("\n SIGNAL THE WORKER TO SLIDE ON."); fflush(stdout);
- }
- pthread_mutex_unlock( &( w_tmp -> s_window_mutex) );
- }
- } while ( myblock -> eraser != '1');
- pthread_exit( NULL );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement