[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Commit-gnuradio] r9411 - usrp2/trunk/host-ng/lib
From: |
eb |
Subject: |
[Commit-gnuradio] r9411 - usrp2/trunk/host-ng/lib |
Date: |
Mon, 25 Aug 2008 15:19:32 -0600 (MDT) |
Author: eb
Date: 2008-08-25 15:19:32 -0600 (Mon, 25 Aug 2008)
New Revision: 9411
Modified:
usrp2/trunk/host-ng/lib/usrp2.cc
Log:
table of weak_ptrs to usrp2s indexed by interface+mac_addr
Modified: usrp2/trunk/host-ng/lib/usrp2.cc
===================================================================
--- usrp2/trunk/host-ng/lib/usrp2.cc 2008-08-25 21:18:30 UTC (rev 9410)
+++ usrp2/trunk/host-ng/lib/usrp2.cc 2008-08-25 21:19:32 UTC (rev 9411)
@@ -22,9 +22,68 @@
#include <usrp2/usrp2.h>
#include "usrp2_impl.h"
+#include <vector>
+#include <boost/thread.hpp>
+#include <boost/weak_ptr.hpp>
+#include <string>
+#include <stdexcept>
namespace usrp2 {
+ // --- Table of weak pointers to usrps we know about ---
+
+ // (Could be cleaned up and turned into a template)
+
+ struct usrp_table_entry {
+ // inteface + normalized mac addr ("eth0:01:23:45:67:89:ab")
+ std::string key;
+ boost::weak_ptr<usrp2::usrp2> value;
+
+ usrp_table_entry(const std::string &_key, boost::weak_ptr<usrp2::usrp2>
_value)
+ : key(_key), value(_value) {}
+ };
+
+ typedef std::vector<usrp_table_entry> usrp_table;
+
+ static boost::mutex s_table_mutex;
+ static usrp_table s_table;
+
+ static usrp2::sptr
+ find_existing_or_make_new(const std::string &ifc, const std::string
&mac_addr)
+ {
+ // FIXME normalize addr
+
+ if (mac_addr.size() != 17)
+ throw std::invalid_argument("invalid mac_addr: " + mac_addr);
+
+ std::string key = ifc + ":" + mac_addr;
+
+ boost::mutex::scoped_lock guard(s_table_mutex);
+
+ for (usrp_table::iterator p = s_table.begin(); p != s_table.end();){
+ if (p->value.expired()) // weak pointer is now dead
+ p = s_table.erase(p); // erase it
+ else {
+ if (key == p->key) // found it
+ return usrp2::sptr(p->value);
+ else
+ ++p; // keep looking
+ }
+ }
+
+ // We don't have the USRP2 we're looking for
+
+ // create a new one and stick it in the table.
+ usrp2::sptr r = usrp2::make(ifc, mac_addr);
+ usrp_table_entry t(key, r);
+ s_table.push_back(t);
+
+ return r;
+ }
+
+ // --- end of table code ---
+
+
// Shared pointer factory function, wraps constructor call
usrp2::sptr
usrp2::make(const std::string &ifc, const std::string &addr)
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Commit-gnuradio] r9411 - usrp2/trunk/host-ng/lib,
eb <=