3 * Tree keeping track of the availability of each bin in a swarm
5 * Created by Riccardo Petrocco
6 * Copyright 2009-2012 Delft University of Technology. All rights reserved.
9 #include "availability.h"
11 using namespace swift;
13 #define DEBUGAVAILABILITY 0
16 uint8_t Availability::get(const bin_t bin)
21 return avail_[bin.toUInt()];
27 void Availability::setBin(bin_t bin)
29 if (bin != bin_t::NONE)
31 bin_t beg = bin.base_left();
32 bin_t end = bin.base_right();
34 for (int i = beg.toUInt(); i<=end.toUInt(); i++)
36 // for the moment keep a counter
37 // TODO make it percentage
45 void Availability::removeBin(bin_t bin)
47 bin_t beg = bin.base_left();
48 bin_t end = bin.base_right();
50 for (int i = beg.toUInt(); i<=end.toUInt(); i++)
57 void Availability::setBinmap(binmap_t * binmap)
60 if (binmap->is_filled())
61 for (int i=0; i<size_; i++)
64 if (!binmap->is_empty())
69 tmp_b = binmap_t::find_complement(tmp_bm, *binmap, 0);
71 while (tmp_b != bin_t::NONE)
74 //binmap_t::copy(tmp_bm, *binmap, tmp_b);
76 tmp_b = binmap_t::find_complement(tmp_bm, *binmap, 0);
85 void Availability::removeBinmap(binmap_t &binmap)
87 if (binmap.is_filled())
88 for (int i=0; i<size_; i++)
91 if (!binmap.is_empty())
95 tmp_b = binmap_t::find_complement(tmp_bm, binmap, 0);
96 while (tmp_b != bin_t::NONE)
100 tmp_b = binmap_t::find_complement(tmp_bm, binmap, 0);
107 void Availability::set(uint32_t channel_id, binmap_t& binmap, bin_t target)
109 if (DEBUGAVAILABILITY)
111 char bin_name_buf[32];
112 dprintf("%s #%u Availability -> setting %s (%llu)\n",tintstr(),channel_id,target.str(bin_name_buf),target.toUInt());
115 if (size_>0 && !binmap.is_filled(target))
117 bin_t beg = target.base_left();
118 bin_t end = target.base_right();
120 for (int i = beg.toUInt(); i<=end.toUInt(); i++)
122 // for the moment keep a counter
123 // TODO make it percentage
124 if (!binmap.is_filled(bin_t(i)))
126 //TODO avoid running into sub-trees that r filled
129 // keep track of the incoming have msgs
133 for (WaitingPeers::iterator vpci = waiting_peers_.begin(); vpci != waiting_peers_.end(); ++vpci)
135 if (vpci->first == channel_id)
137 waiting_peers_.erase(vpci);
142 waiting_peers_.push_back(std::make_pair(channel_id, &binmap));
147 void Availability::remove(uint32_t channel_id, binmap_t& binmap)
149 if (DEBUGAVAILABILITY)
151 dprintf("%s #%u Availability -> removing peer\n",tintstr(),channel_id);
155 WaitingPeers::iterator vpci = waiting_peers_.begin();
156 for(; vpci != waiting_peers_.end(); ++vpci)
158 if (vpci->first == channel_id)
160 waiting_peers_.erase(vpci);
167 removeBinmap(binmap);
168 // remove the binmap from the availability
174 void Availability::setSize(uint64_t size)
178 // TODO can be optimized (bithacks)
182 // check if the binmap is not complete
191 // consider higher layers
194 avail_ = new uint8_t[s]();
196 // Initialize with the binmaps we already received
197 for(WaitingPeers::iterator vpci = waiting_peers_.begin(); vpci != waiting_peers_.end(); ++vpci)
199 setBinmap(vpci->second);
203 if (DEBUGAVAILABILITY)
205 char bin_name_buf[32];
206 dprintf("%s #1 Availability -> setting size in chunk %lu \t avail size %u\n",tintstr(), size, s);
211 bin_t Availability::getRarest(const bin_t range, int width)
213 assert(range.toUInt()<size_);
215 bin_t::uint_t idx = range.toUInt();
217 while (curr.base_length()>width)
220 if ( avail_[curr.left().toUInt()] <= avail_[curr.right().toUInt()] )
228 void Availability::status() const
230 printf("availability:\n");
234 for (int i = 0; i < size_; i++)
235 printf("%d", avail_[i]);