TeBkUmLpqAsync.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. * TeBkUmLpqAsync.h
  15. *
  16. * Created on: 03-Feb-2020
  17. * Author: sumeetc
  18. */
  19. #ifndef WEB_TE_BENCHMARK_UM_INCLUDE_TeBkUmLpqAsync_H_
  20. #define WEB_TE_BENCHMARK_UM_INCLUDE_TeBkUmLpqAsync_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. class TeBkUmLpqAsyncWorld {
  42. int id;
  43. int randomNumber;
  44. public:
  45. TeBkUmLpqAsyncWorld();
  46. TeBkUmLpqAsyncWorld(int id);
  47. TeBkUmLpqAsyncWorld(int id, int randomNumber);
  48. virtual ~TeBkUmLpqAsyncWorld();
  49. int getId() const;
  50. void setId(int id);
  51. int getRandomNumber() const;
  52. void setRandomNumber(int randomNumber);
  53. };
  54. class TeBkUmLpqAsyncFortune {
  55. int id;
  56. public:
  57. std::string message_i;
  58. std::string_view message;
  59. bool allocd;
  60. TeBkUmLpqAsyncFortune(int id);
  61. TeBkUmLpqAsyncFortune(int id, std::string message);
  62. TeBkUmLpqAsyncFortune();
  63. virtual ~TeBkUmLpqAsyncFortune();
  64. int getId() const;
  65. void setId(int id);
  66. bool operator < (const TeBkUmLpqAsyncFortune& other) const;
  67. };
  68. class TeBkUmLpqAsyncMessage {
  69. std::string message;
  70. public:
  71. TeBkUmLpqAsyncMessage();
  72. TeBkUmLpqAsyncMessage(std::string message);
  73. virtual ~TeBkUmLpqAsyncMessage();
  74. const std::string& getMessage() const;
  75. void setMessage(const std::string& message);
  76. };
  77. struct AsyncReq {
  78. float httpVers;
  79. bool conn_clos;
  80. SocketInterface* sif;
  81. LibpqDataSourceImpl* sqli;
  82. TeBkUmLpqAsyncWorld w;
  83. std::vector<TeBkUmLpqAsyncWorld> vec;
  84. std::list<TeBkUmLpqAsyncFortune> flst;
  85. };
  86. struct CacheReq {
  87. CacheInterface* cchi;
  88. std::vector<TeBkUmLpqAsyncWorld> vec;
  89. };
  90. class TeBkUmLpqAsyncRouter : public Router {
  91. static const std::string HELLO_WORLD;
  92. static std::string WORLD;
  93. static std::string WORLD_ONE_QUERY;
  94. static std::string WORLD_ALL_QUERY;
  95. static std::string FORTUNE_ALL_QUERY;
  96. static TemplatePtr tmplFunc;
  97. static Ser m_ser;
  98. static Ser w_ser;
  99. static SerCont wcont_ser;
  100. static bool strToNum(const char* str, int len, int& ret);
  101. void dbAsync(AsyncReq* req);
  102. static void dbAsyncUtil(void* ctx, int rn, int cn, char * d);
  103. static void dbAsyncCh(void* ctx, bool status, const std::string& q, int counter);
  104. void queriesAsync(const char* q, int ql, AsyncReq* req);
  105. static void queriesAsyncUtil(void* ctx, int rn, int cn, char * d);
  106. static void queriesAsyncCh(void* ctx, bool status, const std::string& q, int counter);
  107. #ifndef HAVE_LIBPQ_BATCH
  108. void queriesMultiAsync(const char*, int, AsyncReq*);
  109. static void queriesMultiAsyncUtil(void* ctx, int, int, char *, int);
  110. static void queriesMultiAsyncCh(void* ctx, bool status, const std::string& q, int counter);
  111. #endif
  112. void updatesAsync(const char* q, int ql, AsyncReq* req);
  113. static void updatesAsyncChQ(void* ctx, bool status, const std::string& q, int counter);
  114. static void updatesAsyncChU(void* ctx, bool status, const std::string& q, int counter);
  115. static std::string& getUpdQuery(int count);
  116. void updatesAsyncb(const char* q, int ql, AsyncReq* req);
  117. static void updatesAsyncbChQ(void* ctx, bool status, const std::string& q, int counter);
  118. static void updatesAsyncbChU(void* ctx, bool status, const std::string& q, int counter);
  119. void cachedWorlds(const char*, int, std::vector<TeBkUmLpqAsyncWorld>&);
  120. static void updateCacheAsyncUtil(void* ctx, int rn, std::vector<LibpqRes>& data);
  121. static void updateCacheAsyncCh(void* ctx, bool status, const std::string& q, int counter);
  122. void getContextAsync(AsyncReq* req);
  123. static void getContextAsyncUtil(void* ctx, int rn, int cn, char * d, int l);
  124. static void getContextAsyncCh(void* ctx, bool status, const std::string& q, int counter);
  125. static std::map<int, std::string> _qC;
  126. LibpqDataSourceImpl* sqli;
  127. LibpqDataSourceImpl* getDb();
  128. //static Logger logger;
  129. public:
  130. TeBkUmLpqAsyncRouter& operator=(const TeBkUmLpqAsyncRouter& a) {
  131. return *this;
  132. }
  133. TeBkUmLpqAsyncRouter(const TeBkUmLpqAsyncRouter& a) {
  134. sqli = NULL;
  135. }
  136. TeBkUmLpqAsyncRouter();
  137. virtual ~TeBkUmLpqAsyncRouter();
  138. void updateCache();
  139. bool route(HttpRequest* req, HttpResponse* res, SocketInterface* sif);
  140. };
  141. #endif /* WEB_TE_BENCHMARK_UM_INCLUDE_TeBkUmLpqAsync_H_ */