5 * Created by Victor Grishchenko on 10/6/09.
6 * Copyright 2009 Delft University of Technology. All rights reserved.
12 using namespace swift;
15 /** Picks pieces nearly sequentialy; some local randomization (twisting)
16 is introduced to prevent synchronization among multiple channels. */
17 class SeqPiecePicker : public PiecePicker {
19 binmap_t ack_hint_out_;
21 FileTransfer* transfer_;
26 SeqPiecePicker (FileTransfer* file_to_pick_from) :
27 transfer_(file_to_pick_from), ack_hint_out_(), twist_(0) {
28 ack_hint_out_.copy_range(file().ack_out(),bin64_t::ALL);
30 virtual ~SeqPiecePicker() {}
33 return transfer_->file();
36 virtual void Randomize (uint64_t twist) {
40 virtual bin64_t Pick (binmap_t& offer, uint64_t max_width, tint expires) {
41 while (hint_out_.size() && hint_out_.front().time<NOW-TINT_SEC*3/2) { // FIXME sec
42 ack_hint_out_.copy_range(file().ack_out(), hint_out_.front().bin);
43 hint_out_.pop_front();
46 return bin64_t(0,0); // whoever sends it first
49 twist_ &= (file().peak(0)) & ((1<<6)-1);
52 ack_hint_out_.twist(twist_);
54 bin64_t hint = offer.find_filtered (ack_hint_out_,bin64_t::ALL,binmap_t::FILLED);
56 hint = hint.twisted(twist_);
58 ack_hint_out_.twist(0);
60 if (hint==bin64_t::NONE) {
61 return hint; // TODO: end-game mode
63 if (!file().ack_out().is_empty(hint)) { // unhinted/late data
64 ack_hint_out_.copy_range(file().ack_out(), hint);
67 while (hint.width()>max_width)
69 assert(ack_hint_out_.get(hint)==binmap_t::EMPTY);
70 ack_hint_out_.set(hint);
71 hint_out_.push_back(tintbin(NOW,hint));