Advertisement
vinifr

alteracoes

Aug 7th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. // funçao de thread que verifica quais Nos estao inativos por timer_interv segundos
  2. void *erase_nodes( void *ptr )
  3. {
  4.     struct timespec t1;
  5.     struct timespec t2;
  6.     struct timespec deltaT;             // diferenca entre o timestamp atual e anterior
  7.     std::list<nodes>::iterator it;      // variavel para percorrer a lista
  8.    
  9.     while(1)
  10.     {
  11.         //usleep(10);       // delay de mais ou menos 1 segundo, depende do processador
  12.        
  13.         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);  // pega timestamp atual
  14.        
  15.         //std::cout << "erase_nodes:" << timer_++ << endl;                 
  16.         for (it = nodes_RTS.begin(); it != nodes_RTS.end(); it++) { // percorre a lista
  17.             //pthread_mutex_lock(&lock2);       // protege a variavel t1.tv_sec contra outras threads
  18.             t1.tv_nsec = (*it).t1;                                 
  19.             //pthread_mutex_unlock(&lock2); // libera a variavel
  20.            
  21.             deltaT.tv_nsec = diff(t1, t2).tv_nsec;  // armazena o delta dos timestamps
  22.            
  23.             if(deltaT.tv_nsec > timer_interv)   // nenhum pacote foi enviado nos ultimos timer_interv segundos
  24.             {
  25.                 std::cout << "No rm:" << (*it).num_node << endl;
  26.                 //std::cout << "deltaT:" << deltaT.tv_nsec << endl;
  27.                 it = nodes_RTS.erase(it);   // remove Noh
  28.             }
  29.         }
  30.     }
  31. }
  32.  
  33. // verifica se Noh ja existe e adiciona a lista nodes_RTS
  34. void Mac802_11t::add_list_RTS(int node)
  35. {
  36.     bool add = true;
  37.     struct timespec t1;
  38.     std::list<nodes>::iterator it;                          // variavel para percorrer a lista: it
  39.     list_rts = (struct nodes*)malloc(sizeof(struct nodes)); // variavel para guardar informaçoes sobre o Noh na lista
  40.    
  41.     //percorre a lista
  42.     for (it = nodes_RTS.begin(); it != nodes_RTS.end(); it++)
  43.     {
  44.         if((*it).num_node == node) { // verifica se Noh ja existe na lista
  45.             add = false;           
  46.             clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
  47.             (*it).t1 = t1.tv_nsec;              // guarda timestamp do Noh         
  48.             break;          // sai porque ja achou o noh procurado
  49.         }
  50.     }
  51.    
  52.     if( add ) // o Noh ainda nao existe na lista ou foi removido
  53.     {
  54.         add = false;
  55.        
  56.         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
  57.         list_rts->t1 = t1.tv_nsec;      // guarda timestamp do Noh     
  58.         list_rts->num_node = node;      // guarda o numero do Noh
  59.         nodes_RTS.push_back(*list_rts); // adiciona o Noh a lista
  60.        
  61.         std::cout << "No " << list_rts->num_node <<
  62.             " adicionado" //<< endl;
  63.             << " N:" << nodes_RTS.size() //<< endl;
  64.         << " Fator atual: " << calc_factor() << endl;
  65.     }
  66.     free(list_rts);     // libera variavel
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement