TeBkUmMgr.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. Copyright 2009-2020, Sumeet Chhetri
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*
  14. * TeBkUmMgrUm.cpp
  15. *
  16. * Created on: 03-Feb-2020
  17. * Author: sumeetc
  18. */
  19. #include "TeBkUmMgr.h"
  20. int TeBkUmMgrWorld::getId() const {
  21. return id;
  22. }
  23. void TeBkUmMgrWorld::setId(int id) {
  24. this->id = id;
  25. }
  26. int TeBkUmMgrWorld::getRandomNumber() const {
  27. return randomNumber;
  28. }
  29. void TeBkUmMgrWorld::setRandomNumber(int randomNumber) {
  30. this->randomNumber = randomNumber;
  31. }
  32. TeBkUmMgrWorld::TeBkUmMgrWorld(int id) {
  33. this->id = id;
  34. randomNumber = 0;
  35. }
  36. TeBkUmMgrWorld::TeBkUmMgrWorld(int id, int randomNumber) {
  37. this->id = id;
  38. this->randomNumber = randomNumber;
  39. }
  40. TeBkUmMgrWorld::TeBkUmMgrWorld() {
  41. id = 0;
  42. randomNumber = 0;
  43. }
  44. TeBkUmMgrWorld::~TeBkUmMgrWorld() {
  45. }
  46. int TeBkUmMgrFortune::getId() const {
  47. return id;
  48. }
  49. void TeBkUmMgrFortune::setId(int id) {
  50. this->id = id;
  51. }
  52. const std::string& TeBkUmMgrFortune::getMessage() const {
  53. return message;
  54. }
  55. void TeBkUmMgrFortune::setMessage(const std::string& message) {
  56. this->message = message;
  57. }
  58. TeBkUmMgrFortune::TeBkUmMgrFortune() {
  59. id = 0;
  60. }
  61. TeBkUmMgrFortune::~TeBkUmMgrFortune() {
  62. }
  63. bool TeBkUmMgrFortune::operator < (const TeBkUmMgrFortune& other) const {
  64. return message.compare(other.message)<0;
  65. }
  66. TeBkUmMgrMessage::~TeBkUmMgrMessage() {
  67. }
  68. const std::string& TeBkUmMgrMessage::getMessage() const {
  69. return message;
  70. }
  71. void TeBkUmMgrMessage::setMessage(const std::string& message) {
  72. this->message = message;
  73. }
  74. const std::string TeBkUmMgrRouter::HELLO_WORLD = "Hello, World!";
  75. std::string TeBkUmMgrRouter::WORLD = "world";
  76. std::string TeBkUmMgrRouter::FORTUNE = "fortune";
  77. void TeBkUmMgrRouter::db(TeBkUmMgrWorld& w) {
  78. #ifdef INC_SDORM_MONGO
  79. MongoDBRawDataSourceImpl* sqli = getDb();
  80. int rid = rand() % 10000 + 1;
  81. try {
  82. bson_t q = BSON_INITIALIZER;
  83. bson_append_int32(&q, "_id", 3, rid);
  84. sqli->begin(WORLD);
  85. sqli->executeQuery(&q, &w, &TeBkUmMgrRouter::dbUtil);
  86. sqli->end();
  87. bson_destroy(&q);
  88. } catch(const std::exception& e) {
  89. throw e;
  90. }
  91. #endif
  92. }
  93. #ifdef INC_SDORM_MONGO
  94. void TeBkUmMgrRouter::dbUtil(void* ctx, int rn, std::vector<MgRawRes>& data) {
  95. TeBkUmMgrWorld* w = (TeBkUmMgrWorld*)ctx;
  96. for(int i=0;i<(int)data.size();i++) {
  97. if(data.at(i).n=="_id") {
  98. w->setId((int)data.at(i).d);
  99. if(w->getId()<=0) {
  100. w->setId((int)data.at(i).l);
  101. }
  102. }
  103. if(data.at(i).n=="randomNumber") {
  104. w->setRandomNumber((int)data.at(i).d);
  105. if(w->getRandomNumber()==0) {
  106. w->setRandomNumber((int)data.at(i).l);
  107. }
  108. }
  109. }
  110. }
  111. #endif
  112. void TeBkUmMgrRouter::queries(const char* q, int ql, std::vector<TeBkUmMgrWorld>& wlst) {
  113. int queryCount = 0;
  114. strToNum(q, ql, queryCount);
  115. if(queryCount<1)queryCount=1;
  116. else if(queryCount>500)queryCount=500;
  117. #ifdef INC_SDORM_MONGO
  118. MongoDBRawDataSourceImpl* sqli = getDb();
  119. try {
  120. TeBkUmMgrWorld w;
  121. sqli->begin(WORLD);
  122. for (int c = 0; c < queryCount; ++c) {
  123. int rid = rand() % 10000 + 1;
  124. bson_t q = BSON_INITIALIZER;
  125. bson_append_int32(&q, "_id", 3, rid);
  126. sqli->executeQuery(&q, &w, &TeBkUmMgrRouter::dbUtil);
  127. bson_destroy(&q);
  128. wlst.push_back(w);
  129. }
  130. sqli->end();
  131. } catch(const std::exception& e) {
  132. throw e;
  133. }
  134. #endif
  135. }
  136. void TeBkUmMgrRouter::updates(const char* q, int ql, std::vector<TeBkUmMgrWorld>& wlst) {
  137. int queryCount = 0;
  138. strToNum(q, ql, queryCount);
  139. if(queryCount<1)queryCount=1;
  140. else if(queryCount>500)queryCount=500;
  141. #ifdef INC_SDORM_MONGO
  142. MongoDBRawDataSourceImpl* sqli = getDb();
  143. try {
  144. sqli->startBulk(WORLD);
  145. for (int c = 0; c < queryCount; ++c) {
  146. int rid = rand() % 10000 + 1;
  147. bson_t q;
  148. bson_init(&q);
  149. bson_append_int32(&q, "_id", 3, rid);
  150. TeBkUmMgrWorld w;
  151. sqli->executeQuery(&q, &w, &TeBkUmMgrRouter::dbUtil);
  152. int newRandomNumber = rand() % 10000 + 1;
  153. if(w.getRandomNumber() == newRandomNumber) {
  154. newRandomNumber += 1;
  155. if(newRandomNumber>=10000) {
  156. newRandomNumber = 1;
  157. }
  158. }
  159. w.setRandomNumber(newRandomNumber);
  160. bson_t du;
  161. bson_t d;
  162. bson_init(&du);
  163. bson_append_document_begin(&du, "$set", 4, &d);
  164. //bson_append_int32(&d, "_id", 3, w.getId());
  165. bson_append_int32(&d, "randomNumber", 12, w.getRandomNumber());
  166. bson_append_document_end(&du, &d);
  167. sqli->addBulk(&q, &du);
  168. /*char* str = bson_as_json(&du, NULL);
  169. printf("%s\n", str);
  170. bson_free(str);*/
  171. bson_destroy(&du);
  172. bson_destroy(&q);
  173. wlst.push_back(w);
  174. }
  175. sqli->endBulk();
  176. } catch(const std::exception& e) {
  177. throw e;
  178. }
  179. #endif
  180. }
  181. void TeBkUmMgrRouter::updateCache() {
  182. #ifdef INC_SDORM_MONGO
  183. CacheInterface* cchi = CacheManager::getImpl();
  184. MongoDBRawDataSourceImpl* sqli = getDb();
  185. try {
  186. std::vector<TeBkUmMgrWorld> wlist;
  187. sqli->begin(WORLD);
  188. sqli->executeQuery(NULL, &wlist, &TeBkUmMgrRouter::updateCacheUtil);
  189. sqli->end();
  190. for (int c = 0; c < (int)wlist.size(); ++c) {
  191. TeBkUmMgrWorld& w = wlist.at(c);
  192. char str[12];
  193. sprintf(str, "%d;%d", w.getId(), w.getRandomNumber());
  194. cchi->setRaw(w.getId(), str);
  195. }
  196. CacheManager::cleanImpl(cchi);
  197. CacheManager::triggerAppInitCompletion();
  198. } catch(const std::exception& e) {
  199. CacheManager::cleanImpl(cchi);
  200. throw e;
  201. }
  202. #endif
  203. }
  204. #ifdef INC_SDORM_MONGO
  205. void TeBkUmMgrRouter::updateCacheUtil(void* ctx, int rn, std::vector<MgRawRes>& data) {
  206. std::vector<TeBkUmMgrWorld>* wlist = (std::vector<TeBkUmMgrWorld>*)ctx;
  207. TeBkUmMgrWorld w;
  208. for(int i=0;i<(int)data.size();i++) {
  209. if(data.at(i).n=="_id") {
  210. w.setId((int)data.at(i).d);
  211. if(w.getId()<=0) {
  212. w.setId((int)data.at(i).l);
  213. }
  214. }
  215. if(data.at(i).n=="randomNumber") {
  216. w.setRandomNumber((int)data.at(i).d);
  217. if(w.getRandomNumber()<=0) {
  218. w.setRandomNumber((int)data.at(i).l);
  219. }
  220. }
  221. }
  222. wlist->push_back(w);
  223. }
  224. #endif
  225. void TeBkUmMgrRouter::cachedWorlds(const char* q, int ql, std::vector<TeBkUmMgrWorld>& wlst) {
  226. int queryCount = 0;
  227. strToNum(q, ql, queryCount);
  228. if(queryCount<1)queryCount=1;
  229. else if(queryCount>500)queryCount=500;
  230. CacheInterface* cchi = CacheManager::getImpl();
  231. try {
  232. std::vector<unsigned long long> keys;
  233. for (int c = 0; c < queryCount; ++c) {
  234. int rid = rand() % 10000 + 1;
  235. keys.emplace_back(rid);
  236. }
  237. std::vector<std::string> values;
  238. cchi->getValues(keys, values);
  239. for (int c = 0; c < queryCount; ++c) {
  240. std::string& v = values.at(c);
  241. size_t fn = v.find(";");
  242. int tmp = 0;
  243. CommonUtils::fastStrToNum(v.substr(0, fn).c_str(), fn, tmp);
  244. int tmp1 = 0;
  245. CommonUtils::fastStrToNum(v.substr(fn+1).c_str(), v.length()-fn-1, tmp1);
  246. wlst.emplace_back(tmp, tmp1);
  247. }
  248. CacheManager::cleanImpl(cchi);
  249. } catch(const std::exception& e) {
  250. CacheManager::cleanImpl(cchi);
  251. throw e;
  252. }
  253. }
  254. void TeBkUmMgrRouter::getContext(HttpRequest* request, Context* context) {
  255. #ifdef INC_SDORM_MONGO
  256. MongoDBRawDataSourceImpl* sqli = getDb();
  257. try {
  258. std::vector<TeBkUmMgrFortune>* flst = new std::vector<TeBkUmMgrFortune>;
  259. sqli->begin(FORTUNE);
  260. sqli->executeQuery(NULL, flst, &TeBkUmMgrRouter::getContextUtil);
  261. sqli->end();
  262. TeBkUmMgrFortune nf;
  263. nf.setId(0);
  264. nf.setMessage("Additional fortune added at request time.");
  265. flst->push_back(nf);
  266. std::sort (flst->begin(), flst->end());
  267. context->emplace("fortunes", flst);
  268. } catch(...) {
  269. throw;
  270. }
  271. #endif
  272. }
  273. #ifdef INC_SDORM_MONGO
  274. void TeBkUmMgrRouter::getContextUtil(void* ctx, int rn, std::vector<MgRawRes>& data) {
  275. std::vector<TeBkUmMgrFortune>* flst = (std::vector<TeBkUmMgrFortune>*)ctx;
  276. TeBkUmMgrFortune w;
  277. for(int i=0;i<(int)data.size();i++) {
  278. if(data.at(i).n=="_id") {
  279. w.setId((int)data.at(i).d);
  280. if(w.getId()<=0) {
  281. w.setId((int)data.at(i).l);
  282. }
  283. }
  284. if(data.at(i).n=="message") {
  285. std::string nm = data.at(i).s;
  286. CryptoHandler::sanitizeHtml(nm);
  287. w.setMessage(nm);
  288. }
  289. }
  290. flst->push_back(w);
  291. }
  292. #endif
  293. //https://stackoverflow.com/questions/9631225/convert-strings-specified-by-length-not-nul-terminated-to-int-float
  294. bool TeBkUmMgrRouter::strToNum(const char* str, int len, int& ret) {
  295. ret = 0;
  296. for(int i = 0; i < len; ++i)
  297. {
  298. if(!isdigit(str[i])) return false;
  299. ret = ret * 10 + (str[i] - '0');
  300. }
  301. return true;
  302. }
  303. bool TeBkUmMgrRouter::route(HttpRequest* req, HttpResponse* res, BaseSocket* sif) {
  304. std::string_view path = req->getPath();
  305. if(StringUtil::endsWith(path, "/plaint")) {
  306. res->setContent(HELLO_WORLD);
  307. res->setContentType(ContentTypes::CONTENT_TYPE_TEXT_PLAIN);
  308. res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
  309. } else if(StringUtil::endsWith(path, "/j")) {
  310. TeBkUmMgrMessage msg;
  311. msg.setMessage(HELLO_WORLD);
  312. JSONSerialize::serializeObject(&msg, m_ser, res->getContentP());
  313. res->setContentType(ContentTypes::CONTENT_TYPE_APPLICATION_JSON);
  314. res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
  315. } else if(StringUtil::endsWith(path, "/d")) {
  316. TeBkUmMgrWorld msg;
  317. db(msg);
  318. JSONSerialize::serializeObject(&msg, w_ser, res->getContentP());
  319. res->setContentType(ContentTypes::CONTENT_TYPE_APPLICATION_JSON);
  320. res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
  321. } else if(StringUtil::endsWith(path, "/quer")) {
  322. struct yuarel_param params[1];
  323. yuarel_parse_query((char*)req->getQueryStr().data(), req->getQueryStr().size(), params, 1);
  324. std::vector<TeBkUmMgrWorld> msg;
  325. queries(params[0].val, params[0].val_len, msg);
  326. JSONSerialize::serializeObjectCont(&msg, wcont_ser, "vector", res->getContentP());
  327. res->setContentType(ContentTypes::CONTENT_TYPE_APPLICATION_JSON);
  328. res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
  329. } else if(StringUtil::endsWith(path, "/fortu")) {
  330. Context ctx;
  331. getContext(req, &ctx);
  332. if(tmplFunc!=NULL)
  333. {
  334. fcpstream str;
  335. tmplFunc(&ctx, str);
  336. res->setContent(str.str());
  337. res->setContentType(ContentTypes::CONTENT_TYPE_TEXT_HTML);
  338. res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
  339. }
  340. } else if(StringUtil::endsWith(path, "/updt")) {
  341. struct yuarel_param params[1];
  342. yuarel_parse_query((char*)req->getQueryStr().data(), req->getQueryStr().size(), params, 1);
  343. std::vector<TeBkUmMgrWorld> msg;
  344. updates(params[0].val, params[0].val_len, msg);
  345. JSONSerialize::serializeObjectCont(&msg, wcont_ser, "vector", res->getContentP());
  346. res->setContentType(ContentTypes::CONTENT_TYPE_APPLICATION_JSON);
  347. res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
  348. } else if(StringUtil::endsWith(path, "/cached-wld")) {
  349. struct yuarel_param params[1];
  350. yuarel_parse_query((char*)req->getQueryStr().data(), req->getQueryStr().size(), params, 1);
  351. std::vector<TeBkUmMgrWorld> msg;
  352. cachedWorlds(params[0].val, params[0].val_len, msg);
  353. JSONSerialize::serializeObjectCont(&msg, wcont_ser, "vector", res->getContentP());
  354. res->setContentType(ContentTypes::CONTENT_TYPE_APPLICATION_JSON);
  355. res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
  356. } else {
  357. res->setHTTPResponseStatus(HTTPResponseStatus::NotFound);
  358. }
  359. res->setDone(true);
  360. return true;
  361. }
  362. TemplatePtr TeBkUmMgrRouter::tmplFunc;
  363. Ser TeBkUmMgrRouter::m_ser;
  364. Ser TeBkUmMgrRouter::w_ser;
  365. SerCont TeBkUmMgrRouter::wcont_ser;
  366. TeBkUmMgrRouter::TeBkUmMgrRouter() {
  367. #ifdef INC_SDORM_MONGO
  368. sqli = NULL;
  369. #endif
  370. tmplFunc = TemplateUtil::getTemplateFunc("t2", "tpe/fortunes.tpe");
  371. m_ser = Serializer::getSerFuncForObject("t2", "TeBkUmMgrMessage");
  372. w_ser = Serializer::getSerFuncForObject("t2", "TeBkUmMgrWorld");
  373. wcont_ser = Serializer::getSerFuncForObjectCont("t2", "TeBkUmMgrWorld", "std::vector");
  374. }
  375. TeBkUmMgrRouter::~TeBkUmMgrRouter() {
  376. }
  377. #ifdef INC_SDORM_MONGO
  378. MongoDBRawDataSourceImpl* TeBkUmMgrRouter::getDb() {
  379. if(sqli==NULL) {
  380. sqli = static_cast<MongoDBRawDataSourceImpl*>(DataSourceManager::getRawImpl("MongoDB-DSN", "t2"));
  381. }
  382. return sqli;
  383. }
  384. #endif