TeBkUmLpq.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * TeBkUmLpq.h
  15. *
  16. * Created on: 03-Feb-2020
  17. * Author: sumeetc
  18. */
  19. #ifndef WEB_t1_INCLUDE_TeBkUmLpq_H_
  20. #define WEB_t1_INCLUDE_TeBkUmLpq_H_
  21. #include "TemplateHandler.h"
  22. #include "vector"
  23. #include "list"
  24. #ifndef OS_MINGW
  25. #include <netinet/in.h>
  26. #include <arpa/inet.h>
  27. #endif
  28. #include "DataSourceManager.h"
  29. #include <stdlib.h>
  30. #include <algorithm>
  31. #include "CryptoHandler.h"
  32. #include "CastUtil.h"
  33. #include <stdlib.h>
  34. #include "CacheManager.h"
  35. #include "HttpRequest.h"
  36. #include "HttpResponse.h"
  37. #include "JSONSerialize.h"
  38. #include "string"
  39. #include "yuarel.h"
  40. #include "Router.h"
  41. #include "Reflector.h"
  42. #include <unordered_map>
  43. class TeBkUmLpqWorld {
  44. int id;
  45. int randomNumber;
  46. public:
  47. TeBkUmLpqWorld(int id);
  48. TeBkUmLpqWorld(int id, int randomNumber);
  49. TeBkUmLpqWorld();
  50. virtual ~TeBkUmLpqWorld();
  51. int getId() const;
  52. void setId(int id);
  53. int getRandomNumber() const;
  54. void setRandomNumber(int randomNumber);
  55. bool operator < (const TeBkUmLpqWorld& other) const;
  56. #ifdef HAVE_RAPID_JSON
  57. void toJson(rapidjson::Writer<rapidjson::StringBuffer>& w) {
  58. w.StartObject();
  59. w.String("id", 2);
  60. w.Int(id);
  61. w.String("randomNumber", 12);
  62. w.Int(randomNumber);
  63. w.EndObject();
  64. }
  65. #endif
  66. #ifdef HAVE_RAPID_JSON
  67. static void toJson(std::vector<TeBkUmLpqWorld>& vec, rapidjson::Writer<rapidjson::StringBuffer>& w) {
  68. w.StartArray();
  69. for(auto el: vec) {
  70. el.toJson(w);
  71. }
  72. w.EndArray();
  73. }
  74. #endif
  75. };
  76. struct UpdQrData {
  77. std::vector<TeBkUmLpqWorld>* wlist;
  78. std::stringstream ss;
  79. bool status;
  80. int queryCount;
  81. };
  82. class TeBkUmLpqFortune {
  83. int id;
  84. public:
  85. std::string message_i;
  86. std::string_view message;
  87. bool allocd;
  88. TeBkUmLpqFortune(int id);
  89. TeBkUmLpqFortune(int id, std::string message);
  90. TeBkUmLpqFortune();
  91. virtual ~TeBkUmLpqFortune();
  92. int getId() const;
  93. void setId(int id);
  94. bool operator < (const TeBkUmLpqFortune& other) const;
  95. };
  96. class TeBkUmLpqRouterPicoV;
  97. class TeBkUmLpqMessage {
  98. std::string message;
  99. public:
  100. TeBkUmLpqMessage();
  101. TeBkUmLpqMessage(std::string message);
  102. virtual ~TeBkUmLpqMessage();
  103. const std::string& getMessage() const;
  104. void setMessage(const std::string& message);
  105. #ifdef HAVE_RAPID_JSON
  106. void toJson(rapidjson::Writer<rapidjson::StringBuffer>& w) {
  107. w.StartObject();
  108. w.String("message", 7);
  109. w.String(message.c_str(), static_cast<rapidjson::SizeType>(message.length()));
  110. w.EndObject();
  111. }
  112. #endif
  113. };
  114. class TeBkUmLpqRouter : public Router {
  115. static const std::string HELLO_WORLD;
  116. static const std::string WORLD;
  117. static const std::string WORLD_ONE_QUERY;
  118. static const std::string WORLD_ALL_QUERY;
  119. static const std::string FORTUNE_ALL_QUERY;
  120. static int g_seed;
  121. static TemplatePtr tmplFunc;
  122. static Ser m_ser;
  123. static Ser w_ser;
  124. static SerCont wcont_ser;
  125. void db(TeBkUmLpqWorld&);
  126. void queries(const char*, int, std::vector<TeBkUmLpqWorld>&);
  127. void queriesMulti(const char*, int, std::vector<TeBkUmLpqWorld>&);
  128. void updates(const char*, int, std::vector<TeBkUmLpqWorld>&);
  129. void updatesMulti(const char*, int, std::vector<TeBkUmLpqWorld>&);
  130. void cachedWorlds(const char*, int, std::vector<TeBkUmLpqWorld>&);
  131. void handleTemplate(HttpRequest* req, HttpResponse* res, BaseSocket* sif);
  132. std::string& getUpdQuery(int count);
  133. std::unordered_map<int, std::string> _qC;
  134. LibpqDataSourceImpl* sqli;
  135. LibpqDataSourceImpl* getDb();
  136. friend class TeBkUmLpqRouterPicoV;
  137. public:
  138. TeBkUmLpqRouter();
  139. virtual ~TeBkUmLpqRouter();
  140. void updateCache();
  141. bool route(HttpRequest* req, HttpResponse* res, BaseSocket* sif);
  142. };
  143. class TeBkUmLpqRouterPicoV : public TeBkUmLpqRouter {
  144. void handleTemplate(HttpResponse* res);
  145. public:
  146. TeBkUmLpqRouterPicoV();
  147. virtual ~TeBkUmLpqRouterPicoV();
  148. bool route(HttpRequest* req, HttpResponse* res, BaseSocket* sif);
  149. };
  150. #endif /* WEB_t1_INCLUDE_TeBkUmLpq_H_ */