← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 2433: create port mapping interfaces only when needed

 

------------------------------------------------------------
revno: 2433
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Sun 2011-02-20 18:37:48 +0100
message:
  create port mapping interfaces only when needed
modified:
  dcpp/MappingManager.cpp
  dcpp/MappingManager.h
  dwt/include/dwt/widgets/Splitter.h
  win32/Mapper_MiniUPnPc.cpp
  win32/Mapper_MiniUPnPc.h
  win32/Mapper_WinUPnP.cpp
  win32/Mapper_WinUPnP.h
  win32/main.cpp


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dcpp/MappingManager.cpp'
--- dcpp/MappingManager.cpp	2011-02-19 17:03:15 +0000
+++ dcpp/MappingManager.cpp	2011-02-20 17:37:48 +0000
@@ -30,12 +30,8 @@
 
 namespace dcpp {
 
-void MappingManager::addImplementation(Mapper* mapper) {
-	mappers.push_back(mapper);
-}
-
 bool MappingManager::open() {
-	if(opened)
+	if(getOpened())
 		return false;
 
 	if(mappers.empty()) {
@@ -56,9 +52,14 @@
 void MappingManager::close() {
 	TimerManager::getInstance()->removeListener(this);
 
-	for(auto i = mappers.begin(); i != mappers.end(); ++i)
-		close(*i);
-	opened = false;
+	if(working.get()) {
+		close(*working);
+		working.reset();
+	}
+}
+
+bool MappingManager::getOpened() const {
+	return working.get();
 }
 
 int MappingManager::run() {
@@ -71,7 +72,7 @@
 		search_port = SearchManager::getInstance()->getPort();
 
 	if(renewal) {
-		Mapper& mapper = mappers[working];
+		Mapper& mapper = *working;
 
 		ScopedFunctor([&mapper] { mapper.uninit(); });
 		if(!mapper.init()) {
@@ -103,7 +104,8 @@
 	}
 
 	for(auto i = mappers.begin(); i != mappers.end(); ++i) {
-		Mapper& mapper = *i;
+		unique_ptr<Mapper> pMapper((*i)());
+		Mapper& mapper = *pMapper;
 
 		ScopedFunctor([&mapper] { mapper.uninit(); });
 
@@ -112,8 +114,6 @@
 			continue;
 		}
 
-		mapper.close();
-
 		auto addRule = [this, &mapper](const unsigned short port, Mapper::Protocol protocol, const string& description) -> bool {
 			if(port && !mapper.open(port, protocol, boost::str(F_("%1% %2% port (%3% %4%)") %
 				APPNAME % description % port % Mapper::protocols[protocol])))
@@ -126,15 +126,16 @@
 			return true;
 		};
 
-		opened = addRule(conn_port, Mapper::PROTOCOL_TCP, _("Transfer")) &&
+		if(!(addRule(conn_port, Mapper::PROTOCOL_TCP, _("Transfer")) &&
 			addRule(secure_port, Mapper::PROTOCOL_TCP, _("Encrypted Transfer")) &&
-			addRule(search_port, Mapper::PROTOCOL_UDP, _("Search"));
-		if(!opened)
+			addRule(search_port, Mapper::PROTOCOL_UDP, _("Search"))))
 			continue;
 
 		log(str(F_("Successfully created port mappings (TCP: %1%, UDP: %2%, TLS: %3%) on the %4% device with the %5% interface") %
 			conn_port % search_port % secure_port % deviceString(mapper) % mapper.getName()));
 
+		working = move(pMapper);
+
 		if(!BOOLSETTING(NO_IP_OVERRIDE)) {
 			string externalIP = mapper.getExternalIP();
 			if(!externalIP.empty()) {
@@ -150,13 +151,12 @@
 		auto minutes = mapper.renewal();
 		if(minutes) {
 			renewal = GET_TICK() + std::max(minutes, 10u) * 60 * 1000;
-			working = i - mappers.begin();
 			TimerManager::getInstance()->addListener(this);
 		}
 		break;
 	}
 
-	if(!opened) {
+	if(!getOpened()) {
 		log(_("Failed to create port mappings"));
 		ConnectivityManager::getInstance()->mappingFinished(false);
 	}

=== modified file 'dcpp/MappingManager.h'
--- dcpp/MappingManager.h	2011-02-11 23:08:25 +0000
+++ dcpp/MappingManager.h	2011-02-20 17:37:48 +0000
@@ -25,8 +25,6 @@
 
 #include <atomic>
 
-#include <boost/ptr_container/ptr_vector.hpp>
-
 namespace dcpp {
 
 class MappingManager :
@@ -35,29 +33,24 @@
 	private TimerManagerListener
 {
 public:
-	/**
-	* add an implementation, derived from the base Mapper class.
-	* must be allocated on the heap; its deletion will be managed by MappingManager.
-	* the first added mapper will be tried first.
-	*/
-	void addImplementation(Mapper* mapper);
+	/** add an implementation derived from the base Mapper class, passed as template parameter.
+	the first added mapper will be tried first. */
+	template<typename T> void addImplementation() { mappers.push_back([] { return new T(); }); }
+
 	bool open();
 	void close();
-
-	bool getOpened() const { return opened; }
+	bool getOpened() const;
 
 private:
 	friend class Singleton<MappingManager>;
 
-	boost::ptr_vector<Mapper> mappers;
+	vector<function<Mapper* ()>> mappers;
 
-	bool opened;
 	atomic_flag busy;
-
+	unique_ptr<Mapper> working; /// currently working implementation.
 	uint64_t renewal; /// when the next renewal should happen, if requested by the mapper.
-	size_t working; /// index of the currently working implementation (used for renewal).
 
-	MappingManager() : opened(false), busy(false), renewal(0), working(0) { }
+	MappingManager() : busy(false), renewal(0) { }
 	virtual ~MappingManager() throw() { join(); }
 
 	int run();

=== modified file 'dwt/include/dwt/widgets/Splitter.h'
--- dwt/include/dwt/widgets/Splitter.h	2011-02-20 16:20:37 +0000
+++ dwt/include/dwt/widgets/Splitter.h	2011-02-20 17:37:48 +0000
@@ -127,7 +127,7 @@
 			}
 			theme.drawBackground(canvas, part, state, canvas.getPaintRect());
 
-		} else {
+		} else if(hovering) {
 			// safe to assume that the text color is different enough from the default background.
 			canvas.fill(canvas.getPaintRect(), Brush(Brush::WindowText));
 		}

=== modified file 'win32/Mapper_MiniUPnPc.cpp'
--- win32/Mapper_MiniUPnPc.cpp	2011-02-12 15:11:02 +0000
+++ win32/Mapper_MiniUPnPc.cpp	2011-02-20 17:37:48 +0000
@@ -33,8 +33,6 @@
 const string Mapper_MiniUPnPc::name = "MiniUPnP";
 
 bool Mapper_MiniUPnPc::init() {
-	// only init once.
-	static bool initialized = false;
 	if(initialized)
 		return true;
 

=== modified file 'win32/Mapper_MiniUPnPc.h'
--- win32/Mapper_MiniUPnPc.h	2011-02-11 23:08:25 +0000
+++ win32/Mapper_MiniUPnPc.h	2011-02-20 17:37:48 +0000
@@ -24,7 +24,7 @@
 class Mapper_MiniUPnPc : public Mapper
 {
 public:
-	Mapper_MiniUPnPc() : Mapper() { }
+	Mapper_MiniUPnPc() : Mapper(), initialized(false) { }
 
 private:
 	bool init();
@@ -41,6 +41,8 @@
 	static const string name;
 	const string& getName() const { return name; }
 
+	bool initialized;
+
 	string url;
 	string service;
 	string device;

=== modified file 'win32/Mapper_WinUPnP.cpp'
--- win32/Mapper_WinUPnP.cpp	2011-02-12 15:11:02 +0000
+++ win32/Mapper_WinUPnP.cpp	2011-02-20 17:37:48 +0000
@@ -31,8 +31,6 @@
 #include <natupnp.h>
 	
 bool Mapper_WinUPnP::init() {
-	// only init once.
-	static bool initialized = false;
 	if(initialized)
 		return true;
 

=== modified file 'win32/Mapper_WinUPnP.h'
--- win32/Mapper_WinUPnP.h	2011-02-11 23:08:25 +0000
+++ win32/Mapper_WinUPnP.h	2011-02-20 17:37:48 +0000
@@ -28,7 +28,7 @@
 class Mapper_WinUPnP : public Mapper
 {
 public:
-	Mapper_WinUPnP() : Mapper(), pUN(0), lastPort(0) { }
+	Mapper_WinUPnP() : Mapper(), initialized(false), pUN(0), lastPort(0) { }
 
 private:
 	bool init();
@@ -45,6 +45,8 @@
 	static const string name;
 	const string& getName() const { return name; }
 
+	bool initialized;
+
 	IUPnPNAT* pUN;
 	// this one can become invalidated so we can't cache it
 	IStaticPortMappingCollection* getStaticPortMappingCollection();

=== modified file 'win32/main.cpp'
--- win32/main.cpp	2011-02-20 16:20:37 +0000
+++ win32/main.cpp	2011-02-20 17:37:48 +0000
@@ -148,9 +148,9 @@
 			SetProcessDefaultLayout(LAYOUT_RTL);
 		}
 
-		MappingManager::getInstance()->addImplementation(new Mapper_NATPMP());
-		MappingManager::getInstance()->addImplementation(new Mapper_MiniUPnPc());
-		MappingManager::getInstance()->addImplementation(new Mapper_WinUPnP());
+		MappingManager::getInstance()->addImplementation<Mapper_NATPMP>();
+		MappingManager::getInstance()->addImplementation<Mapper_MiniUPnPc>();
+		MappingManager::getInstance()->addImplementation<Mapper_WinUPnP>();
 
 		WinUtil::init();
 		MainWindow* wnd = new MainWindow;