Send correct informations to a http tracker.
[tairent.git] / src / main / hashchecker.cpp
blob8c0cbd80f3f2d47a08f6c3561da7f73ea5cfb2cf
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 "storage.h"
25 namespace Tairent
28 namespace Main
31 /* {{{ HashChecker::HashChecker(uint32_t, Storage *) */
32 HashChecker::HashChecker(uint32_t i, Storage *s) : index(i)
34 piece = s->getPiece(i);
36 pieceHash = s->getMetaInfo()["pieces"].asString().substr(i * 20, 20);
38 disabler = new Tairon::Core::FunctorDisabler();
39 disabler->incRef();
41 /* }}} */
43 /* {{{ HashChecker::~HashChecker() */
44 HashChecker::~HashChecker()
46 disabler->decRef();
48 /* }}} */
50 /* {{{ HashChecker::check() */
51 void HashChecker::check()
53 Tairon::Core::ThreadFunctorCaller *caller = new Tairon::Core::ThreadMethodDFunctorCaller0<HashChecker>(this, &HashChecker::checkPiece, disabler);
54 Tairon::Core::ThreadManager::self()->getThreadByName("hash")->addFunctorCaller(caller);
56 /* }}} */
58 /* {{{ HashChecker::checkPiece() */
59 void HashChecker::checkPiece()
61 Tairon::Crypto::SHA1 hash;
63 for (std::list<PieceStruct::FilePieceStruct>::const_iterator it = piece->pieces.begin(); it != piece->pieces.end(); ++it)
64 hash.update(it->mem, it->length);
66 if (hash.final() == pieceHash)
67 hashCorrectSignal.emit(index, this);
68 else
69 hashIncorrectSignal.emit(index, this);
71 /* }}} */
73 }; // namespace Main
75 }; // namespace Tairent
77 // vim: ai sw=4 ts=4 noet fdm=marker