IPC.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  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. #if defined(ATOMIC_PLATFORM_OSX) || defined(ATOMIC_PLATFORM_LINUX)
  23. #include <unistd.h>
  24. #endif
  25. #include "../Core/CoreEvents.h"
  26. #include "../Engine/Engine.h"
  27. #include "../IO/Log.h"
  28. #include "IPCBroker.h"
  29. #include "IPCWorker.h"
  30. #include "IPC.h"
  31. #include "IPCEvents.h"
  32. namespace Atomic
  33. {
  34. IPC::IPC(Context* context) : Object(context),
  35. workerChannelID_(0)
  36. {
  37. SubscribeToEvent(E_UPDATE, HANDLER(IPC, HandleUpdate));
  38. }
  39. IPC::~IPC()
  40. {
  41. for (unsigned i = 0; i < brokers_.Size(); i++)
  42. brokers_[i]->Stop();
  43. brokers_.Clear();
  44. if (worker_.NotNull())
  45. worker_->Stop();
  46. worker_ = 0;
  47. }
  48. bool IPC::InitWorker(unsigned id, IPCHandle fd1, IPCHandle fd2)
  49. {
  50. workerChannelID_ = id;
  51. #ifndef ATOMIC_PLATFORM_WINDOWS
  52. // close server fd
  53. close(fd1);
  54. worker_ = new IPCWorker(context_, fd2, id);
  55. #else
  56. worker_ = new IPCWorker(context_, fd1, fd2, id);
  57. #endif
  58. worker_->Run();
  59. SendEventToBroker(E_IPCWORKERSTART);
  60. return true;
  61. }
  62. IPCBroker* IPC::SpawnWorker(const String& command, const Vector<String>& args, const String& initialDirectory)
  63. {
  64. SharedPtr<IPCBroker> broker(new IPCBroker(context_));
  65. if (broker->SpawnWorker(command, args, initialDirectory))
  66. {
  67. brokers_.Push(broker);
  68. return broker;
  69. }
  70. return 0;
  71. }
  72. void IPC::SendEventToBroker(StringHash eventType)
  73. {
  74. SendEventToBroker(eventType, GetEventDataMap());
  75. }
  76. void IPC::SendEventToBroker(StringHash eventType, VariantMap& eventData)
  77. {
  78. if (worker_.NotNull())
  79. {
  80. worker_->PostMessage(eventType, eventData);
  81. }
  82. }
  83. void IPC::HandleUpdate(StringHash eventType, VariantMap& eventData)
  84. {
  85. // If we're a worker, if update fails, time to exit
  86. if (worker_.NotNull())
  87. {
  88. if (!worker_->Update())
  89. {
  90. worker_ = 0;
  91. GetSubsystem<Engine>()->Exit();
  92. return;
  93. }
  94. }
  95. // Update brokers
  96. Vector<IPCBroker*> remove;
  97. for (unsigned i = 0; i < brokers_.Size(); i++)
  98. {
  99. SharedPtr<IPCBroker>& broker = brokers_[i];
  100. if (!broker->Update())
  101. {
  102. VariantMap brokerData;
  103. brokerData[IPCWorkerExit::P_BROKER] = broker;
  104. brokerData[IPCWorkerExit::P_EXITCODE] = 0;
  105. broker->SendEvent(E_IPCWORKEREXIT, brokerData);
  106. remove.Push(broker);
  107. }
  108. }
  109. for (unsigned i = 0; i < remove.Size(); i++)
  110. {
  111. brokers_.Remove(SharedPtr<IPCBroker>(remove[i]));
  112. }
  113. eventMutex_.Acquire();
  114. for (List<QueuedEvent>::Iterator itr = queuedEvents_.Begin(); itr != queuedEvents_.End(); itr++)
  115. {
  116. unsigned channelID = (*itr).channelID_;
  117. StringHash qeventType = (*itr).eventType_;
  118. VariantMap& qeventData = (*itr).eventData_;
  119. bool sent = false;
  120. for (unsigned i = 0; i < brokers_.Size(); i++)
  121. {
  122. SharedPtr<IPCBroker>& broker = brokers_[i];
  123. if (broker->GetID() == channelID) {
  124. broker->SendEvent(qeventType, qeventData);
  125. sent = true;
  126. break;
  127. }
  128. }
  129. if (!sent)
  130. SendEvent(qeventType, qeventData);
  131. }
  132. queuedEvents_.Clear();
  133. eventMutex_.Release();
  134. }
  135. void IPC::QueueEvent(unsigned id, StringHash eventType, VariantMap& eventData)
  136. {
  137. eventMutex_.Acquire();
  138. QueuedEvent event;
  139. event.channelID_ = id;
  140. event.eventType_ = eventType;
  141. event.eventData_ = eventData;
  142. queuedEvents_.Push(event);
  143. eventMutex_.Release();
  144. }
  145. }