123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- /*
- Copyright 2009-2020, Sumeet Chhetri
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- /*
- * TeBkUmMgrUm.cpp
- *
- * Created on: 03-Feb-2020
- * Author: sumeetc
- */
- #include "TeBkUmMgr.h"
- int TeBkUmMgrWorld::getId() const {
- return id;
- }
- void TeBkUmMgrWorld::setId(int id) {
- this->id = id;
- }
- int TeBkUmMgrWorld::getRandomNumber() const {
- return randomNumber;
- }
- void TeBkUmMgrWorld::setRandomNumber(int randomNumber) {
- this->randomNumber = randomNumber;
- }
- TeBkUmMgrWorld::TeBkUmMgrWorld(int id) {
- this->id = id;
- randomNumber = 0;
- }
- TeBkUmMgrWorld::TeBkUmMgrWorld(int id, int randomNumber) {
- this->id = id;
- this->randomNumber = randomNumber;
- }
- TeBkUmMgrWorld::TeBkUmMgrWorld() {
- id = 0;
- randomNumber = 0;
- }
- TeBkUmMgrWorld::~TeBkUmMgrWorld() {
- }
- int TeBkUmMgrFortune::getId() const {
- return id;
- }
- void TeBkUmMgrFortune::setId(int id) {
- this->id = id;
- }
- const std::string& TeBkUmMgrFortune::getMessage() const {
- return message;
- }
- void TeBkUmMgrFortune::setMessage(const std::string& message) {
- this->message = message;
- }
- TeBkUmMgrFortune::TeBkUmMgrFortune() {
- id = 0;
- }
- TeBkUmMgrFortune::~TeBkUmMgrFortune() {
- }
- bool TeBkUmMgrFortune::operator < (const TeBkUmMgrFortune& other) const {
- return message.compare(other.message)<0;
- }
- TeBkUmMgrMessage::~TeBkUmMgrMessage() {
- }
- const std::string& TeBkUmMgrMessage::getMessage() const {
- return message;
- }
- void TeBkUmMgrMessage::setMessage(const std::string& message) {
- this->message = message;
- }
- const std::string TeBkUmMgrRouter::HELLO_WORLD = "Hello, World!";
- std::string TeBkUmMgrRouter::WORLD = "world";
- std::string TeBkUmMgrRouter::FORTUNE = "fortune";
- void TeBkUmMgrRouter::db(TeBkUmMgrWorld& w) {
- #ifdef INC_SDORM_MONGO
- MongoDBRawDataSourceImpl* sqli = getDb();
- int rid = rand() % 10000 + 1;
- try {
- bson_t q = BSON_INITIALIZER;
- bson_append_int32(&q, "_id", 3, rid);
- sqli->begin(WORLD);
- sqli->executeQuery(&q, &w, &TeBkUmMgrRouter::dbUtil);
- sqli->end();
- bson_destroy(&q);
- } catch(const std::exception& e) {
- throw e;
- }
- #endif
- }
- #ifdef INC_SDORM_MONGO
- void TeBkUmMgrRouter::dbUtil(void* ctx, int rn, std::vector<MgRawRes>& data) {
- TeBkUmMgrWorld* w = (TeBkUmMgrWorld*)ctx;
- for(int i=0;i<(int)data.size();i++) {
- if(data.at(i).n=="_id") {
- w->setId((int)data.at(i).d);
- if(w->getId()<=0) {
- w->setId((int)data.at(i).l);
- }
- }
- if(data.at(i).n=="randomNumber") {
- w->setRandomNumber((int)data.at(i).d);
- if(w->getRandomNumber()==0) {
- w->setRandomNumber((int)data.at(i).l);
- }
- }
- }
- }
- #endif
- void TeBkUmMgrRouter::queries(const char* q, int ql, std::vector<TeBkUmMgrWorld>& wlst) {
- int queryCount = 0;
- strToNum(q, ql, queryCount);
- if(queryCount<1)queryCount=1;
- else if(queryCount>500)queryCount=500;
- #ifdef INC_SDORM_MONGO
- MongoDBRawDataSourceImpl* sqli = getDb();
- try {
- TeBkUmMgrWorld w;
- sqli->begin(WORLD);
- for (int c = 0; c < queryCount; ++c) {
- int rid = rand() % 10000 + 1;
- bson_t q = BSON_INITIALIZER;
- bson_append_int32(&q, "_id", 3, rid);
- sqli->executeQuery(&q, &w, &TeBkUmMgrRouter::dbUtil);
- bson_destroy(&q);
- wlst.push_back(w);
- }
- sqli->end();
- } catch(const std::exception& e) {
- throw e;
- }
- #endif
- }
- void TeBkUmMgrRouter::updates(const char* q, int ql, std::vector<TeBkUmMgrWorld>& wlst) {
- int queryCount = 0;
- strToNum(q, ql, queryCount);
- if(queryCount<1)queryCount=1;
- else if(queryCount>500)queryCount=500;
- #ifdef INC_SDORM_MONGO
- MongoDBRawDataSourceImpl* sqli = getDb();
- try {
- sqli->startBulk(WORLD);
- for (int c = 0; c < queryCount; ++c) {
- int rid = rand() % 10000 + 1;
- bson_t q;
- bson_init(&q);
- bson_append_int32(&q, "_id", 3, rid);
- TeBkUmMgrWorld w;
- sqli->executeQuery(&q, &w, &TeBkUmMgrRouter::dbUtil);
- int newRandomNumber = rand() % 10000 + 1;
- if(w.getRandomNumber() == newRandomNumber) {
- newRandomNumber += 1;
- if(newRandomNumber>=10000) {
- newRandomNumber = 1;
- }
- }
- w.setRandomNumber(newRandomNumber);
- bson_t du;
- bson_t d;
- bson_init(&du);
- bson_append_document_begin(&du, "$set", 4, &d);
- //bson_append_int32(&d, "_id", 3, w.getId());
- bson_append_int32(&d, "randomNumber", 12, w.getRandomNumber());
- bson_append_document_end(&du, &d);
- sqli->addBulk(&q, &du);
- /*char* str = bson_as_json(&du, NULL);
- printf("%s\n", str);
- bson_free(str);*/
- bson_destroy(&du);
- bson_destroy(&q);
- wlst.push_back(w);
- }
- sqli->endBulk();
- } catch(const std::exception& e) {
- throw e;
- }
- #endif
- }
- void TeBkUmMgrRouter::updateCache() {
- #ifdef INC_SDORM_MONGO
- CacheInterface* cchi = CacheManager::getImpl();
- MongoDBRawDataSourceImpl* sqli = getDb();
- try {
- std::vector<TeBkUmMgrWorld> wlist;
- sqli->begin(WORLD);
- sqli->executeQuery(NULL, &wlist, &TeBkUmMgrRouter::updateCacheUtil);
- sqli->end();
- for (int c = 0; c < (int)wlist.size(); ++c) {
- TeBkUmMgrWorld& w = wlist.at(c);
- char str[12];
- sprintf(str, "%d;%d", w.getId(), w.getRandomNumber());
- cchi->setRaw(w.getId(), str);
- }
- CacheManager::cleanImpl(cchi);
- CacheManager::triggerAppInitCompletion();
- } catch(const std::exception& e) {
- CacheManager::cleanImpl(cchi);
- throw e;
- }
- #endif
- }
- #ifdef INC_SDORM_MONGO
- void TeBkUmMgrRouter::updateCacheUtil(void* ctx, int rn, std::vector<MgRawRes>& data) {
- std::vector<TeBkUmMgrWorld>* wlist = (std::vector<TeBkUmMgrWorld>*)ctx;
- TeBkUmMgrWorld w;
- for(int i=0;i<(int)data.size();i++) {
- if(data.at(i).n=="_id") {
- w.setId((int)data.at(i).d);
- if(w.getId()<=0) {
- w.setId((int)data.at(i).l);
- }
- }
- if(data.at(i).n=="randomNumber") {
- w.setRandomNumber((int)data.at(i).d);
- if(w.getRandomNumber()<=0) {
- w.setRandomNumber((int)data.at(i).l);
- }
- }
- }
- wlist->push_back(w);
- }
- #endif
- void TeBkUmMgrRouter::cachedWorlds(const char* q, int ql, std::vector<TeBkUmMgrWorld>& wlst) {
- int queryCount = 0;
- strToNum(q, ql, queryCount);
- if(queryCount<1)queryCount=1;
- else if(queryCount>500)queryCount=500;
- CacheInterface* cchi = CacheManager::getImpl();
- try {
- std::vector<unsigned long long> keys;
- for (int c = 0; c < queryCount; ++c) {
- int rid = rand() % 10000 + 1;
- keys.emplace_back(rid);
- }
- std::vector<std::string> values;
- cchi->getValues(keys, values);
- for (int c = 0; c < queryCount; ++c) {
- std::string& v = values.at(c);
- size_t fn = v.find(";");
- int tmp = 0;
- CommonUtils::fastStrToNum(v.substr(0, fn).c_str(), fn, tmp);
- int tmp1 = 0;
- CommonUtils::fastStrToNum(v.substr(fn+1).c_str(), v.length()-fn-1, tmp1);
- wlst.emplace_back(tmp, tmp1);
- }
- CacheManager::cleanImpl(cchi);
- } catch(const std::exception& e) {
- CacheManager::cleanImpl(cchi);
- throw e;
- }
- }
- void TeBkUmMgrRouter::getContext(HttpRequest* request, Context* context) {
- #ifdef INC_SDORM_MONGO
- MongoDBRawDataSourceImpl* sqli = getDb();
- try {
- std::vector<TeBkUmMgrFortune>* flst = new std::vector<TeBkUmMgrFortune>;
- sqli->begin(FORTUNE);
- sqli->executeQuery(NULL, flst, &TeBkUmMgrRouter::getContextUtil);
- sqli->end();
- TeBkUmMgrFortune nf;
- nf.setId(0);
- nf.setMessage("Additional fortune added at request time.");
- flst->push_back(nf);
- std::sort (flst->begin(), flst->end());
- context->emplace("fortunes", flst);
- } catch(...) {
- throw;
- }
- #endif
- }
- #ifdef INC_SDORM_MONGO
- void TeBkUmMgrRouter::getContextUtil(void* ctx, int rn, std::vector<MgRawRes>& data) {
- std::vector<TeBkUmMgrFortune>* flst = (std::vector<TeBkUmMgrFortune>*)ctx;
- TeBkUmMgrFortune w;
- for(int i=0;i<(int)data.size();i++) {
- if(data.at(i).n=="_id") {
- w.setId((int)data.at(i).d);
- if(w.getId()<=0) {
- w.setId((int)data.at(i).l);
- }
- }
- if(data.at(i).n=="message") {
- std::string nm = data.at(i).s;
- CryptoHandler::sanitizeHtml(nm);
- w.setMessage(nm);
- }
- }
- flst->push_back(w);
- }
- #endif
- //https://stackoverflow.com/questions/9631225/convert-strings-specified-by-length-not-nul-terminated-to-int-float
- bool TeBkUmMgrRouter::strToNum(const char* str, int len, int& ret) {
- ret = 0;
- for(int i = 0; i < len; ++i)
- {
- if(!isdigit(str[i])) return false;
- ret = ret * 10 + (str[i] - '0');
- }
- return true;
- }
- bool TeBkUmMgrRouter::route(HttpRequest* req, HttpResponse* res, BaseSocket* sif) {
- std::string_view path = req->getPath();
- if(StringUtil::endsWith(path, "/plaint")) {
- res->setContent(HELLO_WORLD);
- res->setContentType(ContentTypes::CONTENT_TYPE_TEXT_PLAIN);
- res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
- } else if(StringUtil::endsWith(path, "/j")) {
- TeBkUmMgrMessage msg;
- msg.setMessage(HELLO_WORLD);
- JSONSerialize::serializeObject(&msg, m_ser, res->getContentP());
- res->setContentType(ContentTypes::CONTENT_TYPE_APPLICATION_JSON);
- res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
- } else if(StringUtil::endsWith(path, "/d")) {
- TeBkUmMgrWorld msg;
- db(msg);
- JSONSerialize::serializeObject(&msg, w_ser, res->getContentP());
- res->setContentType(ContentTypes::CONTENT_TYPE_APPLICATION_JSON);
- res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
- } else if(StringUtil::endsWith(path, "/quer")) {
- struct yuarel_param params[1];
- yuarel_parse_query((char*)req->getQueryStr().data(), req->getQueryStr().size(), params, 1);
- std::vector<TeBkUmMgrWorld> msg;
- queries(params[0].val, params[0].val_len, msg);
- JSONSerialize::serializeObjectCont(&msg, wcont_ser, "vector", res->getContentP());
- res->setContentType(ContentTypes::CONTENT_TYPE_APPLICATION_JSON);
- res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
- } else if(StringUtil::endsWith(path, "/fortu")) {
- Context ctx;
- getContext(req, &ctx);
- if(tmplFunc!=NULL)
- {
- fcpstream str;
- tmplFunc(&ctx, str);
- res->setContent(str.str());
- res->setContentType(ContentTypes::CONTENT_TYPE_TEXT_HTML);
- res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
- }
- } else if(StringUtil::endsWith(path, "/updt")) {
- struct yuarel_param params[1];
- yuarel_parse_query((char*)req->getQueryStr().data(), req->getQueryStr().size(), params, 1);
- std::vector<TeBkUmMgrWorld> msg;
- updates(params[0].val, params[0].val_len, msg);
- JSONSerialize::serializeObjectCont(&msg, wcont_ser, "vector", res->getContentP());
- res->setContentType(ContentTypes::CONTENT_TYPE_APPLICATION_JSON);
- res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
- } else if(StringUtil::endsWith(path, "/cached-wld")) {
- struct yuarel_param params[1];
- yuarel_parse_query((char*)req->getQueryStr().data(), req->getQueryStr().size(), params, 1);
- std::vector<TeBkUmMgrWorld> msg;
- cachedWorlds(params[0].val, params[0].val_len, msg);
- JSONSerialize::serializeObjectCont(&msg, wcont_ser, "vector", res->getContentP());
- res->setContentType(ContentTypes::CONTENT_TYPE_APPLICATION_JSON);
- res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
- } else {
- res->setHTTPResponseStatus(HTTPResponseStatus::NotFound);
- }
- res->setDone(true);
- return true;
- }
- TemplatePtr TeBkUmMgrRouter::tmplFunc;
- Ser TeBkUmMgrRouter::m_ser;
- Ser TeBkUmMgrRouter::w_ser;
- SerCont TeBkUmMgrRouter::wcont_ser;
- TeBkUmMgrRouter::TeBkUmMgrRouter() {
- #ifdef INC_SDORM_MONGO
- sqli = NULL;
- #endif
- tmplFunc = TemplateUtil::getTemplateFunc("t2", "tpe/fortunes.tpe");
- m_ser = Serializer::getSerFuncForObject("t2", "TeBkUmMgrMessage");
- w_ser = Serializer::getSerFuncForObject("t2", "TeBkUmMgrWorld");
- wcont_ser = Serializer::getSerFuncForObjectCont("t2", "TeBkUmMgrWorld", "std::vector");
- }
- TeBkUmMgrRouter::~TeBkUmMgrRouter() {
- }
- #ifdef INC_SDORM_MONGO
- MongoDBRawDataSourceImpl* TeBkUmMgrRouter::getDb() {
- if(sqli==NULL) {
- sqli = static_cast<MongoDBRawDataSourceImpl*>(DataSourceManager::getRawImpl("MongoDB-DSN", "t2"));
- }
- return sqli;
- }
- #endif
|