First step for using multiple recvs from mptp.
[swifty.git] / src / libswift / availability.h
1 /*
2  *  availability.h
3  *  Tree keeping track of the availability of each bin in a swarm
4  *
5  *  Created by Riccardo Petrocco
6  *  Copyright 2009-2012 Delft University of Technology. All rights reserved.
7  *
8  */
9 //#include "bin.h"
10 //#include "binmap.h"
11 #include "swift.h"
12 #include <cassert>
13
14 #ifndef AVAILABILITY_H
15 #define AVAILABILITY_H
16
17 namespace swift {
18
19 typedef         std::vector< std::pair<uint32_t, binmap_t*> >   WaitingPeers;
20
21 class Availability
22 {
23     public:
24
25                 /**
26              * Constructor
27              */
28             Availability(void) {        size_ = 0;          }
29
30
31             /**
32              * Constructor
33              */
34             explicit Availability(int size)
35             {
36                 assert(size <= 0);
37                 size_ = size;
38                 avail_ = new uint8_t[size];
39             }
40
41         ~Availability(void)
42         {
43             if (size_)
44                 delete [] avail_;
45         }
46
47             /** return the availability array */
48             uint8_t* get() { return avail_; }
49
50             /** returns the availability of a single bin */
51             uint8_t get(const bin_t bin);
52
53             /** set/update the availability */
54             void set(uint32_t channel_id, binmap_t& binmap, bin_t target);
55
56             /** removes the binmap of leaving peers */
57             void remove(uint32_t channel_id, binmap_t& binmap);
58
59             /** returns the size of the availability tree */
60             int size() { return size_; }
61
62             /** sets the size of the availability tree once we know the size of the file */
63             void setSize(uint64_t size);
64
65             /** sets a binmap */
66             void setBinmap(binmap_t *binmap);
67
68             /** get rarest bin, of specified width, within a range */
69             bin_t getRarest(const bin_t range, int width);
70
71             /** Echo the availability status to stdout */
72                 void status() const;
73
74     protected:
75             uint8_t *avail_;
76             uint64_t    size_;
77             // a list of incoming have msgs, those are saved only it the file size is still unknown
78              // TODO fix... set it depending on the # of channels * something
79             WaitingPeers waiting_peers_;
80             //binmap_t *waiting_[20];
81
82
83
84             /** removes the binmap */
85             void removeBinmap(binmap_t& binmap);
86
87             /** removes the bin */
88             void removeBin(bin_t bin);
89
90             /** sets a bin */
91             void setBin(bin_t bin);
92
93 };
94
95 }
96
97 #endif