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
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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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