GlowProcess.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // Copyright (c) 2014-2017 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/Log.h>
  23. #include <Atomic/Core/Context.h>
  24. #include <Atomic/IPC/IPCBroker.h>
  25. #include <Atomic/IPC/IPCEvents.h>
  26. #include "../Common/GlowEvents.h"
  27. #include "GlowService.h"
  28. #include "GlowServiceEvents.h"
  29. #include "GlowProcess.h"
  30. namespace AtomicGlow
  31. {
  32. // Atomic Glow Process
  33. GlowProcess::GlowProcess(Context* context) : IPCServer(context),
  34. exitCalled_(false),
  35. resultHandler_(new GlowProcessResultHandler(context, this))
  36. {
  37. }
  38. GlowProcess::~GlowProcess()
  39. {
  40. }
  41. void GlowProcess::OnIPCWorkerExited()
  42. {
  43. GetSubsystem<GlowService>()->OnGlowProcessExited();
  44. }
  45. void GlowProcess::OnIPCWorkerLog(int level, const String& message)
  46. {
  47. using namespace AtomicGlowServiceLogEvent;
  48. VariantMap eventData;
  49. eventData[P_LEVEL] = level;
  50. eventData[P_MESSAGE] = message;
  51. SendEvent(E_ATOMICGLOWSERVICELOGEVENT, eventData);
  52. }
  53. bool GlowProcess::Start(const String &glowBinaryPath, const StringVector &baseArgs)
  54. {
  55. if (!IPCServer::StartInternal(glowBinaryPath, baseArgs))
  56. {
  57. return false;
  58. }
  59. SubscribeToEvent(GetServerBroker(), E_ATOMICGLOWRESULT, ATOMIC_HANDLER(GlowProcess, HandleAtomicGlowResult));
  60. return true;
  61. }
  62. void GlowProcess::HandleAtomicGlowResult(StringHash eventType, VariantMap& eventData)
  63. {
  64. GlowService* glowService = GetSubsystem<GlowService>();
  65. using namespace AtomicGlowResult;
  66. const String& result = eventData[P_RESULT].GetString();
  67. if (!eventData[P_SUCCESS].GetBool())
  68. {
  69. glowService->OnBakeError(result);
  70. return;
  71. }
  72. Variant variant;
  73. if (!eventData.TryGetValue(P_BAKEDATA, variant) || variant.GetType() != VAR_BUFFER)
  74. {
  75. glowService->OnBakeError("GlowProcess::HandleResult() - Unable to get bake data");
  76. return;
  77. }
  78. VectorBuffer bakeData = variant.GetVectorBuffer();
  79. glowService->ProcessBakeData(bakeData);
  80. glowService->OnBakeSuccess();
  81. }
  82. unsigned GlowProcess::QueueCommand(const VariantMap& cmdMap)
  83. {
  84. return IPCServer::QueueCommand(resultHandler_, cmdMap);
  85. }
  86. void GlowProcess::Exit()
  87. {
  88. if (exitCalled_)
  89. return;
  90. exitCalled_ = true;
  91. using namespace IPCCmd;
  92. VariantMap cmdMap;
  93. cmdMap[P_COMMAND] = "quit";
  94. QueueCommand(cmdMap);
  95. }
  96. void GlowProcess::HandleResult(unsigned cmdID, const VariantMap& cmdResult)
  97. {
  98. using namespace IPCCmdResult;
  99. GlowService* glowService = GetSubsystem<GlowService>();
  100. Variant variant;
  101. String cmd;
  102. String result;
  103. if (!cmdResult.TryGetValue(P_COMMAND, variant) || variant.GetType() != VAR_STRING)
  104. {
  105. ATOMIC_LOGERROR("GlowProcess::HandleResult() - Unable to process result, command key missing");
  106. return;
  107. }
  108. cmd = variant.GetString();
  109. if (!cmdResult.TryGetValue("result", variant) || variant.GetType() != VAR_STRING)
  110. {
  111. ATOMIC_LOGERROR("GlowProcess::HandleResult() - Unable to process result, command key missing");
  112. return;
  113. }
  114. result = variant.GetString();
  115. if (cmd == "bake")
  116. {
  117. if (result != "success")
  118. {
  119. glowService->OnBakeError(result);
  120. return;
  121. }
  122. }
  123. }
  124. // Result Handler
  125. GlowProcessResultHandler::GlowProcessResultHandler(Context* context, GlowProcess* process) :
  126. IPCResultHandler(context),
  127. process_(process)
  128. {
  129. }
  130. GlowProcessResultHandler::~GlowProcessResultHandler()
  131. {
  132. }
  133. void GlowProcessResultHandler::HandleResult(unsigned cmdID, const VariantMap& cmdResult)
  134. {
  135. if (!process_)
  136. {
  137. ATOMIC_LOGWARNING("lowProcessResultHandler::HandleResult() - called without current Glow process");
  138. return;
  139. }
  140. process_->HandleResult(cmdID, cmdResult);
  141. }
  142. }