Skip to content

Commit

Permalink
2008-04-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Browse files Browse the repository at this point in the history
	Rewritten Exception class. Throw exception object, not its pointer and
	catch by reference, so that remove problematic delete operator for
	catched exception.
	* src/Exception.cc
	* src/Exception.h
	* test/ExceptionTest.cc
	* src/*: All files throwing/catching exception.
	* test/*: All files throwing/catching exception.
  • Loading branch information
tatsuhiro-t committed Apr 27, 2008
1 parent a7952cc commit 1ef9993
Show file tree
Hide file tree
Showing 159 changed files with 1,135 additions and 1,000 deletions.
11 changes: 11 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
2008-04-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

Rewritten Exception class. Throw exception object, not its pointer and
catch by reference, so that remove problematic delete operator for
catched exception.
* src/Exception.cc
* src/Exception.h
* test/ExceptionTest.cc
* src/*: All files throwing/catching exception.
* test/*: All files throwing/catching exception.

2008-04-26 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

Now auto protocol detection is enabled without -Z option.
Expand Down
27 changes: 12 additions & 15 deletions src/AbstractCommand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "Socket.h"
#include "message.h"
#include "prefs.h"
#include "StringFormat.h"

namespace aria2 {

Expand Down Expand Up @@ -104,7 +105,7 @@ bool AbstractCommand::execute() {
if(!peerStat.isNull()) {
if(peerStat->getStatus() == PeerStat::REQUEST_IDLE) {
logger->info(MSG_ABORT_REQUESTED, cuid);
onAbort(0);
onAbort();
req->resetUrl();
tryReserved();
return true;
Expand Down Expand Up @@ -141,39 +142,35 @@ bool AbstractCommand::execute() {
return executeInternal();
} else {
if(checkPoint.elapsed(timeout)) {
throw new DlRetryEx(EX_TIME_OUT);
throw DlRetryEx(EX_TIME_OUT);
}
e->commands.push_back(this);
return false;
}
} catch(DlAbortEx* err) {
} catch(DlAbortEx& err) {
logger->error(MSG_DOWNLOAD_ABORTED, err, cuid, req->getUrl().c_str());
onAbort(err);
delete(err);
onAbort();
req->resetUrl();
tryReserved();
return true;
} catch(DlRetryEx* err) {
} catch(DlRetryEx& err) {
logger->info(MSG_RESTARTING_DOWNLOAD, err, cuid, req->getUrl().c_str());
req->addTryCount();
bool isAbort = e->option->getAsInt(PREF_MAX_TRIES) != 0 &&
req->getTryCount() >= (unsigned int)e->option->getAsInt(PREF_MAX_TRIES);
if(isAbort) {
onAbort(err);
onAbort();
}
if(isAbort) {
logger->info(MSG_MAX_TRY, cuid, req->getTryCount());
logger->error(MSG_DOWNLOAD_ABORTED, err, cuid, req->getUrl().c_str());
delete(err);
tryReserved();
return true;
} else {
delete(err);
return prepareForRetry(e->option->getAsInt(PREF_RETRY_WAIT));
}
} catch(DownloadFailureException* err) {
} catch(DownloadFailureException& err) {
logger->error(EX_EXCEPTION_CAUGHT, err);
delete(err);
_requestGroup->setHaltRequested(true);
return true;
}
Expand All @@ -200,7 +197,7 @@ bool AbstractCommand::prepareForRetry(time_t wait) {
return true;
}

void AbstractCommand::onAbort(Exception* ex) {
void AbstractCommand::onAbort() {
logger->debug(MSG_UNREGISTER_CUID, cuid);
//_segmentMan->unregisterId(cuid);
if(!_requestGroup->getPieceStorage().isNull()) {
Expand Down Expand Up @@ -278,9 +275,9 @@ bool AbstractCommand::resolveHostname(const std::string& hostname,
return true;
break;
case NameResolver::STATUS_ERROR:
throw new DlAbortEx(MSG_NAME_RESOLUTION_FAILED, cuid,
hostname.c_str(),
resolver->getError().c_str());
throw DlAbortEx(StringFormat(MSG_NAME_RESOLUTION_FAILED, cuid,
hostname.c_str(),
resolver->getError().c_str()).str());
default:
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class AbstractCommand : public Command, public RequestGroupAware {

void tryReserved();
virtual bool prepareForRetry(time_t wait);
virtual void onAbort(Exception* ex);
virtual void onAbort();
virtual bool executeInternal() = 0;

void setReadCheckSocket(const SharedHandle<SocketCore>& socket);
Expand Down
20 changes: 12 additions & 8 deletions src/AbstractDiskWriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "Logger.h"
#include "DlAbortEx.h"
#include "a2io.h"
#include "StringFormat.h"
#include <cerrno>
#include <cstring>
#include <cassert>
Expand Down Expand Up @@ -79,11 +80,13 @@ void AbstractDiskWriter::openExistingFile(const std::string& filename,
this->filename = filename;
File f(filename);
if(!f.isFile()) {
throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), MSG_FILE_NOT_FOUND);
throw DlAbortEx
(StringFormat(EX_FILE_OPEN, filename.c_str(), MSG_FILE_NOT_FOUND).str());
}

if((fd = open(filename.c_str(), O_RDWR|O_BINARY, OPEN_MODE)) < 0) {
throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno));
throw DlAbortEx
(StringFormat(EX_FILE_OPEN, filename.c_str(), strerror(errno)).str());
}
}

Expand All @@ -93,7 +96,7 @@ void AbstractDiskWriter::createFile(const std::string& filename, int addFlags)
assert(filename.size());
Util::mkdirs(File(filename).getDirname());
if((fd = open(filename.c_str(), O_CREAT|O_RDWR|O_TRUNC|O_BINARY|addFlags, OPEN_MODE)) < 0) {
throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno));
throw DlAbortEx(StringFormat(EX_FILE_OPEN, filename.c_str(), strerror(errno)).str());
}
}

Expand Down Expand Up @@ -121,15 +124,16 @@ ssize_t AbstractDiskWriter::readDataInternal(unsigned char* data, size_t len)
void AbstractDiskWriter::seek(off_t offset)
{
if(offset != lseek(fd, offset, SEEK_SET)) {
throw new DlAbortEx(EX_FILE_SEEK, filename.c_str(), strerror(errno));
throw DlAbortEx
(StringFormat(EX_FILE_SEEK, filename.c_str(), strerror(errno)).str());
}
}

void AbstractDiskWriter::writeData(const unsigned char* data, size_t len, off_t offset)
{
seek(offset);
if(writeDataInternal(data, len) < 0) {
throw new DlAbortEx(EX_FILE_WRITE, filename.c_str(), strerror(errno));
throw DlAbortEx(StringFormat(EX_FILE_WRITE, filename.c_str(), strerror(errno)).str());
}
}

Expand All @@ -138,15 +142,15 @@ ssize_t AbstractDiskWriter::readData(unsigned char* data, size_t len, off_t offs
ssize_t ret;
seek(offset);
if((ret = readDataInternal(data, len)) < 0) {
throw new DlAbortEx(EX_FILE_READ, filename.c_str(), strerror(errno));
throw DlAbortEx(StringFormat(EX_FILE_READ, filename.c_str(), strerror(errno)).str());
}
return ret;
}

void AbstractDiskWriter::truncate(uint64_t length)
{
if(fd == -1) {
throw new DlAbortEx("File not opened.");
throw DlAbortEx("File not opened.");
}
ftruncate(fd, length);
}
Expand All @@ -155,7 +159,7 @@ void AbstractDiskWriter::truncate(uint64_t length)
uint64_t AbstractDiskWriter::size() const
{
if(fd == -1) {
throw new DlAbortEx("File not opened.");
throw DlAbortEx("File not opened.");
}
struct stat fileStat;
if(fstat(fd, &fileStat) < 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractProxyResponseCommand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ bool AbstractProxyResponseCommand::executeInternal() {
return false;
}
if(httpResponse->getResponseStatus() != "200") {
throw new DlRetryEx(EX_PROXY_CONNECTION_FAILED);
throw DlRetryEx(EX_PROXY_CONNECTION_FAILED);
}
e->commands.push_back(getNextCommand());
return true;
Expand Down
12 changes: 8 additions & 4 deletions src/BtAllowedFastMessage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,19 @@
#include "DlAbortEx.h"
#include "message.h"
#include "Peer.h"
#include "StringFormat.h"

namespace aria2 {

BtAllowedFastMessageHandle BtAllowedFastMessage::create(const unsigned char* data, size_t dataLength) {
if(dataLength != 5) {
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "allowed fast", dataLength, 5);
throw DlAbortEx
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "allowed fast", dataLength, 5).str());
}
uint8_t id = PeerMessageUtil::getId(data);
if(id != ID) {
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "allowed fast", ID);
throw DlAbortEx
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "allowed fast", ID).str());
}
BtAllowedFastMessageHandle message(new BtAllowedFastMessage());
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
Expand All @@ -56,8 +59,9 @@ BtAllowedFastMessageHandle BtAllowedFastMessage::create(const unsigned char* dat

void BtAllowedFastMessage::doReceivedAction() {
if(!peer->isFastExtensionEnabled()) {
throw new DlAbortEx("%s received while fast extension is disabled",
toString().c_str());
throw DlAbortEx
(StringFormat("%s received while fast extension is disabled",
toString().c_str()).str());
}
peer->addPeerAllowedIndex(index);
}
Expand Down
7 changes: 5 additions & 2 deletions src/BtBitfieldMessage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "DlAbortEx.h"
#include "message.h"
#include "Peer.h"
#include "StringFormat.h"
#include <cstring>

namespace aria2 {
Expand All @@ -57,11 +58,13 @@ BtBitfieldMessageHandle
BtBitfieldMessage::create(const unsigned char* data, size_t dataLength)
{
if(dataLength <= 1) {
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "bitfield", dataLength, 1);
throw DlAbortEx
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "bitfield", dataLength, 1).str());
}
uint8_t id = PeerMessageUtil::getId(data);
if(id != ID) {
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "bitfield", ID);
throw DlAbortEx
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "bitfield", ID).str());
}
BtBitfieldMessageHandle message(new BtBitfieldMessage());
message->setBitfield((unsigned char*)data+1, dataLength-1);
Expand Down
7 changes: 5 additions & 2 deletions src/BtCancelMessage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,19 @@
#include "DlAbortEx.h"
#include "message.h"
#include "BtMessageDispatcher.h"
#include "StringFormat.h"

namespace aria2 {

BtCancelMessageHandle BtCancelMessage::create(const unsigned char* data, size_t dataLength) {
if(dataLength != 13) {
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "cancel", dataLength, 13);
throw DlAbortEx
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "cancel", dataLength, 13).str());
}
uint8_t id = PeerMessageUtil::getId(data);
if(id != ID) {
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "cancel", ID);
throw DlAbortEx
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "cancel", ID).str());
}
BtCancelMessageHandle message(new BtCancelMessage());
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
Expand Down
7 changes: 5 additions & 2 deletions src/BtChokeMessage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@
#include "Peer.h"
#include "BtMessageDispatcher.h"
#include "BtRequestFactory.h"
#include "StringFormat.h"

namespace aria2 {

BtChokeMessageHandle BtChokeMessage::create(const unsigned char* data, size_t dataLength) {
if(dataLength != 1) {
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "choke", dataLength, 1);
throw DlAbortEx
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "choke", dataLength, 1).str());
}
uint8_t id = PeerMessageUtil::getId(data);
if(id != ID) {
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "choke", ID);
throw DlAbortEx
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "choke", ID).str());
}
BtChokeMessageHandle chokeMessage(new BtChokeMessage());
return chokeMessage;
Expand Down
3 changes: 1 addition & 2 deletions src/BtDependency.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ bool BtDependency::resolve()
btContext->setPeerIdPrefix(_option->get(PREF_PEER_ID_PREFIX));
}
btContext->setDir(_dependant->getDownloadContext()->getDir());
} catch(RecoverableException* e) {
} catch(RecoverableException& e) {
_logger->error(EX_EXCEPTION_CAUGHT, e);
delete e;
_logger->debug("BtDependency for GID#%d failed. Go without Bt.",
_dependant->getGID());
return true;
Expand Down
7 changes: 5 additions & 2 deletions src/BtExtendedMessage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "DlAbortEx.h"
#include "message.h"
#include "Util.h"
#include "StringFormat.h"
#include <cassert>
#include <cstring>

Expand Down Expand Up @@ -100,11 +101,13 @@ BtExtendedMessage::create(const BtContextHandle& btContext,
const unsigned char* data, size_t dataLength)
{
if(dataLength < 2) {
throw new DlAbortEx(MSG_TOO_SMALL_PAYLOAD_SIZE, "extended", dataLength);
throw DlAbortEx
(StringFormat(MSG_TOO_SMALL_PAYLOAD_SIZE, "extended", dataLength).str());
}
uint8_t id = PeerMessageUtil::getId(data);
if(id != ID) {
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "extended", ID);
throw DlAbortEx
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "extended", ID).str());
}
ExtensionMessageFactoryHandle factory = EXTENSION_MESSAGE_FACTORY(btContext,
peer);
Expand Down
17 changes: 10 additions & 7 deletions src/BtHandshakeMessageValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "BtHandshakeMessage.h"
#include "Util.h"
#include "PeerMessageUtil.h"
#include "StringFormat.h"
#include <cstring>

namespace aria2 {
Expand All @@ -58,17 +59,19 @@ class BtHandshakeMessageValidator : public BtMessageValidator {
virtual bool validate(Errors& error) {
// TODO
if(message->getPstrlen() != 19) {
throw new DlAbortEx("invalid handshake pstrlen=%u",
message->getPstrlen());
throw DlAbortEx(StringFormat("invalid handshake pstrlen=%u",
message->getPstrlen()).str());
}
if(memcmp(BtHandshakeMessage::BT_PSTR, message->getPstr(), 19) != 0) {
throw new DlAbortEx("invalid handshake pstr=%s",
Util::urlencode(message->getPstr(), 19).c_str());
throw DlAbortEx
(StringFormat("invalid handshake pstr=%s",
Util::urlencode(message->getPstr(), 19).c_str()).str());
}
if(memcmp(infoHash, message->getInfoHash(), 20) != 0) {
throw new DlAbortEx("invalid handshake info hash: expected:%s, actual:%s",
Util::toHex(infoHash, 20).c_str(),
Util::toHex(message->getInfoHash(), 20).c_str());
throw DlAbortEx
(StringFormat("invalid handshake info hash: expected:%s, actual:%s",
Util::toHex(infoHash, 20).c_str(),
Util::toHex(message->getInfoHash(), 20).c_str()).str());
}
return true;
}
Expand Down
Loading

0 comments on commit 1ef9993

Please sign in to comment.