Browse Source

Prep for real tests like alltoall.

Adam Ierymenko 10 years ago
parent
commit
8672ca9cf8

+ 3 - 11
testnet/TestEthernetTap.cpp

@@ -42,8 +42,6 @@
 
 namespace ZeroTier {
 
-static Mutex printLock;
-
 TestEthernetTap::TestEthernetTap(
 	TestEthernetTapFactory *parent,
 	const MAC &mac,
@@ -55,6 +53,7 @@ TestEthernetTap::TestEthernetTap(
 	void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
 	void *arg) :
 	EthernetTap("TestEthernetTap",mac,mtu,metric),
+	_nwid(nwid),
 	_parent(parent),
 	_handler(handler),
 	_arg(arg),
@@ -114,9 +113,8 @@ std::set<InetAddress> TestEthernetTap::ips() const
 
 void TestEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
 {
-	Mutex::Lock _l(printLock);
-	fprintf(stdout,"[%s] %s << %s %.4x %s"ZT_EOL_S,_dev.c_str(),to.toString().c_str(),from.toString().c_str(),etherType,std::string((const char *)data,len).c_str());
-	fflush(stdout);
+	Mutex::Lock _l(_gq_m);
+	_gq.push_back(TestFrame(from,to,data,etherType,len));
 }
 
 std::string TestEthernetTap::deviceName() const
@@ -144,12 +142,6 @@ bool TestEthernetTap::injectPacketFromHost(const MAC &from,const MAC &to,unsigne
 	}
 	_pq_c.signal();
 
-	{
-		Mutex::Lock _l(printLock);
-		fprintf(stdout,"[%s] %s >> %s %.4x %s"ZT_EOL_S,_dev.c_str(),from.toString().c_str(),to.toString().c_str(),etherType,std::string((const char *)data,len).c_str());
-		fflush(stdout);
-	}
-
 	return true;
 }
 

+ 16 - 2
testnet/TestEthernetTap.hpp

@@ -60,7 +60,7 @@ class TestEthernetTap : public EthernetTap
 {
 	friend class SharedPtr<TestEthernetTap>;
 
-private:
+public:
 	struct TestFrame
 	{
 		TestFrame() : from(),to(),etherType(0),len(0) {}
@@ -79,7 +79,6 @@ private:
 		char data[4096];
 	};
 
-public:
 	TestEthernetTap(
 		TestEthernetTapFactory *parent,
 		const MAC &mac,
@@ -104,10 +103,22 @@ public:
 	virtual bool updateMulticastGroups(std::set<MulticastGroup> &groups);
 	virtual bool injectPacketFromHost(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
 
+	inline uint64_t nwid() const { return _nwid; }
+
+	// Get things that have been put() and empty queue
+	inline void get(std::vector<TestFrame> &v,bool clearQueue = true)
+	{
+		Mutex::Lock _l(_gq_m);
+		v = _gq;
+		if (clearQueue)
+			_gq.clear();
+	}
+
 	void threadMain()
 		throw();
 
 private:
+	uint64_t _nwid;
 	TestEthernetTapFactory *_parent;
 
 	void (*_handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &);
@@ -120,6 +131,9 @@ private:
 	Mutex _pq_m;
 	Condition _pq_c;
 
+	std::vector< TestFrame > _gq;
+	Mutex _gq_m;
+
 	AtomicCounter __refCount;
 };
 

+ 8 - 0
testnet/TestEthernetTapFactory.cpp

@@ -57,6 +57,10 @@ EthernetTap *TestEthernetTapFactory::open(
 		Mutex::Lock _l(_tapsByMac_m);
 		_tapsByMac[mac] = tap;
 	}
+	{
+		Mutex::Lock _l(_tapsByNwid_m);
+		_tapsByNwid[nwid] = tap;
+	}
 	return tap.ptr();
 }
 
@@ -73,6 +77,10 @@ void TestEthernetTapFactory::close(EthernetTap *tap,bool destroyPersistentDevice
 		Mutex::Lock _l(_tapsByMac_m);
 		_tapsByMac.erase(tapp->mac());
 	}
+	{
+		Mutex::Lock _l(_tapsByNwid_m);
+		_tapsByNwid.erase(tapp->nwid());
+	}
 }
 
 } // namespace ZeroTier

+ 13 - 1
testnet/TestEthernetTapFactory.hpp

@@ -68,13 +68,25 @@ public:
 		return t->second;
 	}
 
+	inline SharedPtr<TestEthernetTap> getByNwid(uint64_t nwid) const
+	{
+		Mutex::Lock _l(_tapsByNwid_m);
+		std::map< uint64_t,SharedPtr<TestEthernetTap> >::const_iterator t(_tapsByNwid.find(nwid));
+		if (t == _tapsByNwid.end())
+			return SharedPtr<TestEthernetTap>();
+		return t->second;
+	}
+
 private:
 	std::set< SharedPtr<TestEthernetTap> > _taps;
 	Mutex _taps_m;
 
-	std::map<MAC,SharedPtr<TestEthernetTap> > _tapsByMac;
+	std::map< MAC,SharedPtr<TestEthernetTap> > _tapsByMac;
 	Mutex _tapsByMac_m;
 
+	std::map< uint64_t,SharedPtr<TestEthernetTap> > _tapsByNwid;
+	Mutex _tapsByNwid_m;
+
 	CMWC4096 _prng;
 	Mutex _prng_m;
 };