Commit d6baba0f authored by Paweł Wegner's avatar Paweł Wegner

IRequest::Pointer: now a unique_ptr.

parent b4b69e06
......@@ -336,9 +336,9 @@ IHttpServer::IResponse::Pointer MegaNz::HttpServerCallback::handle(
auto buffer = std::make_shared<Buffer>();
auto download_request = std::make_shared<Request<EitherError<void>>>(
std::weak_ptr<CloudProvider>(provider_->shared_from_this()));
download_request->set(provider_->downloadResolver(
auto resolver = provider_->downloadResolver(
provider_->toItem(node.get()),
util::make_unique<HttpDataCallback>(buffer), range.start, range.size));
util::make_unique<HttpDataCallback>(buffer), range.start, range.size);
provider_->addStreamRequest(download_request);
auto data = util::make_unique<HttpData>(buffer, provider_, download_request);
auto response = request.response(code, headers, range.size, std::move(data));
......@@ -347,7 +347,7 @@ IHttpServer::IResponse::Pointer MegaNz::HttpServerCallback::handle(
std::unique_lock<std::mutex> lock(buffer->response_mutex_);
buffer->response_ = nullptr;
});
download_request->run();
resolver(download_request);
return std::move(response);
}
......@@ -378,23 +378,25 @@ MegaNz::~MegaNz() {
mega_ = nullptr;
}
void MegaNz::addStreamRequest(DownloadFileRequest::Pointer r) {
void MegaNz::addStreamRequest(std::shared_ptr<DownloadFileRequest> r) {
std::lock_guard<std::mutex> lock(mutex_);
stream_requests_.insert(r);
}
void MegaNz::removeStreamRequest(DownloadFileRequest::Pointer r) {
void MegaNz::removeStreamRequest(std::shared_ptr<DownloadFileRequest> r) {
r->cancel();
std::lock_guard<std::mutex> lock(mutex_);
stream_requests_.erase(r);
}
void MegaNz::addRequestListener(IRequest<EitherError<void>>::Pointer p) {
void MegaNz::addRequestListener(
std::shared_ptr<IRequest<EitherError<void>>> p) {
std::lock_guard<std::mutex> lock(mutex_);
request_listeners_.insert(p);
}
void MegaNz::removeRequestListener(IRequest<EitherError<void>>::Pointer p) {
void MegaNz::removeRequestListener(
std::shared_ptr<IRequest<EitherError<void>>> p) {
std::lock_guard<std::mutex> lock(mutex_);
request_listeners_.erase(request_listeners_.find(p));
}
......
......@@ -110,11 +110,11 @@ class MegaNz : public CloudProvider {
IAuth::Token::Pointer authorizationCodeToToken(const std::string& code) const;
void addStreamRequest(DownloadFileRequest::Pointer);
void removeStreamRequest(DownloadFileRequest::Pointer);
void addStreamRequest(std::shared_ptr<DownloadFileRequest>);
void removeStreamRequest(std::shared_ptr<DownloadFileRequest>);
void addRequestListener(IRequest<EitherError<void>>::Pointer);
void removeRequestListener(IRequest<EitherError<void>>::Pointer);
void addRequestListener(std::shared_ptr<IRequest<EitherError<void>>>);
void removeRequestListener(std::shared_ptr<IRequest<EitherError<void>>>);
class Auth : public cloudstorage::Auth {
public:
......@@ -141,8 +141,9 @@ class MegaNz : public CloudProvider {
IHttpServer::Pointer daemon_;
std::string temporary_directory_;
std::string file_url_;
std::unordered_set<DownloadFileRequest::Pointer> stream_requests_;
std::unordered_set<IRequest<EitherError<void>>::Pointer> request_listeners_;
std::unordered_set<std::shared_ptr<DownloadFileRequest>> stream_requests_;
std::unordered_set<std::shared_ptr<IRequest<EitherError<void>>>>
request_listeners_;
bool deleted_;
};
......
......@@ -74,7 +74,7 @@ class IGenericRequest {
template <class ReturnValue>
class IRequest : public IGenericRequest {
public:
using Pointer = std::shared_ptr<IRequest>;
using Pointer = std::unique_ptr<IRequest>;
/**
* Retrieves the result, blocks if it wasn't computed just yet.
......
......@@ -32,6 +32,29 @@
namespace cloudstorage {
template <class T>
Request<T>::Wrapper::Wrapper(Request::Ptr r) : request_(r) {}
template <class T>
Request<T>::Wrapper::~Wrapper() {
cancel();
}
template <class T>
void Request<T>::Wrapper::finish() {
request_->finish();
}
template <class T>
void Request<T>::Wrapper::cancel() {
request_->cancel();
}
template <class T>
T Request<T>::Wrapper::result() {
return request_->result();
}
template <class T>
Request<T>::Request(std::shared_ptr<CloudProvider> provider)
: future_(value_.get_future()),
......@@ -119,11 +142,11 @@ void Request<T>::set(Resolver r) {
}
template <typename T>
typename Request<T>::Ptr Request<T>::run() {
typename Request<T>::Wrapper::Pointer Request<T>::run() {
auto r = std::move(resolver_);
if (!r) throw std::runtime_error("resolver not set");
r(this->shared_from_this());
return this->shared_from_this();
return util::make_unique<Wrapper>(this->shared_from_this());
}
template <class T>
......
......@@ -51,6 +51,19 @@ class Request : public IRequest<ReturnValue>,
using AuthorizeCompleted = std::function<void(EitherError<void>)>;
using RequestCompleted = std::function<void(EitherError<util::Output>)>;
class Wrapper : public IRequest<ReturnValue> {
public:
Wrapper(Request::Ptr);
~Wrapper();
void finish() override;
void cancel() override;
ReturnValue result() override;
private:
Request::Ptr request_;
};
Request(std::shared_ptr<CloudProvider>);
Request(std::weak_ptr<CloudProvider>);
~Request();
......@@ -60,7 +73,7 @@ class Request : public IRequest<ReturnValue>,
ReturnValue result() override;
void set(Resolver);
Ptr run();
typename Wrapper::Pointer run();
void done(const ReturnValue&);
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment