TeBkUmLpqAsync.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. typedef void (*TeBkUmLpqAsyncTemplatePtr) (Context*, std::string&);
  42. class TeBkUmLpqAsyncWorld {
  43. int id;
  44. int randomNumber;
  45. public:
  46. TeBkUmLpqAsyncWorld();
  47. virtual ~TeBkUmLpqAsyncWorld();
  48. int getId() const;
  49. void setId(int id);
  50. int getRandomNumber() const;
  51. void setRandomNumber(int randomNumber);
  52. };
  53. class TeBkUmLpqAsyncFortune {
  54. int id;
  55. std::string message;
  56. public:
  57. TeBkUmLpqAsyncFortune();
  58. virtual ~TeBkUmLpqAsyncFortune();
  59. int getId() const;
  60. void setId(int id);
  61. const std::string& getMessage() const;
  62. void setMessage(const std::string& message);
  63. bool operator < (const TeBkUmLpqAsyncFortune& other) const;
  64. };
  65. class TeBkUmLpqAsyncMessage {
  66. std::string message;
  67. public:
  68. virtual ~TeBkUmLpqAsyncMessage();
  69. const std::string& getMessage() const;
  70. void setMessage(const std::string& message);
  71. };
  72. struct AsyncReq {
  73. HttpResponse r;
  74. SocketInterface* sif;
  75. void* d;
  76. void* ddlib;
  77. LibpqDataSourceImpl* sqli;
  78. };
  79. struct CacheReq {
  80. void* d;
  81. CacheInterface* cchi;
  82. };
  83. class TeBkUmLpqAsyncRouter : public Router {
  84. static const std::string HELLO_WORLD;
  85. static std::string WORLD;
  86. static std::string WORLD_ONE_QUERY;
  87. static std::string WORLD_ALL_QUERY;
  88. static std::string FORTUNE_ALL_QUERY;
  89. static std::string APP_NAME;
  90. static std::string TPE_FN_NAME;
  91. std::atomic<bool> which = { false };
  92. bool strToNum(const char* str, int len, int& ret);
  93. void dbAsync(AsyncReq* req);
  94. static void dbAsyncUtil(void* ctx, int rn, std::vector<LibpqRes>& data);
  95. static void dbAsyncCh(void* ctx, bool status, std::string q, int counter);
  96. void queriesAsync(const char* q, int ql, AsyncReq* req);
  97. static void queriesAsyncUtil(void* ctx, int rn, std::vector<LibpqRes>& data);
  98. static void queriesAsyncCh(void* ctx, bool status, std::string q, int counter);
  99. void updatesAsync(const char* q, int ql, AsyncReq* req);
  100. static void updatesAsyncChQ(void* ctx, bool status, std::string q, int counter);
  101. static void updatesAsyncChU(void* ctx, bool status, std::string q, int counter);
  102. void cachedWorlds(const char*, int, std::vector<TeBkUmLpqAsyncWorld>&);
  103. static void updateCacheAsyncUtil(void* ctx, int rn, std::vector<LibpqRes>& data);
  104. static void updateCacheAsyncCh(void* ctx, bool status, std::string q, int counter);
  105. void getContextAsync(AsyncReq* req);
  106. static void getContextAsyncUtil(void* ctx, int rn, std::vector<LibpqRes>& data);
  107. static void getContextAsyncCh(void* ctx, bool status, std::string q, int counter);
  108. LibpqDataSourceImpl* sqli;
  109. LibpqDataSourceImpl* getDb();
  110. public:
  111. TeBkUmLpqAsyncRouter& operator=(const TeBkUmLpqAsyncRouter& a) {
  112. which = false;
  113. return *this;
  114. }
  115. TeBkUmLpqAsyncRouter(const TeBkUmLpqAsyncRouter& a) {
  116. which = false;
  117. sqli = NULL;
  118. }
  119. TeBkUmLpqAsyncRouter();
  120. virtual ~TeBkUmLpqAsyncRouter();
  121. void updateCache();
  122. bool route(HttpRequest* req, HttpResponse* res, void* dlib, void* ddlib, SocketInterface* sif);
  123. };
  124. #endif /* WEB_TE_BENCHMARK_UM_INCLUDE_TeBkUmLpqAsync_H_ */