Fix Tairent::Main::TorrentManager::save() method's comment.
[tairent.git] / src / main / hashchecker.cpp
blob1505ed8302a7d5d08faf875fc1490234b003d14e
1 /***************************************************************************
2 * *
3 * Copyright (C) 2006 David Brodsky *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation and appearing *
8 * in the file LICENSE.GPL included in the packaging of this file. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * General Public License for more details. *
14 * *
15 ***************************************************************************/
17 #include <tairon/core/signals.h>
18 #include <tairon/core/threadmanager.h>
19 #include <tairon/crypto/sha1.h>
21 #include "hashchecker.h"
23 #include "hashcheckerthread.h"
24 #include "storage.h"
26 namespace Tairent
29 namespace Main
32 /* {{{ HashChecker::HashChecker(uint32_t, Storage *) */
33 HashChecker::HashChecker(uint32_t i, Storage *s) : index(i)
35 piece = s->getPiece(i);
37 pieceHash = s->getMetaInfo()["pieces"].asString().substr(i * 20, 20);
39 disabler = new Tairon::Core::FunctorDisabler();
40 disabler->incRef();
42 /* }}} */
44 /* {{{ HashChecker::~HashChecker() */
45 HashChecker::~HashChecker()
47 disabler->decRef();
49 /* }}} */
51 /* {{{ HashChecker::check() */
52 void HashChecker::check()
54 Tairon::Core::ThreadFunctorCaller *caller = new Tairon::Core::ThreadMethodDFunctorCaller0<HashChecker>(this, &HashChecker::checkPiece, disabler);
55 Tairon::Core::ThreadManager::self()->getThreadByName("hash")->addFunctorCaller(caller);
57 /* }}} */
59 /* {{{ HashChecker::checkPiece() */
60 void HashChecker::checkPiece()
62 Tairon::Crypto::SHA1 hash;
64 for (std::list<PieceStruct::FilePieceStruct>::const_iterator it = piece->pieces.begin(); it != piece->pieces.end(); ++it)
65 hash.update(it->mem, it->length);
67 if (hash.final() == pieceHash)
68 hashCorrectSignal.emit(index, this);
69 else
70 hashIncorrectSignal.emit(index, this);
72 /* }}} */
74 /* {{{ HashChecker::destroy() */
75 void HashChecker::destroy()
77 HashCheckerThread::self()->addCheckerToDelete(this);
79 /* }}} */
81 }; // namespace Main
83 }; // namespace Tairent
85 // vim: ai sw=4 ts=4 noet fdm=marker