123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <sstream>
- #include <string.h>
- #include <AzCore/RTTI/BehaviorContext.h>
- #include <AzCore/Serialization/SerializeContext.h>
- #include <Metastream_Traits_Platform.h>
- #include "MetastreamGem.h"
- #if AZ_TRAIT_METASTREAM_USE_CIVET
- #include "CivetHttpServer.h"
- #endif // AZ_TRAIT_METASTREAM_USE_CIVET
- namespace Metastream
- {
- void Metastream::MetastreamReflectComponent::Reflect(AZ::ReflectContext* context)
- {
- if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
- {
- serializeContext->Class<MetastreamReflectComponent, AZ::Component>()
- ->Version(0)
- ;
- }
- AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
- if (behaviorContext)
- {
- behaviorContext->EBus<MetastreamRequestBus>("MetastreamRequestBus")
- ->Event("StartHTTPServer", &MetastreamRequestBus::Events::StartHTTPServer)
- ->Event("StopHTTPServer", &MetastreamRequestBus::Events::StopHTTPServer)
- ->Event("AddStringToCache", &MetastreamRequestBus::Events::AddStringToCache)
- ->Event("AddBoolToCache", &MetastreamRequestBus::Events::AddBoolToCache)
- ->Event("AddDoubleToCache", &MetastreamRequestBus::Events::AddDoubleToCache)
- ->Event("AddUnsigned64ToCache", &MetastreamRequestBus::Events::AddUnsigned64ToCache)
- ->Event("AddSigned64ToCache", &MetastreamRequestBus::Events::AddSigned64ToCache)
- ->Event("AddEntityIdToCache", &MetastreamRequestBus::Events::AddEntityIdToCache)
- ->Event("AddArrayToCache", &MetastreamRequestBus::Events::AddArrayToCache)
- ->Event("AddObjectToCache", &MetastreamRequestBus::Events::AddObjectToCache)
-
- ->Event("AddArrayToObject", &MetastreamRequestBus::Events::AddArrayToObject)
- ->Event("AddObjectToObject", &MetastreamRequestBus::Events::AddObjectToObject)
- ->Event("AddObjectToArray", &MetastreamRequestBus::Events::AddObjectToArray)
- ->Event("AddStringToArray", &MetastreamRequestBus::Events::AddStringToArray)
- ->Event("AddBoolToArray", &MetastreamRequestBus::Events::AddBoolToArray)
- ->Event("AddDoubleToArray", &MetastreamRequestBus::Events::AddDoubleToArray)
- ->Event("AddUnsigned64ToArray", &MetastreamRequestBus::Events::AddUnsigned64ToArray)
- ->Event("AddSigned64ToArray", &MetastreamRequestBus::Events::AddSigned64ToArray)
- ->Event("AddEntityIdToArray", &MetastreamRequestBus::Events::AddEntityIdToArray)
- ->Event("AddStringToObject", &MetastreamRequestBus::Events::AddStringToObject)
- ->Event("AddBoolToObject", &MetastreamRequestBus::Events::AddBoolToObject)
- ->Event("AddDoubleToObject", &MetastreamRequestBus::Events::AddDoubleToObject)
- ->Event("AddUnsigned64ToObject", &MetastreamRequestBus::Events::AddUnsigned64ToObject)
- ->Event("AddSigned64ToObject", &MetastreamRequestBus::Events::AddSigned64ToObject)
- ->Event("AddEntityIdToObject", &MetastreamRequestBus::Events::AddEntityIdToObject)
- ;
- }
- }
- MetastreamGem::MetastreamGem()
- : m_serverEnabled(0)
- , m_serverOptionsCVar(nullptr)
- {
- m_descriptors.push_back(MetastreamReflectComponent::CreateDescriptor());
- // Initialise the cache
- m_cache = std::unique_ptr<DataCache>(new DataCache());
- MetastreamRequestBus::Handler::BusConnect();
- }
- MetastreamGem::~MetastreamGem()
- {
- MetastreamRequestBus::Handler::BusDisconnect();
- }
- void MetastreamGem::OnSystemEvent(ESystemEvent event, [[maybe_unused]] UINT_PTR wparam, [[maybe_unused]] UINT_PTR lparam)
- {
- using namespace Metastream;
- switch (event)
- {
- case ESYSTEM_EVENT_GAME_POST_INIT:
- // 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
- //
- // Note: 1) For security reasons, the option "enable_directory_listing" is forced to "no".
- // 2) The following options are ignored for security reasons: enable_directory_listing, cgi_interpreter, run_as_user, put_delete_auth_file
- // 3) Options are a set of key=value separated by the semi colon character. If a option needs to use the ';' or '='
- // then use $semi or $equ.
- //
- m_serverOptionsCVar = REGISTER_STRING("metastream_serveroptions", "document_root=Gems/Metastream/Files;listening_ports=8082", VF_NULL, "Metastream HTTP Server options");
- REGISTER_CVAR2("metastream_enabled", &m_serverEnabled, 0, VF_READONLY, "State of the Metastream HTTP server (READONLY)");
- REGISTER_COMMAND("metastream_start", StartHTTPServerCmd, 0, "Starts the Metastream HTTP server");
- REGISTER_COMMAND("metastream_stop", StopHTTPServerCmd, 0, "Stops the Metastream HTTP server");
- break;
- case ESYSTEM_EVENT_FULL_SHUTDOWN:
- case ESYSTEM_EVENT_FAST_SHUTDOWN:
- // Put your shutdown code here
- // Other Gems may have been shutdown already, but none will have destructed
- if (m_server.get())
- {
- m_server->Stop();
- m_serverEnabled = 0;
- }
- break;
- }
- }
- void MetastreamGem::AddStringToCache(const char* table, const char* key, const char* value)
- {
- if (m_cache.get())
- {
- m_cache->AddToCache(table, key, value);
- }
- }
- void MetastreamGem::AddBoolToCache(const char* table, const char* key, bool value)
- {
- if (m_cache.get())
- {
- m_cache->AddToCache(table, key, value);
- }
- }
- void MetastreamGem::AddVec3ToCache(const char* table, const char* key, const Vec3 & value)
- {
- if (m_cache.get())
- {
- m_cache->AddToCache(table, key, AZ::Vector3(value.x, value.y, value.z));
- }
- }
- void MetastreamGem::AddDoubleToCache(const char* table, const char* key, double value)
- {
- if (m_cache.get())
- {
- m_cache->AddToCache(table, key, value);
- }
- }
- void MetastreamGem::AddUnsigned64ToCache(const char* table, const char* key, AZ::u64 value)
- {
- if (m_cache.get())
- {
- m_cache->AddToCache(table, key, value);
- }
- }
- void MetastreamGem::AddSigned64ToCache(const char* table, const char* key, AZ::s64 value)
- {
- if (m_cache.get())
- {
- m_cache->AddToCache(table, key, value);
- }
- }
- void MetastreamGem::AddArrayToCache(const char* table, const char* key, const char* arrayName)
- {
- if (m_cache.get())
- {
- m_cache->AddArrayToCache(table, key, arrayName);
- }
- }
- void MetastreamGem::AddObjectToCache(const char* table, const char* key, const char* objectName)
- {
- if (m_cache.get())
- {
- m_cache->AddObjectToCache(table, key, objectName);
- }
- }
- void MetastreamGem::AddStringToArray(const char* table, const char* arrayName, const char* value)
- {
- if (m_cache.get())
- {
- m_cache->AddToArray(table, arrayName, value);
- }
- }
- void MetastreamGem::AddBoolToArray(const char* table, const char* arrayName, bool value)
- {
- if (m_cache.get())
- {
- m_cache->AddToArray(table, arrayName, value);
- }
- }
- void MetastreamGem::AddVec3ToArray(const char* table, const char* arrayName, const Vec3 & value)
- {
- if (m_cache.get())
- {
- m_cache->AddToArray(table, arrayName, AZStd::string::format("[%f,%f,%f]", value.x, value.y, value.z).c_str());
- }
- }
- void MetastreamGem::AddDoubleToArray(const char* table, const char* arrayName, double value)
- {
- if (m_cache.get())
- {
- m_cache->AddToArray(table, arrayName, value);
- }
- }
- void MetastreamGem::AddUnsigned64ToArray(const char* table, const char* arrayName, AZ::u64 value)
- {
- if (m_cache.get())
- {
- m_cache->AddToArray(table, arrayName, value);
- }
- }
- void MetastreamGem::AddSigned64ToArray(const char* table, const char* arrayName, AZ::s64 value)
- {
- if (m_cache.get())
- {
- m_cache->AddToArray(table, arrayName, value);
- }
- }
- void MetastreamGem::AddArrayToObject(const char* table, const char* destObjectName, const char *key, const char *srcArrayName)
- {
- if (m_cache.get())
- {
- m_cache->AddArrayToObject(table, destObjectName, key, srcArrayName);
- }
- }
- void MetastreamGem::AddObjectToObject(const char* table, const char* destObjectName, const char *key, const char* sourceObjectName)
- {
- if (m_cache.get())
- {
- m_cache->AddObjectToObject(table, destObjectName, key, sourceObjectName);
- }
- }
- void MetastreamGem::AddObjectToArray(const char* table, const char* destArrayName, const char* sourceObjectName)
- {
- if (m_cache.get())
- {
- m_cache->AddObjectToArray(table, destArrayName, sourceObjectName);
- }
- }
- void MetastreamGem::AddStringToObject(const char *table, const char* objectName, const char* key, const char* value)
- {
- if (m_cache.get())
- {
- m_cache->AddToObject(table, objectName, key, value);
- }
- }
- void MetastreamGem::AddBoolToObject(const char* table, const char* objectName, const char* key, bool value)
- {
- if (m_cache.get())
- {
- m_cache->AddToObject(table, objectName, key, value);
- }
- }
- void MetastreamGem::AddVec3ToObject(const char *table, const char* objectName, const char* key, const Vec3 & value)
- {
- if (m_cache.get())
- {
- m_cache->AddToObject(table, objectName, key, AZ::Vector3(value.x, value.y, value.z));
- }
- }
- void MetastreamGem::AddDoubleToObject(const char *table, const char* objectName, const char* key, double value)
- {
- if (m_cache.get())
- {
- m_cache->AddToObject(table, objectName, key, value);
- }
- }
- void MetastreamGem::AddUnsigned64ToObject(const char *table, const char* objectName, const char* key, AZ::u64 value)
- {
- if (m_cache.get())
- {
- m_cache->AddToObject(table, objectName, key, value);
- }
- }
- void MetastreamGem::AddSigned64ToObject(const char *table, const char* objectName, const char* key, AZ::s64 value)
- {
- if (m_cache.get())
- {
- m_cache->AddToObject(table, objectName, key, value);
- }
- }
- bool MetastreamGem::StartHTTPServer()
- {
- if (!m_serverOptionsCVar)
- {
- return false;
- }
- #if AZ_TRAIT_METASTREAM_USE_CIVET
- // Start server if it is not started
- if (!m_server.get())
- {
- // Initialise and start the HTTP server
- m_server = std::unique_ptr<BaseHttpServer>(new CivetHttpServer(m_cache.get()));
- AZStd::string serverOptions = m_serverOptionsCVar->GetString();
- CryLogAlways("Initializing Metastream: Options=\"%s\"", serverOptions.c_str());
- bool result = m_server->Start(serverOptions.c_str());
- if (result)
- {
- m_serverEnabled = 1;
- }
- return result;
- }
- else
- {
- // Server already started
- return true;
- }
- #else
- return false;
- #endif
- }
- void Metastream::MetastreamGem::StopHTTPServer()
- {
- // Stop server if it is started
- if (m_server.get())
- {
- m_server->Stop();
- m_server.reset();
- ClearCache();
- m_serverEnabled = 0;
- }
- }
- void Metastream::MetastreamGem::StartHTTPServerCmd(IConsoleCmdArgs* /*args*/)
- {
- Metastream::MetastreamRequestBus::Broadcast(&Metastream::MetastreamRequestBus::Events::StartHTTPServer);
- }
- void Metastream::MetastreamGem::StopHTTPServerCmd(IConsoleCmdArgs* /*args*/)
- {
- Metastream::MetastreamRequestBus::Broadcast(&Metastream::MetastreamRequestBus::Events::StopHTTPServer);
- }
- // Unit test methods to get at m_cache and status
- bool Metastream::MetastreamGem::IsServerEnabled() const
- {
- return (m_serverEnabled == 1);
- }
- std::string Metastream::MetastreamGem::GetDatabasesJSON() const
- {
- if (m_cache)
- {
- return m_cache->GetDatabasesJSON();
- }
- return "";
- }
- std::string Metastream::MetastreamGem::GetTableKeysJSON(const std::string& tableName) const
- {
- if (m_cache)
- {
- return m_cache->GetTableKeysJSON(tableName);
- }
- return "";
- }
- bool Metastream::MetastreamGem::ClearCache()
- {
- if (m_cache)
- {
- m_cache->ClearCache();
- return true;
- }
- return false;
- }
- }
- #if defined(O3DE_GEM_NAME)
- AZ_DECLARE_MODULE_CLASS(AZ_JOIN(Gem_, O3DE_GEM_NAME), Metastream::MetastreamGem)
- #else
- AZ_DECLARE_MODULE_CLASS(Gem_Metastream, Metastream::MetastreamGem)
- #endif
|