From f8a183661cfa6a42d41423fbffc5f81cb5bf19f3 Mon Sep 17 00:00:00 2001 From: Sarang Bharadwaj Date: Wed, 6 Jun 2012 16:42:27 +0530 Subject: [PATCH] Polling users home timeline --- backends/twitter/Requests/TimelineRequest.cpp | 8 +++++++- backends/twitter/TwitterPlugin.cpp | 19 +++++++++++++++++++ backends/twitter/TwitterPlugin.h | 4 ++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/backends/twitter/Requests/TimelineRequest.cpp b/backends/twitter/Requests/TimelineRequest.cpp index 4503b478..431078dc 100644 --- a/backends/twitter/Requests/TimelineRequest.cpp +++ b/backends/twitter/Requests/TimelineRequest.cpp @@ -2,8 +2,14 @@ DEFINE_LOGGER(logger, "TimelineRequest") void TimelineRequest::run() { + + bool success; + + if(userRequested != "") success = twitObj->timelineUserGet(false, false, 20, userRequested, false); + else success = twitObj->timelineHomeGet(); + replyMsg = ""; - if( twitObj->timelineUserGet(false, false, 20, userRequested, false) ) { + if(success) { LOG4CXX_INFO(logger, "Sending timeline request for user " << user) while(replyMsg.length() == 0) { diff --git a/backends/twitter/TwitterPlugin.cpp b/backends/twitter/TwitterPlugin.cpp index 4f266f27..c112339f 100644 --- a/backends/twitter/TwitterPlugin.cpp +++ b/backends/twitter/TwitterPlugin.cpp @@ -33,6 +33,10 @@ TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, Stora tp = new ThreadPool(loop_, 10); + m_timer = m_factories->getTimerFactory()->createTimer(60000); + m_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForTweets, this)); + m_timer->start(); + LOG4CXX_INFO(logger, "Starting the plugin."); } @@ -95,6 +99,7 @@ void TwitterPlugin::handleLogoutRequest(const std::string &user, const std::stri delete sessions[user]; sessions[user] = NULL; connectionState[user] = DISCONNECTED; + onlineUsers.erase(user); } @@ -134,6 +139,19 @@ void TwitterPlugin::handleBuddyRemovedRequest(const std::string &user, const std } +void TwitterPlugin::pollForTweets() +{ + boost::mutex::scoped_lock lock(userlock); + std::set::iterator it = onlineUsers.begin(); + while(it != onlineUsers.end()) { + std::string user = *it; + tp->runAsThread(new TimelineRequest(np, sessions[user], user, "")); + it++; + } + m_timer->start(); +} + + bool TwitterPlugin::getUserOAuthKeyAndSecret(const std::string user, std::string &key, std::string &secret) { boost::mutex::scoped_lock lock(dblock); @@ -218,4 +236,5 @@ void TwitterPlugin::pinExchangeComplete(const std::string user, const std::strin sessions[user]->getOAuth().setOAuthTokenKey( OAuthAccessTokenKey ); sessions[user]->getOAuth().setOAuthTokenSecret( OAuthAccessTokenSecret ); connectionState[user] = CONNECTED; + onlineUsers.insert(user); } diff --git a/backends/twitter/TwitterPlugin.h b/backends/twitter/TwitterPlugin.h index 979985f3..77351c3e 100644 --- a/backends/twitter/TwitterPlugin.h +++ b/backends/twitter/TwitterPlugin.h @@ -48,6 +48,7 @@ class TwitterPlugin : public NetworkPlugin { Swift::BoostNetworkFactories *m_factories; Swift::BoostIOServiceThread m_boostIOServiceThread; boost::shared_ptr m_conn; + Swift::Timer::ref m_timer; StorageBackend *storagebackend; TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port); @@ -71,6 +72,8 @@ class TwitterPlugin : public NetworkPlugin { void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector &groups); + void pollForTweets(); + bool getUserOAuthKeyAndSecret(const std::string user, std::string &key, std::string &secret); bool storeUserOAuthKeyAndSecret(const std::string user, const std::string OAuthKey, const std::string OAuthSecret); @@ -96,6 +99,7 @@ class TwitterPlugin : public NetworkPlugin { ThreadPool *tp; std::map sessions; std::map connectionState; + std::set onlineUsers; }; #endif