| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- // CPP-Signal-Bus.cpp : This file contains the 'main' function. Program execution begins and ends there.
- // https://github.com/catsums/CPP-Signal-Bus/tree/main
- #if 1
- #include <cstdlib>
- #include <ctime>
- #include <random>
- #include <cmath>
- #include <math.h>
- #include <functional>
- #include "ActionManager/ActionManager.h"
- #include "myHelper.cpp"
- using namespace std;
- int getRandomInt(int a, int b) {
- return myHelper::uniformDistribution<int>(a, b);
- }
- string getRandomString(int size) {
- return myHelper::randomString(size);
- }
- class myContext {
- public:
- string name;
- int reqCount = 5;
- vector<ActionRequest*> requests;
- myContext(string n) {
- name = n;
- }
- ~myContext() {
- }
- void checkRequests() {
- for (int i = 0; i < (int)requests.size();i++) {
- ActionRequest* req = requests[i];
- if (req->getWaitCount() <= 0) {
- requests.erase(requests.begin() + i);
- i--;
- }
- }
- }
- ActionRequest* createRequest(map<string, string>& data, ActionManager* actionManager) {
- ActionRequest* req = new ActionRequest(getRandomInt(2, 8), data, [this](SignalEvent* e) {
- onHandle(e);
- });
- actionManager->pushRequest(req);
- reqCount--;
- requests.push_back(req);
- return req;
- }
- void onProcess(ActionResult* res) {
- res->setData("fish", name);
- }
- void onHandle(SignalEvent* e) {
- try {
- ActionResult* res = dynamic_cast<ActionResult*>(e);
- if (res->isFinished()) {
- if (res->isSuccess()) {
- cout << "SUCCESS OK" << endl;
- cout << "> Fishes ID:" << res->getData("fish") << endl;
- }
- else {
- cout << "FAIL" << endl;
- }
- }
- else {
- cout << "Still Processing..." << endl;
- onProcess(res);
- }
- }
- catch (const bad_cast& err) {
- cout << "Error trying to cast SignalEvent to ActionEvent" << endl;
- }
- }
- };
- class StepManager {
- public:
- ActionManager actionManager;
- vector<myContext*> arr;
- int step = 0;
- int maxStep = 10;
- StepManager() {
- }
- ~StepManager() {
- }
- void mainLoop() {
- while (step < maxStep) {
- cout << "----Step " << step << "----" << endl;
- cout << "------------------------" << endl;
- //run all requests
- cout << ">>> Running requests" << endl;
- while (!actionManager.isEmptyQueue()) {
- actionManager.handleCurrRequest();
- }
- actionManager.placeNextQueue();
- //try to get requests
- cout << ">>> Pushing requests" << endl;
- checkEnvironment();
- step++;
- }
- }
- void checkEnvironment() {
- for (int i = 0; i < (int)arr.size();i++) {
- myContext* ctx = arr[i];
- int coin = getRandomInt(0, 1);
- if (coin == 1) {
- cout << ctx->name << " opened a request" << endl;
- map<string, string> reqData;
- reqData["fish"] = getRandomString(8);
- ctx->createRequest(reqData, &actionManager);
- }
- }
- }
- };
- void managerTest()
- {
- StepManager manager;
- manager.arr.push_back(new myContext("carp"));
- manager.arr.push_back(new myContext("hake"));
- manager.arr.push_back(new myContext("jellyfish"));
- manager.mainLoop();
- }
- int main() {
- managerTest();
- return 0;
- }
- #else
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <random>
- #include <cmath>
- #include <math.h>
- #include <functional>
- #include "SignalBus.h"
- #include "ObjectSignalHandler.h"
- #include "../myHelper.cpp"
- using namespace std;
- class SillyEvent : public SignalEvent {
- public:
- SillyEvent(string n, string msg) :SignalEvent(n) {
- message = msg;
- }
- SillyEvent(SillyEvent& other) {
- name = other.name;
- other.message = other.message;
- }
- ~SillyEvent() {}
- SignalEvent* clone() {
- return new SillyEvent(*this);
- }
- string getMessage() const {
- return message;
- }
- protected:
- string message;
- };
- class SillyHandler : public SignalHandler {
- friend class myContext;
- public:
- SillyHandler(function<void(SillyEvent*)> f) {
- func = f;
- }
- ~SillyHandler() {
- }
- protected:
- function<void(SillyEvent*)> func;
- virtual void handle(SignalEvent* event) {
- SillyEvent* e = static_cast<SillyEvent*>(event);
- cout << "Message is being set to " << e->getMessage() << endl;
- func(e);
- }
- };
- class SillySignalBus : public SignalBus {
- public:
- SillySignalBus() {}
- ~SillySignalBus() {}
- virtual void emit(string n, string p) {
- if (hasSignal(n)) {
- vector<SignalHandler*> listeners = (*signals[n]);
- SignalEvent* event = createSignalEvent(n, p);
- sendEvent(n, event);
- }
- }
- int getListenerCount(string n) {
- if (hasSignal(n)) {
- vector<SignalHandler*>* listeners = signals[n];
- return listeners->size();
- }
- return 0;
- }
- protected:
- virtual SignalEvent* createSignalEvent(string n) {
- return new SillyEvent(n, myHelper::randomString(10));
- }
- virtual SignalEvent* createSignalEvent(string n, string p) {
- return new SillyEvent(n, p);
- }
- };
- class myContext {
- public:
- myContext() {
- function<void(SignalEvent*)> f = [this](SignalEvent* e) {
- handle(e);
- };
- handler = new FunctionHandler(f);
- }
- ~myContext() {}
- SignalHandler* handler;
- protected:
- void handle(SignalEvent* _e) {
- SillyEvent* e = static_cast<SillyEvent*>(_e);
- cout << "Message: " << e->getMessage() << endl;
- }
- };
- void signalTest() {
- SillySignalBus* bus = new SillySignalBus();
- myContext context;
- bus->subscribe("set", context.handler);
- for (int i = 0; i < 4; i++) {
- if (i % 2 == 0) {
- bus->emit("set", "Mishka is old");#include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <random>
- #include <cmath>
- #include <math.h>
- #include <functional>
- #include "SignalBus.h"
- #include "ObjectSignalHandler.h"
- #include "../myHelper.cpp"
- using namespace std;
- class SillyEvent : public SignalEvent {
- public:
- SillyEvent(string n, string msg) :SignalEvent(n) {
- message = msg;
- }
- SillyEvent(SillyEvent& other) {
- name = other.name;
- other.message = other.message;
- }
- ~SillyEvent() {}
- SignalEvent* clone() {
- return new SillyEvent(*this);
- }
- string getMessage() const {
- return message;
- }
- protected:
- string message;
- };
- class SillyHandler : public SignalHandler {
- friend class myContext;
- public:
- SillyHandler(function<void(SillyEvent*)> f) {
- func = f;
- }
- ~SillyHandler() {
- }
- protected:
- function<void(SillyEvent*)> func;
- virtual void handle(SignalEvent* event) {
- SillyEvent* e = static_cast<SillyEvent*>(event);
- cout << "Message is being set to " << e->getMessage() << endl;
- func(e);
- }
- };
- class SillySignalBus : public SignalBus {
- public:
- SillySignalBus() {}
- ~SillySignalBus() {}
- virtual void emit(string n, string p) {
- if (hasSignal(n)) {
- vector<SignalHandler*> listeners = (*signals[n]);
- SignalEvent* event = createSignalEvent(n, p);
- sendEvent(n, event);
- }
- }
- int getListenerCount(string n) {
- if (hasSignal(n)) {
- vector<SignalHandler*>* listeners = signals[n];
- return listeners->size();
- }
- return 0;
- }
- protected:
- virtual SignalEvent* createSignalEvent(string n) {
- return new SillyEvent(n, myHelper::randomString(10));
- }
- virtual SignalEvent* createSignalEvent(string n, string p) {
- return new SillyEvent(n, p);
- }
- };
- class myContext {
- public:
- myContext() {
- function<void(SignalEvent*)> f = [this](SignalEvent* e) {
- handle(e);
- };
- handler = new FunctionHandler(f);
- }
- ~myContext() {}
- SignalHandler* handler;
- protected:
- void handle(SignalEvent* _e) {
- SillyEvent* e = static_cast<SillyEvent*>(_e);
- cout << "Message: " << e->getMessage() << endl;
- }
- };
- void signalTest() {
- SillySignalBus* bus = new SillySignalBus();
- myContext context;
- bus->subscribe("set", context.handler);
- for (int i = 0; i < 4; i++) {
- if (i % 2 == 0) {
- bus->emit("set", "Mishka is old");
- }
- else {
- cout << "Nothing at " << i << endl;
- }
- }
- bus->unsubscribe("set", context.handler);
- }
- // int main(){
- // signalTest();
- // return 0;
- // }
- }
- else {
- cout << "Nothing at " << i << endl;
- }
- }
- bus->unsubscribe("set", context.handler);
- }
- // int main(){
- // signalTest();
- // return 0;
- // }
- #endif
|