IPCServer.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // Copyright (c) 2014-2016 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/IO/FileSystem.h>
  24. #include <Atomic/IPC/IPC.h>
  25. #include <Atomic/IPC/IPCEvents.h>
  26. #include <Atomic/IPC/IPCBroker.h>
  27. #include <Atomic/Core/CoreEvents.h>
  28. #include "IPCServer.h"
  29. namespace Atomic
  30. {
  31. unsigned IPCServer::cmdID_ = 1;
  32. IPCResultHandler::IPCResultHandler(Context* context) :
  33. Object(context)
  34. {
  35. }
  36. IPCResultHandler::~IPCResultHandler()
  37. {
  38. }
  39. IPCServer::IPCServer(Context* context) :
  40. Object(context),
  41. brokerEnabled_(false)
  42. {
  43. }
  44. IPCServer::~IPCServer()
  45. {
  46. }
  47. void IPCServer::HandleIPCWorkerStarted(StringHash eventType, VariantMap& eventData)
  48. {
  49. VariantMap startupData;
  50. serverBroker_->PostMessage(E_IPCINITIALIZE, startupData);
  51. brokerEnabled_ = true;
  52. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(IPCServer, HandleUpdate));
  53. SubscribeToEvent(serverBroker_, E_IPCCMDRESULT, ATOMIC_HANDLER(IPCServer, HandleIPCCmdResult));
  54. }
  55. void IPCServer::HandleIPCWorkerExit(StringHash eventType, VariantMap& eventData)
  56. {
  57. if (eventData[IPCWorkerExit::P_BROKER] == serverBroker_)
  58. {
  59. serverBroker_ = 0;
  60. brokerEnabled_ = false;
  61. }
  62. }
  63. void IPCServer::HandleIPCWorkerLog(StringHash eventType, VariantMap& eventData)
  64. {
  65. using namespace IPCWorkerLog;
  66. // convert to a server log
  67. VariantMap serverLogData;
  68. serverLogData["message"] = eventData[P_MESSAGE].GetString();
  69. serverLogData["level"] = eventData[P_LEVEL].GetInt();
  70. SendEvent("IPCServerLog", serverLogData);
  71. }
  72. bool IPCServer::StartInternal(const String& exec, const Vector<String>& args)
  73. {
  74. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  75. clientExecutable_ = exec;
  76. if (!clientExecutable_.Length() || !fileSystem->FileExists(clientExecutable_))
  77. {
  78. ATOMIC_LOGERRORF("IPCServer::Start - Client Executable does not exist: %s", clientExecutable_.CString());
  79. return false;
  80. }
  81. String dump;
  82. dump.Join(args, " ");
  83. ATOMIC_LOGDEBUGF("Launching Broker %s %s", clientExecutable_.CString(), dump.CString());
  84. IPC* ipc = GetSubsystem<IPC>();
  85. serverBroker_ = ipc->SpawnWorker(clientExecutable_, args);
  86. if (serverBroker_)
  87. {
  88. SubscribeToEvent(serverBroker_, E_IPCWORKERSTART, ATOMIC_HANDLER(IPCServer, HandleIPCWorkerStarted));
  89. SubscribeToEvent(serverBroker_, E_IPCWORKEREXIT, ATOMIC_HANDLER(IPCServer, HandleIPCWorkerExit));
  90. SubscribeToEvent(serverBroker_, E_IPCWORKERLOG, ATOMIC_HANDLER(IPCServer, HandleIPCWorkerLog));
  91. }
  92. else
  93. {
  94. ATOMIC_LOGERRORF("Error Spawning Broker %s %s", clientExecutable_.CString(), dump.CString());
  95. }
  96. return serverBroker_.NotNull();
  97. }
  98. bool IPCServer::GetBrokerEnabled() const
  99. {
  100. return brokerEnabled_;
  101. }
  102. void IPCServer::HandleIPCCmdResult(StringHash eventType, VariantMap& eventData)
  103. {
  104. using namespace IPCCmdResult;
  105. unsigned id = eventData[P_ID].GetUInt();
  106. List<IPCCommand>::Iterator itr = cmdProcess_.Begin();
  107. bool found = false;
  108. while (itr != cmdProcess_.End())
  109. {
  110. if ((*itr).id_ == id)
  111. {
  112. IPCCommand cmd = *itr;
  113. cmdProcess_.Erase(itr);
  114. found = true;
  115. if (cmd.handler_.Expired())
  116. {
  117. ATOMIC_LOGERROR("IPCServer::HandleIPCNETCmdResult - IPCNETResult for expired client");
  118. break;
  119. }
  120. cmd.handler_->HandleResult(cmd.id_, eventData);
  121. break;
  122. }
  123. itr++;
  124. }
  125. if (!found)
  126. {
  127. ATOMIC_LOGERRORF("IPCServer::HandleIPCNETCmdResult - IPCNETResult command %u not found in process queue", id);
  128. }
  129. }
  130. unsigned IPCServer::QueueCommand(IPCResultHandler* handler, const VariantMap& cmdMap)
  131. {
  132. IPCCommand cmd;
  133. cmd.id_ = cmdID_++;
  134. cmd.handler_ = handler;
  135. cmd.cmdMap_ = cmdMap;
  136. cmd.cmdMap_[IPCCmd::P_ID] = cmd.id_;
  137. cmdQueue_.Push(cmd);
  138. return cmd.id_;
  139. }
  140. void IPCServer::HandleUpdate(StringHash eventType, VariantMap& eventData)
  141. {
  142. if (!cmdQueue_.Size())
  143. return;
  144. IPCCommand cmd = cmdQueue_.Front();
  145. cmdQueue_.PopFront();
  146. String cmdString = cmd.cmdMap_["command"].GetString();
  147. if (!serverBroker_)
  148. {
  149. ATOMIC_LOGERRORF("IPCServer::HandleUpdate - null player broker for command: %s", cmdString.CString());
  150. return;
  151. }
  152. cmdProcess_.Push(cmd);
  153. serverBroker_->PostMessage(E_IPCCMD, cmd.cmdMap_);
  154. }
  155. }