Files
@ d2ed4065362c
Branch filter:
Location: libtransport.git/backends/twitter/main.cpp
d2ed4065362c
17.5 KiB
text/x-c++hdr
Multi-threaded twitter requests
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 | #include "transport/config.h"
#include "transport/networkplugin.h"
#include "transport/logging.h"
#include "transport/sqlite3backend.h"
#include "transport/mysqlbackend.h"
#include "transport/pqxxbackend.h"
#include "transport/storagebackend.h"
#include "Swiften/Swiften.h"
#include "unistd.h"
#include "signal.h"
#include "sys/wait.h"
#include "sys/signal.h"
#include <boost/algorithm/string.hpp>
#include <boost/signal.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include "twitcurl.h"
#include "TwitterResponseParser.h"
#include <iostream>
#include <sstream>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <cstdio>
#include "userdb.h"
using namespace boost::filesystem;
using namespace boost::program_options;
using namespace Transport;
DEFINE_LOGGER(logger, "Twitter Backend");
Swift::SimpleEventLoop *loop_; // Event Loop
class TwitterPlugin; // The plugin
TwitterPlugin * np = NULL;
StorageBackend *storagebackend;
#define STR(x) (std::string("(") + x.from + ", " + x.to + ", " + x.message + ")")
class TwitterPlugin : public NetworkPlugin {
private:
struct Request;
public:
Swift::BoostNetworkFactories *m_factories;
Swift::BoostIOServiceThread m_boostIOServiceThread;
boost::shared_ptr<Swift::Connection> m_conn;
TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
this->config = config;
if(CONFIG_HAS_KEY(config, "twitter.consumer_key") == false ||
CONFIG_HAS_KEY(config, "twitter.consumer_secret") == false) {
LOG4CXX_ERROR(logger, "Couldn't find consumer key and/or secret. Please check config file.");
exit(0);
}
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));
onDispatchRequest.connect(boost::bind(&TwitterPlugin::requestDispatcher, this, _1, _2));
activeThreadCount = 0;
MAX_THREADS = 50;
LOG4CXX_INFO(logger, "Starting the plugin.");
}
~TwitterPlugin() {
delete storagebackend;
std::map<std::string, twitCurl*>::iterator it;
for(it = sessions.begin() ; it != sessions.end() ; it++) delete it->second;
}
// Send data to NetworkPlugin server
void sendData(const std::string &string) {
m_conn->write(Swift::createSafeByteArray(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) {
std::string d(data->begin(), data->end());
handleDataRead(d);
}
// User trying to login into his twitter account
void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) {
if(connectionState.count(user) && (connectionState[user] == NEW ||
connectionState[user] == CONNECTED ||
connectionState[user] == WAITING_FOR_PIN)) {
LOG4CXX_INFO(logger, std::string("A session corresponding to ") + user + std::string(" is already active"))
return;
}
LOG4CXX_INFO(logger, std::string("Received login request for ") + user)
std::string username = user.substr(0,user.find('@'));
std::string passwd = password;
LOG4CXX_INFO(logger, username + " " + passwd)
sessions[user] = new twitCurl();
handleConnected(user);
handleBuddyChanged(user, "twitter-account", "twitter", std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
if(CONFIG_HAS_KEY(config,"proxy.server")) {
std::string ip = CONFIG_STRING(config,"proxy.server");
std::ostringstream out;
out << CONFIG_INT(config,"proxy.port");
std::string port = out.str();
std::string puser = CONFIG_STRING(config,"proxy.user");
std::string ppasswd = CONFIG_STRING(config,"proxy.password");
LOG4CXX_INFO(logger, ip << " " << port << " " << puser << " " << ppasswd)
if(ip != "localhost" && port != "0") {
sessions[user]->setProxyServerIp(ip);
sessions[user]->setProxyServerPort(port);
sessions[user]->setProxyUserName(puser);
sessions[user]->setProxyPassword(ppasswd);
}
}
connectionState[user] = NEW;
sessions[user]->setTwitterUsername(username);
sessions[user]->setTwitterPassword(passwd);
sessions[user]->getOAuth().setConsumerKey(consumerKey);
sessions[user]->getOAuth().setConsumerSecret(consumerSecret);
LOG4CXX_INFO(logger, "Querying database for usersettings of " << user)
UserInfo info;
storagebackend->getUser(user, info);
std::string key="", secret="";
int type;
storagebackend->getUserSetting((long)info.id, OAUTH_KEY, type, key);
storagebackend->getUserSetting((long)info.id, OAUTH_SECRET, type, secret);
if(key == "" || secret == "") {
LOG4CXX_INFO(logger, "Intiating oauthflow for user " << user)
std::string authUrl;
if (sessions[user]->oAuthRequestToken( authUrl ) == false ) {
LOG4CXX_ERROR(logger, "Error creating twitter authorization url!");
handleLogoutRequest(user, username);
return;
}
handleMessage(user, "twitter-account", std::string("Please visit the following link and authorize this application: ") + authUrl);
handleMessage(user, "twitter-account", std::string("Please reply with the PIN provided by twitter. Prefix the pin with 'pin:'. Ex. 'pin: 1234'"));
connectionState[user] = WAITING_FOR_PIN;
} else {
LOG4CXX_INFO(logger, user << " is already registerd. Using the stored oauth key and secret")
LOG4CXX_INFO(logger, key << " " << secret)
sessions[user]->getOAuth().setOAuthTokenKey( key );
sessions[user]->getOAuth().setOAuthTokenSecret( secret );
connectionState[user] = CONNECTED;
}
}
// User logging out
void handleLogoutRequest(const std::string &user, const std::string &legacyName) {
delete sessions[user];
sessions[user] = NULL;
connectionState[user] = DISCONNECTED;
}
void handlePINExchange(const std::string &user, std::string &data) {
sessions[user]->getOAuth().setOAuthPin( data );
if (sessions[user]->oAuthAccessToken() == false) {
LOG4CXX_ERROR(logger, user << ": Error while exchanging PIN for Access Token!")
handleLogoutRequest(user, "");
return;
}
std::string OAuthAccessTokenKey, OAuthAccessTokenSecret;
sessions[user]->getOAuth().getOAuthTokenKey( OAuthAccessTokenKey );
sessions[user]->getOAuth().getOAuthTokenSecret( OAuthAccessTokenSecret );
UserInfo info;
if(storagebackend->getUser(user, info) == false) {
LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
handleLogoutRequest(user, "");
return;
}
storagebackend->updateUserSetting((long)info.id, OAUTH_KEY, OAuthAccessTokenKey);
storagebackend->updateUserSetting((long)info.id, OAUTH_SECRET, OAuthAccessTokenSecret);
connectionState[user] = CONNECTED;
LOG4CXX_INFO(logger, user << ": Sent PIN " << data << " and obtained Access Token");
}
void printHelpMessage(const std::string &user) {
std::string helpMsg = "";
helpMsg = helpMsg
+ "\nHELP\n"
+ "#status:<your status> - Update your status\n"
+ "#timeline - Retrieve your timeline\n"
+ "@<username>:<message> - Send a directed message to the user <username>\n"
+ "#help - print this help message\n";
handleMessage(user, "twitter-account", helpMsg);
}
void handleDirectMessage(const std::string &user, std::string &username, std::string &data) {
if(sessions[user]->directMessageSend(username, data, false) == false) {
LOG4CXX_ERROR(logger, user << ": Error while sending directed message to user " << username );
return;
}
LOG4CXX_INFO(logger, user << ": Sending " << data << " to " << username)
std::string replyMsg;
sessions[user]->getLastWebResponse( replyMsg );
LOG4CXX_INFO(logger, replyMsg);
}
void handleStatusUpdate(const std::string &user, std::string &data) {
if(connectionState[user] != CONNECTED) {
LOG4CXX_ERROR(logger, "Trying to update status for " << user << " when not connected!");
return;
}
std::string replyMsg;
if( sessions[user]->statusUpdate( data ) ) {
replyMsg = "";
while(replyMsg.length() == 0) {
sessions[user]->getLastWebResponse( replyMsg );
}
LOG4CXX_INFO(logger, user << ": twitCurl:statusUpdate web response: " << replyMsg );
} else {
sessions[user]->getLastCurlError( replyMsg );
LOG4CXX_INFO(logger, user << ": twitCurl::statusUpdate error: " << replyMsg );
}
LOG4CXX_INFO(logger, "Updated status for " << user << ": " << data);
}
void fetchTimeline(const std::string &user) {
if(connectionState[user] != CONNECTED) {
LOG4CXX_ERROR(logger, "Trying to fetch timeline for " << user << " when not connected!");
return;
}
std::string replyMsg = "";
if( sessions[user]->timelineHomeGet()) {
while(replyMsg.length() == 0) {
sessions[user]->getLastWebResponse( replyMsg );
}
LOG4CXX_INFO(logger, user << ": twitCurl::timeline web response: " << replyMsg.length() << " " << replyMsg << "\n" );
std::vector<Status> tweets = getTimeline(replyMsg);
std::string timeline = "\n";
for(int i=0 ; i<tweets.size() ; i++) {
timeline += tweets[i].getTweet() + "\n";
}
handleMessage(user, "twitter-account", timeline);
} else {
sessions[user]->getLastCurlError( replyMsg );
LOG4CXX_INFO(logger, user << ": twitCurl::timeline error: " << replyMsg );
}
}
void fetchFriends(const std::string &user) {
if(connectionState[user] != CONNECTED) {
LOG4CXX_ERROR(logger, "Trying to fetch friends of " << user << " when not connected!");
return;
}
std::string replyMsg = "";
if( sessions[user]->friendsIdsGet(sessions[user]->getTwitterUsername())) {
while(replyMsg.length() == 0) {
sessions[user]->getLastWebResponse( replyMsg );
}
LOG4CXX_INFO(logger, user << ": twitCurl::friendsIdsGet web response: " << replyMsg.length() << " " << replyMsg << "\n" );
std::vector<std::string> IDs = getIDs( replyMsg );
/*for(int i=0 ; i<IDs.size() ; i++) {
//LOG4CXX_INFO(logger, "ID #" << i+1 << ": " << IDs[i]);
}*/
sessions[user]->userLookup(IDs, true);
sessions[user]->getLastWebResponse( replyMsg );
LOG4CXX_INFO(logger, user << ": twitCurl::UserLookUp web response: " << replyMsg.length() << " " << replyMsg << "\n" );
std::vector<User> users = getUsers( replyMsg );
std::string userlist = "\n***************USER LIST****************\n";
for(int i=0 ; i < users.size() ; i++) {
userlist += "*)" + users[i].getUserName() + " (" + users[i].getScreenName() + ")\n";
}
userlist += "***************************************\n";
handleMessage(user, "twitter-account", userlist);
} else {
sessions[user]->getLastCurlError( replyMsg );
LOG4CXX_INFO(logger, user << ": twitCurl::friendsIdsGet error: " << replyMsg );
}
}
void spawnThreadForRequest(Request r) {
std::string &user = r.from;
std::string &legacyName = r.to;
std::string &message = r.message;
LOG4CXX_INFO(logger, "Sending message from " << user << " to " << legacyName << ".");
if(legacyName == "twitter-account") {
std::string cmd = message.substr(0, message.find(':'));
std::string data = message.substr(message.find(':') + 1);
handleMessage(user, "twitter-account", cmd + " " + data);
if(cmd == "#pin") handlePINExchange(user, data);
else if(cmd == "#help") printHelpMessage(user);
else if(cmd[0] == '@') {std::string username = cmd.substr(1); handleDirectMessage(user, username, data);}
else if(cmd == "#status") handleStatusUpdate(user, data);
else if(cmd == "#timeline") fetchTimeline(user);
else if(cmd == "#friends") fetchFriends(user);
}
updateActiveThreadCount(-1);
onDispatchRequest(r, false);
}
/*
* usersBeingServed - set of users being served at present
* usersToServe - queue of users who have requests pending in their request queue and are yet to be served; Each user appears only once here.
*/
void requestDispatcher(Request r, bool incoming) {
criticalRegion.lock();
if(incoming) {
std::string user = r.from;
if(getActiveThreadCount() < MAX_THREADS && usersBeingServed.count(user) == false) {
updateActiveThreadCount(1);
boost::thread(&TwitterPlugin::spawnThreadForRequest, this, r);
usersBeingServed.insert(user);
LOG4CXX_INFO(logger, user << ": Sending request " << STR(r) << " to twitter")
} else {
requests[user].push(r);
LOG4CXX_INFO(logger, user << " is already being served! Adding " << STR(r) << " to request queue");
if (!usersBeingServed.count(user)) {
usersToServe.push(user);
}
}
} else {
usersBeingServed.erase(r.from);
if(requests[r.from].size()) usersToServe.push(r.from);
while(getActiveThreadCount() < MAX_THREADS && !usersToServe.empty()) {
std::string user = usersToServe.front(); usersToServe.pop();
Request s = requests[user].front(); requests[user].pop();
updateActiveThreadCount(1);
boost::thread(&TwitterPlugin::spawnThreadForRequest, this, s);
usersBeingServed.insert(user);
LOG4CXX_INFO(logger, user << ": Sending request " << STR(s) << " to twitter")
}
}
criticalRegion.unlock();
}
void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml = "") {
Request r;
r.from = user;
r.to = legacyName;
r.message = message;
LOG4CXX_INFO(logger, user << "Dispatching request " << STR(r))
onDispatchRequest(r,true);
//requestDispatcher(r, true);
/*if(legacyName == "twitter-account") {
std::string cmd = message.substr(0, message.find(':'));
std::string data = message.substr(message.find(':') + 1);
handleMessage(user, "twitter-account", cmd + " " + data);
if(cmd == "#pin") handlePINExchange(user, data);
else if(cmd == "#help") printHelpMessage(user);
else if(cmd[0] == '@') {std::string username = cmd.substr(1); handleDirectMessage(user, username, data);}
else if(cmd == "#status") handleStatusUpdate(user, data);
else if(cmd == "#timeline") fetchTimeline(user);
else if(cmd == "#friends") fetchFriends(user);
}*/
}
void 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 handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups) {
}
int getActiveThreadCount() {
int res;
threadLock.lock();
res = activeThreadCount;
threadLock.unlock();
return res;
}
void updateActiveThreadCount(int k) {
threadLock.lock();
activeThreadCount+=k;
threadLock.unlock();
}
private:
enum status {NEW, WAITING_FOR_PIN, CONNECTED, DISCONNECTED};
struct Request {
std::string from;
std::string to;
std::string message;
};
Config *config;
std::string consumerKey;
std::string consumerSecret;
std::string OAUTH_KEY;
std::string OAUTH_SECRET;
int activeThreadCount;
int MAX_THREADS;
boost::mutex criticalRegion;
boost::mutex threadLock;
std::map<std::string, twitCurl*> sessions;
std::map<std::string, std::queue<Request> > requests;
std::queue<std::string> usersToServe;
std::set<std::string> usersBeingServed;
std::map<std::string, status> connectionState;
boost::signal< void (Request, bool) > onDispatchRequest;
};
static void spectrum_sigchld_handler(int sig)
{
int status;
pid_t pid;
do {
pid = waitpid(-1, &status, WNOHANG);
} while (pid != 0 && pid != (pid_t)-1);
if ((pid == (pid_t) - 1) && (errno != ECHILD)) {
char errmsg[BUFSIZ];
snprintf(errmsg, BUFSIZ, "Warning: waitpid() returned %d", pid);
perror(errmsg);
}
}
int main (int argc, char* argv[]) {
std::string host;
int port;
if (signal(SIGCHLD, spectrum_sigchld_handler) == SIG_ERR) {
std::cout << "SIGCHLD handler can't be set\n";
return -1;
}
boost::program_options::options_description desc("Usage: spectrum [OPTIONS] <config_file.cfg>\nAllowed options");
desc.add_options()
("host,h", value<std::string>(&host), "host")
("port,p", value<int>(&port), "port")
;
try
{
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
boost::program_options::notify(vm);
}
catch (std::runtime_error& e)
{
std::cout << desc << "\n";
exit(1);
}
catch (...)
{
std::cout << desc << "\n";
exit(1);
}
if (argc < 5) {
return 1;
}
Config config;
if (!config.load(argv[5])) {
std::cerr << "Can't open " << argv[1] << " configuration file.\n";
return 1;
}
Logging::initBackendLogging(&config);
std::string error;
storagebackend = StorageBackend::createBackend(&config, error);
if (storagebackend == NULL) {
LOG4CXX_ERROR(logger, "Error creating StorageBackend! " << error)
return -2;
}
else if (!storagebackend->connect()) {
LOG4CXX_ERROR(logger, "Can't connect to database!")
return -1;
}
Swift::SimpleEventLoop eventLoop;
loop_ = &eventLoop;
np = new TwitterPlugin(&config, &eventLoop, host, port);
loop_->run();
return 0;
}
|