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 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
|
/*
* commandhandlers.cpp
* backend
*
* Created by Mikael Gransell on 2/28/06.
* Copyright 2006 __MyCompanyName__. All rights reserved.
*
*/
#include "commandhandlers.h"
#include "SessionManager.h"
#include "ShareManager.h"
#include "TransferManager.h"
#include "QueueManager.h"
#include "RpcArgumentFactory.h"
#include "SettingsMapper.h"
#include "log.h"
#include <iostream>
#include <boost/thread/recursive_mutex.hpp>
using namespace std;
namespace dcqt_backend
{
void AuthenticateCmdHandler::handleCommand(int clientId, const std::list<boost::any>& params)
{
cout << "authenticate" << endl;
// Check if clientid is allowed to authenticate
time_t currt = time(NULL);
if( clientAccessMap.find(clientId)==clientAccessMap.end() ) clientAccessMap[clientId] = currt;
else {
time_t lastAttempt = clientAccessMap[clientId];
if( currt - lastAttempt < 2 ) {
logger->warn( boost::format("Authenticate: client rejected (too many tries in too short time)"));
return;
}
}
try{
string p = boost::any_cast<string>(params.front());
if(p==password) {
// register clientId as an authenticated client.
driver->setClientAuthenticated(clientId,true);
logger->info( boost::format("Authenticate: login success" ));
}
else {
// Wrong password, do something clever here maybe.
logger->warn( boost::format("Authenticate: wrong password"));
}
}
catch(const boost::bad_any_cast& e) {}
}
void TestBoolHandler::handleCommand(int cid,const list<boost::any>& params) {
bool b = boost::any_cast<bool>(params.front());
cout << "bool: " << b << endl;
}
void CreateSessionCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
// Params inly include the url that we should connect to
string url = boost::any_cast<string>(params.front());
SessionManager::instance()->createSession(url);
}
catch( const boost::bad_any_cast& e ) {
cout << "Invalid params to createSession. " << e.what() << endl;
}
}
void CloseSessionCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
int sessionId = boost::any_cast<int>(params.front());
SessionManager::instance()->closeSession(sessionId);
}
catch( const boost::bad_any_cast& e ) {
cout << "Invalid param to closeSession. " << e.what() << endl;
}
}
void RequestRunningSessionsCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
// Will contain the shares
rpc::CmdPtr cmd( new list<boost::any> );
cmd->push_back( string("runningSessions") );
list< boost::any > sessionList;
//SessionManager::instance()->aquireLock();
boost::recursive_mutex::scoped_lock sl(SessionManager::instance()->sessionLock);
map<int,Session*> *sm = SessionManager::instance()->getSessionMap();
map<int,Session*>::iterator it = sm->begin();
int index = 0;
while(it != sm->end() ) {
Session *session = it->second;
list<boost::any> sessionInfo;
sessionInfo.push_back(session->getId());
sessionInfo.push_back(session->getHubName());
sessionInfo.push_back(session->getUrl());
list<boost::any> userList;
hash_set<int>* users = session->getUsers();
hash_set<int>::iterator userit= users->begin();
while(userit!=users->end()) {
int userId = (*userit);
dcqt_backend::User& u = SessionManager::instance()->getUserPtrMap()[SessionManager::instance()->getUserIdMap()[userId]];
userList.push_back(rpc_argument::createUser(u));
userit++;
}
sessionInfo.push_back(userList);
sessionList.push_back(sessionInfo);
index++;
it++;
} // end building session list
//SessionManager::instance()->releaseLock();
// Build the queue list
list<boost::any> queueList;
QueueItem::StringMap& queue = QueueManager::getInstance()->lockQueue();
for(QueueItem::StringMap::iterator it=queue.begin();it!=queue.end();it++) {
QueueItem* item = it->second;
int itemId = TransferManager::getInstance()->queueItemIds.getId(item);
queueList.push_back( rpc_argument::createQueueItem( item, itemId ));
}
QueueManager::getInstance()->unlockQueue();
// Add the two lists
cmd->push_back( sessionList );
cmd->push_back( queueList );
// Send the info back to the client that requested it
cmdSenderMethod( clientId, cmd );
}
catch( const boost::bad_any_cast& ce ) {
cout << "Invalid params to search. " << ce.what() << endl;
}
catch( const boost::bad_function_call& fe ) {
cout << "Invalid sender method set" << fe.what() << endl;
}
}
void SendPasswordCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
list<boost::any>::const_iterator it = params.begin();
int sessionId = boost::any_cast<int>(*it);
++it;
string pass = boost::any_cast<string>(*it);
Session* session = SessionManager::instance()->getSession(sessionId);
if( session != NULL ) {
session->password(pass);
}
else {
cout << "Invalid session id(" << sessionId << ")" << endl;
}
}
catch( const boost::bad_any_cast& e ) {
cout << "Invalid param to sendChat. " << e.what() << endl;
}
}
void SendChatCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
list<boost::any>::const_iterator it = params.begin();
int sessionId = boost::any_cast<int>(*it);
++it;
string msg = boost::any_cast<string>(*it);
Session* s = SessionManager::instance()->getSession(sessionId);
if( s != NULL ) {
s->sendChat(msg);
}
else {
cout << "Invalid session id(" << sessionId << ")" << endl;
}
}
catch( const boost::bad_any_cast& e ) {
cout << "Invalid param to sendChat. " << e.what() << endl;
}
}
void SearchCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
list<boost::any>::const_iterator it = params.begin();
int sessionId = boost::any_cast<int>(*it);
++it;
string searchString = boost::any_cast<string>(*it);
++it;
int64_t size = boost::any_cast<int64_t>(*it);
++it;
int sizeMode = boost::any_cast<int>(*it);
++it;
int typeMode = boost::any_cast<int>(*it);
Session* session = SessionManager::instance()->getSession(sessionId);
if( session != NULL ) {
// We dont send the token at the moment. Need to find
// out what it means first
session->search( sizeMode, size, typeMode, searchString, "" );
}
else {
//cout << "Invalid session id(" << sessionId << ")"
//<< endl;
// Since we search all hubs anyway at the moment, we
// might as well just search here.
map<int,Session*>* arne = SessionManager::instance()->getSessionMap();
map<int,Session*>::iterator sit = arne->begin();
Session* session = sit->second;
session->search(sizeMode, size, typeMode, searchString, "");
}
}
catch( const boost::bad_any_cast& e ) {
cout << "Invalid params to search. " << e.what() << endl;
}
}
void GetSharedDirectoriesCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
// Will contain the shares
rpc::CmdPtr cmd( new list<boost::any> );
cmd->push_back( string("sharedDirs") );
list< boost::any > shares;
StringPairList dirs = ShareManager::getInstance()->getDirectories();
for(StringPairList::iterator it = dirs.begin(); it != dirs.end(); it++) {
string vdir = it->first;
string sdir = it->second;
int64_t size = ShareManager::getInstance()->getShareSize(it->second);
list<boost::any> share;
share.push_back( vdir );
share.push_back( sdir );
share.push_back( size );
shares.push_back(share);
}
cmd->push_back( shares );
// Send the info back to the client that requested it
cmdSenderMethod( clientId, cmd );
}
catch( const boost::bad_any_cast& ce ) {
cout << "Invalid params to search. " << ce.what() << endl;
}
catch( const boost::bad_function_call& fe ) {
cout << "Invalid sender method set" << fe.what() << endl;
}
}
void AddShareCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
list<boost::any>::const_iterator it = params.begin();
string name = boost::any_cast<string>(*it);
++it;
string dir = boost::any_cast<string>(*it);
ShareManager::getInstance()->addDirectory(dir,name);
}
catch( const ShareException& se ) {
cout << "Could not add share. " << se.getError() << endl;
}
catch( const boost::bad_any_cast& ce ) {
cout << "Invalid params to addShare. " << ce.what() << endl;
}
}
void RemoveShareCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
string name = boost::any_cast<string>(params.front());
ShareManager::getInstance()->removeDirectory(name);
}
catch( const boost::bad_any_cast& ce ) {
cout << "Invalid params to removeShare. " << ce.what() << endl;
}
}
void DownloadFileCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
list<boost::any>::const_iterator it = params.begin();
string file = boost::any_cast<string>(*it);
++it;
int64_t size = boost::any_cast<int64_t>(*it);
++it;
int userid = boost::any_cast<int>(*it);
++it;
string dir = boost::any_cast<string>(*it);
++it;
string tth = boost::any_cast<string>(*it);
//SessionManager::instance()->aquireLock();
boost::recursive_mutex::scoped_lock sl(SessionManager::instance()->sessionLock);
TransferManager::getInstance()->downloadFile(file,size,userid,dir,tth);
//SessionManager::instance()->releaseLock();
}
catch( const boost::bad_any_cast& ce ) {
cout << "Invalid params to downloadFile. " << ce.what() << endl;
}
}
void GetUserFileListCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
int userId = boost::any_cast<int>(params.front());
//SessionManager::instance()->aquireLock();
boost::recursive_mutex::scoped_lock sl(SessionManager::instance()->sessionLock);
TransferManager::getInstance()->getUserFileList( userId );
//SessionManager::instance()->releaseLock();
}
catch( const boost::bad_any_cast& ce ) {
cout << "Invalid params to getUserFileList. " << ce.what() << endl;
}
}
void GetHubListCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
bool refresh = boost::any_cast<bool>(params.front());
if( !refresh ) {
rpc::CmdPtr cmd( new list<boost::any> );
cmd->push_back( string("newHubList") );
cmd->push_back( rpc_argument::createHubList(SessionManager::instance()->getPublicHubs()) );
// Send the info back to the client that requested it
cmdSenderMethod( clientId, cmd );
}
else {
SessionManager::instance()->refreshHublist();
}
}
catch( const boost::bad_function_call& fe ) {
cout << "Invalid sender method set" << fe.what() << endl;
}
catch( const boost::bad_any_cast& ce ) {
cout << "Invalid params to getHubList. " << ce.what() << endl;
}
}
void ConnectToUserCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
int userId = boost::any_cast<int>(params.front());
TransferManager::getInstance()->forceConnect(userId);
}
catch( const boost::bad_any_cast& ce ) {
cout << "Invalid params to connectToUser. " << ce.what() << endl;
}
}
void RemoveQueueItemCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
int qid = boost::any_cast<int>(params.front());
TransferManager::getInstance()->removeQueueItem(qid);
}
catch( const boost::bad_any_cast& ce ) {
cout << "Invalid params to removeQueueItem. " << ce.what() << endl;
}
}
void AddFavoriteHubCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
list<boost::any>::const_iterator it = params.begin();
FavoriteHubEntry he;
he.setNick(boost::any_cast<string>(*it));
++it;
he.setUserDescription(boost::any_cast<string>(*it));
++it;
he.setName(boost::any_cast<string>(*it));
++it;
he.setServer(boost::any_cast<string>(*it));
++it;
he.setDescription(boost::any_cast<string>(*it));
++it;
he.setPassword(boost::any_cast<string>(*it));
++it;
he.setConnect(boost::any_cast<bool>(*it));
SessionManager::instance()->addFavouriteHub(he);
}
catch( const boost::bad_any_cast& ce ) {
cout << "Invalid params to addFavouriteHub. " << ce.what() << endl;
}
}
void RemoveFavoriteHubCmdHandler::handleCommand(int clientId,
const list<boost::any>& params)
{
try {
string server = boost::any_cast<string>(params.front());
SessionManager::instance()->removeFavouriteHub(server);
}
catch( const boost::bad_any_cast& ce ) {
cout << "Invalid params to removeFavouriteHub. " << ce.what() << endl;
}
}
void GetSettingsHandler::handleCommand(int clientId,const list<boost::any>& params)
{
try {
list<boost::any> settings = boost::any_cast< list<boost::any> >(params.front());
list<boost::any>::const_iterator it = settings.begin();
list<boost::any> result;
// Put all requested settings in a result list
while(it!=settings.end()) {
result.push_back(SettingsMapper::instance()->getSetting( boost::any_cast<string>(*it) ));
it++;
}
// Send the result back together with the original key list (easier handling on client side)
rpc::CmdPtr cmd( new list<boost::any> );
cmd->push_back( string("settingsInfo") );
cmd->push_back( settings );
cmd->push_back( result );
cmdSenderMethod( clientId, cmd );
}
catch( const boost::bad_function_call& fe ) {
cout << "Invalid sender method set" << fe.what() << endl;
}
catch( const boost::bad_any_cast& ce ) {
cout << "Invalid params to getHubList. " << ce.what() << endl;
}
}
void SetSettingsHandler::handleCommand( int clientId, const list<boost::any>& params)
{
try {
list<boost::any>::const_iterator it = params.begin();
list<boost::any> keys = boost::any_cast< list<boost::any> >(*it);
it++;
list<boost::any> values = boost::any_cast< list<boost::any> >(*it);
it = keys.begin();
list<boost::any>::const_iterator vit = values.begin();
while(it!=keys.end()) {
SettingsMapper::instance()->setSetting(boost::any_cast<string>(*it),*vit);
it++;
vit++;
}
}
catch( const boost::bad_any_cast& ce ) {
cout << "Invalid params to getHubList. " << ce.what() << endl;
}
}
void GetFavHubsHandler::handleCommand( int clientId, const list<boost::any>& params)
{
try {
FavoriteHubEntry::List& favHubs = SessionManager::instance()->getFavouriteHubs();
list<boost::any> result;
for(FavoriteHubEntry::List::iterator it=favHubs.begin();it!=favHubs.end(); it++)
{
list<boost::any> favStruct;
favStruct.push_back((*it)->getNick());
favStruct.push_back((*it)->getUserDescription());
favStruct.push_back((*it)->getName());
favStruct.push_back((*it)->getServer());
favStruct.push_back( (*it)->getDescription());
favStruct.push_back((*it)->getPassword());
favStruct.push_back((*it)->getConnect());
result.push_back(favStruct);
}
rpc::CmdPtr cmd( new list<boost::any> );
cmd->push_back( string("favouriteHubList") );
cmd->push_back( result );
cmdSenderMethod( clientId, cmd );
}
catch( const boost::bad_function_call& fe ) {
cout << "Invalid sender method set" << fe.what() << endl;
}
catch( const boost::bad_any_cast& ce ) {
cout << "Invalid params to getHubList. " << ce.what() << endl;
}
}
void RemoveSourceHandler::handleCommand( int clientId, const list<boost::any>& params)
{
try {
list<boost::any>::const_iterator it = params.begin();
int qid = boost::any_cast<int>(*it);
it++;
int uid = boost::any_cast<int>(*it);
TransferManager::getInstance()->removeSource(qid,uid);
}
catch( const boost::bad_any_cast& ce ) {
cout << "Invalid params to removeSource. " << ce.what() << endl;
}
}
void DieCmdHandler::handleCommand( int clientId, const list<boost::any>& params)
{
cout << "die" << endl;
driver->stopSender();
}
}
|