IPC.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifdef __APPLE__
  2. #include <unistd.h>
  3. #endif
  4. #include "../Core/CoreEvents.h"
  5. #include "../IO/Log.h"
  6. #include "IPCBroker.h"
  7. #include "IPCWorker.h"
  8. #include "IPC.h"
  9. #include "IPCEvents.h"
  10. namespace Atomic
  11. {
  12. IPC::IPC(Context* context) : Object(context)
  13. {
  14. SubscribeToEvent(E_UPDATE, HANDLER(IPC, HandleUpdate));
  15. }
  16. IPC::~IPC()
  17. {
  18. }
  19. bool IPC::InitWorker(int fd1, int fd2)
  20. {
  21. // close server fd
  22. close(fd1);
  23. worker_ = new IPCWorker(fd2, context_);
  24. worker_->Run();
  25. SendEventToBroker(E_IPCWORKERSTART);
  26. return true;
  27. }
  28. IPCBroker* IPC::SpawnWorker(const String& command, const Vector<String>& args, const String& initialDirectory)
  29. {
  30. SharedPtr<IPCBroker> broker(new IPCBroker(context_));
  31. if (broker->SpawnWorker(command, args, initialDirectory))
  32. {
  33. brokers_.Push(broker);
  34. return broker;
  35. }
  36. return 0;
  37. }
  38. void IPC::SendEventToBroker(StringHash eventType)
  39. {
  40. SendEventToBroker(eventType, GetEventDataMap());
  41. }
  42. void IPC::SendEventToBroker(StringHash eventType, VariantMap& eventData)
  43. {
  44. if (worker_.NotNull())
  45. {
  46. worker_->PostMessage(eventType, eventData);
  47. }
  48. }
  49. void IPC::HandleUpdate(StringHash eventType, VariantMap& eventData)
  50. {
  51. eventMutex_.Acquire();
  52. for (List<QueuedEvent>::Iterator itr = queuedEvents_.Begin(); itr != queuedEvents_.End(); itr++)
  53. {
  54. StringHash qeventType = (*itr).eventType_;
  55. VariantMap& qeventData = (*itr).eventData_;
  56. SendEvent(qeventType, qeventData);
  57. }
  58. queuedEvents_.Clear();
  59. eventMutex_.Release();
  60. }
  61. void IPC::QueueEvent(StringHash eventType, VariantMap& eventData)
  62. {
  63. eventMutex_.Acquire();
  64. QueuedEvent event;
  65. event.eventType_ = eventType;
  66. event.eventData_ = eventData;
  67. queuedEvents_.Push(event);
  68. eventMutex_.Release();
  69. }
  70. }