Advertisement
Gianmarco

ack keeper

Aug 17th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. void * acknowledgment_keeper( void * _block ){
  2.  
  3.     signal( SIGUSR1, erase );
  4.  
  5.  
  6.     int     ret,            len = sizeof( struct sockaddr_in );
  7.  
  8.     char    *id,            *seq_num;
  9.  
  10.  
  11.     char    *buffer = malloc( sizeof( char ) * MAXLINE );
  12.  
  13.     block   *myblock = ( block * ) _block;
  14.  
  15.     worker  *w_tmp = ( myblock -> workers );
  16.  
  17.     sw_slot *sw_tmp;
  18.  
  19.     printf("\n ACKNOWLEDGMENT KEEPER RUNNING FOR BLOCK MATCHED TO FILE : %s\n ", myblock -> filename ); fflush(stdout);
  20.  
  21.     do {
  22.  
  23.         /*  Receive a packet (acknowledgments) from related block's socket.  */
  24.  
  25.         struct sockaddr_in     client_address;
  26.  
  27.         memset( buffer, 0, sizeof( buffer ) );
  28.  
  29.         ret = recvfrom( myblock -> server_sock_desc, (char *) buffer, MAXLINE , MSG_WAITALL, ( struct sockaddr *) &client_address, &len);
  30.         if (ret <= 0) {
  31.             printf("\n ACK KEEPER EXITS...");
  32.             pthread_exit( NULL );
  33.         }
  34.  
  35.         printf("\n ACK received : ");
  36.  
  37.         /*  Parse the packet to keep separated the identifier and sequence number fields.  */
  38.  
  39.         id = strtok( buffer, "/" );
  40.         if (ret == -1)      Error_("Error in function sprintf (acknowledgment_demultiplexer).", 1);
  41.  
  42.         seq_num = strtok( NULL, "/" ) ;
  43.         if (ret == -1)      Error_("Error in function sprintf (acknowledgment_demultiplexer).", 1);
  44.  
  45.  
  46.         /*  Find the block's worker with identifier as specified on ACK. */
  47.  
  48.         while ( ( w_tmp -> identifier ) != atoi(id) ) {
  49.  
  50.             w_tmp = ( w_tmp -> next );
  51.  
  52.         }
  53.  
  54.  
  55.         /*  Once the worker is found, find the worker's window's slot with sequence number as specified on ACK  */
  56.  
  57.         sw_tmp = ( w_tmp -> sliding_window_slot_ );
  58.  
  59.         while ( ( sw_tmp -> sequence_number ) != atoi(seq_num) ) {
  60.  
  61.             sw_tmp = ( sw_tmp -> next );
  62.  
  63.         }
  64.  
  65.  
  66.         {
  67.             pthread_mutex_lock( &( w_tmp -> s_window_mutex) );
  68.  
  69.             /*  THIS IS A CRITICAL SECTION FOR ACCESS ON THE SLIDING WINDOW (shared by ack-keeper thread and the relative worker).
  70.                 Update worker window's slot's status from SENT to ACKED.
  71.                 If the slot is the first of the sliding window, forward a SIGUSR2 signal to worker-thread to get the window sliding on. */
  72.  
  73.             if ( ( sw_tmp -> status ) != SENT )  {
  74.                 printf("\n Error in acknowledgemnt keeper : unexpected window status = %d", sw_tmp ->status);
  75.             }
  76.  
  77.             sw_tmp -> status = ACKED;
  78.  
  79.             printf(" %d", sw_tmp -> sequence_number );                  fflush(stdout);
  80.  
  81.             //current_timestamp( sw_tmp -> acked_timestamp );
  82.  
  83.             if ( ( sw_tmp -> is_first ) == '1' )    {
  84.                 pthread_kill( ( w_tmp -> tid ), SIGUSR2 );
  85.                 printf("\n SIGNAL THE WORKER TO SLIDE ON.");            fflush(stdout);
  86.             }
  87.  
  88.             pthread_mutex_unlock( &( w_tmp -> s_window_mutex) );
  89.  
  90.         }        
  91.  
  92.  
  93.     } while ( myblock -> eraser != '1');
  94.  
  95.     pthread_exit( NULL );
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement