1 /***************************************************************************
3 * Copyright (C) 2006 David Brodsky *
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. *
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. *
15 ***************************************************************************/
17 #include <tairon/core/signals.h>
18 #include <tairon/core/threadmanager.h>
19 #include <tairon/crypto/sha1.h>
21 #include "hashchecker.h"
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();
43 /* {{{ HashChecker::~HashChecker() */
44 HashChecker::~HashChecker()
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
);
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);
69 hashIncorrectSignal
.emit(index
, this);
75 }; // namespace Tairent
77 // vim: ai sw=4 ts=4 noet fdm=marker