CLOUD: Refactor Request
Added ErrorResponse and ErrorCallback. Each Request now has an ErrorCallback, which should be called instead of usual callback in case of failure.
This commit is contained in:
parent
001b417f33
commit
eb63b50b7f
29 changed files with 652 additions and 507 deletions
|
@ -27,31 +27,55 @@
|
|||
|
||||
namespace Cloud {
|
||||
|
||||
DownloadRequest::DownloadRequest(Storage *storage, Storage::BoolCallback callback, Common::String remoteFile, Common::DumpFile *dumpFile):
|
||||
Request(0), _boolCallback(callback), _remoteFileStream(0), _localFile(dumpFile) {
|
||||
storage->streamFile(remoteFile, new Common::Callback<DownloadRequest, Networking::NetworkReadStreamResponse>(this, &DownloadRequest::streamCallback));
|
||||
DownloadRequest::DownloadRequest(Storage *storage, Storage::BoolCallback callback, Networking::ErrorCallback ecb, Common::String remoteFile, Common::DumpFile *dumpFile):
|
||||
Request(nullptr, ecb), _boolCallback(callback), _localFile(dumpFile), _remoteFileName(remoteFile), _storage(storage),
|
||||
_remoteFileStream(nullptr), _workingRequest(nullptr), _ignoreCallback(false) {
|
||||
start();
|
||||
}
|
||||
|
||||
void DownloadRequest::streamCallback(Networking::NetworkReadStreamResponse pair) {
|
||||
if (!pair.value) {
|
||||
warning("DownloadRequest: no ReadStream passed");
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
DownloadRequest::~DownloadRequest() {
|
||||
_ignoreCallback = true;
|
||||
if (_workingRequest) _workingRequest->finish();
|
||||
delete _boolCallback;
|
||||
delete _localFile;
|
||||
}
|
||||
|
||||
_remoteFileStream = (Networking::NetworkReadStream *)pair.value;
|
||||
void DownloadRequest::start() {
|
||||
_ignoreCallback = true;
|
||||
if (_workingRequest) _workingRequest->finish();
|
||||
_remoteFileStream = nullptr;
|
||||
//TODO: reopen DumpFile
|
||||
_ignoreCallback = false;
|
||||
|
||||
_workingRequest = _storage->streamFile(
|
||||
_remoteFileName,
|
||||
new Common::Callback<DownloadRequest, Networking::NetworkReadStreamResponse>(this, &DownloadRequest::streamCallback),
|
||||
new Common::Callback<DownloadRequest, Networking::ErrorResponse>(this, &DownloadRequest::streamErrorCallback)
|
||||
);
|
||||
}
|
||||
|
||||
void DownloadRequest::streamCallback(Networking::NetworkReadStreamResponse response) {
|
||||
_workingRequest = nullptr;
|
||||
if (_ignoreCallback) return;
|
||||
_remoteFileStream = (Networking::NetworkReadStream *)response.value;
|
||||
}
|
||||
|
||||
void DownloadRequest::streamErrorCallback(Networking::ErrorResponse error) {
|
||||
_workingRequest = nullptr;
|
||||
if (_ignoreCallback) return;
|
||||
finishError(error);
|
||||
}
|
||||
|
||||
void DownloadRequest::handle() {
|
||||
if (!_localFile) {
|
||||
warning("DownloadRequest: no file to write");
|
||||
finish();
|
||||
finishError(Networking::ErrorResponse(this, false, true, "", -1));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_localFile->isOpen()) {
|
||||
warning("DownloadRequest: failed to open file to write");
|
||||
finish();
|
||||
finishError(Networking::ErrorResponse(this, false, true, "", -1));
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -67,7 +91,7 @@ void DownloadRequest::handle() {
|
|||
if (readBytes != 0)
|
||||
if (_localFile->write(buf, readBytes) != readBytes) {
|
||||
warning("DownloadRequest: unable to write all received bytes into output file");
|
||||
finish();
|
||||
finishError(Networking::ErrorResponse(this, false, true, "", -1));
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -77,26 +101,20 @@ void DownloadRequest::handle() {
|
|||
//TODO: do something about it actually
|
||||
}
|
||||
|
||||
finishBool(_remoteFileStream->httpResponseCode() == 200);
|
||||
finishSuccess(_remoteFileStream->httpResponseCode() == 200);
|
||||
|
||||
_localFile->close(); //yes, I know it's closed automatically in ~DumpFile()
|
||||
}
|
||||
}
|
||||
|
||||
void DownloadRequest::restart() {
|
||||
//this request doesn't know anything about the _remoteFileStream it's reading
|
||||
//thus, it can't restart it
|
||||
warning("DownloadRequest: cannot be restarted");
|
||||
finish();
|
||||
//TODO: fix that
|
||||
warning("DownloadRequest: can't restart as there are no means to reopen DumpFile");
|
||||
finishError(Networking::ErrorResponse(this, false, true, "", -1));
|
||||
//start();
|
||||
}
|
||||
|
||||
void DownloadRequest::finish() {
|
||||
finishBool(false);
|
||||
}
|
||||
|
||||
void DownloadRequest::finishBool(bool success) {
|
||||
Request::finish();
|
||||
void DownloadRequest::finishSuccess(bool success) {
|
||||
Request::finishSuccess();
|
||||
if (_boolCallback) (*_boolCallback)(Storage::BoolResponse(this, success));
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue