/*-------------------------------------------------------------------*/ void message_rx( message_t *msg, distance_measurement_t *d ) { uint8_t received_word = msg->data[0]; uint8_t cur_distance = estimate_distance(d); if( cur_distance > 100 ) { return; } /* Check if the received word is within the inventory */ int i = 0; bool word_known = false; for( i = 0; i < inventory_size; ++i ) { if( received_word == inventory[i] ) { word_known = true; break; } } /* if the word is known, remove all other words from the inventory, otherwise insert it */ if( word_known ) { inventory[0] = received_word; inventory_size = 1; } else { inventory[inventory_size] = received_word; inventory_size += 1; } }
// ---------------------------------------------------------------------- bool LocalizationGPSfreeLCSModule:: process_gpsfree_lcs_init_message( const LocalizationGPSfreeLCSInitMessage& lgflcsim ) throw() { const Node& source = lgflcsim.source(); Vec source_pos = lgflcsim.source().real_position(); double distance = estimate_distance( source, node() ); last_useful_msg_ = simulation_round(); neighborhood_w().update_neighbor( source, distance ); return true; }
// ---------------------------------------------------------------------- bool LocalizationIterLaterationModule:: process_iter_lateration_message( const LocalizationIterLaterationMessage& lilm ) throw() { if ( owner().is_anchor() || state_ == il_finished ) return true; const Node& source = lilm.source(); Vec source_real_pos = lilm.source().real_position(); Vec source_est_pos = lilm.source().est_position(); double distance = estimate_distance( source, node() ); neighborhood_w().update_neighbor( source, distance ); localization::NeighborhoodIterator it = neighborhood_w().find_w( source ); it->second->set_pos( source_est_pos ); it->second->set_confidence( lilm.confidence() ); return true; }
void message_rx(message_t *msg, distance_measurement_t *distance_measurement){ msg_rcvd = 1; curr_dist = estimate_distance(distance_measurement); }
/* To be called once we've found somewhere close enough to (but never past) the start time will do a linear search to find the start time, and then print everything from there to the end pass in the regex used for the dates, the file_time_range search params, and the log file. returns 0 on success, 1 on error */ int print_file_range(FILE *log_file, file_time_range *range, regex_t *date_reg) {/*{{{*/ char buffer[BUFFER_SIZE+1]; char *start_ptr = NULL; my_time cur_time; int cur_day; char *buf_ptr; int end_not_found = 1; int reg_err; regmatch_t date_match[2]; // find the point that the first line we care about starts at // by searching regex match by regex match while(!start_ptr){ if(0 >= fread(buffer, 1, BUFFER_SIZE, log_file)){ if(feof(log_file)){ fprintf(stderr, "reached eof while searching for first date?? Talk to Thomas.\n"); break; } break; } else buffer[BUFFER_SIZE] = '\0'; buf_ptr = buffer; while(1){ // keep matching until we find the first match reg_err = regexec(date_reg, buf_ptr, 2, date_match, 0); if(reg_err == REG_NOMATCH) break; else if(reg_err) printf("forever alone\n"); else{ sscanf(buf_ptr + date_match[1].rm_so, "%d%d:%d:%d", &cur_day, &(cur_time.hour), &(cur_time.min), &(cur_time.sec)); cur_time.day = (cur_day == range->start_day) ? 0 : 1; if(time_diff(&cur_time, range->time1) >= 0){ start_ptr = buf_ptr + date_match[0].rm_so; break; } else buf_ptr += date_match[0].rm_eo; } } } // by now we have the start time, so find the end time // if the end time is in the current buffer, then put a '\0' after it // print the whole buffer while(end_not_found){ // estimate whether the end is in this buffer. If it is clearly past, check the end of the buffer off_t estimate = estimate_distance(range, range->time2); // check the end of the buffer to see if it is still in the time range // if it is, skip to printing the buffer out if(estimate > BUFFER_SIZE){ buf_ptr = buffer + BUFFER_SIZE - MAX_LINE_LEN; while(1){ reg_err = regexec(date_reg, buf_ptr, 2, date_match, 0); if(reg_err == REG_NOMATCH) break; else if(reg_err) printf("forever alone\n"); else{ sscanf(buf_ptr + date_match[1].rm_so, "%d%d:%d:%d", &cur_day, &(cur_time.hour), &(cur_time.min), &(cur_time.sec)); cur_time.day = (cur_day == range->start_day) ? 0 : 1; // if somewhere in this buffer there's a time past the end of the time range, just use linear search if(time_diff(&cur_time, range->time2) > 0){ break; } else buf_ptr += date_match[0].rm_eo; } } // we now either have the last time in the buffer loaded into cur_time // or a time that is outside the range we want // no matter what if it's within the range we want, we should just print it. if(time_diff(range->time2, &cur_time) > 0) goto print_file_range_print_buffer; } // linear search for the end buf_ptr = start_ptr; while(1){ reg_err = regexec(date_reg, buf_ptr, 2, date_match, 0); if(reg_err == REG_NOMATCH) break; else if(reg_err) printf("forever alone\n"); else{ sscanf(buf_ptr + date_match[1].rm_so, "%d%d:%d:%d", &cur_day, &(cur_time.hour), &(cur_time.min), &(cur_time.sec)); cur_time.day = (cur_day == range->start_day) ? 0 : 1; if(time_diff(&cur_time, range->time2) > 0){ // if we're strictly after time2 buf_ptr[date_match[0].rm_so] = '\0'; // termitate this string end_not_found = 0; break; } else buf_ptr += date_match[0].rm_eo; } } print_file_range_print_buffer: printf("%s",start_ptr); if(end_not_found){ if(0 >= fread(buffer,1,BUFFER_SIZE,log_file)){ fprintf(stderr, "hit eof"); if(feof(log_file)) break; else // not sure if I should do something else on a diff err... break; } buffer[BUFFER_SIZE] = '\0'; start_ptr = buffer; } } return 0; }/*}}}*/
/* Estimates the location, goes to it, calls self base case: seems to be close enough to the file Assumes we are currently at range->off1 range->ct1 pass in buffer to avoid excess memory allocation probably should have just made it a loop instead */ int rec_search_file(FILE *log_file, file_time_range *range, regex_t *date_reg, char *buffer) {/*{{{*/ my_time cur_time; int cur_day; off_t cur_off; off_t min_dist = (range->avg_sec_size / 2 > MIN_PRINT_RANGE) ? range->avg_sec_size/2 : MIN_PRINT_RANGE; regmatch_t date_match[2]; off_t est_dist; // find out where to seek to if(strict_binary_search) cur_off = range->off1/2 + range->off2/2; else cur_off = range->off1 + estimate_distance(range, range->pre_time1); // if we are jumping by a trivial amount, just bump it up a bit if(0 < cur_off - range->off1 && cur_off - range->off1 < min_dist && range->off2 - range->off1 > 2 * min_dist) cur_off = range->off1 + min_dist; if(0 > cur_off - range->off1 && cur_off - range->off1 > min_dist && range->off2 - range->off1 > 2 * min_dist) cur_off = range->off1 - min_dist; // might happen if you're searching for the beginning of a file if(cur_off < 0) cur_off = 0; fseeko(log_file, cur_off, SEEK_SET); // get time where we are if(0 >= fread(buffer, 1, MAX_LINE_LEN, log_file)){ if(feof(log_file)) fprintf(stderr, "hit eof at offset %lld\n", cur_off); else fprintf(stderr, "read error!\n"); return 1; } else buffer[MAX_LINE_LEN] = '\0'; int reg_err = regexec(date_reg, buffer, 2, date_match, 0); if(reg_err == REG_NOMATCH){ fprintf(stderr, "REGEX! Y U NO MATCH?\n"); return 1; } else if(reg_err){ printf("forever alone\n"); return 1; } else{ sscanf(buffer + date_match[1].rm_so, "%d%d:%d:%d", &cur_day, &(cur_time.hour), &(cur_time.min), &(cur_time.sec)); cur_time.day = (cur_day == range->start_day) ? 0 : 1; } // decide which params to pass to the next recursive call file_time_range new_range; // if cur_time is ahead of time1, then we make it our new ct2 // otherwise we make it our new ct1 if(time_diff(&cur_time, range->time1) >= 0) set_range(range, &new_range, range->ct1, &cur_time, range->off1, cur_off); else set_range(range, &new_range, &cur_time, range->ct2, cur_off, range->off2); // REAL BASE CASE! // if the estimated distance is greater than 0 (we have to be before the time1) // and it's less than the minimum print range, then print // - the only case where we accept an est_dist of 0 is at the beginning of the file est_dist = estimate_distance(&new_range, range->time1); if(((0 < est_dist) && (est_dist < MIN_PRINT_RANGE)) || ((0 == est_dist) && (0 == new_range.off1))){ fseeko(log_file, new_range.off1, SEEK_SET); print_file_range(log_file, &new_range, date_reg); return 0; } // recurse return rec_search_file(log_file, &new_range, date_reg, buffer); }/*}}}*/