main.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // CPP-Signal-Bus.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. // https://github.com/catsums/CPP-Signal-Bus/tree/main
  3. #if 1
  4. #include <cstdlib>
  5. #include <ctime>
  6. #include <random>
  7. #include <cmath>
  8. #include <math.h>
  9. #include <functional>
  10. #include "ActionManager/ActionManager.h"
  11. #include "myHelper.cpp"
  12. using namespace std;
  13. int getRandomInt(int a, int b) {
  14. return myHelper::uniformDistribution<int>(a, b);
  15. }
  16. string getRandomString(int size) {
  17. return myHelper::randomString(size);
  18. }
  19. class myContext {
  20. public:
  21. string name;
  22. int reqCount = 5;
  23. vector<ActionRequest*> requests;
  24. myContext(string n) {
  25. name = n;
  26. }
  27. ~myContext() {
  28. }
  29. void checkRequests() {
  30. for (int i = 0; i < (int)requests.size();i++) {
  31. ActionRequest* req = requests[i];
  32. if (req->getWaitCount() <= 0) {
  33. requests.erase(requests.begin() + i);
  34. i--;
  35. }
  36. }
  37. }
  38. ActionRequest* createRequest(map<string, string>& data, ActionManager* actionManager) {
  39. ActionRequest* req = new ActionRequest(getRandomInt(2, 8), data, [this](SignalEvent* e) {
  40. onHandle(e);
  41. });
  42. actionManager->pushRequest(req);
  43. reqCount--;
  44. requests.push_back(req);
  45. return req;
  46. }
  47. void onProcess(ActionResult* res) {
  48. res->setData("fish", name);
  49. }
  50. void onHandle(SignalEvent* e) {
  51. try {
  52. ActionResult* res = dynamic_cast<ActionResult*>(e);
  53. if (res->isFinished()) {
  54. if (res->isSuccess()) {
  55. cout << "SUCCESS OK" << endl;
  56. cout << "> Fishes ID:" << res->getData("fish") << endl;
  57. }
  58. else {
  59. cout << "FAIL" << endl;
  60. }
  61. }
  62. else {
  63. cout << "Still Processing..." << endl;
  64. onProcess(res);
  65. }
  66. }
  67. catch (const bad_cast& err) {
  68. cout << "Error trying to cast SignalEvent to ActionEvent" << endl;
  69. }
  70. }
  71. };
  72. class StepManager {
  73. public:
  74. ActionManager actionManager;
  75. vector<myContext*> arr;
  76. int step = 0;
  77. int maxStep = 10;
  78. StepManager() {
  79. }
  80. ~StepManager() {
  81. }
  82. void mainLoop() {
  83. while (step < maxStep) {
  84. cout << "----Step " << step << "----" << endl;
  85. cout << "------------------------" << endl;
  86. //run all requests
  87. cout << ">>> Running requests" << endl;
  88. while (!actionManager.isEmptyQueue()) {
  89. actionManager.handleCurrRequest();
  90. }
  91. actionManager.placeNextQueue();
  92. //try to get requests
  93. cout << ">>> Pushing requests" << endl;
  94. checkEnvironment();
  95. step++;
  96. }
  97. }
  98. void checkEnvironment() {
  99. for (int i = 0; i < (int)arr.size();i++) {
  100. myContext* ctx = arr[i];
  101. int coin = getRandomInt(0, 1);
  102. if (coin == 1) {
  103. cout << ctx->name << " opened a request" << endl;
  104. map<string, string> reqData;
  105. reqData["fish"] = getRandomString(8);
  106. ctx->createRequest(reqData, &actionManager);
  107. }
  108. }
  109. }
  110. };
  111. void managerTest()
  112. {
  113. StepManager manager;
  114. manager.arr.push_back(new myContext("carp"));
  115. manager.arr.push_back(new myContext("hake"));
  116. manager.arr.push_back(new myContext("jellyfish"));
  117. manager.mainLoop();
  118. }
  119. int main() {
  120. managerTest();
  121. return 0;
  122. }
  123. #else
  124. #include <iostream>
  125. #include <cstdlib>
  126. #include <ctime>
  127. #include <random>
  128. #include <cmath>
  129. #include <math.h>
  130. #include <functional>
  131. #include "SignalBus.h"
  132. #include "ObjectSignalHandler.h"
  133. #include "../myHelper.cpp"
  134. using namespace std;
  135. class SillyEvent : public SignalEvent {
  136. public:
  137. SillyEvent(string n, string msg) :SignalEvent(n) {
  138. message = msg;
  139. }
  140. SillyEvent(SillyEvent& other) {
  141. name = other.name;
  142. other.message = other.message;
  143. }
  144. ~SillyEvent() {}
  145. SignalEvent* clone() {
  146. return new SillyEvent(*this);
  147. }
  148. string getMessage() const {
  149. return message;
  150. }
  151. protected:
  152. string message;
  153. };
  154. class SillyHandler : public SignalHandler {
  155. friend class myContext;
  156. public:
  157. SillyHandler(function<void(SillyEvent*)> f) {
  158. func = f;
  159. }
  160. ~SillyHandler() {
  161. }
  162. protected:
  163. function<void(SillyEvent*)> func;
  164. virtual void handle(SignalEvent* event) {
  165. SillyEvent* e = static_cast<SillyEvent*>(event);
  166. cout << "Message is being set to " << e->getMessage() << endl;
  167. func(e);
  168. }
  169. };
  170. class SillySignalBus : public SignalBus {
  171. public:
  172. SillySignalBus() {}
  173. ~SillySignalBus() {}
  174. virtual void emit(string n, string p) {
  175. if (hasSignal(n)) {
  176. vector<SignalHandler*> listeners = (*signals[n]);
  177. SignalEvent* event = createSignalEvent(n, p);
  178. sendEvent(n, event);
  179. }
  180. }
  181. int getListenerCount(string n) {
  182. if (hasSignal(n)) {
  183. vector<SignalHandler*>* listeners = signals[n];
  184. return listeners->size();
  185. }
  186. return 0;
  187. }
  188. protected:
  189. virtual SignalEvent* createSignalEvent(string n) {
  190. return new SillyEvent(n, myHelper::randomString(10));
  191. }
  192. virtual SignalEvent* createSignalEvent(string n, string p) {
  193. return new SillyEvent(n, p);
  194. }
  195. };
  196. class myContext {
  197. public:
  198. myContext() {
  199. function<void(SignalEvent*)> f = [this](SignalEvent* e) {
  200. handle(e);
  201. };
  202. handler = new FunctionHandler(f);
  203. }
  204. ~myContext() {}
  205. SignalHandler* handler;
  206. protected:
  207. void handle(SignalEvent* _e) {
  208. SillyEvent* e = static_cast<SillyEvent*>(_e);
  209. cout << "Message: " << e->getMessage() << endl;
  210. }
  211. };
  212. void signalTest() {
  213. SillySignalBus* bus = new SillySignalBus();
  214. myContext context;
  215. bus->subscribe("set", context.handler);
  216. for (int i = 0; i < 4; i++) {
  217. if (i % 2 == 0) {
  218. bus->emit("set", "Mishka is old");#include <iostream>
  219. #include <cstdlib>
  220. #include <ctime>
  221. #include <random>
  222. #include <cmath>
  223. #include <math.h>
  224. #include <functional>
  225. #include "SignalBus.h"
  226. #include "ObjectSignalHandler.h"
  227. #include "../myHelper.cpp"
  228. using namespace std;
  229. class SillyEvent : public SignalEvent {
  230. public:
  231. SillyEvent(string n, string msg) :SignalEvent(n) {
  232. message = msg;
  233. }
  234. SillyEvent(SillyEvent& other) {
  235. name = other.name;
  236. other.message = other.message;
  237. }
  238. ~SillyEvent() {}
  239. SignalEvent* clone() {
  240. return new SillyEvent(*this);
  241. }
  242. string getMessage() const {
  243. return message;
  244. }
  245. protected:
  246. string message;
  247. };
  248. class SillyHandler : public SignalHandler {
  249. friend class myContext;
  250. public:
  251. SillyHandler(function<void(SillyEvent*)> f) {
  252. func = f;
  253. }
  254. ~SillyHandler() {
  255. }
  256. protected:
  257. function<void(SillyEvent*)> func;
  258. virtual void handle(SignalEvent* event) {
  259. SillyEvent* e = static_cast<SillyEvent*>(event);
  260. cout << "Message is being set to " << e->getMessage() << endl;
  261. func(e);
  262. }
  263. };
  264. class SillySignalBus : public SignalBus {
  265. public:
  266. SillySignalBus() {}
  267. ~SillySignalBus() {}
  268. virtual void emit(string n, string p) {
  269. if (hasSignal(n)) {
  270. vector<SignalHandler*> listeners = (*signals[n]);
  271. SignalEvent* event = createSignalEvent(n, p);
  272. sendEvent(n, event);
  273. }
  274. }
  275. int getListenerCount(string n) {
  276. if (hasSignal(n)) {
  277. vector<SignalHandler*>* listeners = signals[n];
  278. return listeners->size();
  279. }
  280. return 0;
  281. }
  282. protected:
  283. virtual SignalEvent* createSignalEvent(string n) {
  284. return new SillyEvent(n, myHelper::randomString(10));
  285. }
  286. virtual SignalEvent* createSignalEvent(string n, string p) {
  287. return new SillyEvent(n, p);
  288. }
  289. };
  290. class myContext {
  291. public:
  292. myContext() {
  293. function<void(SignalEvent*)> f = [this](SignalEvent* e) {
  294. handle(e);
  295. };
  296. handler = new FunctionHandler(f);
  297. }
  298. ~myContext() {}
  299. SignalHandler* handler;
  300. protected:
  301. void handle(SignalEvent* _e) {
  302. SillyEvent* e = static_cast<SillyEvent*>(_e);
  303. cout << "Message: " << e->getMessage() << endl;
  304. }
  305. };
  306. void signalTest() {
  307. SillySignalBus* bus = new SillySignalBus();
  308. myContext context;
  309. bus->subscribe("set", context.handler);
  310. for (int i = 0; i < 4; i++) {
  311. if (i % 2 == 0) {
  312. bus->emit("set", "Mishka is old");
  313. }
  314. else {
  315. cout << "Nothing at " << i << endl;
  316. }
  317. }
  318. bus->unsubscribe("set", context.handler);
  319. }
  320. // int main(){
  321. // signalTest();
  322. // return 0;
  323. // }
  324. }
  325. else {
  326. cout << "Nothing at " << i << endl;
  327. }
  328. }
  329. bus->unsubscribe("set", context.handler);
  330. }
  331. // int main(){
  332. // signalTest();
  333. // return 0;
  334. // }
  335. #endif