world.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // world.h
  2. #ifndef WORLD_H
  3. #define WORLD_H 1
  4. #include <ulib/orm/orm.h>
  5. #include <ulib/json/value.h>
  6. #include <ulib/utility/uhttp.h>
  7. #include <ulib/orm/orm_driver.h>
  8. #include <ulib/net/server/client_image.h>
  9. #ifdef U_STATIC_ORM_DRIVER_PGSQL
  10. # include <ulib/orm/driver/orm_driver_pgsql.h>
  11. #endif
  12. class World {
  13. public:
  14. uint32_t id, randomNumber;
  15. World()
  16. {
  17. U_TRACE_CTOR(5, World, "")
  18. // coverity[uninit_ctor]
  19. # ifdef U_COVERITY_FALSE_POSITIVE
  20. id = randomNumber = 0;
  21. # endif
  22. }
  23. World(uint32_t _id, uint32_t _randomNumber) : id(_id), randomNumber(_randomNumber)
  24. {
  25. U_TRACE_CTOR(5, World, "%u,%u", _id, _randomNumber)
  26. }
  27. World(const World& w) : id(w.id), randomNumber(w.randomNumber)
  28. {
  29. U_TRACE_CTOR(5, World, "%p", &w)
  30. U_MEMORY_TEST_COPY(w)
  31. }
  32. ~World()
  33. {
  34. U_TRACE_DTOR(5, World)
  35. }
  36. // JSON
  37. void toJSON(UString& json)
  38. {
  39. U_TRACE(5, "World::toJSON(%V)", json.rep)
  40. json.toJSON(U_JSON_METHOD_HANDLER(id, unsigned int));
  41. json.toJSON(U_JSON_METHOD_HANDLER(randomNumber, unsigned int));
  42. }
  43. void fromJSON(UValue& json)
  44. {
  45. U_TRACE(5, "World::fromJSON(%p)", &json)
  46. json.fromJSON(U_JSON_METHOD_HANDLER(id, unsigned int));
  47. json.fromJSON(U_JSON_METHOD_HANDLER(randomNumber, unsigned int));
  48. }
  49. // ORM
  50. void bindParam(UOrmStatement* stmt)
  51. {
  52. U_TRACE(5, "World::bindParam(%p)", stmt)
  53. stmt->bindParam(U_ORM_TYPE_HANDLER(id, unsigned int));
  54. stmt->bindParam(U_ORM_TYPE_HANDLER(randomNumber, unsigned int));
  55. }
  56. void bindResult(UOrmStatement* stmt)
  57. {
  58. U_TRACE(5, "World::bindResult(%p)", stmt)
  59. stmt->bindResult(U_ORM_TYPE_HANDLER(id, unsigned int));
  60. stmt->bindResult(U_ORM_TYPE_HANDLER(randomNumber, unsigned int));
  61. }
  62. // SERVICE
  63. bool operator<(const World& other) const { return cmp_obj(&id, &other.id); }
  64. static int cmp_obj(const void* a, const void* b)
  65. {
  66. U_TRACE(5, "World::cmp_obj(%p,%p)", a, b)
  67. # ifdef U_STDCPP_ENABLE
  68. /**
  69. * The comparison function must follow a strict-weak-ordering
  70. *
  71. * 1) For all x, it is not the case that x < x (irreflexivity)
  72. * 2) For all x, y, if x < y then it is not the case that y < x (asymmetry)
  73. * 3) For all x, y, and z, if x < y and y < z then x < z (transitivity)
  74. * 4) For all x, y, and z, if x is incomparable with y, and y is incomparable with z, then x is incomparable with z (transitivity of incomparability)
  75. */
  76. return (((const World*)a)->id < (((const World*)b)->id));
  77. # else
  78. return (*(const World**)a)->id < ((*(const World**)b)->id);
  79. # endif
  80. }
  81. static char* pwbuffer;
  82. static char wbuffer[18000];
  83. static uint32_t rnum, rnumber[500];
  84. static World* pworld_query;
  85. static UOrmSession* psql_query;
  86. static UOrmStatement* pstmt_query;
  87. #ifdef U_STATIC_ORM_DRIVER_PGSQL
  88. static PGconn* conn;
  89. static UOrmDriverPgSql* pdrv;
  90. static UPgSqlStatement* pstmt;
  91. static char num2str[sizeof(unsigned int)];
  92. static bool initPipeline()
  93. {
  94. U_TRACE(5, "World::initPipeline()")
  95. if (pdrv)
  96. {
  97. (void) U_SYSCALL(PQsetnonblocking, "%p,%u", conn, 1);
  98. (void) U_SYSCALL(PQenterBatchMode, "%p", conn);
  99. U_RETURN(true);
  100. }
  101. U_RETURN(false);
  102. }
  103. static PGresult* execPrepared()
  104. {
  105. U_TRACE_NO_PARAM(5, "World::execPrepared()")
  106. U_INTERNAL_ASSERT_MAJOR(rnumber[0], 0)
  107. *(unsigned int*)num2str = htonl(rnumber[0]);
  108. PGresult* res = (PGresult*) U_SYSCALL(PQexecPrepared, "%p,%S,%u,%p,%p,%p,%u", conn, pstmt->stmtName, 1, pstmt->paramValues, pstmt->paramLengths, pstmt->paramFormats, 1);
  109. U_RETURN_POINTER(res, PGresult);
  110. }
  111. static PGresult* execPrepared(uint32_t i)
  112. {
  113. U_TRACE(5, "World::execPrepared(%u)", i)
  114. U_INTERNAL_ASSERT_MAJOR(rnumber[i], 0)
  115. *(unsigned int*)num2str = htonl(rnumber[i]);
  116. PGresult* res = (PGresult*) U_SYSCALL(PQexecPrepared, "%p,%S,%u,%p,%p,%p,%u", conn, pstmt->stmtName, 1, pstmt->paramValues, pstmt->paramLengths, pstmt->paramFormats, 1);
  117. U_RETURN_POINTER(res, PGresult);
  118. }
  119. static void sendQueryPrepared(uint32_t i)
  120. {
  121. U_TRACE(5, "World::sendQueryPrepared(%u)", i)
  122. U_INTERNAL_ASSERT_MAJOR(rnumber[i], 0)
  123. *(unsigned int*)num2str = htonl(rnumber[i]);
  124. (void) U_SYSCALL(PQsendQueryPrepared, "%p,%S,%u,%p,%p,%p,%u", conn, pstmt->stmtName, 1, pstmt->paramValues, pstmt->paramLengths, pstmt->paramFormats, 1);
  125. }
  126. #endif
  127. static void initResult()
  128. {
  129. U_TRACE(5, "World::initResult()")
  130. u_put_unalignedp64(wbuffer, U_MULTICHAR_CONSTANT64('C','o','n','t','e','n','t','-'));
  131. u_put_unalignedp64(wbuffer+8, U_MULTICHAR_CONSTANT64('L','e','n','g','t','h',':',' '));
  132. u_put_unalignedp64(wbuffer+16, U_MULTICHAR_CONSTANT64('1','3','3','3','1','\r','\n','C'));
  133. u_put_unalignedp64(wbuffer+24, U_MULTICHAR_CONSTANT64('o','n','t','e','n','t','-','T'));
  134. u_put_unalignedp64(wbuffer+32, U_MULTICHAR_CONSTANT64('y','p','e',':',' ','a','p','p'));
  135. u_put_unalignedp64(wbuffer+40, U_MULTICHAR_CONSTANT64('l','i','c','a','t','i','o','n'));
  136. u_put_unalignedp64(wbuffer+48, U_MULTICHAR_CONSTANT64('/','j','s','o','n','\r','\n','\r'));
  137. u_put_unalignedp16(wbuffer+56, U_MULTICHAR_CONSTANT16('\n','['));
  138. pwbuffer = wbuffer + U_CONSTANT_SIZE("Content-Length: 13331\r\nContent-Type: application/json\r\n\r\n[");
  139. }
  140. static void endResult()
  141. {
  142. U_TRACE_NO_PARAM(5, "World::endResult()")
  143. *(pwbuffer-1) = ']';
  144. uint32_t len = pwbuffer-wbuffer,
  145. body_len = len - U_CONSTANT_SIZE("Content-Length: 13331\r\nContent-Type: application/json\r\n\r\n");
  146. pwbuffer = u_num2str32(body_len, wbuffer + U_CONSTANT_SIZE("Content-Length: "));
  147. while (*pwbuffer != '\r') *pwbuffer++ = ' ';
  148. UClientImage_Base::wbuffer->setConstant(wbuffer, len);
  149. }
  150. static void initOneResult()
  151. {
  152. U_TRACE(5, "World::initOneResult()")
  153. u_put_unalignedp64(wbuffer, U_MULTICHAR_CONSTANT64('C','o','n','t','e','n','t','-'));
  154. u_put_unalignedp64(wbuffer+8, U_MULTICHAR_CONSTANT64('L','e','n','g','t','h',':',' '));
  155. u_put_unalignedp32(wbuffer+16, U_MULTICHAR_CONSTANT32('3','1','\r','\n'));
  156. u_put_unalignedp64(wbuffer+20, U_MULTICHAR_CONSTANT64('C','o','n','t','e','n','t','-'));
  157. u_put_unalignedp64(wbuffer+28, U_MULTICHAR_CONSTANT64('T','y','p','e',':',' ','a','p'));
  158. u_put_unalignedp64(wbuffer+36, U_MULTICHAR_CONSTANT64('p','l','i','c','a','t','i','o'));
  159. u_put_unalignedp64(wbuffer+44, U_MULTICHAR_CONSTANT64('n','/','j','s','o','n','\r','\n'));
  160. u_put_unalignedp32(wbuffer+52, U_MULTICHAR_CONSTANT32('\r','\n','{','\0'));
  161. pwbuffer = wbuffer + U_CONSTANT_SIZE("Content-Length: 31\r\nContent-Type: application/json\r\n\r\n{");
  162. }
  163. static void endOneResult()
  164. {
  165. U_TRACE_NO_PARAM(5, "World::endOneResult()")
  166. *pwbuffer = '}';
  167. uint32_t len = pwbuffer-wbuffer+1,
  168. body_len = len - U_CONSTANT_SIZE("Content-Length: 31\r\nContent-Type: application/json\r\n\r\n");
  169. (void) u_num2str32(body_len, wbuffer+U_CONSTANT_SIZE("Content-Length: "));
  170. UClientImage_Base::wbuffer->setConstant(wbuffer, len);
  171. }
  172. static void handlerOneResult(uint32_t uid, uint32_t random)
  173. {
  174. U_TRACE(5, "World::handlerOneResult(%u,%u)", uid, random)
  175. u_put_unalignedp32(pwbuffer, U_MULTICHAR_CONSTANT32('"','i','d','"'));
  176. pwbuffer[4] = ':';
  177. pwbuffer = u_num2str32(uid, pwbuffer+5);
  178. u_put_unalignedp64(pwbuffer, U_MULTICHAR_CONSTANT64(',','"','r','a','n','d','o','m'));
  179. u_put_unalignedp64(pwbuffer+8, U_MULTICHAR_CONSTANT64('N','u','m','b','e','r','"',':'));
  180. pwbuffer = u_num2str32(random, pwbuffer+16);
  181. }
  182. static void handlerResult(uint32_t uid, uint32_t random)
  183. {
  184. U_TRACE(5, "World::handlerResult(%u,%u)", uid, random)
  185. u_put_unalignedp32(pwbuffer, U_MULTICHAR_CONSTANT32('{','"','i','d'));
  186. u_put_unalignedp16(pwbuffer+4, U_MULTICHAR_CONSTANT16('"',':'));
  187. pwbuffer = u_num2str32(uid, pwbuffer+6);
  188. u_put_unalignedp64(pwbuffer, U_MULTICHAR_CONSTANT64(',','"','r','a','n','d','o','m'));
  189. u_put_unalignedp64(pwbuffer+8, U_MULTICHAR_CONSTANT64('N','u','m','b','e','r','"',':'));
  190. pwbuffer = u_num2str32(random, pwbuffer+16);
  191. u_put_unalignedp16(pwbuffer, U_MULTICHAR_CONSTANT16('}',','));
  192. pwbuffer += 2;
  193. }
  194. static void handlerResult(uint32_t i)
  195. {
  196. U_TRACE(5, "World::handlerResult(%u)", i)
  197. U_INTERNAL_ASSERT_POINTER(pworld_query)
  198. U_INTERNAL_DUMP("pworld_query->randomNumber = %u", pworld_query->randomNumber)
  199. }
  200. static void handlerResultSql(uint32_t i)
  201. {
  202. U_TRACE(5, "World::handlerResultSql(%u)", i)
  203. U_INTERNAL_ASSERT_POINTER(pworld_query)
  204. handlerResult(rnumber[i], pworld_query->randomNumber);
  205. }
  206. static void doUpdateNoSql(vPFu handlerUpdateNoSql)
  207. {
  208. U_TRACE(5, "World::doUpdateNoSql(%p)", handlerUpdateNoSql)
  209. initResult();
  210. for (uint32_t i = 0, n = UHTTP::getFormFirstNumericValue(1, 500); i < n; ++i)
  211. {
  212. handlerUpdateNoSql(i);
  213. handlerResult(rnumber[i], rnum);
  214. }
  215. endResult();
  216. }
  217. static void handlerFork()
  218. {
  219. U_TRACE_NO_PARAM(5, "World::handlerFork()")
  220. if (rnumber[0] == 0) for (uint32_t i = 0; i < 500; ++i) rnumber[i] = u_get_num_random_range1(10000);
  221. }
  222. static void handlerForkSql()
  223. {
  224. U_TRACE_NO_PARAM(5, "World::handlerForkSql()")
  225. if (psql_query == U_NULLPTR)
  226. {
  227. U_NEW(UOrmSession, psql_query, UOrmSession(U_CONSTANT_TO_PARAM("hello_world")));
  228. if (psql_query->isReady() == false)
  229. {
  230. U_WARNING("World::handlerForkSql(): we cound't connect to db");
  231. U_DELETE(psql_query)
  232. psql_query = U_NULLPTR;
  233. return;
  234. }
  235. U_NEW(UOrmStatement, pstmt_query, UOrmStatement(*psql_query, U_CONSTANT_TO_PARAM("SELECT randomNumber FROM World WHERE id = ?")));
  236. U_NEW(World, pworld_query, World);
  237. pstmt_query->use( pworld_query->id);
  238. pstmt_query->into(pworld_query->randomNumber);
  239. # ifdef U_STATIC_ORM_DRIVER_PGSQL
  240. if (UOrmDriver::isPGSQL())
  241. {
  242. conn = (PGconn*)(pdrv = (UOrmDriverPgSql*)psql_query->getDriver())->UOrmDriver::connection;
  243. pstmt = (UPgSqlStatement*)pstmt_query->getStatement();
  244. (void) pstmt->setBindParam(pdrv);
  245. pstmt->paramValues[0] = num2str;
  246. pstmt->paramLengths[0] = sizeof(unsigned int);
  247. }
  248. # endif
  249. handlerFork();
  250. }
  251. }
  252. const char* dump(bool breset) const;
  253. #endif
  254. private:
  255. U_DISALLOW_ASSIGN(World)
  256. };
  257. #endif