DownloadEngine:: Use std::unique_ptr for webSocketSessionMan_

Notifier::addDownloadEventListener now takes pointer to
DownloadEventListener. Session holds unique_ptr to
ApiCallbackDownloadEventListener object.
pull/106/head
Tatsuhiro Tsujikawa 2013-07-06 18:33:59 +09:00
parent 81f46fbf92
commit 9130dc6776
7 changed files with 25 additions and 33 deletions

View File

@ -610,9 +610,9 @@ void DownloadEngine::setAsyncDNSServers(ares_addr_node* asyncDNSServers)
#ifdef ENABLE_WEBSOCKET #ifdef ENABLE_WEBSOCKET
void DownloadEngine::setWebSocketSessionMan void DownloadEngine::setWebSocketSessionMan
(const std::shared_ptr<rpc::WebSocketSessionMan>& wsman) (std::unique_ptr<rpc::WebSocketSessionMan> wsman)
{ {
webSocketSessionMan_ = wsman; webSocketSessionMan_ = std::move(wsman);
} }
#endif // ENABLE_WEBSOCKET #endif // ENABLE_WEBSOCKET

View File

@ -150,7 +150,7 @@ private:
std::unique_ptr<AuthConfigFactory> authConfigFactory_; std::unique_ptr<AuthConfigFactory> authConfigFactory_;
#ifdef ENABLE_WEBSOCKET #ifdef ENABLE_WEBSOCKET
std::shared_ptr<rpc::WebSocketSessionMan> webSocketSessionMan_; std::unique_ptr<rpc::WebSocketSessionMan> webSocketSessionMan_;
#endif // ENABLE_WEBSOCKET #endif // ENABLE_WEBSOCKET
/** /**
@ -352,9 +352,9 @@ public:
#endif // HAVE_ARES_ADDR_NODE #endif // HAVE_ARES_ADDR_NODE
#ifdef ENABLE_WEBSOCKET #ifdef ENABLE_WEBSOCKET
void setWebSocketSessionMan void setWebSocketSessionMan(std::unique_ptr<rpc::WebSocketSessionMan> wsman);
(const std::shared_ptr<rpc::WebSocketSessionMan>& wsman); const std::unique_ptr<rpc::WebSocketSessionMan>& getWebSocketSessionMan()
const std::shared_ptr<rpc::WebSocketSessionMan>& getWebSocketSessionMan() const const
{ {
return webSocketSessionMan_; return webSocketSessionMan_;
} }

View File

@ -166,11 +166,9 @@ int MultiUrlRequestInfo::prepare()
#ifdef ENABLE_WEBSOCKET #ifdef ENABLE_WEBSOCKET
if(option_->getAsBool(PREF_ENABLE_RPC)) { if(option_->getAsBool(PREF_ENABLE_RPC)) {
std::shared_ptr<rpc::WebSocketSessionMan> wsSessionMan e_->setWebSocketSessionMan(make_unique<rpc::WebSocketSessionMan>());
(new rpc::WebSocketSessionMan());
e_->setWebSocketSessionMan(wsSessionMan);
SingletonHolder<Notifier>::instance()->addDownloadEventListener SingletonHolder<Notifier>::instance()->addDownloadEventListener
(wsSessionMan); (e_->getWebSocketSessionMan().get());
} }
#endif // ENABLE_WEBSOCKET #endif // ENABLE_WEBSOCKET

View File

@ -42,8 +42,7 @@ Notifier::Notifier() {}
Notifier::~Notifier() {} Notifier::~Notifier() {}
void Notifier::addDownloadEventListener void Notifier::addDownloadEventListener(DownloadEventListener* listener)
(const std::shared_ptr<DownloadEventListener>& listener)
{ {
listeners_.push_back(listener); listeners_.push_back(listener);
} }
@ -51,9 +50,8 @@ void Notifier::addDownloadEventListener
void Notifier::notifyDownloadEvent void Notifier::notifyDownloadEvent
(DownloadEvent event, const RequestGroup* group) (DownloadEvent event, const RequestGroup* group)
{ {
for(std::vector<std::shared_ptr<DownloadEventListener> >::const_iterator i = for(auto listener : listeners_) {
listeners_.begin(), eoi = listeners_.end(); i != eoi; ++i) { listener->onEvent(event, group);
(*i)->onEvent(event, group);
} }
} }

View File

@ -55,8 +55,7 @@ class Notifier {
public: public:
Notifier(); Notifier();
~Notifier(); ~Notifier();
void addDownloadEventListener void addDownloadEventListener(DownloadEventListener* listener);
(const std::shared_ptr<DownloadEventListener>& listener);
// Notifies the download event to all listeners. // Notifies the download event to all listeners.
void notifyDownloadEvent(DownloadEvent event, const RequestGroup* group); void notifyDownloadEvent(DownloadEvent event, const RequestGroup* group);
@ -66,7 +65,7 @@ public:
notifyDownloadEvent(event, group.get()); notifyDownloadEvent(event, group.get());
} }
private: private:
std::vector<std::shared_ptr<DownloadEventListener> > listeners_; std::vector<DownloadEventListener*> listeners_;
}; };
} // namespace aria2 } // namespace aria2

View File

@ -108,11 +108,11 @@ int libraryDeinit()
Session* sessionNew(const KeyVals& options, const SessionConfig& config) Session* sessionNew(const KeyVals& options, const SessionConfig& config)
{ {
int rv; int rv;
Session* session; std::unique_ptr<Session> session;
try { try {
session = new Session(options); session = make_unique<Session>(options);
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
return 0; return nullptr;
} }
if(session->context->reqinfo) { if(session->context->reqinfo) {
if(!config.useSignalHandler) { if(!config.useSignalHandler) {
@ -120,29 +120,24 @@ Session* sessionNew(const KeyVals& options, const SessionConfig& config)
} }
rv = session->context->reqinfo->prepare(); rv = session->context->reqinfo->prepare();
if(rv != 0) { if(rv != 0) {
delete session; return nullptr;
return 0;
} }
const std::shared_ptr<DownloadEngine>& e = auto& e = session->context->reqinfo->getDownloadEngine();
session->context->reqinfo->getDownloadEngine();
if(config.keepRunning) { if(config.keepRunning) {
e->getRequestGroupMan()->setKeepRunning(true); e->getRequestGroupMan()->setKeepRunning(true);
// Add command to make aria2 keep event polling // Add command to make aria2 keep event polling
e->addCommand(make_unique<KeepRunningCommand>(e->newCUID(), e.get())); e->addCommand(make_unique<KeepRunningCommand>(e->newCUID(), e.get()));
} }
if(config.downloadEventCallback) { if(config.downloadEventCallback) {
std::shared_ptr<DownloadEventListener> listener session->listener = make_unique<ApiCallbackDownloadEventListener>
(new ApiCallbackDownloadEventListener(session, (session.get(), config.downloadEventCallback, config.userData);
config.downloadEventCallback,
config.userData));
SingletonHolder<Notifier>::instance() SingletonHolder<Notifier>::instance()
->addDownloadEventListener(listener); ->addDownloadEventListener(session->listener.get());
} }
} else { } else {
delete session; return nullptr;
return 0;
} }
return session; return session.release();
} }
int sessionFinal(Session* session) int sessionFinal(Session* session)

View File

@ -44,11 +44,13 @@
namespace aria2 { namespace aria2 {
struct Context; struct Context;
class ApiCallbackDownloadEventListener;
struct Session { struct Session {
Session(const KeyVals& options); Session(const KeyVals& options);
~Session(); ~Session();
std::shared_ptr<Context> context; std::shared_ptr<Context> context;
std::unique_ptr<ApiCallbackDownloadEventListener> listener;
}; };
} // namespace aria2 } // namespace aria2