treapset *treap_find(treapset *root, TREAPTYPE value) { if (root == NULL) return NULL; if (value < root->data) return treap_find(root->left, value); if (value > root->data) return treap_find(root->right, value); return root; }
void *consume_packets(rwindow *rwin) { // First packet number that we receive is one with SEQ #1 int next_seq = 1; packet_t *pkt; #ifdef DEBUG char file_name[300]; // Open the file for writing sprintf(file_name, "%s.out", "test"); FILE *pf = fopen(file_name, "w"); ASSERT(pf); #endif double sleep_time; BOOL last_pkt_found = FALSE; do { pthread_mutex_lock(rwin->mutex); treap_node *tn = treap_find(&rwin->t_rwin, next_seq); while (tn != NULL) { pkt = (packet_t *) tn->data; treap_delete(&rwin->t_rwin, next_seq); #if DEBUG int treap_sz = treap_size(&rwin->t_rwin); VERBOSE("==== Read packet %d with datalen %d and flags %x, treap_sz: %d ====\n", next_seq, pkt->datalen, pkt->flags, treap_sz); int ret = fwrite(pkt->data, pkt->datalen, 1, pf); VERBOSE("fwrite returned with ret = %d\n", ret); #else if (!(pkt->flags & FLAG_FIN)) { INFO("\n==== BEGIN PACKET #%d DATA ===" "\n%s" "\n==== END PACKET #%d DATA ===\n", pkt->seq, pkt->data, pkt->seq); } #endif if (pkt->flags & FLAG_FIN) { last_pkt_found = TRUE; break; } next_seq++; tn = treap_find(&rwin->t_rwin, next_seq); } pthread_mutex_unlock(rwin->mutex); if (last_pkt_found) { break; } sleep_time = -1.0 * cargs->mean * log(drand48()); #ifdef DEBUG VERBOSE("sleep_time: %lf\n", sleep_time); #endif usleep(sleep_time * 1000); } while (1); #ifdef DEBUG fclose(pf); #endif return NULL; }