TeBkUmLpqQwAsync.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * TeBkUmLpqQwAsync.h
  15. *
  16. * Created on: 03-Feb-2020
  17. * Author: sumeetc
  18. */
  19. #ifndef WEB_t1_INCLUDE_TeBkUmLpqQwAsync_H_
  20. #define WEB_t1_INCLUDE_TeBkUmLpqQwAsync_H_
  21. #include "TemplateHandler.h"
  22. #include "vector"
  23. #ifndef OS_MINGW
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. #endif
  27. #include "DataSourceManager.h"
  28. #include <stdlib.h>
  29. #include <algorithm>
  30. #include "CryptoHandler.h"
  31. #include "vector"
  32. #include "CastUtil.h"
  33. #include "CacheManager.h"
  34. #include <stdlib.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 <unordered_map>
  42. #include "ConfigurationData.h"
  43. class TeBkUmLpqQwAsyncWorld;
  44. class TeBkUmLpqQwAsyncWorld {
  45. int id;
  46. int randomNumber;
  47. public:
  48. TeBkUmLpqQwAsyncWorld();
  49. TeBkUmLpqQwAsyncWorld(int id);
  50. TeBkUmLpqQwAsyncWorld(int id, int randomNumber);
  51. virtual ~TeBkUmLpqQwAsyncWorld();
  52. int getId() const;
  53. void setId(int id);
  54. int getRandomNumber() const;
  55. void setRandomNumber(int randomNumber);
  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<TeBkUmLpqQwAsyncWorld>& 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. class TeBkUmLpqQwAsyncFortune {
  77. int id;
  78. public:
  79. std::string message_i;
  80. std::string_view message;
  81. bool allocd;
  82. TeBkUmLpqQwAsyncFortune(int id);
  83. TeBkUmLpqQwAsyncFortune(int id, std::string message);
  84. TeBkUmLpqQwAsyncFortune();
  85. virtual ~TeBkUmLpqQwAsyncFortune();
  86. int getId() const;
  87. void setId(int id);
  88. bool operator < (const TeBkUmLpqQwAsyncFortune& other) const;
  89. };
  90. class TeBkUmLpqQwAsyncMessage {
  91. std::string message;
  92. public:
  93. TeBkUmLpqQwAsyncMessage();
  94. TeBkUmLpqQwAsyncMessage(std::string message);
  95. virtual ~TeBkUmLpqQwAsyncMessage();
  96. const std::string& getMessage() const;
  97. void setMessage(const std::string& message);
  98. #ifdef HAVE_RAPID_JSON
  99. void toJson(rapidjson::Writer<rapidjson::StringBuffer>& w) {
  100. w.StartObject();
  101. w.String("message", 7);
  102. w.String(message.c_str(), static_cast<rapidjson::SizeType>(message.length()));
  103. w.EndObject();
  104. }
  105. #endif
  106. };
  107. struct AsyncUpdatesReqWq {
  108. float httpVers;
  109. bool conn_clos;
  110. BaseSocket* sif;
  111. LibpqDataSourceImpl* sqli;
  112. std::vector<TeBkUmLpqQwAsyncWorld> vec;
  113. };
  114. class TeBkUmLpqQwAsyncRouter : 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. static std::string& getUpdQuery(int count);
  126. void dbAsync(BaseSocket* sif);
  127. void queriesAsync(const char* q, int ql, BaseSocket* sif);
  128. void updatesAsync(const char* q, int ql, AsyncUpdatesReqWq* req);
  129. void updatesAsyncb(const char* q, int ql, AsyncUpdatesReqWq* req);
  130. void fortunes(BaseSocket* sif);
  131. void queriesMultiAsync(const char*, int, BaseSocket* sif);
  132. void updatesMulti(const char*, int, AsyncUpdatesReqWq*);
  133. static std::unordered_map<int, std::string> _qC;
  134. LibpqDataSourceImpl* sqli;
  135. protected:
  136. virtual LibpqDataSourceImpl* getDb(int max = 0);
  137. public:
  138. TeBkUmLpqQwAsyncRouter& operator=(const TeBkUmLpqQwAsyncRouter& a) {
  139. return *this;
  140. }
  141. TeBkUmLpqQwAsyncRouter(const TeBkUmLpqQwAsyncRouter& a) {
  142. sqli = NULL;
  143. }
  144. TeBkUmLpqQwAsyncRouter();
  145. virtual ~TeBkUmLpqQwAsyncRouter();
  146. /* These functions are here just for test purposes and serve no purpose START */
  147. static void temp() {
  148. }
  149. virtual void temp1() const {
  150. }
  151. std::map<std::string, std::string> l(std::map<std::string, std::string> a1, std::map<std::string, std::string> a2) {
  152. return std::map<std::string, std::string>();
  153. }
  154. /* END */
  155. bool route(HttpRequest* req, HttpResponse* res, BaseSocket* sif);
  156. };
  157. class TeBkUmLpqQwAsyncRouterPooled : public TeBkUmLpqQwAsyncRouter {
  158. LibpqDataSourceImpl* getDb(int max = 0);
  159. std::atomic<int> opt;
  160. bool inited;
  161. int maxconns;
  162. std::vector<LibpqDataSourceImpl*> pool;
  163. public:
  164. TeBkUmLpqQwAsyncRouterPooled& operator=(const TeBkUmLpqQwAsyncRouterPooled& a) {
  165. return *this;
  166. }
  167. TeBkUmLpqQwAsyncRouterPooled(const TeBkUmLpqQwAsyncRouterPooled& a) {
  168. this->opt = 0;
  169. this->inited = false;
  170. this->maxconns = 7;
  171. }
  172. TeBkUmLpqQwAsyncRouterPooled();
  173. virtual ~TeBkUmLpqQwAsyncRouterPooled();
  174. };
  175. #endif /* WEB_t1_INCLUDE_TeBkUmLpqQwAsync_H_ */