DDS  ver. 3.6
CommanderChannel.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 //
4 //
5 
6 #ifndef __DDS__AGENT__CCommanderChannel__
7 #define __DDS__AGENT__CCommanderChannel__
8 
9 // DDS
10 #include "ClientChannelImpl.h"
11 #include "SMIntercomChannel.h"
12 #include "TopoCore.h"
13 
14 namespace dds
15 {
16  namespace agent_cmd
17  {
18  using slotId_t = uint64_t;
19  using taskId_t = uint64_t;
20 
21  struct SSlotInfo
22  {
23  using container_t = std::map<slotId_t, SSlotInfo>;
24 
25  slotId_t m_id{ 0 };
26  std::string m_sUsrExe;
27  std::string m_sUsrEnv;
29  uint32_t m_taskIndex{ 0 };
30  uint32_t m_collectionIndex{ 0 };
31  std::string m_taskPath;
32  std::string m_groupName;
33  std::string m_collectionName;
34  std::string m_taskName;
35  pid_t m_pid{ 0 };
36  };
37 
38  class CCommanderChannel : public protocol_api::CClientChannelImpl<CCommanderChannel>
39  {
40  using pidContainer_t = std::vector<pid_t>;
41  using stringContainer_t = std::vector<std::string>;
42  using timer_t = boost::asio::steady_timer;
43  using timerPtr_t = std::unique_ptr<timer_t>;
44  using terminateChildrenOnComplete_t = std::function<void()>;
45 
46  public:
47  CCommanderChannel(boost::asio::io_context& _service,
48  uint64_t _ProtocolHeaderID,
49  boost::asio::io_context& _intercomService);
50 
51  public:
53 
55  MESSAGE_HANDLER(cmdREPLY, on_cmdREPLY)
56  MESSAGE_HANDLER(cmdSIMPLE_MSG, on_cmdSIMPLE_MSG)
57  MESSAGE_HANDLER(cmdGET_HOST_INFO, on_cmdGET_HOST_INFO)
58  MESSAGE_HANDLER(cmdSHUTDOWN, on_cmdSHUTDOWN)
59  MESSAGE_HANDLER(cmdBINARY_ATTACHMENT_RECEIVED, on_cmdBINARY_ATTACHMENT_RECEIVED)
60  MESSAGE_HANDLER(cmdGET_ID, on_cmdGET_ID)
61  MESSAGE_HANDLER(cmdSET_ID, on_cmdSET_ID)
62  MESSAGE_HANDLER(cmdGET_LOG, on_cmdGET_LOG)
63  MESSAGE_HANDLER(cmdASSIGN_USER_TASK, on_cmdASSIGN_USER_TASK)
64  MESSAGE_HANDLER(cmdACTIVATE_USER_TASK, on_cmdACTIVATE_USER_TASK)
65  MESSAGE_HANDLER(cmdSTOP_USER_TASK, on_cmdSTOP_USER_TASK)
66  MESSAGE_HANDLER(cmdUPDATE_KEY, on_cmdUPDATE_KEY)
67  MESSAGE_HANDLER(cmdCUSTOM_CMD, on_cmdCUSTOM_CMD)
68  MESSAGE_HANDLER(cmdADD_SLOT, on_cmdADD_SLOT)
69  MESSAGE_HANDLER(cmdUSER_TASK_DONE, on_cmdUSER_TASK_DONE)
70  END_MSG_MAP()
71 
72  public:
73  void stopChannel();
74  void setNumberOfSlots(size_t _nSlots);
75 
76  private:
77  // Message Handlers
79  protocol_api::SSenderInfo& _sender);
81  protocol_api::SSenderInfo& _sender);
82  bool on_cmdGET_HOST_INFO(
84  protocol_api::SSenderInfo& _sender);
86  protocol_api::SSenderInfo& _sender);
87  bool on_cmdBINARY_ATTACHMENT_RECEIVED(
89  protocol_api::SSenderInfo& _sender);
91  protocol_api::SSenderInfo& _sender);
93  protocol_api::SSenderInfo& _sender);
95  protocol_api::SSenderInfo& _sender);
96  bool on_cmdASSIGN_USER_TASK(
98  protocol_api::SSenderInfo& _sender);
99  bool on_cmdACTIVATE_USER_TASK(
101  protocol_api::SSenderInfo& _sender);
102  bool on_cmdSTOP_USER_TASK(
104  protocol_api::SSenderInfo& _sender);
106  protocol_api::SSenderInfo& _sender);
108  protocol_api::SSenderInfo& _sender);
110  protocol_api::SSenderInfo& _sender);
111  void send_cmdUPDATE_KEY(
112  const protocol_api::SSenderInfo& _sender,
114  bool on_cmdUSER_TASK_DONE(
116  protocol_api::SSenderInfo& _sender);
117 
118  private:
119  void readAgentIDFile();
120  void createAgentIDFile() const;
121  void deleteAgentIDFile() const;
122  void onNewUserTask(uint64_t _slotID, pid_t _pid);
129  void terminateChildrenProcesses(pid_t _parentPid, const terminateChildrenOnComplete_t& _onCompleteSlot);
130  void terminateChildrenProcesses(timerPtr_t& _timer,
131  const pidContainer_t& _children,
132  const std::chrono::steady_clock::time_point& _wait_until,
133  const terminateChildrenOnComplete_t& _onCompleteSlot);
134  void enumChildProcesses(pid_t _forPid, stringContainer_t& _chilren);
135  void taskExited(uint64_t _taskID, int _exitCode);
136  SSlotInfo& getSlotInfoById(const slotId_t& _slotID);
137  bool isLowDiskSpace(uintmax_t* _available = nullptr);
138  void startResourceMonitor(boost::asio::io_context& _service, const std::chrono::seconds& _interval);
139 
140  private:
141  uint64_t m_id{ 0 };
142  uint16_t m_connectionAttempts{ 1 };
143  std::mutex m_taskIDToSlotIDMapMutex;
144  std::map<uint64_t, uint64_t> m_taskIDToSlotIDMap;
145  topology_api::CTopoCore::Ptr_t m_topo{ std::make_shared<topology_api::CTopoCore>() };
146  std::mutex m_topoMutex;
147 
148  CSMIntercomChannel::connectionPtr_t m_intercomChannel;
149 
150  std::mutex m_mutexSlots;
151  SSlotInfo::container_t m_slots;
152  size_t m_nSlots{ 0 };
153  timerPtr_t m_resourceMonitorTimer;
154  };
155  } // namespace agent_cmd
156 } // namespace dds
157 
158 #endif /* defined(__DDS__AGENT__CCommanderChannel__) */
Definition: ProtocolCommands.h:49
Definition: BaseEventHandlersImpl.h:48
Definition: ProtocolCommands.h:45
Definition: ProtocolCommands.h:68
Definition: ProtocolCommands.h:65
#define REGISTER_DEFAULT_REMOTE_ID_STRING
Definition: BaseChannelImpl.h:185
std::string m_collectionName
Definition: CommanderChannel.h:33
std::string m_taskPath
Definition: CommanderChannel.h:31
uint32_t m_collectionIndex
Definition: CommanderChannel.h:30
slotId_t m_id
Definition: CommanderChannel.h:25
Definition: CommanderChannel.h:38
std::shared_ptr< SEmptyCmd > ptr_t
Definition: CommandAttachmentImpl.h:64
uint32_t m_taskIndex
Definition: CommanderChannel.h:29
Definition: ProtocolCommands.h:30
std::map< slotId_t, SSlotInfo > container_t
Definition: CommanderChannel.h:23
Definition: ProtocolCommands.h:43
Definition: ProtocolCommands.h:33
REGISTER_DEFAULT_REMOTE_ID_STRING BEGIN_MSG_MAP(CCommanderChannel) MESSAGE_HANDLER(cmdREPLY
std::shared_ptr< CSMIntercomChannel > connectionPtr_t
Definition: BaseSMChannelImpl.h:188
CCommanderChannel(boost::asio::io_context &_service, uint64_t _ProtocolHeaderID, boost::asio::io_context &_intercomService)
Definition: CommanderChannel.cpp:33
#define END_MSG_MAP()
Definition: BaseChannelImpl.h:148
Definition: ProtocolCommands.h:36
Miscellaneous functions and helpers are located here.
Definition: AgentConnectionManager.h:13
Definition: ProtocolCommands.h:62
std::string m_groupName
Definition: CommanderChannel.h:32
taskId_t m_taskID
Definition: CommanderChannel.h:28
Definition: ProtocolCommands.h:55
REGISTER_DEFAULT_REMOTE_ID_STRING on_cmdREPLY on_cmdGET_HOST_INFO on_cmdBINARY_ATTACHMENT_RECEIVED on_cmdSET_ID on_cmdASSIGN_USER_TASK on_cmdSTOP_USER_TASK on_cmdCUSTOM_CMD on_cmdUSER_TASK_DONE END_MSG_MAP() public void setNumberOfSlots(size_t _nSlots)
Definition: CommanderChannel.cpp:127
Definition: ProtocolCommands.h:52
std::string m_sUsrExe
Definition: CommanderChannel.h:26
Definition: ProtocolCommands.h:51
Definition: BaseChannelImpl.h:35
Definition: ProtocolCommands.h:46
Definition: CommanderChannel.h:21
Definition: ProtocolCommands.h:41
std::shared_ptr< CTopoCore > Ptr_t
std::shared_ptr
Definition: TopoCore.h:36
Definition: ProtocolCommands.h:53
uint64_t slotId_t
Definition: CommanderChannel.h:18
pid_t m_pid
Definition: CommanderChannel.h:35
uint64_t taskId_t
Definition: CommanderChannel.h:19
std::string m_taskName
Definition: CommanderChannel.h:34
REGISTER_DEFAULT_REMOTE_ID_STRING on_cmdREPLY MESSAGE_HANDLER(cmdSIMPLE_MSG, on_cmdSIMPLE_MSG) MESSAGE_HANDLER(cmdGET_HOST_INFO
std::string m_sUsrEnv
Definition: CommanderChannel.h:27