diff --git a/backends/twitter/TwitterPlugin.cpp b/backends/twitter/TwitterPlugin.cpp index 9d2fe101..3eca8cd5 100644 --- a/backends/twitter/TwitterPlugin.cpp +++ b/backends/twitter/TwitterPlugin.cpp @@ -11,6 +11,20 @@ DEFINE_LOGGER(logger, "Twitter Backend"); TwitterPlugin *np = NULL; Swift::SimpleEventLoop *loop_; // Event Loop + +#define abs(x) ((x)<0?-(x):(x)) +static int cmp(std::string a, std::string b) +{ + int diff = abs((int)a.size() - (int)b.size()); + if(a.size() < b.size()) a = std::string(diff,'0') + a; + else b = std::string(diff,'0') + b; + + if(a == b) return 0; + if(a < b) return -1; + return 1; +} + + TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin() { this->config = config; @@ -40,10 +54,14 @@ 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->onTick.connect(boost::bind(&TwitterPlugin::pollForDirectMessages, this)); - m_timer->start(); + tweet_timer = m_factories->getTimerFactory()->createTimer(60000); + message_timer = m_factories->getTimerFactory()->createTimer(60000); + + tweet_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForTweets, this)); + message_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForDirectMessages, this)); + + tweet_timer->start(); + message_timer->start(); LOG4CXX_INFO(logger, "Starting the plugin."); } @@ -169,20 +187,20 @@ void TwitterPlugin::pollForTweets() boost::bind(&TwitterPlugin::displayTweets, this, _1, _2, _3, _4))); it++; } - m_timer->start(); + tweet_timer->start(); } void TwitterPlugin::pollForDirectMessages() { - /*boost::mutex::scoped_lock lock(userlock); + boost::mutex::scoped_lock lock(userlock); std::set::iterator it = onlineUsers.begin(); while(it != onlineUsers.end()) { std::string user = *it; - tp->runAsThread(new DirectMessage(sessions[user], user, "", mostRecentDirectMessageID[user], + tp->runAsThread(new DirectMessageRequest(sessions[user], user, "", mostRecentDirectMessageID[user], boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3))); it++; } - m_timer->start();*/ + message_timer->start(); } @@ -304,7 +322,7 @@ void TwitterPlugin::updateLastDMID(const std::string user, const std::string ID) std::string TwitterPlugin::getMostRecentDMID(const std::string user) { boost::mutex::scoped_lock lock(userlock); - std::string ID = "-1"; + std::string ID = ""; if(onlineUsers.count(user)) ID = mostRecentDirectMessageID[user]; return ID; } @@ -371,8 +389,18 @@ void TwitterPlugin::directMessageResponse(std::string &user, std::vector m_conn; - Swift::Timer::ref m_timer; + Swift::Timer::ref tweet_timer; + Swift::Timer::ref message_timer; StorageBackend *storagebackend; TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port);