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"
23 #include "hashcheckerthread.h"
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();
44 /* {{{ HashChecker::~HashChecker() */
45 HashChecker::~HashChecker()
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
);
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);
70 hashIncorrectSignal
.emit(index
, this);
74 /* {{{ HashChecker::destroy() */
75 void HashChecker::destroy()
77 HashCheckerThread::self()->addCheckerToDelete(this);
83 }; // namespace Tairent
85 // vim: ai sw=4 ts=4 noet fdm=marker