To add LogFactory which creates singleton logger:

* src/LogFactory.h: New class.
	* src/LogFactory.cc: New class.
	* src/Command.h (logger): New variable.
	(Constructor): Use LogFactory.
	* src/AbstractCommand.cc: Use Command::logger
	* src/PeerConnection.cc (Constructor): Deleted the argument 
logger.
	Use LogFactory.
	* src/SegmentSplitter.h : Made logger protected.
	* src/SegmentSplitter.cc (Constructor): Use LogFactory.
	* src/SegmentMan.cc (Constructor): Use LogFactory.
	* src/DownloadEngine.h : Made logger protected.
	* src/DownloadEngine.cc (Constructor): Use LogFactory.
	* src/PeerInteractionCommand.cc : Use Command::logger.
	* src/HttpResponseCommand.cc : Use Command::logger.
	* src/SegmentMan.h : Made logger private.
	* src/TorrentMan.h : Made logger private.
	* src/TorrentMan.cc : Use LogFactory.
	* src/FtpNegotiateCommand.cc : Use Command::logger.
	* src/HttpConnection.h (Constructor): Deleted the argument 
logger.
	* src/HttpConnection.cc (Constructor): Deleted the argument 
logger.
	Use LogFactory.
	* src/FtpConnection.h (Constructor): Deleted the argument 
logger.
	* src/FtpConnection.cc (Constructor): Deleted the argument 
logger.
	Use LogFactory.
	* src/DownloadCommand.cc : Use Command::logger.
	* src/PeerAbstractCommand.cc : Use Command::logger.
	* src/PeerListenCommand.cc : Use Command::logger.
	* src/PeerInitiateConnectionCommand.cc : Use Command::logger.
	* src/HttpInitiateConnectionCommand.cc : Use Command::logger.
	* src/FtpInitiateConnectionCommand.cc : Use Command::logger.
	* src/TrackerWatcherCommand.cc : Use Command::logger.
	* src/TrackerUpdateCommand.cc : Use Command::logger.
	* src/TrackerDownloadCommand.cc : Use Command::logger.
	* src/RequestSlotMan.cc (Constructor): Deleted the argument 
logger.
	Use LogFactory.
	* src/SendMessageQueue.h (Constructor): Deleted the argument 
logger.
	* src/SendMessageQueue.cc (Constructor): Deleted the argument 
logger.
	Use LogFactory.
	* src/main.cc (main): Use LogFactory.
	* src/DiskAdaptor.h (logger): New variable.
	* src/DiskAdaptor.cc (Constructor): Use LogFactory.
	* src/CopyDiskAdaptor.cc (fixFilename): Added a log message.
pull/1/head
Tatsuhiro Tsujikawa 2006-04-17 16:17:20 +00:00
parent dcc225a145
commit 39276dcb00
48 changed files with 309 additions and 138 deletions

View File

@ -1,3 +1,50 @@
2006-04-18 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To add LogFactory which creates singleton logger:
* src/LogFactory.h: New class.
* src/LogFactory.cc: New class.
* src/Command.h (logger): New variable.
(Constructor): Use LogFactory.
* src/AbstractCommand.cc: Use Command::logger
* src/PeerConnection.cc (Constructor): Deleted the argument logger.
Use LogFactory.
* src/SegmentSplitter.h : Made logger protected.
* src/SegmentSplitter.cc (Constructor): Use LogFactory.
* src/SegmentMan.cc (Constructor): Use LogFactory.
* src/DownloadEngine.h : Made logger protected.
* src/DownloadEngine.cc (Constructor): Use LogFactory.
* src/PeerInteractionCommand.cc : Use Command::logger.
* src/HttpResponseCommand.cc : Use Command::logger.
* src/SegmentMan.h : Made logger private.
* src/TorrentMan.h : Made logger private.
* src/TorrentMan.cc : Use LogFactory.
* src/FtpNegotiateCommand.cc : Use Command::logger.
* src/HttpConnection.h (Constructor): Deleted the argument logger.
* src/HttpConnection.cc (Constructor): Deleted the argument logger.
Use LogFactory.
* src/FtpConnection.h (Constructor): Deleted the argument logger.
* src/FtpConnection.cc (Constructor): Deleted the argument logger.
Use LogFactory.
* src/DownloadCommand.cc : Use Command::logger.
* src/PeerAbstractCommand.cc : Use Command::logger.
* src/PeerListenCommand.cc : Use Command::logger.
* src/PeerInitiateConnectionCommand.cc : Use Command::logger.
* src/HttpInitiateConnectionCommand.cc : Use Command::logger.
* src/FtpInitiateConnectionCommand.cc : Use Command::logger.
* src/TrackerWatcherCommand.cc : Use Command::logger.
* src/TrackerUpdateCommand.cc : Use Command::logger.
* src/TrackerDownloadCommand.cc : Use Command::logger.
* src/RequestSlotMan.cc (Constructor): Deleted the argument logger.
Use LogFactory.
* src/SendMessageQueue.h (Constructor): Deleted the argument logger.
* src/SendMessageQueue.cc (Constructor): Deleted the argument logger.
Use LogFactory.
* src/main.cc (main): Use LogFactory.
* src/DiskAdaptor.h (logger): New variable.
* src/DiskAdaptor.cc (Constructor): Use LogFactory.
* src/CopyDiskAdaptor.cc (fixFilename): Added a log message.
2006-04-17 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> 2006-04-17 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
* src/TrackerInitCommand.cc (TrackerInitCommand): Added a "key" * src/TrackerInitCommand.cc (TrackerInitCommand): Added a "key"

1
TODO
View File

@ -14,7 +14,6 @@
0.4.0 release 0.4.0 release
* try to use ftruncate to allocate file. * try to use ftruncate to allocate file.
* Add loggerfactory
* add log message when download completes * add log message when download completes
0.4.1 release 0.4.1 release

View File

@ -23,11 +23,11 @@
#include "DlAbortEx.h" #include "DlAbortEx.h"
#include "DlRetryEx.h" #include "DlRetryEx.h"
#include "InitiateConnectionCommandFactory.h" #include "InitiateConnectionCommandFactory.h"
#include <sys/time.h>
#include "Util.h" #include "Util.h"
#include "message.h" #include "message.h"
#include "SleepCommand.h" #include "SleepCommand.h"
#include "prefs.h" #include "prefs.h"
#include <sys/time.h>
AbstractCommand::AbstractCommand(int cuid, Request* req, DownloadEngine* e, const Socket* s): AbstractCommand::AbstractCommand(int cuid, Request* req, DownloadEngine* e, const Socket* s):
Command(cuid), req(req), e(e), checkSocketIsReadable(false), checkSocketIsWritable(false) { Command(cuid), req(req), e(e), checkSocketIsReadable(false), checkSocketIsWritable(false) {
@ -86,19 +86,19 @@ bool AbstractCommand::execute() {
// get segment information in order to set Range header. // get segment information in order to set Range header.
if(!e->segmentMan->getSegment(seg, cuid)) { if(!e->segmentMan->getSegment(seg, cuid)) {
// no segment available // no segment available
e->logger->info(MSG_NO_SEGMENT_AVAILABLE, cuid); logger->info(MSG_NO_SEGMENT_AVAILABLE, cuid);
return true; return true;
} }
} }
return executeInternal(seg); return executeInternal(seg);
} catch(DlAbortEx* err) { } catch(DlAbortEx* err) {
e->logger->error(MSG_DOWNLOAD_ABORTED, err, cuid); logger->error(MSG_DOWNLOAD_ABORTED, err, cuid);
onAbort(err); onAbort(err);
delete(err); delete(err);
req->resetUrl(); req->resetUrl();
return true; return true;
} catch(DlRetryEx* err) { } catch(DlRetryEx* err) {
e->logger->error(MSG_RESTARTING_DOWNLOAD, err, cuid); logger->error(MSG_RESTARTING_DOWNLOAD, err, cuid);
req->addTryCount(); req->addTryCount();
bool isAbort = e->option->getAsInt(PREF_MAX_TRIES) != 0 && bool isAbort = e->option->getAsInt(PREF_MAX_TRIES) != 0 &&
req->getTryCount() >= e->option->getAsInt(PREF_MAX_TRIES); req->getTryCount() >= e->option->getAsInt(PREF_MAX_TRIES);
@ -107,7 +107,7 @@ bool AbstractCommand::execute() {
} }
delete(err); delete(err);
if(isAbort) { if(isAbort) {
e->logger->error(MSG_MAX_TRY, cuid, req->getTryCount()); logger->error(MSG_MAX_TRY, cuid, req->getTryCount());
return true; return true;
} else { } else {
return prepareForRetry(e->option->getAsInt(PREF_RETRY_WAIT)); return prepareForRetry(e->option->getAsInt(PREF_RETRY_WAIT));
@ -127,7 +127,7 @@ bool AbstractCommand::prepareForRetry(int wait) {
} }
void AbstractCommand::onAbort(Exception* ex) { void AbstractCommand::onAbort(Exception* ex) {
e->logger->debug(MSG_UNREGISTER_CUID, cuid); logger->debug(MSG_UNREGISTER_CUID, cuid);
e->segmentMan->unregisterId(cuid); e->segmentMan->unregisterId(cuid);
} }

View File

@ -23,12 +23,16 @@
#define _D_COMMAND_H_ #define _D_COMMAND_H_
#include "common.h" #include "common.h"
#include "LogFactory.h"
class Command { class Command {
protected: protected:
int cuid; int cuid;
const Logger* logger;
public: public:
Command(int cuid):cuid(cuid) {} Command(int cuid):cuid(cuid) {
logger = LogFactory::getInstance();
}
virtual ~Command() {} virtual ~Command() {}
virtual bool execute() = 0; virtual bool execute() = 0;

View File

@ -41,7 +41,7 @@ void CopyDiskAdaptor::fixFilename() {
itr != fileEntries.end(); itr++) { itr != fileEntries.end(); itr++) {
if(!itr->extracted && itr->requested) { if(!itr->extracted && itr->requested) {
string dest = storeDir+"/"+itr->path; string dest = storeDir+"/"+itr->path;
//logger->info("writing file %s", dest.c_str()); logger->info("writing file %s", dest.c_str());
Util::rangedFileCopy(dest, getFilePath(), offset, itr->length); Util::rangedFileCopy(dest, getFilePath(), offset, itr->length);
itr->extracted = true; itr->extracted = true;
} }

View File

@ -21,6 +21,18 @@
/* copyright --> */ /* copyright --> */
#include "DiskAdaptor.h" #include "DiskAdaptor.h"
#include "DlAbortEx.h" #include "DlAbortEx.h"
#include "LogFactory.h"
DiskAdaptor::DiskAdaptor(DiskWriter* diskWriter):diskWriter(diskWriter), topDir(NULL) {
logger = LogFactory::getInstance();
}
DiskAdaptor::~DiskAdaptor() {
delete diskWriter;
if(topDir != NULL) {
delete topDir;
}
}
void DiskAdaptor::openFile() { void DiskAdaptor::openFile() {
diskWriter->openFile(getFilePath()); diskWriter->openFile(getFilePath());

View File

@ -26,6 +26,7 @@
#include "FileEntry.h" #include "FileEntry.h"
#include "Directory.h" #include "Directory.h"
#include "DiskWriter.h" #include "DiskWriter.h"
#include "Logger.h"
class DiskAdaptor { class DiskAdaptor {
protected: protected:
@ -33,15 +34,11 @@ protected:
string storeDir; string storeDir;
FileEntries fileEntries; FileEntries fileEntries;
const Directory* topDir; const Directory* topDir;
const Logger* logger;
virtual string getFilePath() const = 0; virtual string getFilePath() const = 0;
public: public:
DiskAdaptor(DiskWriter* diskWriter):diskWriter(diskWriter), topDir(NULL) {} DiskAdaptor(DiskWriter* diskWriter);
virtual ~DiskAdaptor() { virtual ~DiskAdaptor();
delete diskWriter;
if(topDir != NULL) {
delete topDir;
}
}
virtual void openFile(); virtual void openFile();
virtual void closeFile(); virtual void closeFile();

View File

@ -74,7 +74,7 @@ bool DownloadCommand::executeInternal(Segment seg) {
|| te == NULL && seg.ds >= seg.ep-seg.sp+1 || te == NULL && seg.ds >= seg.ep-seg.sp+1
|| bufSize == 0) { || bufSize == 0) {
if(te != NULL) te->end(); if(te != NULL) te->end();
e->logger->info(MSG_DOWNLOAD_COMPLETED, cuid); logger->info(MSG_DOWNLOAD_COMPLETED, cuid);
seg.ds = seg.ep-seg.sp+1; seg.ds = seg.ep-seg.sp+1;
seg.finish = true; seg.finish = true;
e->segmentMan->updateSegment(seg); e->segmentMan->updateSegment(seg);

View File

@ -21,6 +21,7 @@
/* copyright --> */ /* copyright --> */
#include "DownloadEngine.h" #include "DownloadEngine.h"
#include "Util.h" #include "Util.h"
#include "LogFactory.h"
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -29,7 +30,9 @@
using namespace std; using namespace std;
DownloadEngine::DownloadEngine():noWait(false), segmentMan(NULL) {} DownloadEngine::DownloadEngine():noWait(false), segmentMan(NULL) {
logger = LogFactory::getInstance();
}
DownloadEngine::~DownloadEngine() { DownloadEngine::~DownloadEngine() {
assert(rsockets.empty()); assert(rsockets.empty());

View File

@ -48,6 +48,7 @@ private:
bool addSocket(Sockets& sockets, Socket* socket); bool addSocket(Sockets& sockets, Socket* socket);
bool deleteSocket(Sockets& sockets, Socket* socket); bool deleteSocket(Sockets& sockets, Socket* socket);
protected: protected:
const Logger* logger;
virtual void initStatistics() = 0; virtual void initStatistics() = 0;
virtual void calculateStatistics() = 0; virtual void calculateStatistics() = 0;
virtual void onEndOfRun() = 0; virtual void onEndOfRun() = 0;
@ -57,7 +58,6 @@ public:
Commands commands; Commands commands;
SegmentMan* segmentMan; SegmentMan* segmentMan;
DiskWriter* diskWriter; DiskWriter* diskWriter;
const Logger* logger;
const Option* option; const Option* option;
DownloadEngine(); DownloadEngine();

View File

@ -25,8 +25,11 @@
#include "DlRetryEx.h" #include "DlRetryEx.h"
#include "message.h" #include "message.h"
#include "prefs.h" #include "prefs.h"
#include "LogFactory.h"
FtpConnection::FtpConnection(int cuid, const Socket* socket, const Request* req, const Option* op, const Logger* logger):cuid(cuid), socket(socket), req(req), option(op), logger(logger) {} FtpConnection::FtpConnection(int cuid, const Socket* socket, const Request* req, const Option* op):cuid(cuid), socket(socket), req(req), option(op) {
logger = LogFactory::getInstance();
}
FtpConnection::~FtpConnection() {} FtpConnection::~FtpConnection() {}

View File

@ -46,7 +46,7 @@ private:
bool isEndOfResponse(int status, const string& response) const; bool isEndOfResponse(int status, const string& response) const;
bool bulkReceiveResponse(pair<int, string>& response); bool bulkReceiveResponse(pair<int, string>& response);
public: public:
FtpConnection(int cuid, const Socket* socket, const Request* req, const Option* op, const Logger* logger); FtpConnection(int cuid, const Socket* socket, const Request* req, const Option* op);
~FtpConnection(); ~FtpConnection();
void sendUser() const; void sendUser() const;
void sendPass() const; void sendPass() const;

View File

@ -47,9 +47,9 @@ bool FtpInitiateConnectionCommand::executeInternal(Segment segment) {
socket = new Socket(); socket = new Socket();
Command* command; Command* command;
if(useHttpProxy()) { if(useHttpProxy()) {
e->logger->info(MSG_CONNECTING_TO_SERVER, cuid, logger->info(MSG_CONNECTING_TO_SERVER, cuid,
e->option->get(PREF_HTTP_PROXY_HOST).c_str(), e->option->get(PREF_HTTP_PROXY_HOST).c_str(),
e->option->getAsInt(PREF_HTTP_PROXY_PORT)); e->option->getAsInt(PREF_HTTP_PROXY_PORT));
socket->establishConnection(e->option->get(PREF_HTTP_PROXY_HOST), socket->establishConnection(e->option->get(PREF_HTTP_PROXY_HOST),
e->option->getAsInt(PREF_HTTP_PROXY_PORT)); e->option->getAsInt(PREF_HTTP_PROXY_PORT));
@ -62,8 +62,8 @@ bool FtpInitiateConnectionCommand::executeInternal(Segment segment) {
throw new DlAbortEx("ERROR"); throw new DlAbortEx("ERROR");
} }
} else { } else {
e->logger->info(MSG_CONNECTING_TO_SERVER, cuid, req->getHost().c_str(), logger->info(MSG_CONNECTING_TO_SERVER, cuid, req->getHost().c_str(),
req->getPort()); req->getPort());
socket->establishConnection(req->getHost(), req->getPort()); socket->establishConnection(req->getHost(), req->getPort());
command = new FtpNegotiationCommand(cuid, req, e, socket); command = new FtpNegotiationCommand(cuid, req, e, socket);
} }

View File

@ -30,7 +30,7 @@ FtpNegotiationCommand::FtpNegotiationCommand(int cuid, Request* req, DownloadEng
AbstractCommand(cuid, req, e, s), AbstractCommand(cuid, req, e, s),
dataSocket(NULL), serverSocket(NULL), sequence(SEQ_RECV_GREETING) dataSocket(NULL), serverSocket(NULL), sequence(SEQ_RECV_GREETING)
{ {
ftp = new FtpConnection(cuid, socket, req, e->option, e->logger); ftp = new FtpConnection(cuid, socket, req, e->option);
setReadCheckSocket(NULL); setReadCheckSocket(NULL);
setWriteCheckSocket(socket); setWriteCheckSocket(socket);
} }
@ -221,9 +221,9 @@ bool FtpNegotiationCommand::recvPasv() {
// make a data connection to the server. // make a data connection to the server.
dataSocket = new Socket(); dataSocket = new Socket();
e->logger->info(MSG_CONNECTING_TO_SERVER, cuid, logger->info(MSG_CONNECTING_TO_SERVER, cuid,
dest.first.c_str(), dest.first.c_str(),
dest.second); dest.second);
dataSocket->establishConnection(dest.first, dest.second); dataSocket->establishConnection(dest.first, dest.second);
setReadCheckSocket(NULL); setReadCheckSocket(NULL);

View File

@ -32,7 +32,7 @@ FtpTunnelRequestCommand::~FtpTunnelRequestCommand() {}
bool FtpTunnelRequestCommand::executeInternal(Segment segment) { bool FtpTunnelRequestCommand::executeInternal(Segment segment) {
socket->setBlockingMode(); socket->setBlockingMode();
HttpConnection httpConnection(cuid, socket, req, e->option, e->logger); HttpConnection httpConnection(cuid, socket, req, e->option);
httpConnection.sendProxyRequest(); httpConnection.sendProxyRequest();
FtpTunnelResponseCommand* command = new FtpTunnelResponseCommand(cuid, req, e, socket); FtpTunnelResponseCommand* command = new FtpTunnelResponseCommand(cuid, req, e, socket);

View File

@ -25,7 +25,7 @@
#include "message.h" #include "message.h"
FtpTunnelResponseCommand::FtpTunnelResponseCommand(int cuid, Request* req, DownloadEngine* e, const Socket* s):AbstractCommand(cuid, req, e, s) { FtpTunnelResponseCommand::FtpTunnelResponseCommand(int cuid, Request* req, DownloadEngine* e, const Socket* s):AbstractCommand(cuid, req, e, s) {
http = new HttpConnection(cuid, socket, req, e->option, e->logger); http = new HttpConnection(cuid, socket, req, e->option);
} }
FtpTunnelResponseCommand::~FtpTunnelResponseCommand() { FtpTunnelResponseCommand::~FtpTunnelResponseCommand() {

View File

@ -25,9 +25,12 @@
#include "Base64.h" #include "Base64.h"
#include "message.h" #include "message.h"
#include "prefs.h" #include "prefs.h"
#include "LogFactory.h"
HttpConnection::HttpConnection(int cuid, const Socket* socket, const Request* req, const Option* op, const Logger* logger): HttpConnection::HttpConnection(int cuid, const Socket* socket, const Request* req, const Option* op):
cuid(cuid), socket(socket), req(req), option(op), logger(logger), headerBufLength(0) {} cuid(cuid), socket(socket), req(req), option(op), headerBufLength(0) {
logger = LogFactory::getInstance();
}
void HttpConnection::sendRequest(const Segment& segment) const { void HttpConnection::sendRequest(const Segment& segment) const {
string request = createRequest(segment); string request = createRequest(segment);

View File

@ -29,6 +29,7 @@
#include "Logger.h" #include "Logger.h"
#include "HttpHeader.h" #include "HttpHeader.h"
#include "common.h" #include "common.h"
#include "Logger.h"
#include <string> #include <string>
using namespace std; using namespace std;
@ -52,7 +53,7 @@ private:
char headerBuf[HEADERBUF_SIZE+1]; char headerBuf[HEADERBUF_SIZE+1];
int headerBufLength; int headerBufLength;
public: public:
HttpConnection(int cuid, const Socket* socket, const Request* req, const Option* op, const Logger* logger); HttpConnection(int cuid, const Socket* socket, const Request* req, const Option* op);
/** /**
* Sends Http request. * Sends Http request.

View File

@ -36,9 +36,9 @@ bool HttpInitiateConnectionCommand::executeInternal(Segment segment) {
// socket->establishConnection(...); // socket->establishConnection(...);
Command* command; Command* command;
if(useProxy()) { if(useProxy()) {
e->logger->info(MSG_CONNECTING_TO_SERVER, cuid, logger->info(MSG_CONNECTING_TO_SERVER, cuid,
e->option->get(PREF_HTTP_PROXY_HOST).c_str(), e->option->get(PREF_HTTP_PROXY_HOST).c_str(),
e->option->getAsInt(PREF_HTTP_PROXY_PORT)); e->option->getAsInt(PREF_HTTP_PROXY_PORT));
socket->establishConnection(e->option->get(PREF_HTTP_PROXY_HOST), socket->establishConnection(e->option->get(PREF_HTTP_PROXY_HOST),
e->option->getAsInt(PREF_HTTP_PROXY_PORT)); e->option->getAsInt(PREF_HTTP_PROXY_PORT));
if(useProxyTunnel()) { if(useProxyTunnel()) {
@ -50,8 +50,8 @@ bool HttpInitiateConnectionCommand::executeInternal(Segment segment) {
throw new DlAbortEx("ERROR"); throw new DlAbortEx("ERROR");
} }
} else { } else {
e->logger->info(MSG_CONNECTING_TO_SERVER, cuid, req->getHost().c_str(), logger->info(MSG_CONNECTING_TO_SERVER, cuid, req->getHost().c_str(),
req->getPort()); req->getPort());
socket->establishConnection(req->getHost(), req->getPort()); socket->establishConnection(req->getHost(), req->getPort());
command = new HttpRequestCommand(cuid, req, e, socket); command = new HttpRequestCommand(cuid, req, e, socket);
} }

View File

@ -32,7 +32,7 @@ HttpProxyRequestCommand::~HttpProxyRequestCommand() {}
bool HttpProxyRequestCommand::executeInternal(Segment segment) { bool HttpProxyRequestCommand::executeInternal(Segment segment) {
socket->setBlockingMode(); socket->setBlockingMode();
HttpConnection httpConnection(cuid, socket, req, e->option, e->logger); HttpConnection httpConnection(cuid, socket, req, e->option);
httpConnection.sendProxyRequest(); httpConnection.sendProxyRequest();
HttpProxyResponseCommand* command = new HttpProxyResponseCommand(cuid, req, e, socket); HttpProxyResponseCommand* command = new HttpProxyResponseCommand(cuid, req, e, socket);

View File

@ -25,7 +25,7 @@
#include "message.h" #include "message.h"
HttpProxyResponseCommand::HttpProxyResponseCommand(int cuid, Request* req, DownloadEngine* e, const Socket* s):AbstractCommand(cuid, req, e, s) { HttpProxyResponseCommand::HttpProxyResponseCommand(int cuid, Request* req, DownloadEngine* e, const Socket* s):AbstractCommand(cuid, req, e, s) {
http = new HttpConnection(cuid, socket, req, e->option, e->logger); http = new HttpConnection(cuid, socket, req, e->option);
} }
HttpProxyResponseCommand::~HttpProxyResponseCommand() { HttpProxyResponseCommand::~HttpProxyResponseCommand() {

View File

@ -35,7 +35,7 @@ bool HttpRequestCommand::executeInternal(Segment seg) {
if(req->getProtocol() == "https") { if(req->getProtocol() == "https") {
socket->initiateSecureConnection(); socket->initiateSecureConnection();
} }
HttpConnection http(cuid, socket, req, e->option, e->logger); HttpConnection http(cuid, socket, req, e->option);
// set seg to request in order to remember the request range // set seg to request in order to remember the request range
req->seg = seg; req->seg = seg;
http.sendRequest(seg); http.sendRequest(seg);

View File

@ -32,7 +32,7 @@
HttpResponseCommand::HttpResponseCommand(int cuid, Request* req, DownloadEngine* e, const Socket* s): HttpResponseCommand::HttpResponseCommand(int cuid, Request* req, DownloadEngine* e, const Socket* s):
AbstractCommand(cuid, req, e, s) { AbstractCommand(cuid, req, e, s) {
http = new HttpConnection(cuid, socket, req, e->option, e->logger); http = new HttpConnection(cuid, socket, req, e->option);
} }
HttpResponseCommand::~HttpResponseCommand() { HttpResponseCommand::~HttpResponseCommand() {
@ -41,7 +41,7 @@ HttpResponseCommand::~HttpResponseCommand() {
bool HttpResponseCommand::executeInternal(Segment seg) { bool HttpResponseCommand::executeInternal(Segment seg) {
if(req->seg.sp != seg.sp) { if(req->seg.sp != seg.sp) {
e->logger->info(MSG_SEGMENT_CHANGED, cuid); logger->info(MSG_SEGMENT_CHANGED, cuid);
return prepareForRetry(0); return prepareForRetry(0);
} }
HttpHeader headers; HttpHeader headers;
@ -86,7 +86,7 @@ void HttpResponseCommand::checkResponse(int status, const Segment& segment) {
bool HttpResponseCommand::handleRedirect(string url, const HttpHeader& headers) { bool HttpResponseCommand::handleRedirect(string url, const HttpHeader& headers) {
req->redirectUrl(url); req->redirectUrl(url);
e->logger->info(MSG_REDIRECT, cuid, url.c_str()); logger->info(MSG_REDIRECT, cuid, url.c_str());
e->noWait = true; e->noWait = true;
return prepareForRetry(0); return prepareForRetry(0);
} }

37
src/LogFactory.cc Normal file
View File

@ -0,0 +1,37 @@
/* <!-- copyright */
/*
* aria2 - a simple utility for downloading files faster
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* 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
*/
/* copyright --> */
#include "LogFactory.h"
#include "SimpleLogger.h"
string LogFactory::filename;
Logger* LogFactory::logger = NULL;
Logger* LogFactory::getInstance() {
if(logger == NULL) {
if(filename.empty()) {
logger = new SimpleLogger("/dev/null");
} else {
logger = new SimpleLogger(filename);
}
}
return logger;
}

47
src/LogFactory.h Normal file
View File

@ -0,0 +1,47 @@
/* <!-- copyright */
/*
* aria2 - a simple utility for downloading files faster
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* 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
*/
/* copyright --> */
#ifndef _D_LOG_FACTORY_H_
#define _D_LOG_FACTORY_H_
#include "common.h"
#include "Logger.h"
class LogFactory {
private:
static string filename;
static Logger* logger;
public:
/**
* Get logger instance. Returned logger is singleton.
* This function is not thread-safe.
*/
static Logger* getInstance();
/**
* Set a filename to write log.
*/
static void setLogFile(const string& name) {
filename = name;
}
};
#endif // _D_LOG_FACTORY_H_

View File

@ -84,7 +84,8 @@ SRCS = Socket.cc Socket.h\
CopyDiskAdaptor.cc CopyDiskAdaptor.h\ CopyDiskAdaptor.cc CopyDiskAdaptor.h\
DirectDiskAdaptor.cc DirectDiskAdaptor.h\ DirectDiskAdaptor.cc DirectDiskAdaptor.h\
MultiDiskAdaptor.cc MultiDiskAdaptor.h\ MultiDiskAdaptor.cc MultiDiskAdaptor.h\
FileEntry.h FileEntry.h\
LogFactory.cc LogFactory.h
noinst_LIBRARIES = libaria2c.a noinst_LIBRARIES = libaria2c.a
libaria2c_a_SOURCES = $(SRCS) libaria2c_a_SOURCES = $(SRCS)
aria2c_LDADD = libaria2c.a @LIBINTL@ @ALLOCA@ @LIBGNUTLS_LIBS@\ aria2c_LDADD = libaria2c.a @LIBINTL@ @ALLOCA@ @LIBGNUTLS_LIBS@\

View File

@ -100,7 +100,8 @@ am__objects_1 = Socket.$(OBJEXT) SocketCore.$(OBJEXT) \
Directory.$(OBJEXT) TrackerWatcherCommand.$(OBJEXT) \ Directory.$(OBJEXT) TrackerWatcherCommand.$(OBJEXT) \
SendMessageQueue.$(OBJEXT) MultiDiskWriter.$(OBJEXT) \ SendMessageQueue.$(OBJEXT) MultiDiskWriter.$(OBJEXT) \
DiskAdaptor.$(OBJEXT) CopyDiskAdaptor.$(OBJEXT) \ DiskAdaptor.$(OBJEXT) CopyDiskAdaptor.$(OBJEXT) \
DirectDiskAdaptor.$(OBJEXT) MultiDiskAdaptor.$(OBJEXT) DirectDiskAdaptor.$(OBJEXT) MultiDiskAdaptor.$(OBJEXT) \
LogFactory.$(OBJEXT)
am_libaria2c_a_OBJECTS = $(am__objects_1) am_libaria2c_a_OBJECTS = $(am__objects_1)
libaria2c_a_OBJECTS = $(am_libaria2c_a_OBJECTS) libaria2c_a_OBJECTS = $(am_libaria2c_a_OBJECTS)
am__installdirs = "$(DESTDIR)$(bindir)" am__installdirs = "$(DESTDIR)$(bindir)"
@ -334,7 +335,8 @@ SRCS = Socket.cc Socket.h\
CopyDiskAdaptor.cc CopyDiskAdaptor.h\ CopyDiskAdaptor.cc CopyDiskAdaptor.h\
DirectDiskAdaptor.cc DirectDiskAdaptor.h\ DirectDiskAdaptor.cc DirectDiskAdaptor.h\
MultiDiskAdaptor.cc MultiDiskAdaptor.h\ MultiDiskAdaptor.cc MultiDiskAdaptor.h\
FileEntry.h FileEntry.h\
LogFactory.cc LogFactory.h
noinst_LIBRARIES = libaria2c.a noinst_LIBRARIES = libaria2c.a
libaria2c_a_SOURCES = $(SRCS) libaria2c_a_SOURCES = $(SRCS)
@ -453,6 +455,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HttpResponseCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HttpResponseCommand.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/InitiateConnectionCommandFactory.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/InitiateConnectionCommandFactory.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/List.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/List.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/LogFactory.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/MetaFileUtil.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/MetaFileUtil.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/MultiDiskAdaptor.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/MultiDiskAdaptor.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/MultiDiskWriter.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/MultiDiskWriter.Po@am__quote@

View File

@ -22,10 +22,10 @@
#include "PeerAbstractCommand.h" #include "PeerAbstractCommand.h"
#include "DlAbortEx.h" #include "DlAbortEx.h"
#include "DlRetryEx.h" #include "DlRetryEx.h"
#include <sys/time.h>
#include "Util.h" #include "Util.h"
#include "message.h" #include "message.h"
#include "prefs.h" #include "prefs.h"
#include <sys/time.h>
PeerAbstractCommand::PeerAbstractCommand(int cuid, Peer* peer, TorrentDownloadEngine* e, const Socket* s): PeerAbstractCommand::PeerAbstractCommand(int cuid, Peer* peer, TorrentDownloadEngine* e, const Socket* s):
Command(cuid), e(e), peer(peer), checkSocketIsReadable(false), checkSocketIsWritable(false) { Command(cuid), e(e), peer(peer), checkSocketIsReadable(false), checkSocketIsWritable(false) {
@ -90,13 +90,13 @@ bool PeerAbstractCommand::execute() {
//e->torrentMan->updatePeer(peer); //e->torrentMan->updatePeer(peer);
return returnValue; return returnValue;
} catch(Exception* err) { } catch(Exception* err) {
e->logger->error(MSG_DOWNLOAD_ABORTED, err, cuid); logger->error(MSG_DOWNLOAD_ABORTED, err, cuid);
onAbort(err); onAbort(err);
delete(err); delete(err);
return prepareForNextPeer(0); return prepareForNextPeer(0);
} }
/*catch(DlRetryEx* err) { /*catch(DlRetryEx* err) {
e->logger->error(MSG_RESTARTING_DOWNLOAD, err, cuid); logger->error(MSG_RESTARTING_DOWNLOAD, err, cuid);
peer->tryCount++; peer->tryCount++;
bool isAbort = e->option->getAsInt(PREF_MAX_TRIES) != 0 && bool isAbort = e->option->getAsInt(PREF_MAX_TRIES) != 0 &&
peer->tryCount >= e->option->getAsInt(PREF_MAX_TRIES); peer->tryCount >= e->option->getAsInt(PREF_MAX_TRIES);
@ -106,7 +106,7 @@ bool PeerAbstractCommand::execute() {
} }
delete(err); delete(err);
if(isAbort) { if(isAbort) {
e->logger->error(MSG_MAX_TRY, cuid, tryCount); logger->error(MSG_MAX_TRY, cuid, tryCount);
return true; return true;
} else { } else {
return prepareForRetry(e->option->getAsInt(PREF_RETRY_WAIT)); return prepareForRetry(e->option->getAsInt(PREF_RETRY_WAIT));
@ -136,7 +136,7 @@ void PeerAbstractCommand::onAbort(Exception* ex) {
peer->amInterested = false; peer->amInterested = false;
peer->peerChoking = true; peer->peerChoking = true;
peer->peerInterested = false; peer->peerInterested = false;
e->logger->debug("CUID#%d - peer %s:%d banned.", cuid, peer->ipaddr.c_str(), peer->port); logger->debug("CUID#%d - peer %s:%d banned.", cuid, peer->ipaddr.c_str(), peer->port);
} }
void PeerAbstractCommand::setReadCheckSocket(Socket* socket) { void PeerAbstractCommand::setReadCheckSocket(Socket* socket) {

View File

@ -24,14 +24,17 @@
#include "DlAbortEx.h" #include "DlAbortEx.h"
#include "PeerMessageUtil.h" #include "PeerMessageUtil.h"
#include "Util.h" #include "Util.h"
#include "LogFactory.h"
#include <netinet/in.h> #include <netinet/in.h>
PeerConnection::PeerConnection(int cuid, const Socket* socket, PeerConnection::PeerConnection(int cuid, const Socket* socket,
const Option* op, const Logger* logger, const Option* op,
Peer* peer, TorrentMan* torrentMan) Peer* peer, TorrentMan* torrentMan)
:cuid(cuid), socket(socket), option(op), logger(logger), peer(peer), :cuid(cuid), socket(socket), option(op), logger(logger), peer(peer),
torrentMan(torrentMan), torrentMan(torrentMan),
resbufLength(0), currentPayloadLength(0), lenbufLength(0) {} resbufLength(0), currentPayloadLength(0), lenbufLength(0) {
logger = LogFactory::getInstance();
}
PeerConnection::~PeerConnection() {} PeerConnection::~PeerConnection() {}

View File

@ -57,7 +57,7 @@ private:
void writeOutgoingMessageLog(const char* msg, int index, int begin, int length) const; void writeOutgoingMessageLog(const char* msg, int index, int begin, int length) const;
public: public:
PeerConnection(int cuid, const Socket* socket, const Option* op, PeerConnection(int cuid, const Socket* socket, const Option* op,
const Logger* logger, Peer* peer, TorrentMan* torrenMan); Peer* peer, TorrentMan* torrenMan);
~PeerConnection(); ~PeerConnection();
void sendHandshake() const; void sendHandshake() const;

View File

@ -38,8 +38,8 @@ bool PeerInitiateConnectionCommand::executeInternal() {
// socket->establishConnection(...); // socket->establishConnection(...);
Command* command; Command* command;
e->logger->info(MSG_CONNECTING_TO_SERVER, cuid, peer->ipaddr.c_str(), logger->info(MSG_CONNECTING_TO_SERVER, cuid, peer->ipaddr.c_str(),
peer->port); peer->port);
socket->establishConnection(peer->ipaddr, peer->port); socket->establishConnection(peer->ipaddr, peer->port);
command = new PeerInteractionCommand(cuid, peer, e, socket, PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE); command = new PeerInteractionCommand(cuid, peer, e, socket, PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE);

View File

@ -37,10 +37,8 @@ PeerInteractionCommand::PeerInteractionCommand(int cuid, Peer* peer,
setWriteCheckSocket(socket); setWriteCheckSocket(socket);
setTimeout(e->option->getAsInt(PREF_PEER_CONNECTION_TIMEOUT)); setTimeout(e->option->getAsInt(PREF_PEER_CONNECTION_TIMEOUT));
} }
peerConnection = new PeerConnection(cuid, socket, e->option, e->logger, peerConnection = new PeerConnection(cuid, socket, e->option, peer, e->torrentMan);
peer, e->torrentMan); sendMessageQueue = new SendMessageQueue(cuid, peerConnection, e->torrentMan);
sendMessageQueue = new SendMessageQueue(cuid, peerConnection, e->torrentMan,
e->logger);
piece = Piece::nullPiece; piece = Piece::nullPiece;
keepAliveCheckPoint.tv_sec = 0; keepAliveCheckPoint.tv_sec = 0;
keepAliveCheckPoint.tv_usec = 0; keepAliveCheckPoint.tv_usec = 0;
@ -78,9 +76,9 @@ bool PeerInteractionCommand::executeInternal() {
break; break;
} }
peer->setPeerId(handshakeMessage->peerId); peer->setPeerId(handshakeMessage->peerId);
e->logger->info(MSG_RECEIVE_PEER_MESSAGE, cuid, logger->info(MSG_RECEIVE_PEER_MESSAGE, cuid,
peer->ipaddr.c_str(), peer->port, peer->ipaddr.c_str(), peer->port,
handshakeMessage->toString().c_str()); handshakeMessage->toString().c_str());
delete handshakeMessage; delete handshakeMessage;
if(e->torrentMan->getDownloadLength() > 0) { if(e->torrentMan->getDownloadLength() > 0) {
peerConnection->sendBitfield(); peerConnection->sendBitfield();
@ -94,9 +92,9 @@ bool PeerInteractionCommand::executeInternal() {
break; break;
} }
peer->setPeerId(handshakeMessage->peerId); peer->setPeerId(handshakeMessage->peerId);
e->logger->info(MSG_RECEIVE_PEER_MESSAGE, cuid, logger->info(MSG_RECEIVE_PEER_MESSAGE, cuid,
peer->ipaddr.c_str(), peer->port, peer->ipaddr.c_str(), peer->port,
handshakeMessage->toString().c_str()); handshakeMessage->toString().c_str());
delete handshakeMessage; delete handshakeMessage;
peerConnection->sendHandshake(); peerConnection->sendHandshake();
if(e->torrentMan->getDownloadLength() > 0) { if(e->torrentMan->getDownloadLength() > 0) {
@ -217,9 +215,9 @@ void PeerInteractionCommand::receiveMessage() {
if(message == NULL) { if(message == NULL) {
return; return;
} }
e->logger->info(MSG_RECEIVE_PEER_MESSAGE, cuid, logger->info(MSG_RECEIVE_PEER_MESSAGE, cuid,
peer->ipaddr.c_str(), peer->port, peer->ipaddr.c_str(), peer->port,
message->toString().c_str()); message->toString().c_str());
try { try {
switch(message->getId()) { switch(message->getId()) {
case PeerMessage::KEEP_ALIVE: case PeerMessage::KEEP_ALIVE:
@ -272,16 +270,16 @@ void PeerInteractionCommand::receiveMessage() {
if(!Piece::isNull(piece) && !RequestSlot::isNull(slot)) { if(!Piece::isNull(piece) && !RequestSlot::isNull(slot)) {
long long int offset = long long int offset =
((long long int)message->getIndex())*e->torrentMan->pieceLength+message->getBegin(); ((long long int)message->getIndex())*e->torrentMan->pieceLength+message->getBegin();
e->logger->debug("CUID#%d - write block length = %d, offset=%lld", logger->debug("CUID#%d - write block length = %d, offset=%lld",
cuid, message->getBlockLength(), offset); cuid, message->getBlockLength(), offset);
e->torrentMan->diskAdaptor->writeData(message->getBlock(), e->torrentMan->diskAdaptor->writeData(message->getBlock(),
message->getBlockLength(), message->getBlockLength(),
offset); offset);
piece.completeBlock(slot.getBlockIndex()); piece.completeBlock(slot.getBlockIndex());
sendMessageQueue->deleteRequestSlot(slot); sendMessageQueue->deleteRequestSlot(slot);
e->torrentMan->updatePiece(piece); e->torrentMan->updatePiece(piece);
e->logger->debug("CUID#%d - setting piece bit index=%d", cuid, logger->debug("CUID#%d - setting piece bit index=%d", cuid,
slot.getBlockIndex()); slot.getBlockIndex());
e->torrentMan->addDeltaDownloadLength(message->getBlockLength()); e->torrentMan->addDeltaDownloadLength(message->getBlockLength());
if(piece.pieceComplete()) { if(piece.pieceComplete()) {
if(checkPieceHash(piece)) { if(checkPieceHash(piece)) {
@ -302,14 +300,14 @@ void PeerInteractionCommand::receiveMessage() {
} }
void PeerInteractionCommand::onGotNewPiece() { void PeerInteractionCommand::onGotNewPiece() {
e->logger->info(MSG_GOT_NEW_PIECE, cuid, piece.getIndex()); logger->info(MSG_GOT_NEW_PIECE, cuid, piece.getIndex());
e->torrentMan->completePiece(piece); e->torrentMan->completePiece(piece);
e->torrentMan->advertisePiece(cuid, piece.getIndex()); e->torrentMan->advertisePiece(cuid, piece.getIndex());
piece = Piece::nullPiece; piece = Piece::nullPiece;
} }
void PeerInteractionCommand::onGotWrongPiece() { void PeerInteractionCommand::onGotWrongPiece() {
e->logger->error(MSG_GOT_WRONG_PIECE, cuid, piece.getIndex()); logger->error(MSG_GOT_WRONG_PIECE, cuid, piece.getIndex());
erasePieceOnDisk(piece); erasePieceOnDisk(piece);
piece.clearAllBlock(); piece.clearAllBlock();
e->torrentMan->updatePiece(piece); e->torrentMan->updatePiece(piece);
@ -336,12 +334,12 @@ Piece PeerInteractionCommand::getNewPieceAndSendInterest() {
sendMessageQueue->cancelAllRequest(); sendMessageQueue->cancelAllRequest();
Piece piece = e->torrentMan->getMissingPiece(peer); Piece piece = e->torrentMan->getMissingPiece(peer);
if(Piece::isNull(piece)) { if(Piece::isNull(piece)) {
e->logger->debug("CUID#%d - try to send not-interested", cuid); logger->debug("CUID#%d - try to send not-interested", cuid);
PendingMessage pendingMessage(PeerMessage::NOT_INTERESTED, peerConnection); PendingMessage pendingMessage(PeerMessage::NOT_INTERESTED, peerConnection);
sendMessageQueue->addPendingMessage(pendingMessage); sendMessageQueue->addPendingMessage(pendingMessage);
} else { } else {
e->logger->debug("CUID#%d - starting download for piece index=%d", cuid, piece.getIndex()); logger->debug("CUID#%d - starting download for piece index=%d", cuid, piece.getIndex());
e->logger->debug("CUID#%d - try to send interested", cuid); logger->debug("CUID#%d - try to send interested", cuid);
PendingMessage pendingMessage(PeerMessage::INTERESTED, peerConnection); PendingMessage pendingMessage(PeerMessage::INTERESTED, peerConnection);
sendMessageQueue->addPendingMessage(pendingMessage); sendMessageQueue->addPendingMessage(pendingMessage);
} }

View File

@ -23,8 +23,7 @@
#include "PeerInteractionCommand.h" #include "PeerInteractionCommand.h"
PeerListenCommand::PeerListenCommand(int cuid, TorrentDownloadEngine* e) PeerListenCommand::PeerListenCommand(int cuid, TorrentDownloadEngine* e)
:Command(cuid), e(e), socket(NULL) { :Command(cuid), e(e), socket(NULL) {}
}
PeerListenCommand::~PeerListenCommand() { PeerListenCommand::~PeerListenCommand() {
if(socket != NULL) { if(socket != NULL) {
@ -40,12 +39,12 @@ int PeerListenCommand::bindPort(int portRangeStart, int portRangeEnd) {
try { try {
socket = new Socket(); socket = new Socket();
socket->beginListen(port); socket->beginListen(port);
e->logger->info("CUID#%d - using port %d for accepting new connections", logger->info("CUID#%d - using port %d for accepting new connections",
cuid, port); cuid, port);
return port; return port;
} catch(Exception* ex) { } catch(Exception* ex) {
e->logger->error("CUID#%d - an error occurred while binding port=%d", logger->error("CUID#%d - an error occurred while binding port=%d",
ex, cuid, port); ex, cuid, port);
delete ex; delete ex;
delete socket; delete socket;
socket = NULL; socket = NULL;
@ -75,14 +74,14 @@ bool PeerListenCommand::execute() {
new PeerInteractionCommand(newCuid, peer, e, peerSocket, new PeerInteractionCommand(newCuid, peer, e, peerSocket,
PeerInteractionCommand::RECEIVER_WAIT_HANDSHAKE); PeerInteractionCommand::RECEIVER_WAIT_HANDSHAKE);
e->commands.push(command); e->commands.push(command);
e->logger->debug("CUID#%d - incoming connection, adding new command CUID#%d", cuid, newCuid); logger->debug("CUID#%d - incoming connection, adding new command CUID#%d", cuid, newCuid);
} else { } else {
delete peer; delete peer;
} }
} }
delete peerSocket; delete peerSocket;
} catch(Exception* ex) { } catch(Exception* ex) {
e->logger->error("CUID#%d - error in accepting connection", ex, cuid); logger->error("CUID#%d - error in accepting connection", ex, cuid);
delete ex; delete ex;
if(peerSocket != NULL) { if(peerSocket != NULL) {
delete peerSocket; delete peerSocket;

View File

@ -29,7 +29,6 @@ class PeerListenCommand : public Command {
private: private:
TorrentDownloadEngine* e; TorrentDownloadEngine* e;
Socket* socket; Socket* socket;
public: public:
PeerListenCommand(int cuid, TorrentDownloadEngine* e); PeerListenCommand(int cuid, TorrentDownloadEngine* e);
~PeerListenCommand(); ~PeerListenCommand();

View File

@ -20,6 +20,19 @@
*/ */
/* copyright --> */ /* copyright --> */
#include "RequestSlotMan.h" #include "RequestSlotMan.h"
#include "LogFactory.h"
RequestSlotMan::RequestSlotMan(int cuid,
PendingMessages* pendingMessages,
PeerConnection* peerConnection,
TorrentMan* torrentMan):
cuid(cuid),
timeout(DEFAULT_TIME_OUT),
pendingMessages(pendingMessages),
peerConnection(peerConnection),
torrentMan(torrentMan) {
logger = LogFactory::getInstance();
}
void RequestSlotMan::addRequestSlot(const RequestSlot& requestSlot) { void RequestSlotMan::addRequestSlot(const RequestSlot& requestSlot) {
requestSlots.push_back(requestSlot); requestSlots.push_back(requestSlot);

View File

@ -48,10 +48,7 @@ public:
RequestSlotMan(int cuid, RequestSlotMan(int cuid,
PendingMessages* pendingMessages, PendingMessages* pendingMessages,
PeerConnection* peerConnection, PeerConnection* peerConnection,
TorrentMan* torrentMan, TorrentMan* torrentMan);
const Logger* logger):cuid(cuid), timeout(DEFAULT_TIME_OUT),
pendingMessages(pendingMessages), peerConnection(peerConnection),
torrentMan(torrentMan), logger(logger) {}
~RequestSlotMan() {} ~RequestSlotMan() {}
void addRequestSlot(const RequestSlot& requestSlot); void addRequestSlot(const RequestSlot& requestSlot);

View File

@ -21,16 +21,19 @@
/* copyright --> */ /* copyright --> */
#include "SegmentMan.h" #include "SegmentMan.h"
#include "DlAbortEx.h" #include "DlAbortEx.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include "Util.h" #include "Util.h"
#include "File.h" #include "File.h"
#include "message.h" #include "message.h"
#include "prefs.h" #include "prefs.h"
#include "LogFactory.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
SegmentMan::SegmentMan():totalSize(0),isSplittable(true),downloadStarted(false),dir(".") {} SegmentMan::SegmentMan():totalSize(0),isSplittable(true),downloadStarted(false),dir(".") {
logger = LogFactory::getInstance();
}
SegmentMan::~SegmentMan() {} SegmentMan::~SegmentMan() {}

View File

@ -37,6 +37,8 @@ using namespace std;
*/ */
class SegmentMan { class SegmentMan {
private: private:
const Logger* logger;
void read(FILE* file); void read(FILE* file);
FILE* openSegFile(string segFilename, string mode) const; FILE* openSegFile(string segFilename, string mode) const;
public: public:
@ -81,7 +83,6 @@ public:
*/ */
string ufilename; string ufilename;
const Logger* logger;
const Option* option; const Option* option;
SegmentSplitter* splitter; SegmentSplitter* splitter;

View File

@ -21,6 +21,11 @@
/* copyright --> */ /* copyright --> */
#include "SegmentSplitter.h" #include "SegmentSplitter.h"
#include "Util.h" #include "Util.h"
#include "LogFactory.h"
SegmentSplitter::SegmentSplitter() {
logger = LogFactory::getInstance();
}
void SegmentSplitter::split(Segment& seg, int cuid, Segment& s) const { void SegmentSplitter::split(Segment& seg, int cuid, Segment& s) const {
long long int nep = (s.ep-(s.sp+s.ds))/2+(s.sp+s.ds); long long int nep = (s.ep-(s.sp+s.ds))/2+(s.sp+s.ds);

View File

@ -29,11 +29,11 @@
class SegmentSplitter { class SegmentSplitter {
protected: protected:
long long int minSegmentSize; long long int minSegmentSize;
const Logger* logger;
void split(Segment& seg, int cuid, Segment& s) const; void split(Segment& seg, int cuid, Segment& s) const;
public: public:
const Logger* logger; SegmentSplitter();
virtual ~SegmentSplitter() {} virtual ~SegmentSplitter() {}
virtual bool splitSegment(Segment& newSegment, int cuid, Segments& segments) = 0; virtual bool splitSegment(Segment& newSegment, int cuid, Segments& segments) = 0;
void setMinSegmentSize(long long int size) { minSegmentSize = size; } void setMinSegmentSize(long long int size) { minSegmentSize = size; }

View File

@ -20,13 +20,14 @@
*/ */
/* copyright --> */ /* copyright --> */
#include "SendMessageQueue.h" #include "SendMessageQueue.h"
#include "LogFactory.h"
SendMessageQueue::SendMessageQueue(int cuid, PeerConnection* peerConnection, SendMessageQueue::SendMessageQueue(int cuid, PeerConnection* peerConnection,
TorrentMan* torrentMan, TorrentMan* torrentMan)
const Logger* logger) :cuid(cuid) {
:cuid(cuid), logger(logger) {
requestSlotMan = new RequestSlotMan(cuid, &pendingMessages, peerConnection, requestSlotMan = new RequestSlotMan(cuid, &pendingMessages, peerConnection,
torrentMan, logger); torrentMan);
logger = LogFactory::getInstance();
} }
SendMessageQueue::~SendMessageQueue() { SendMessageQueue::~SendMessageQueue() {

View File

@ -33,7 +33,7 @@ private:
const Logger* logger; const Logger* logger;
public: public:
SendMessageQueue(int cuid, PeerConnection* peerConnection, SendMessageQueue(int cuid, PeerConnection* peerConnection,
TorrentMan* torrentMan, const Logger* logger); TorrentMan* torrentMan);
~SendMessageQueue(); ~SendMessageQueue();
void send(); void send();

View File

@ -35,6 +35,7 @@
#include "CopyDiskAdaptor.h" #include "CopyDiskAdaptor.h"
#include "DirectDiskAdaptor.h" #include "DirectDiskAdaptor.h"
#include "MultiDiskAdaptor.h" #include "MultiDiskAdaptor.h"
#include "LogFactory.h"
#include <errno.h> #include <errno.h>
#include <libgen.h> #include <libgen.h>
#include <string.h> #include <string.h>
@ -49,7 +50,9 @@ TorrentMan::TorrentMan():bitfield(NULL),
interval(DEFAULT_ANNOUNCE_INTERVAL), interval(DEFAULT_ANNOUNCE_INTERVAL),
minInterval(DEFAULT_ANNOUNCE_MIN_INTERVAL), minInterval(DEFAULT_ANNOUNCE_MIN_INTERVAL),
complete(0), incomplete(0), complete(0), incomplete(0),
connections(0), diskAdaptor(NULL) {} connections(0), diskAdaptor(NULL) {
logger = LogFactory::getInstance();
}
TorrentMan::~TorrentMan() { TorrentMan::~TorrentMan() {
if(bitfield != NULL) { if(bitfield != NULL) {

View File

@ -73,6 +73,7 @@ private:
Haves haves; Haves haves;
UsedPieces usedPieces; UsedPieces usedPieces;
bool setupComplete; bool setupComplete;
const Logger* logger;
FILE* openSegFile(string segFilename, string mode) const; FILE* openSegFile(string segFilename, string mode) const;
void read(FILE* file); void read(FILE* file);
@ -100,7 +101,6 @@ public:
TorrentMan(); TorrentMan();
~TorrentMan(); ~TorrentMan();
const Logger* logger;
DiskAdaptor* diskAdaptor; DiskAdaptor* diskAdaptor;
const Option* option; const Option* option;

View File

@ -76,7 +76,7 @@ bool TrackerDownloadCommand::executeInternal(Segment seg) {
|| te == NULL && len == e->segmentMan->totalSize || te == NULL && len == e->segmentMan->totalSize
|| bufSize == 0) { || bufSize == 0) {
if(te != NULL) te->end(); if(te != NULL) te->end();
e->logger->info(MSG_DOWNLOAD_COMPLETED, cuid); logger->info(MSG_DOWNLOAD_COMPLETED, cuid);
MetaEntry* entry = MetaFileUtil::bdecoding(res, len); MetaEntry* entry = MetaFileUtil::bdecoding(res, len);
e->commands.push(new TrackerUpdateCommand(cuid, req, (TorrentDownloadEngine*)e, entry)); e->commands.push(new TrackerUpdateCommand(cuid, req, (TorrentDownloadEngine*)e, entry));
return true; return true;

View File

@ -31,7 +31,8 @@
TrackerUpdateCommand::TrackerUpdateCommand(int cuid, Request* req, TrackerUpdateCommand::TrackerUpdateCommand(int cuid, Request* req,
TorrentDownloadEngine* e, TorrentDownloadEngine* e,
MetaEntry* trackerResponse) MetaEntry* trackerResponse)
:Command(cuid), req(req), e(e), trackerResponse(trackerResponse) {} :Command(cuid), req(req), e(e), trackerResponse(trackerResponse) {
}
TrackerUpdateCommand::~TrackerUpdateCommand() { TrackerUpdateCommand::~TrackerUpdateCommand() {
delete trackerResponse; delete trackerResponse;
@ -45,22 +46,22 @@ bool TrackerUpdateCommand::execute() {
} }
Data* warningMessage = (Data*)response->get("warning message"); Data* warningMessage = (Data*)response->get("warning message");
if(warningMessage != NULL) { if(warningMessage != NULL) {
e->logger->warn(MSG_TRACKER_WARNING_MESSAGE, cuid, warningMessage->toString().c_str()); logger->warn(MSG_TRACKER_WARNING_MESSAGE, cuid, warningMessage->toString().c_str());
} }
Data* trackerId = (Data*)response->get("tracker id"); Data* trackerId = (Data*)response->get("tracker id");
if(trackerId != NULL) { if(trackerId != NULL) {
e->torrentMan->trackerId = trackerId->toString(); e->torrentMan->trackerId = trackerId->toString();
e->logger->debug("CUID#%d - Tracker ID:%s", cuid, e->torrentMan->trackerId.c_str()); logger->debug("CUID#%d - Tracker ID:%s", cuid, e->torrentMan->trackerId.c_str());
} }
Data* interval = (Data*)response->get("interval"); Data* interval = (Data*)response->get("interval");
if(interval != NULL) { if(interval != NULL) {
e->torrentMan->interval = interval->toInt(); e->torrentMan->interval = interval->toInt();
e->logger->debug("CUID#%d - interval:%d", cuid, e->torrentMan->interval); logger->debug("CUID#%d - interval:%d", cuid, e->torrentMan->interval);
} }
Data* minInterval = (Data*)response->get("min interval"); Data* minInterval = (Data*)response->get("min interval");
if(minInterval != NULL) { if(minInterval != NULL) {
e->torrentMan->minInterval = minInterval->toInt(); e->torrentMan->minInterval = minInterval->toInt();
e->logger->debug("CUID#%d - min interval:%d", cuid, e->torrentMan->minInterval); logger->debug("CUID#%d - min interval:%d", cuid, e->torrentMan->minInterval);
} }
if(e->torrentMan->minInterval > e->torrentMan->interval) { if(e->torrentMan->minInterval > e->torrentMan->interval) {
e->torrentMan->minInterval = e->torrentMan->interval; e->torrentMan->minInterval = e->torrentMan->interval;
@ -68,12 +69,12 @@ bool TrackerUpdateCommand::execute() {
Data* complete = (Data*)response->get("complete"); Data* complete = (Data*)response->get("complete");
if(complete != NULL) { if(complete != NULL) {
e->torrentMan->complete = complete->toInt(); e->torrentMan->complete = complete->toInt();
e->logger->debug("CUID#%d - complete:%d", cuid, e->torrentMan->complete); logger->debug("CUID#%d - complete:%d", cuid, e->torrentMan->complete);
} }
Data* incomplete = (Data*)response->get("incomplete"); Data* incomplete = (Data*)response->get("incomplete");
if(incomplete != NULL) { if(incomplete != NULL) {
e->torrentMan->incomplete = incomplete->toInt(); e->torrentMan->incomplete = incomplete->toInt();
e->logger->debug("CUID#%d - incomplete:%d", cuid, e->torrentMan->incomplete); logger->debug("CUID#%d - incomplete:%d", cuid, e->torrentMan->incomplete);
} }
Data* peers = (Data*)response->get("peers"); Data* peers = (Data*)response->get("peers");
if(peers != NULL) { if(peers != NULL) {
@ -90,14 +91,14 @@ bool TrackerUpdateCommand::execute() {
Peer* peer = new Peer(ipaddr, port, e->torrentMan->pieceLength, Peer* peer = new Peer(ipaddr, port, e->torrentMan->pieceLength,
e->torrentMan->getTotalLength()); e->torrentMan->getTotalLength());
if(e->torrentMan->addPeer(peer)) { if(e->torrentMan->addPeer(peer)) {
e->logger->debug("CUID#%d - adding peer %s:%d", cuid, logger->debug("CUID#%d - adding peer %s:%d", cuid,
peer->ipaddr.c_str(), peer->port); peer->ipaddr.c_str(), peer->port);
} else { } else {
delete peer; delete peer;
} }
} }
} else { } else {
e->logger->info("CUID#%d - no peer list received.", cuid); logger->info("CUID#%d - no peer list received.", cuid);
} }
while(e->torrentMan->isPeerAvailable() && while(e->torrentMan->isPeerAvailable() &&
e->torrentMan->connections < MAX_PEER_UPDATE) { e->torrentMan->connections < MAX_PEER_UPDATE) {
@ -106,7 +107,7 @@ bool TrackerUpdateCommand::execute() {
peer->cuid = newCuid; peer->cuid = newCuid;
PeerInitiateConnectionCommand* command = new PeerInitiateConnectionCommand(newCuid, peer, e); PeerInitiateConnectionCommand* command = new PeerInitiateConnectionCommand(newCuid, peer, e);
e->commands.push(command); e->commands.push(command);
e->logger->debug("CUID#%d - adding new command CUID#%d", cuid, newCuid); logger->debug("CUID#%d - adding new command CUID#%d", cuid, newCuid);
} }
if(req->getTrackerEvent() == Request::STARTED) { if(req->getTrackerEvent() == Request::STARTED) {
req->setTrackerEvent(Request::AUTO); req->setTrackerEvent(Request::AUTO);

View File

@ -25,7 +25,8 @@
TrackerWatcherCommand::TrackerWatcherCommand(int cuid, Request* req, TrackerWatcherCommand::TrackerWatcherCommand(int cuid, Request* req,
TorrentDownloadEngine* e): TorrentDownloadEngine* e):
Command(cuid), req(req), e(e) {} Command(cuid), req(req), e(e) {
}
TrackerWatcherCommand::~TrackerWatcherCommand() {} TrackerWatcherCommand::~TrackerWatcherCommand() {}
@ -34,8 +35,8 @@ bool TrackerWatcherCommand::execute() {
Command* command = new TrackerInitCommand(e->torrentMan->getNewCuid(), Command* command = new TrackerInitCommand(e->torrentMan->getNewCuid(),
req, req,
e); e);
e->logger->info("CUID#%d - creating new tracker request command #%d", cuid, logger->info("CUID#%d - creating new tracker request command #%d", cuid,
command->getCuid()); command->getCuid());
e->commands.push(command); e->commands.push(command);
SleepCommand* slpCommand = new SleepCommand(cuid, e, this, SleepCommand* slpCommand = new SleepCommand(cuid, e, this,

View File

@ -25,7 +25,7 @@
#include "SegmentMan.h" #include "SegmentMan.h"
#include "TorrentMan.h" #include "TorrentMan.h"
#include "SplitSlowestSegmentSplitter.h" #include "SplitSlowestSegmentSplitter.h"
#include "SimpleLogger.h" #include "LogFactory.h"
#include "common.h" #include "common.h"
#include "DefaultDiskWriter.h" #include "DefaultDiskWriter.h"
#include "Util.h" #include "Util.h"
@ -533,14 +533,12 @@ int main(int argc, char* argv[]) {
gnutls_global_init(); gnutls_global_init();
#endif // HAVE_LIBGNUTLS #endif // HAVE_LIBGNUTLS
srandom(time(NULL)); srandom(time(NULL));
SimpleLogger* logger;
if(stdoutLog) { if(stdoutLog) {
logger = new SimpleLogger(stdout); LogFactory::setLogFile("/dev/stdout");
} else if(logfile.size()) { } else if(logfile.size()) {
logger = new SimpleLogger(logfile); LogFactory::setLogFile(logfile);
} else {
logger = new SimpleLogger("/dev/null");
} }
SegmentSplitter* splitter = new SplitSlowestSegmentSplitter(); SegmentSplitter* splitter = new SplitSlowestSegmentSplitter();
splitter->setMinSegmentSize(op->getAsLLInt(PREF_MIN_SEGMENT_SIZE)); splitter->setMinSegmentSize(op->getAsLLInt(PREF_MIN_SEGMENT_SIZE));
@ -552,15 +550,12 @@ int main(int argc, char* argv[]) {
setSignalHander(SIGINT, handler); setSignalHander(SIGINT, handler);
setSignalHander(SIGTERM, handler); setSignalHander(SIGTERM, handler);
splitter->logger = logger;
e = new ConsoleDownloadEngine(); e = new ConsoleDownloadEngine();
e->logger = logger;
e->option = op; e->option = op;
e->diskWriter = new DefaultDiskWriter(); e->diskWriter = new DefaultDiskWriter();
e->segmentMan = new SegmentMan(); e->segmentMan = new SegmentMan();
e->segmentMan->dir = dir; e->segmentMan->dir = dir;
e->segmentMan->ufilename = ufilename; e->segmentMan->ufilename = ufilename;
e->segmentMan->logger = logger;
e->segmentMan->option = op; e->segmentMan->option = op;
e->segmentMan->splitter = splitter; e->segmentMan->splitter = splitter;
@ -601,16 +596,13 @@ int main(int argc, char* argv[]) {
req->isTorrent = true; req->isTorrent = true;
req->setTrackerEvent(Request::STARTED); req->setTrackerEvent(Request::STARTED);
te = new TorrentConsoleDownloadEngine(); te = new TorrentConsoleDownloadEngine();
te->logger = logger;
te->option = op; te->option = op;
te->diskWriter = new DefaultDiskWriter(); te->diskWriter = new DefaultDiskWriter();
te->segmentMan = new SegmentMan(); te->segmentMan = new SegmentMan();
te->segmentMan->logger = logger;
te->segmentMan->option = op; te->segmentMan->option = op;
te->segmentMan->splitter = splitter; te->segmentMan->splitter = splitter;
te->torrentMan = new TorrentMan(); te->torrentMan = new TorrentMan();
te->torrentMan->setStoreDir(dir); te->torrentMan->setStoreDir(dir);
te->torrentMan->logger = logger;
te->torrentMan->option = op; te->torrentMan->option = op;
string targetTorrentFile = torrentFile.empty() ? string targetTorrentFile = torrentFile.empty() ?
downloadedTorrentFile : torrentFile; downloadedTorrentFile : torrentFile;
@ -662,11 +654,9 @@ int main(int argc, char* argv[]) {
exit(1); exit(1);
} }
} }
delete(logger);
delete(op); delete(op);
delete(splitter); delete(splitter);
delete(LogFactory::getInstance());
#ifdef HAVE_LIBGNUTLS #ifdef HAVE_LIBGNUTLS
gnutls_global_deinit(); gnutls_global_deinit();
#endif // HAVE_LIBGNUTLS #endif // HAVE_LIBGNUTLS