TeBkUm.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. * TeBkUmUm.cpp
  15. *
  16. * Created on: 03-Feb-2020
  17. * Author: sumeetc
  18. */
  19. #include "TeBkUm.h"
  20. int TeBkUmFortune::getId() const {
  21. return id;
  22. }
  23. void TeBkUmFortune::setId(int id) {
  24. this->id = id;
  25. }
  26. const std::string& TeBkUmFortune::getMessage() const {
  27. return message;
  28. }
  29. void TeBkUmFortune::setMessage(const std::string& message) {
  30. this->message = message;
  31. }
  32. TeBkUmFortune::TeBkUmFortune() {
  33. id = 0;
  34. }
  35. TeBkUmFortune::~TeBkUmFortune() {
  36. }
  37. bool TeBkUmFortune::operator < (const TeBkUmFortune& other) const {
  38. return message.compare(other.message)<0;
  39. }
  40. TeBkUmMessage::~TeBkUmMessage() {
  41. }
  42. const std::string& TeBkUmMessage::getMessage() const {
  43. return message;
  44. }
  45. void TeBkUmMessage::setMessage(const std::string& message) {
  46. this->message = message;
  47. }
  48. const std::string TeBkUmRouter::HELLO_WORLD = "Hello, World!";
  49. std::string TeBkUmRouter::WORLD = "world";
  50. void TeBkUmRouter::db(TeBkUmWorld& w) {
  51. DataSourceInterface* sqli = DataSourceManager::getImpl();
  52. int rid = rand() % 10000 + 1;
  53. GenericObject id;
  54. id << rid;
  55. try {
  56. w = sqli->get<TeBkUmWorld>(id);
  57. DataSourceManager::cleanImpl(sqli);
  58. } catch(const std::exception& e) {
  59. DataSourceManager::cleanImpl(sqli);
  60. throw e;
  61. }
  62. }
  63. void TeBkUmRouter::queries(const char* q, int ql, std::vector<TeBkUmWorld>& wlst) {
  64. int queryCount = 0;
  65. strToNum(q, ql, queryCount);
  66. if(queryCount<1)queryCount=1;
  67. else if(queryCount>500)queryCount=500;
  68. DataSourceInterface* sqli = DataSourceManager::getImpl();
  69. try {
  70. sqli->startSession(&WORLD);
  71. for (int c = 0; c < queryCount; ++c) {
  72. int rid = rand() % 10000 + 1;
  73. GenericObject id;
  74. id << rid;
  75. TeBkUmWorld w = sqli->get<TeBkUmWorld>(id);
  76. wlst.push_back(w);
  77. }
  78. sqli->endSession();
  79. DataSourceManager::cleanImpl(sqli);
  80. } catch(const std::exception& e) {
  81. DataSourceManager::cleanImpl(sqli);
  82. throw e;
  83. }
  84. }
  85. void TeBkUmRouter::updates(const char* q, int ql, std::vector<TeBkUmWorld>& wlst) {
  86. int queryCount = 0;
  87. strToNum(q, ql, queryCount);
  88. if(queryCount<1)queryCount=1;
  89. else if(queryCount>500)queryCount=500;
  90. DataSourceInterface* sqli = DataSourceManager::getImpl();
  91. try {
  92. sqli->startSession(&WORLD);
  93. for (int c = 0; c < queryCount; ++c) {
  94. int rid = rand() % 10000 + 1;
  95. GenericObject id;
  96. id << rid;
  97. TeBkUmWorld w = sqli->get<TeBkUmWorld>(id);
  98. int newRandomNumber = rand() % 10000 + 1;
  99. if(w.getRandomNumber() == newRandomNumber) {
  100. newRandomNumber += 1;
  101. if(newRandomNumber>=10000) {
  102. newRandomNumber = 1;
  103. }
  104. }
  105. w.setRandomNumber(newRandomNumber);
  106. wlst.push_back(w);
  107. }
  108. sqli->startTransaction();
  109. sqli->bulkUpdate<TeBkUmWorld>(wlst);
  110. sqli->commit();
  111. sqli->endSession();
  112. DataSourceManager::cleanImpl(sqli);
  113. } catch(const std::exception& e) {
  114. DataSourceManager::cleanImpl(sqli);
  115. throw e;
  116. }
  117. }
  118. void TeBkUmRouter::updateCache() {
  119. CacheInterface* cchi = CacheManager::getImpl();
  120. DataSourceInterface* sqli = DataSourceManager::getImpl();
  121. try {
  122. sqli->startSession(&WORLD);
  123. std::vector<TeBkUmWorld> wlist = sqli->getAll<TeBkUmWorld>();
  124. sqli->endSession();
  125. for (int c = 0; c < (int)wlist.size(); ++c) {
  126. TeBkUmWorld& w = wlist.at(c);
  127. cchi->setO(CastUtil::fromNumber(w.getId()), w);
  128. }
  129. DataSourceManager::cleanImpl(sqli);
  130. CacheManager::cleanImpl(cchi);
  131. CacheManager::triggerAppInitCompletion();
  132. } catch(const std::exception& e) {
  133. DataSourceManager::cleanImpl(sqli);
  134. CacheManager::cleanImpl(cchi);
  135. throw e;
  136. }
  137. }
  138. void TeBkUmRouter::cachedWorlds(const char* q, int ql, std::vector<TeBkUmWorld>& wlst) {
  139. int queryCount = 0;
  140. strToNum(q, ql, queryCount);
  141. if(queryCount<1)queryCount=1;
  142. else if(queryCount>500)queryCount=500;
  143. CacheInterface* cchi = CacheManager::getImpl();
  144. try {
  145. std::vector<std::string> keys;
  146. for (int c = 0; c < queryCount; ++c) {
  147. int rid = rand() % 10000 + 1;
  148. keys.push_back(CastUtil::fromNumber(rid));
  149. }
  150. cchi->mgetO<TeBkUmWorld>(keys, wlst);
  151. CacheManager::cleanImpl(cchi);
  152. } catch(const std::exception& e) {
  153. CacheManager::cleanImpl(cchi);
  154. throw e;
  155. }
  156. }
  157. void TeBkUmRouter::getContext(HttpRequest* request, Context* context) {
  158. DataSourceInterface* sqli = DataSourceManager::getImpl();
  159. try {
  160. std::vector<TeBkUmFortune> flstT = sqli->getAll<TeBkUmFortune>();
  161. std::vector<TeBkUmFortune>* flst = new std::vector<TeBkUmFortune>;
  162. flst->swap(flstT);
  163. for(int i=0;i<(int)flst->size();i++)
  164. {
  165. std::string nm = flst->at(i).getMessage();
  166. CryptoHandler::sanitizeHtml(nm);
  167. flst->at(i).setMessage(nm);
  168. }
  169. TeBkUmFortune nf;
  170. nf.setId(0);
  171. nf.setMessage("Additional fortune added at request time.");
  172. flst->push_back(nf);
  173. std::sort (flst->begin(), flst->end());
  174. context->insert(std::pair<std::string, void*>("fortunes", flst));
  175. DataSourceManager::cleanImpl(sqli);
  176. } catch(...) {
  177. DataSourceManager::cleanImpl(sqli);
  178. throw;
  179. }
  180. }
  181. //https://stackoverflow.com/questions/9631225/convert-strings-specified-by-length-not-nul-terminated-to-int-float
  182. bool TeBkUmRouter::strToNum(const char* str, int len, int& ret) {
  183. ret = 0;
  184. for(int i = 0; i < len; ++i)
  185. {
  186. if(!isdigit(str[i])) return false;
  187. ret = ret * 10 + (str[i] - '0');
  188. }
  189. return true;
  190. }
  191. bool TeBkUmRouter::route(HttpRequest* req, HttpResponse* res, void* dlib, void* ddlib, SocketInterface* sif) {
  192. //Timer t;
  193. //t.start();
  194. std::string_view path = req->getPath();
  195. if(StringUtil::endsWith(path, "/plaintext")) {
  196. //t.end();
  197. //CommonUtils::tsContRstLkp += t.timerNanoSeconds();
  198. //t.start();
  199. res->setContent(HELLO_WORLD);
  200. res->setContentType(ContentTypes::CONTENT_TYPE_TEXT_PLAIN);
  201. res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
  202. //t.end();
  203. //CommonUtils::tsContRstSer += t.timerNanoSeconds();
  204. } else if(StringUtil::endsWith(path, "/json")) {
  205. //t.end();
  206. //CommonUtils::tsContRstLkp += t.timerNanoSeconds();
  207. //t.start();
  208. TeBkUmMessage msg;
  209. msg.setMessage(HELLO_WORLD);
  210. JSONSerialize::serializeUnknown(&msg, 0, "TeBkUmMessage", res->getContentP());
  211. res->setContentType(ContentTypes::CONTENT_TYPE_APPLICATION_JSON);
  212. res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
  213. //t.end();
  214. //CommonUtils::tsContRstSer += t.timerNanoSeconds();
  215. } else if(StringUtil::endsWith(path, "/db")) {
  216. //t.end();
  217. //CommonUtils::tsContRstLkp += t.timerNanoSeconds();
  218. //t.start();
  219. TeBkUmWorld msg;
  220. db(msg);
  221. //t.end();
  222. //CommonUtils::tsContExec += t.timerNanoSeconds();
  223. //t.start();
  224. JSONSerialize::serializeUnknown(&msg, 0, "TeBkUmWorld", res->getContentP());
  225. res->setContentType(ContentTypes::CONTENT_TYPE_APPLICATION_JSON);
  226. res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
  227. //t.end();
  228. //CommonUtils::tsContRstSer += t.timerNanoSeconds();
  229. } else if(StringUtil::endsWith(path, "/queries")) {
  230. //t.end();
  231. //CommonUtils::tsContRstLkp += t.timerNanoSeconds();
  232. //t.start();
  233. struct yuarel_param params[1];
  234. yuarel_parse_query((char*)req->getQueryStr().data(), req->getQueryStr().size(), params, 1);
  235. std::vector<TeBkUmWorld> msg;
  236. queries(params[0].val, params[0].val_len, msg);
  237. //t.end();
  238. //CommonUtils::tsContExec += t.timerNanoSeconds();
  239. //t.start();
  240. JSONSerialize::serializeUnknown(&msg, 100, "std::vector<TeBkUmWorld>", res->getContentP());
  241. res->setContentType(ContentTypes::CONTENT_TYPE_APPLICATION_JSON);
  242. res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
  243. //t.end();
  244. //CommonUtils::tsContRstSer += t.timerNanoSeconds();
  245. } else if(StringUtil::endsWith(path, "/fortunes")) {
  246. Context ctx;
  247. getContext(req, &ctx);
  248. void* mkr = dlsym(ddlib, TPE_FN_NAME.c_str());
  249. if(mkr!=NULL)
  250. {
  251. TeBkUmTemplatePtr f = (TeBkUmTemplatePtr)mkr;
  252. std::string msg;
  253. f(&ctx, msg);
  254. res->setContent(msg);
  255. res->setContentType(ContentTypes::CONTENT_TYPE_TEXT_SHTML);
  256. res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
  257. }
  258. } else if(StringUtil::endsWith(path, "/updates")) {
  259. //t.end();
  260. //CommonUtils::tsContRstLkp += t.timerNanoSeconds();
  261. //t.start();
  262. struct yuarel_param params[1];
  263. yuarel_parse_query((char*)req->getQueryStr().data(), req->getQueryStr().size(), params, 1);
  264. std::vector<TeBkUmWorld> msg;
  265. updates(params[0].val, params[0].val_len, msg);
  266. //t.end();
  267. //CommonUtils::tsContExec += t.timerNanoSeconds();
  268. //t.start();
  269. JSONSerialize::serializeUnknown(&msg, 100, "std::vector<TeBkUmWorld>", res->getContentP());
  270. res->setContentType(ContentTypes::CONTENT_TYPE_APPLICATION_JSON);
  271. res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
  272. //t.end();
  273. //CommonUtils::tsContRstSer += t.timerNanoSeconds();
  274. } else if(StringUtil::endsWith(path, "/cached-worlds")) {
  275. //t.end();
  276. //CommonUtils::tsContRstLkp += t.timerNanoSeconds();
  277. //t.start();
  278. struct yuarel_param params[1];
  279. yuarel_parse_query((char*)req->getQueryStr().data(), req->getQueryStr().size(), params, 1);
  280. std::vector<TeBkUmWorld> msg;
  281. cachedWorlds(params[0].val, params[0].val_len, msg);
  282. //t.end();
  283. //CommonUtils::tsContExec += t.timerNanoSeconds();
  284. //t.start();
  285. JSONSerialize::serializeUnknown(&msg, 100, "std::vector<TeBkUmWorld>", res->getContentP());
  286. res->setContentType(ContentTypes::CONTENT_TYPE_APPLICATION_JSON);
  287. res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
  288. //t.end();
  289. //CommonUtils::tsContRstSer += t.timerNanoSeconds();
  290. } else {
  291. res->setHTTPResponseStatus(HTTPResponseStatus::NotFound);
  292. }
  293. res->setDone(true);
  294. return true;
  295. }
  296. std::string TeBkUmRouter::APP_NAME = "";
  297. std::string TeBkUmRouter::TPE_FN_NAME = "";
  298. TeBkUmRouter::TeBkUmRouter() {
  299. if(APP_NAME=="") {
  300. APP_NAME = CommonUtils::normalizeAppName("te-benchmark-um");
  301. TPE_FN_NAME = CommonUtils::getTpeFnName("tpe/fortunes.tpe", "te-benchmark-um");
  302. }
  303. }
  304. TeBkUmRouter::~TeBkUmRouter() {
  305. }