← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 2767: Move out tree output stream

 

------------------------------------------------------------
revno: 2767
committer: Jacek Sieka <arnetheduck@xxxxxxxxx>
branch nick: dcplusplus
timestamp: Wed 2011-12-28 19:52:30 +0100
message:
  Move out tree output stream
added:
  dcpp/MerkleTreeOutputStream.h
modified:
  dcpp/QueueManager.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
=== added file 'dcpp/MerkleTreeOutputStream.h'
--- dcpp/MerkleTreeOutputStream.h	1970-01-01 00:00:00 +0000
+++ dcpp/MerkleTreeOutputStream.h	2011-12-28 18:52:30 +0000
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2001-2011 Jacek Sieka, arnetheduck on gmail point com
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef DCPLUSPLUS_DCPP_MERKLE_TREE_OUTPUT_STREAM_H
+#define DCPLUSPLUS_DCPP_MERKLE_TREE_OUTPUT_STREAM_H
+
+#include "Streams.h"
+#include "MerkleTree.h"
+
+namespace dcpp {
+
+template<typename TreeType>
+class MerkleTreeOutputStream : public OutputStream {
+public:
+	MerkleTreeOutputStream(TreeType& aTree) : tree(aTree), bufPos(0) { }
+
+	virtual size_t write(const void* xbuf, size_t len) {
+		size_t pos = 0;
+		uint8_t* b = (uint8_t*)xbuf;
+		while(pos < len) {
+			size_t left = len - pos;
+			if(bufPos == 0 && left >= TreeType::BYTES) {
+				tree.getLeaves().push_back(typename TreeType::MerkleValue(b + pos));
+				pos += TreeType::BYTES;
+			} else {
+				size_t bytes = min(TreeType::BYTES - bufPos, left);
+				memcpy(buf + bufPos, b + pos, bytes);
+				bufPos += bytes;
+				pos += bytes;
+				if(bufPos == TreeType::BYTES) {
+					tree.getLeaves().push_back(typename TreeType::MerkleValue(buf));
+					bufPos = 0;
+				}
+			}
+		}
+		return len;
+	}
+
+	virtual size_t flush() {
+		return 0;
+	}
+private:
+	TreeType& tree;
+	uint8_t buf[TreeType::BYTES];
+	size_t bufPos;
+};
+
+}
+
+#endif /* DCPLUSPLUS_DCPP_MERKLE_CHECK_OUTPUT_STREAM_H */

=== modified file 'dcpp/QueueManager.cpp'
--- dcpp/QueueManager.cpp	2011-12-28 18:37:58 +0000
+++ dcpp/QueueManager.cpp	2011-12-28 18:52:30 +0000
@@ -31,6 +31,7 @@
 #include "HashManager.h"
 #include "LogManager.h"
 #include "MerkleCheckOutputStream.h"
+#include "MerkleTreeOutputStream.h"
 #include "SearchManager.h"
 #include "SearchResult.h"
 #include "SFVReader.h"
@@ -939,45 +940,6 @@
 	return d;
 }
 
-namespace {
-class TreeOutputStream : public OutputStream {
-public:
-	TreeOutputStream(TigerTree& aTree) : tree(aTree), bufPos(0) {
-	}
-
-	virtual size_t write(const void* xbuf, size_t len) {
-		size_t pos = 0;
-		uint8_t* b = (uint8_t*)xbuf;
-		while(pos < len) {
-			size_t left = len - pos;
-			if(bufPos == 0 && left >= TigerTree::BYTES) {
-				tree.getLeaves().push_back(TTHValue(b + pos));
-				pos += TigerTree::BYTES;
-			} else {
-				size_t bytes = min(TigerTree::BYTES - bufPos, left);
-				memcpy(buf + bufPos, b + pos, bytes);
-				bufPos += bytes;
-				pos += bytes;
-				if(bufPos == TigerTree::BYTES) {
-					tree.getLeaves().push_back(TTHValue(buf));
-					bufPos = 0;
-				}
-			}
-		}
-		return len;
-	}
-
-	virtual size_t flush() {
-		return 0;
-	}
-private:
-	TigerTree& tree;
-	uint8_t buf[TigerTree::BYTES];
-	size_t bufPos;
-};
-
-}
-
 void QueueManager::setFile(Download* d) {
 	if(d->getType() == Transfer::TYPE_FILE) {
 		Lock l(cs);
@@ -1019,7 +981,7 @@
 	} else if(d->getType() == Transfer::TYPE_PARTIAL_LIST) {
 		d->setFile(new StringOutputStream(d->getPFS()));
 	} else if(d->getType() == Transfer::TYPE_TREE) {
-		d->setFile(new TreeOutputStream(d->getTigerTree()));
+		d->setFile(new MerkleTreeOutputStream<TigerTree>(d->getTigerTree()));
 	}
 }