MetastreamGem.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <sstream>
  9. #include <string.h>
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <Metastream_Traits_Platform.h>
  13. #include "MetastreamGem.h"
  14. #if AZ_TRAIT_METASTREAM_USE_CIVET
  15. #include "CivetHttpServer.h"
  16. #endif // AZ_TRAIT_METASTREAM_USE_CIVET
  17. namespace Metastream
  18. {
  19. void Metastream::MetastreamReflectComponent::Reflect(AZ::ReflectContext* context)
  20. {
  21. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. serializeContext->Class<MetastreamReflectComponent, AZ::Component>()
  24. ->Version(0)
  25. ;
  26. }
  27. AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
  28. if (behaviorContext)
  29. {
  30. behaviorContext->EBus<MetastreamRequestBus>("MetastreamRequestBus")
  31. ->Event("StartHTTPServer", &MetastreamRequestBus::Events::StartHTTPServer)
  32. ->Event("StopHTTPServer", &MetastreamRequestBus::Events::StopHTTPServer)
  33. ->Event("AddStringToCache", &MetastreamRequestBus::Events::AddStringToCache)
  34. ->Event("AddBoolToCache", &MetastreamRequestBus::Events::AddBoolToCache)
  35. ->Event("AddDoubleToCache", &MetastreamRequestBus::Events::AddDoubleToCache)
  36. ->Event("AddUnsigned64ToCache", &MetastreamRequestBus::Events::AddUnsigned64ToCache)
  37. ->Event("AddSigned64ToCache", &MetastreamRequestBus::Events::AddSigned64ToCache)
  38. ->Event("AddEntityIdToCache", &MetastreamRequestBus::Events::AddEntityIdToCache)
  39. ->Event("AddArrayToCache", &MetastreamRequestBus::Events::AddArrayToCache)
  40. ->Event("AddObjectToCache", &MetastreamRequestBus::Events::AddObjectToCache)
  41. ->Event("AddArrayToObject", &MetastreamRequestBus::Events::AddArrayToObject)
  42. ->Event("AddObjectToObject", &MetastreamRequestBus::Events::AddObjectToObject)
  43. ->Event("AddObjectToArray", &MetastreamRequestBus::Events::AddObjectToArray)
  44. ->Event("AddStringToArray", &MetastreamRequestBus::Events::AddStringToArray)
  45. ->Event("AddBoolToArray", &MetastreamRequestBus::Events::AddBoolToArray)
  46. ->Event("AddDoubleToArray", &MetastreamRequestBus::Events::AddDoubleToArray)
  47. ->Event("AddUnsigned64ToArray", &MetastreamRequestBus::Events::AddUnsigned64ToArray)
  48. ->Event("AddSigned64ToArray", &MetastreamRequestBus::Events::AddSigned64ToArray)
  49. ->Event("AddEntityIdToArray", &MetastreamRequestBus::Events::AddEntityIdToArray)
  50. ->Event("AddStringToObject", &MetastreamRequestBus::Events::AddStringToObject)
  51. ->Event("AddBoolToObject", &MetastreamRequestBus::Events::AddBoolToObject)
  52. ->Event("AddDoubleToObject", &MetastreamRequestBus::Events::AddDoubleToObject)
  53. ->Event("AddUnsigned64ToObject", &MetastreamRequestBus::Events::AddUnsigned64ToObject)
  54. ->Event("AddSigned64ToObject", &MetastreamRequestBus::Events::AddSigned64ToObject)
  55. ->Event("AddEntityIdToObject", &MetastreamRequestBus::Events::AddEntityIdToObject)
  56. ;
  57. }
  58. }
  59. MetastreamGem::MetastreamGem()
  60. : m_serverEnabled(0)
  61. , m_serverOptionsCVar(nullptr)
  62. {
  63. m_descriptors.push_back(MetastreamReflectComponent::CreateDescriptor());
  64. // Initialise the cache
  65. m_cache = std::unique_ptr<DataCache>(new DataCache());
  66. MetastreamRequestBus::Handler::BusConnect();
  67. }
  68. MetastreamGem::~MetastreamGem()
  69. {
  70. MetastreamRequestBus::Handler::BusDisconnect();
  71. }
  72. void MetastreamGem::OnSystemEvent(ESystemEvent event, [[maybe_unused]] UINT_PTR wparam, [[maybe_unused]] UINT_PTR lparam)
  73. {
  74. using namespace Metastream;
  75. switch (event)
  76. {
  77. case ESYSTEM_EVENT_GAME_POST_INIT:
  78. // All other Gems will exist at this point, for a full list of civet options, see https://github.com/civetweb/civetweb/blob/master/docs/UserManual.md
  79. //
  80. // Note: 1) For security reasons, the option "enable_directory_listing" is forced to "no".
  81. // 2) The following options are ignored for security reasons: enable_directory_listing, cgi_interpreter, run_as_user, put_delete_auth_file
  82. // 3) Options are a set of key=value separated by the semi colon character. If a option needs to use the ';' or '='
  83. // then use $semi or $equ.
  84. //
  85. m_serverOptionsCVar = REGISTER_STRING("metastream_serveroptions", "document_root=Gems/Metastream/Files;listening_ports=8082", VF_NULL, "Metastream HTTP Server options");
  86. REGISTER_CVAR2("metastream_enabled", &m_serverEnabled, 0, VF_READONLY, "State of the Metastream HTTP server (READONLY)");
  87. REGISTER_COMMAND("metastream_start", StartHTTPServerCmd, 0, "Starts the Metastream HTTP server");
  88. REGISTER_COMMAND("metastream_stop", StopHTTPServerCmd, 0, "Stops the Metastream HTTP server");
  89. break;
  90. case ESYSTEM_EVENT_FULL_SHUTDOWN:
  91. case ESYSTEM_EVENT_FAST_SHUTDOWN:
  92. // Put your shutdown code here
  93. // Other Gems may have been shutdown already, but none will have destructed
  94. if (m_server.get())
  95. {
  96. m_server->Stop();
  97. m_serverEnabled = 0;
  98. }
  99. break;
  100. }
  101. }
  102. void MetastreamGem::AddStringToCache(const char* table, const char* key, const char* value)
  103. {
  104. if (m_cache.get())
  105. {
  106. m_cache->AddToCache(table, key, value);
  107. }
  108. }
  109. void MetastreamGem::AddBoolToCache(const char* table, const char* key, bool value)
  110. {
  111. if (m_cache.get())
  112. {
  113. m_cache->AddToCache(table, key, value);
  114. }
  115. }
  116. void MetastreamGem::AddVec3ToCache(const char* table, const char* key, const Vec3 & value)
  117. {
  118. if (m_cache.get())
  119. {
  120. m_cache->AddToCache(table, key, AZ::Vector3(value.x, value.y, value.z));
  121. }
  122. }
  123. void MetastreamGem::AddDoubleToCache(const char* table, const char* key, double value)
  124. {
  125. if (m_cache.get())
  126. {
  127. m_cache->AddToCache(table, key, value);
  128. }
  129. }
  130. void MetastreamGem::AddUnsigned64ToCache(const char* table, const char* key, AZ::u64 value)
  131. {
  132. if (m_cache.get())
  133. {
  134. m_cache->AddToCache(table, key, value);
  135. }
  136. }
  137. void MetastreamGem::AddSigned64ToCache(const char* table, const char* key, AZ::s64 value)
  138. {
  139. if (m_cache.get())
  140. {
  141. m_cache->AddToCache(table, key, value);
  142. }
  143. }
  144. void MetastreamGem::AddArrayToCache(const char* table, const char* key, const char* arrayName)
  145. {
  146. if (m_cache.get())
  147. {
  148. m_cache->AddArrayToCache(table, key, arrayName);
  149. }
  150. }
  151. void MetastreamGem::AddObjectToCache(const char* table, const char* key, const char* objectName)
  152. {
  153. if (m_cache.get())
  154. {
  155. m_cache->AddObjectToCache(table, key, objectName);
  156. }
  157. }
  158. void MetastreamGem::AddStringToArray(const char* table, const char* arrayName, const char* value)
  159. {
  160. if (m_cache.get())
  161. {
  162. m_cache->AddToArray(table, arrayName, value);
  163. }
  164. }
  165. void MetastreamGem::AddBoolToArray(const char* table, const char* arrayName, bool value)
  166. {
  167. if (m_cache.get())
  168. {
  169. m_cache->AddToArray(table, arrayName, value);
  170. }
  171. }
  172. void MetastreamGem::AddVec3ToArray(const char* table, const char* arrayName, const Vec3 & value)
  173. {
  174. if (m_cache.get())
  175. {
  176. m_cache->AddToArray(table, arrayName, AZStd::string::format("[%f,%f,%f]", value.x, value.y, value.z).c_str());
  177. }
  178. }
  179. void MetastreamGem::AddDoubleToArray(const char* table, const char* arrayName, double value)
  180. {
  181. if (m_cache.get())
  182. {
  183. m_cache->AddToArray(table, arrayName, value);
  184. }
  185. }
  186. void MetastreamGem::AddUnsigned64ToArray(const char* table, const char* arrayName, AZ::u64 value)
  187. {
  188. if (m_cache.get())
  189. {
  190. m_cache->AddToArray(table, arrayName, value);
  191. }
  192. }
  193. void MetastreamGem::AddSigned64ToArray(const char* table, const char* arrayName, AZ::s64 value)
  194. {
  195. if (m_cache.get())
  196. {
  197. m_cache->AddToArray(table, arrayName, value);
  198. }
  199. }
  200. void MetastreamGem::AddArrayToObject(const char* table, const char* destObjectName, const char *key, const char *srcArrayName)
  201. {
  202. if (m_cache.get())
  203. {
  204. m_cache->AddArrayToObject(table, destObjectName, key, srcArrayName);
  205. }
  206. }
  207. void MetastreamGem::AddObjectToObject(const char* table, const char* destObjectName, const char *key, const char* sourceObjectName)
  208. {
  209. if (m_cache.get())
  210. {
  211. m_cache->AddObjectToObject(table, destObjectName, key, sourceObjectName);
  212. }
  213. }
  214. void MetastreamGem::AddObjectToArray(const char* table, const char* destArrayName, const char* sourceObjectName)
  215. {
  216. if (m_cache.get())
  217. {
  218. m_cache->AddObjectToArray(table, destArrayName, sourceObjectName);
  219. }
  220. }
  221. void MetastreamGem::AddStringToObject(const char *table, const char* objectName, const char* key, const char* value)
  222. {
  223. if (m_cache.get())
  224. {
  225. m_cache->AddToObject(table, objectName, key, value);
  226. }
  227. }
  228. void MetastreamGem::AddBoolToObject(const char* table, const char* objectName, const char* key, bool value)
  229. {
  230. if (m_cache.get())
  231. {
  232. m_cache->AddToObject(table, objectName, key, value);
  233. }
  234. }
  235. void MetastreamGem::AddVec3ToObject(const char *table, const char* objectName, const char* key, const Vec3 & value)
  236. {
  237. if (m_cache.get())
  238. {
  239. m_cache->AddToObject(table, objectName, key, AZ::Vector3(value.x, value.y, value.z));
  240. }
  241. }
  242. void MetastreamGem::AddDoubleToObject(const char *table, const char* objectName, const char* key, double value)
  243. {
  244. if (m_cache.get())
  245. {
  246. m_cache->AddToObject(table, objectName, key, value);
  247. }
  248. }
  249. void MetastreamGem::AddUnsigned64ToObject(const char *table, const char* objectName, const char* key, AZ::u64 value)
  250. {
  251. if (m_cache.get())
  252. {
  253. m_cache->AddToObject(table, objectName, key, value);
  254. }
  255. }
  256. void MetastreamGem::AddSigned64ToObject(const char *table, const char* objectName, const char* key, AZ::s64 value)
  257. {
  258. if (m_cache.get())
  259. {
  260. m_cache->AddToObject(table, objectName, key, value);
  261. }
  262. }
  263. bool MetastreamGem::StartHTTPServer()
  264. {
  265. if (!m_serverOptionsCVar)
  266. {
  267. return false;
  268. }
  269. #if AZ_TRAIT_METASTREAM_USE_CIVET
  270. // Start server if it is not started
  271. if (!m_server.get())
  272. {
  273. // Initialise and start the HTTP server
  274. m_server = std::unique_ptr<BaseHttpServer>(new CivetHttpServer(m_cache.get()));
  275. AZStd::string serverOptions = m_serverOptionsCVar->GetString();
  276. CryLogAlways("Initializing Metastream: Options=\"%s\"", serverOptions.c_str());
  277. bool result = m_server->Start(serverOptions.c_str());
  278. if (result)
  279. {
  280. m_serverEnabled = 1;
  281. }
  282. return result;
  283. }
  284. else
  285. {
  286. // Server already started
  287. return true;
  288. }
  289. #else
  290. return false;
  291. #endif
  292. }
  293. void Metastream::MetastreamGem::StopHTTPServer()
  294. {
  295. // Stop server if it is started
  296. if (m_server.get())
  297. {
  298. m_server->Stop();
  299. m_server.reset();
  300. ClearCache();
  301. m_serverEnabled = 0;
  302. }
  303. }
  304. void Metastream::MetastreamGem::StartHTTPServerCmd(IConsoleCmdArgs* /*args*/)
  305. {
  306. Metastream::MetastreamRequestBus::Broadcast(&Metastream::MetastreamRequestBus::Events::StartHTTPServer);
  307. }
  308. void Metastream::MetastreamGem::StopHTTPServerCmd(IConsoleCmdArgs* /*args*/)
  309. {
  310. Metastream::MetastreamRequestBus::Broadcast(&Metastream::MetastreamRequestBus::Events::StopHTTPServer);
  311. }
  312. // Unit test methods to get at m_cache and status
  313. bool Metastream::MetastreamGem::IsServerEnabled() const
  314. {
  315. return (m_serverEnabled == 1);
  316. }
  317. std::string Metastream::MetastreamGem::GetDatabasesJSON() const
  318. {
  319. if (m_cache)
  320. {
  321. return m_cache->GetDatabasesJSON();
  322. }
  323. return "";
  324. }
  325. std::string Metastream::MetastreamGem::GetTableKeysJSON(const std::string& tableName) const
  326. {
  327. if (m_cache)
  328. {
  329. return m_cache->GetTableKeysJSON(tableName);
  330. }
  331. return "";
  332. }
  333. bool Metastream::MetastreamGem::ClearCache()
  334. {
  335. if (m_cache)
  336. {
  337. m_cache->ClearCache();
  338. return true;
  339. }
  340. return false;
  341. }
  342. }
  343. #if defined(O3DE_GEM_NAME)
  344. AZ_DECLARE_MODULE_CLASS(AZ_JOIN(Gem_, O3DE_GEM_NAME), Metastream::MetastreamGem)
  345. #else
  346. AZ_DECLARE_MODULE_CLASS(Gem_Metastream, Metastream::MetastreamGem)
  347. #endif