StrusContext::StrusContext( cppcms::service *srv, unsigned int nof_threads, const std::string moduleDir, const std::vector<std::string> modules_ ) : context_map( ), modules( ), timer( srv->get_io_service( ) ) { errorhnd = strus::createErrorBuffer_standard( 0, nof_threads ); BOOSTER_DEBUG( PACKAGE ) << "Search directory for modules implementing extensions is '" << moduleDir << "'"; for( std::vector<std::string>::const_iterator it = modules_.begin( ); it != modules_.end( ); it++ ) { boost::filesystem::path path( moduleDir ); boost::filesystem::path fullPath = boost::filesystem::absolute( path ) /= *it; BOOSTER_DEBUG( PACKAGE ) << "Loading module '" << fullPath.string( ) << "'"; strus::ModuleEntryPoint::Status status; const strus::ModuleEntryPoint *entrypoint = strus::loadModuleEntryPoint( fullPath.string( ).c_str( ), status, &matchModuleVersion ); if( !entrypoint ) { BOOSTER_WARNING( PACKAGE ) << "failed loading extension module '" << fullPath.string( ) << "': " << status.errormsg; continue; } if( entrypoint->type != strus::ModuleEntryPoint::Storage ) { BOOSTER_WARNING( PACKAGE ) << "module '" << fullPath.string( ) << "' is not a storage module and is not loaded into the server"; continue; } modules.push_back( entrypoint ); } transaction_max_idle_time = srv->settings( ).get<unsigned int>( "transactions.max_idle_time", DEFAULT_TRANSACTION_MAX_IDLE_TIME ); last_wake = time( 0 ); onTimer( booster::system::error_code( ) ); }
void dbcontroller::logIP(int teamid) { std::stringstream ss; ss.str(""); ss << "teamip_" << teamid; teamip ti; std::string IP = cur_ctx->request().remote_addr(); BOOSTER_NOTICE("scoreboard") << "team " << teamid << " has IP " << IP; if (cur_ctx->cache().fetch_data(ss.str(), ti)) { for (std::vector<std::string>::iterator it = ti.seenip.begin(); it != ti.seenip.end(); ++it) { if (IP.compare(*it) == 0) { //already known IP for this team BOOSTER_DEBUG("scoreboard") << "found in cache."; return; } } } //Still not known. :( std::shared_ptr<QSqlQuery> stmt(new QSqlQuery(this->db)); bool ok = doQuery(stmt, [&](std::shared_ptr<QSqlQuery> stmt) { if (!stmt->prepare("INSERT IGNORE INTO ipteams set idteam=:team, ip=:ip;")) return false; stmt->bindValue("team", teamid); stmt->bindValue("ip", QString::fromStdString(IP)); return true; }); if (!ok) { BOOSTER_ERROR("scoreboard") << "Could not add IP to teams IPs, aborting"; return; } ok = doQuery(stmt, [&](std::shared_ptr<QSqlQuery> stmt) { if (!stmt->prepare("SELECT ip FROM ipteams WHERE idteam=:team;")) return false; stmt->bindValue("team", teamid); return true; }); if (!ok) { BOOSTER_ERROR("scoreboard") << "Could not fetch team IP, aborting"; return; } if (!ok) return; ti.id = teamid; ti.seenip.clear(); while (stmt->next()) { ti.seenip.push_back(stmt->record().value(0).toString().toStdString()); BOOSTER_DEBUG("scoreboard") << "Pushing in cache for team " << teamid << " value " << stmt->record().value(0).toString().toStdString(); } cur_ctx->cache().store_data(ss.str(), ti, -1); return; }
void StrusContext::terminateIdleTransactions( ) { BOOSTER_DEBUG( PACKAGE ) << "Checking for long-running transactions"; for( std::map<std::string, StrusIndexContext *>::iterator it = context_map.begin( ); it != context_map.end( ); it++ ) { it->second->terminateIdleTransactions( transaction_max_idle_time ); } }
StrusContext::~StrusContext( ) { BOOSTER_DEBUG( PACKAGE ) << "Shutting down strus context"; for( std::map<std::string, StrusIndexContext *>::iterator it = context_map.begin( ); it != context_map.end( ); it++ ) { delete it->second; } }
void JokeHandler::main(std::string url) { std::string start_index = request().get("start_index"); std::string length = request().get("length"); std::string status = request().get("status"); int joke_status = status!=""?std::atoi(status.c_str()):1; int start = std::atoi(start_index.c_str()); int end = std::atoi(length.c_str()); DatabaseOperator *databaseOperator = new DatabaseOperator(); std::shared_ptr<std::vector<std::shared_ptr<Joke>>> jokes = databaseOperator->jokeManager->get_jokes(start, end, joke_status); cppcms::json::value json_result; unsigned long current_page_joke_number = jokes->size(); json_result["current_joke_counts"] = current_page_joke_number; json_result["joke_counts"] = databaseOperator->jokeManager->get_joke_count(joke_status); json_result["status"] = joke_status; json_result["current_page"] = start / end + 1; for (int i = 0; i < current_page_joke_number; ++i) { cppcms::json::value json_joke; int joke_id = jokes->at(i)->get_joke_id(); json_joke["id"] = joke_id; json_joke["title"] = jokes->at(i)->get_title(); json_joke["content"] = jokes->at(i)->get_content(); json_joke["comment_count"] = databaseOperator->commentManager->get_comment_vector(joke_id)->size(); json_result["jokes"][i] = json_joke; } response_as_json(json_result); BOOSTER_DEBUG("HiJoke") << json_result; delete databaseOperator; }
std::shared_ptr<dbcontroller> dbcontroller::getInstance() { std::lock_guard<std::mutex> guard(dbcontroller::instances_mutex); std::thread::id me = std::this_thread::get_id(); std::shared_ptr<dbcontroller> inst; if (dbcontroller::thread_instances.count(me) == 0) { inst = std::shared_ptr<dbcontroller>(new dbcontroller()); dbcontroller::thread_instances[me] = std::shared_ptr<dbcontroller>(inst); BOOSTER_DEBUG("scoreboard") << "Initializing dbc for thread " << std::this_thread::get_id(); } else { inst = dbcontroller::thread_instances[me]; } return std::shared_ptr<dbcontroller>(inst); }
dbcontroller::dbcontroller() { cppcms::json::value settings = srv->settings(); std::stringstream ss; ss.clear(); ss.str("conn_thread_"); ss << std::this_thread::get_id(); this->db = QSqlDatabase::addDatabase("QMYSQL", QString::fromStdString(ss.str())); this->db.setHostName(QString::fromStdString(settings.get<std::string>("db.hostname"))); this->db.setDatabaseName(QString::fromStdString(settings.get<std::string>("db.dbname"))); this->db.setUserName(QString::fromStdString(settings.get<std::string>("db.username"))); this->db.setPassword(QString::fromStdString(settings.get<std::string>("db.password"))); if (!this->db.open()) { BOOSTER_CRITICAL("scoreboard") << "Error while connecting to database. Aborting."; throw 1; } BOOSTER_DEBUG("scoreboard") << "Opened db conn: " << this->db.connectionName().toStdString() << " from thread " << std::this_thread::get_id(); }