Calculates and returns the current threshold value for the buffer's split based on the average number of bytes transferred per transfer opportunity and the hop counts of the messages in the buffer. Method is public only to make testing easier. @return current threshold value (hop count) for the buff
()
| 394 | * @return current threshold value (hop count) for the buffer's split |
| 395 | */ |
| 396 | public int calcThreshold() { |
| 397 | /* b, x and p refer to respective variables in the paper's equations */ |
| 398 | int b = this.getBufferSize(); |
| 399 | int x = this.avgTransferredBytes; |
| 400 | int p; |
| 401 | |
| 402 | if (x == 0) { |
| 403 | /* can't calc the threshold because there's no transfer data */ |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | /* calculates the portion (bytes) of the buffer selected for priority */ |
| 408 | if (x < b/2) { |
| 409 | p = x; |
| 410 | } |
| 411 | else if (b/2 <= x && x < b) { |
| 412 | p = Math.min(x, b-x); |
| 413 | } |
| 414 | else { |
| 415 | return 0; // no need for the threshold |
| 416 | } |
| 417 | |
| 418 | /* creates a copy of the messages list, sorted by hop count */ |
| 419 | ArrayList<Message> msgs = new ArrayList<Message>(); |
| 420 | msgs.addAll(getMessageCollection()); |
| 421 | if (msgs.size() == 0) { |
| 422 | return 0; // no messages -> no need for threshold |
| 423 | } |
| 424 | /* anonymous comparator class for hop count comparison */ |
| 425 | Comparator<Message> hopCountComparator = new Comparator<Message>() { |
| 426 | public int compare(Message m1, Message m2) { |
| 427 | return m1.getHopCount() - m2.getHopCount(); |
| 428 | } |
| 429 | }; |
| 430 | Collections.sort(msgs, hopCountComparator); |
| 431 | |
| 432 | /* finds the first message that is beyond the calculated portion */ |
| 433 | int i=0; |
| 434 | for (int n=msgs.size(); i<n && p>0; i++) { |
| 435 | p -= msgs.get(i).getSize(); |
| 436 | } |
| 437 | |
| 438 | i--; // the last round moved i one index too far |
| 439 | if (i < 0) { |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | /* now i points to the first packet that exceeds portion p; |
| 444 | * the threshold is that packet's hop count + 1 (so that packet and |
| 445 | * perhaps some more are included in the priority part) */ |
| 446 | return msgs.get(i).getHopCount() + 1; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Message comparator for the MaxProp routing module. |