| | 32 | |
|---|
| | 33 | // --- Table of weak pointers to usrps we know about --- |
|---|
| | 34 | |
|---|
| | 35 | // (Could be cleaned up and turned into a template) |
|---|
| | 36 | |
|---|
| | 37 | struct usrp_table_entry { |
|---|
| | 38 | // inteface + normalized mac addr ("eth0:01:23:45:67:89:ab") |
|---|
| | 39 | std::string key; |
|---|
| | 40 | boost::weak_ptr<usrp2::usrp2> value; |
|---|
| | 41 | |
|---|
| | 42 | usrp_table_entry(const std::string &_key, boost::weak_ptr<usrp2::usrp2> _value) |
|---|
| | 43 | : key(_key), value(_value) {} |
|---|
| | 44 | }; |
|---|
| | 45 | |
|---|
| | 46 | typedef std::vector<usrp_table_entry> usrp_table; |
|---|
| | 47 | |
|---|
| | 48 | static boost::mutex s_table_mutex; |
|---|
| | 49 | static usrp_table s_table; |
|---|
| | 50 | |
|---|
| | 51 | static usrp2::sptr |
|---|
| | 52 | find_existing_or_make_new(const std::string &ifc, const std::string &mac_addr) |
|---|
| | 53 | { |
|---|
| | 54 | // FIXME normalize addr |
|---|
| | 55 | |
|---|
| | 56 | if (mac_addr.size() != 17) |
|---|
| | 57 | throw std::invalid_argument("invalid mac_addr: " + mac_addr); |
|---|
| | 58 | |
|---|
| | 59 | std::string key = ifc + ":" + mac_addr; |
|---|
| | 60 | |
|---|
| | 61 | boost::mutex::scoped_lock guard(s_table_mutex); |
|---|
| | 62 | |
|---|
| | 63 | for (usrp_table::iterator p = s_table.begin(); p != s_table.end();){ |
|---|
| | 64 | if (p->value.expired()) // weak pointer is now dead |
|---|
| | 65 | p = s_table.erase(p); // erase it |
|---|
| | 66 | else { |
|---|
| | 67 | if (key == p->key) // found it |
|---|
| | 68 | return usrp2::sptr(p->value); |
|---|
| | 69 | else |
|---|
| | 70 | ++p; // keep looking |
|---|
| | 71 | } |
|---|
| | 72 | } |
|---|
| | 73 | |
|---|
| | 74 | // We don't have the USRP2 we're looking for |
|---|
| | 75 | |
|---|
| | 76 | // create a new one and stick it in the table. |
|---|
| | 77 | usrp2::sptr r = usrp2::make(ifc, mac_addr); |
|---|
| | 78 | usrp_table_entry t(key, r); |
|---|
| | 79 | s_table.push_back(t); |
|---|
| | 80 | |
|---|
| | 81 | return r; |
|---|
| | 82 | } |
|---|
| | 83 | |
|---|
| | 84 | // --- end of table code --- |
|---|
| | 85 | |
|---|