Wyatt  1.0.1
CommandManager.cpp
1 //
2 // Created by Arthur Lockman on 4/5/17.
3 //
4 
5 #include "CommandManager.h"
6 
8 {
9  std::cout << "Initialized command manager. Starting..." << std::endl;
10  this->start();
11 }
12 
14 {
15 
16 }
17 
19 {
20  mtx.lock();
21  m_commands.push_back(command);
22  m_command_flags.insert({command, 0});
23  mtx.unlock();
24  return true;
25 }
26 
28 {
29  mtx.lock();
30  m_command_flags[command] = -1;
31  mtx.unlock();
32  return true;
33 }
34 
36 {
37  mtx.lock();
38  for (auto &command : m_commands) {
39  m_command_flags[command] = -1;
40  }
41  mtx.unlock();
42  return true;
43 }
44 
46 {
47  while (m_signal == 0) {
48  mtx.lock();
49  for (auto &command : m_commands) {
50  int flag = m_command_flags[command];
51  if (!command->isFinished()) {
52  if (!command->isInitialized()) {
53  command->init();
54  command->setIsRunning(true);
55  } else if (flag == -1) // Cancel
56  {
57  command->cancel();
58  m_command_flags[command] = 0;
59  } else if (command->isRunning()) {
60  command->setIsRunning(command->execute());
61  } else {
62  command->stop();
63  }
64  }
65  }
66  m_commands.remove_if([](Command* c){ return c->isFinished(); });
67  mtx.unlock();
68  }
69  std::cout << "Received stop signal." << std::endl;
70  return NULL;
71 }
72 
74 {
75  std::cout << "Stopping command manager." << std::endl;
76  m_signal = 1;
77  return Thread::join();
78 }
79 
81 {
82  return this->join();
83 }
84 
85 unsigned long CommandManager::inFlight()
86 {
87  mtx.lock();
88  unsigned long sz = m_commands.size();
89  mtx.unlock();
90  return sz;
91 }
bool cancel(Command *command)
Definition: Command.h:6
bool runCommand(Command *command)
bool isFinished()
Definition: Command.cpp:64
unsigned long inFlight()