Changeset - f8a183661cfa
[Not reviewed]
0 3 0
Sarang Bharadwaj - 13 years ago 2012-06-06 13:12:27
sarang.bh@gmail.com
Polling users home timeline
3 files changed with 30 insertions and 1 deletions:
0 comments (0 inline, 0 general)
backends/twitter/Requests/TimelineRequest.cpp
Show inline comments
 
#include "TimelineRequest.h"
 
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) {
 
			twitObj->getLastWebResponse( replyMsg );
 
		}
 

	
 
		LOG4CXX_INFO(logger, user << " - " << replyMsg.length() << " " << replyMsg << "\n" );
 
		
 
		std::vector<Status> tweets = getTimeline(replyMsg);
 
		timeline = "\n";
 
		for(int i=0 ; i<tweets.size() ; i++) {
 
			timeline += tweets[i].getTweet() + "\n";
backends/twitter/TwitterPlugin.cpp
Show inline comments
 
@@ -24,24 +24,28 @@ TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, Stora
 
	consumerKey = CONFIG_STRING(config, "twitter.consumer_key");
 
	consumerSecret = CONFIG_STRING(config, "twitter.consumer_secret");
 
	OAUTH_KEY = "oauth_key";
 
	OAUTH_SECRET = "oauth_secret";
 

	
 
	m_factories = new Swift::BoostNetworkFactories(loop);
 
	m_conn = m_factories->getConnectionFactory()->createConnection();
 
	m_conn->onDataRead.connect(boost::bind(&TwitterPlugin::_handleDataRead, this, _1));
 
	m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(host), port));
 

	
 
	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.");
 
}
 

	
 
TwitterPlugin::~TwitterPlugin() 
 
{
 
	delete storagebackend;
 
	std::map<std::string, twitCurl*>::iterator it;
 
	for(it = sessions.begin() ; it != sessions.end() ; it++) delete it->second;
 
	delete tp;
 
}
 

	
 
// Send data to NetworkPlugin server
 
@@ -86,24 +90,25 @@ void TwitterPlugin::handleLoginRequest(const std::string &user, const std::strin
 
		LOG4CXX_INFO(logger, user << " is already registerd. Using the stored oauth key and secret")
 
		LOG4CXX_INFO(logger, key << " " << secret)	
 
		pinExchangeComplete(user, key, secret);
 
	}
 
}
 

	
 
// User logging out
 
void TwitterPlugin::handleLogoutRequest(const std::string &user, const std::string &legacyName) 
 
{
 
	delete sessions[user];
 
	sessions[user] = NULL;
 
	connectionState[user] = DISCONNECTED;
 
	onlineUsers.erase(user);
 
}
 

	
 

	
 
void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml) 
 
{
 
	
 
	if(legacyName == "twitter-account") {
 

	
 
		std::string cmd = "", data = "";
 
		std::istringstream in(message.c_str());
 
		in >> cmd >> data;
 
		
 
@@ -125,24 +130,37 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std:
 
void TwitterPlugin::handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) 
 
{
 
	LOG4CXX_INFO(logger, user << ": Added buddy " << buddyName << ".");
 
	handleBuddyChanged(user, buddyName, alias, groups, pbnetwork::STATUS_ONLINE);
 
}
 

	
 
void TwitterPlugin::handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups) 
 
{
 

	
 
}
 

	
 

	
 
void TwitterPlugin::pollForTweets()
 
{
 
	boost::mutex::scoped_lock lock(userlock);
 
	std::set<std::string>::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);
 
	
 
	UserInfo info;
 
	if(storagebackend->getUser(user, info) == false) {
 
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
		return false;
 
	}
 

	
 
	key="", secret=""; int type;
 
	storagebackend->getUserSetting((long)info.id, OAUTH_KEY, type, key);
 
@@ -209,13 +227,14 @@ void TwitterPlugin::OAuthFlowComplete(const std::string user, twitCurl *obj)
 
	delete sessions[user];
 
	sessions[user] = obj->clone();	
 
	connectionState[user] = WAITING_FOR_PIN;
 
}	
 

	
 
void TwitterPlugin::pinExchangeComplete(const std::string user, const std::string OAuthAccessTokenKey, const std::string OAuthAccessTokenSecret) 
 
{
 
	boost::mutex::scoped_lock lock(userlock);	
 
		
 
	sessions[user]->getOAuth().setOAuthTokenKey( OAuthAccessTokenKey );
 
	sessions[user]->getOAuth().setOAuthTokenSecret( OAuthAccessTokenSecret );
 
	connectionState[user] = CONNECTED;
 
	onlineUsers.insert(user);
 
}	
backends/twitter/TwitterPlugin.h
Show inline comments
 
@@ -39,47 +39,50 @@ using namespace Transport;
 

	
 

	
 
#define STR(x) (std::string("(") + x.from + ", " + x.to + ", " + x.message + ")")
 
class TwitterPlugin;
 
extern TwitterPlugin *np;
 
extern Swift::SimpleEventLoop *loop_; // Event Loop
 

	
 
class TwitterPlugin : public NetworkPlugin {
 
	public:
 
		Swift::BoostNetworkFactories *m_factories;
 
		Swift::BoostIOServiceThread m_boostIOServiceThread;
 
		boost::shared_ptr<Swift::Connection> m_conn;
 
		Swift::Timer::ref m_timer;
 
		StorageBackend *storagebackend;
 

	
 
		TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port);
 
		~TwitterPlugin();
 

	
 
		// Send data to NetworkPlugin server
 
		void sendData(const std::string &string);
 

	
 
		// Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class)
 
		void _handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data);
 
	
 
		// User trying to login into his twitter account
 
		void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password);
 
		
 
		// User logging out
 
		void handleLogoutRequest(const std::string &user, const std::string &legacyName);
 

	
 
		void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml = "");
 

	
 
		void handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups);
 

	
 
		void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &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);
 
		
 
		void initUserSession(const std::string user, const std::string password);
 
		
 
		void OAuthFlowComplete(const std::string user, twitCurl *obj);
 
		
 
		void pinExchangeComplete(const std::string user, const std::string OAuthAccessTokenKey, const std::string OAuthAccessTokenSecret);
 

	
 
	private:
 
		enum status {NEW, WAITING_FOR_PIN, CONNECTED, DISCONNECTED};
 
@@ -87,15 +90,16 @@ class TwitterPlugin : public NetworkPlugin {
 
		Config *config;
 

	
 
		std::string consumerKey;
 
		std::string consumerSecret;
 
		std::string OAUTH_KEY;
 
		std::string OAUTH_SECRET;
 

	
 
		boost::mutex dblock, userlock;
 

	
 
		ThreadPool *tp;
 
		std::map<std::string, twitCurl*> sessions;		
 
		std::map<std::string, status> connectionState;
 
		std::set<std::string> onlineUsers;
 
};
 

	
 
#endif
0 comments (0 inline, 0 general)