|
|
|
@ -1,40 +1,42 @@ |
|
|
|
|
#define NUM_OF_RUNNERS 2 |
|
|
|
|
|
|
|
|
|
// #include "../lib/asynco.hpp"
|
|
|
|
|
// #include "../lib/event.hpp"
|
|
|
|
|
// #include "../lib/rotor.hpp"
|
|
|
|
|
#include "../lib/filesystem.hpp" |
|
|
|
|
#include "../lib/asynco.hpp" |
|
|
|
|
#include "../lib/event.hpp" |
|
|
|
|
#include "../lib/rotor.hpp" |
|
|
|
|
// #include "../lib/filesystem.hpp"
|
|
|
|
|
|
|
|
|
|
#include <iostream> |
|
|
|
|
#include <unistd.h> |
|
|
|
|
|
|
|
|
|
using namespace std; |
|
|
|
|
using namespace marcelb; |
|
|
|
|
using namespace marcelb::asynco; |
|
|
|
|
using namespace events; |
|
|
|
|
// using namespace asynco;
|
|
|
|
|
using namespace this_thread; |
|
|
|
|
|
|
|
|
|
// void sleep_to (int _time) {
|
|
|
|
|
// promise<void> _promise;
|
|
|
|
|
// timeout t( [&]() {
|
|
|
|
|
// _promise.set_value();
|
|
|
|
|
// }, _time);
|
|
|
|
|
|
|
|
|
|
// return _promise.get_future().get();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// void promise_reject (int _time) {
|
|
|
|
|
// promise<void> _promise;
|
|
|
|
|
// timeout t( [&]() {
|
|
|
|
|
// try {
|
|
|
|
|
// // simulate except
|
|
|
|
|
// throw runtime_error("Error simulation");
|
|
|
|
|
// _promise.set_value();
|
|
|
|
|
// } catch (...) {
|
|
|
|
|
// _promise.set_exception(current_exception());
|
|
|
|
|
// }
|
|
|
|
|
// }, _time);
|
|
|
|
|
|
|
|
|
|
// return _promise.get_future().get();
|
|
|
|
|
// }
|
|
|
|
|
void sleep_to (int _time) { |
|
|
|
|
promise<void> _promise; |
|
|
|
|
timeout t( [&]() { |
|
|
|
|
_promise.set_value(); |
|
|
|
|
}, _time); |
|
|
|
|
|
|
|
|
|
return _promise.get_future().get(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void promise_reject (int _time) { |
|
|
|
|
promise<void> _promise; |
|
|
|
|
timeout t( [&]() { |
|
|
|
|
try { |
|
|
|
|
// simulate except
|
|
|
|
|
throw runtime_error("Error simulation"); |
|
|
|
|
_promise.set_value(); |
|
|
|
|
} catch (...) { |
|
|
|
|
_promise.set_exception(current_exception()); |
|
|
|
|
} |
|
|
|
|
}, _time); |
|
|
|
|
|
|
|
|
|
return _promise.get_future().get(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void notLambdaFunction() { |
|
|
|
|
cout << "Call to not lambda function" << endl; |
|
|
|
@ -49,42 +51,42 @@ class clm { |
|
|
|
|
|
|
|
|
|
// ------------------ EXTEND OWN CLASS WITH EVENTS -------------------
|
|
|
|
|
|
|
|
|
|
// class myOwnClass : public event<int> {
|
|
|
|
|
// public:
|
|
|
|
|
// myOwnClass() : event() {};
|
|
|
|
|
// };
|
|
|
|
|
class myOwnClass : public event<int> { |
|
|
|
|
public: |
|
|
|
|
myOwnClass() : event() {}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main () { |
|
|
|
|
|
|
|
|
|
// auto start = rtime_ms();
|
|
|
|
|
auto start = rtime_ms(); |
|
|
|
|
|
|
|
|
|
// --------------- TIME ASYNCHRONOUS FUNCTIONS --------------
|
|
|
|
|
// // --------------- TIME ASYNCHRONOUS FUNCTIONS --------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Init interval and timeout; clear interval and timeout |
|
|
|
|
*/ |
|
|
|
|
// /**
|
|
|
|
|
// * Init interval and timeout; clear interval and timeout
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
interval( [&] () { |
|
|
|
|
cout << "interval 1: " << rtime_ms() - start << endl; |
|
|
|
|
}, 50); |
|
|
|
|
// interval( [&] () {
|
|
|
|
|
// cout << "interval 1: " << rtime_ms() - start << endl;
|
|
|
|
|
// }, 50);
|
|
|
|
|
|
|
|
|
|
interval( [&] () { |
|
|
|
|
cout << "interval 1: " << rtime_ms() - start << endl; |
|
|
|
|
}, 100); |
|
|
|
|
// interval( [&] () {
|
|
|
|
|
// cout << "interval 1: " << rtime_ms() - start << endl;
|
|
|
|
|
// }, 100);
|
|
|
|
|
|
|
|
|
|
interval( [&] () { |
|
|
|
|
cout << "interval 2: " << rtime_ms() - start << endl; |
|
|
|
|
}, 200); |
|
|
|
|
// interval( [&] () {
|
|
|
|
|
// cout << "interval 2: " << rtime_ms() - start << endl;
|
|
|
|
|
// }, 200);
|
|
|
|
|
|
|
|
|
|
interval( [&] () { |
|
|
|
|
cout << "interval 3: " << rtime_ms() - start << endl; |
|
|
|
|
}, 300); |
|
|
|
|
// interval( [&] () {
|
|
|
|
|
// cout << "interval 3: " << rtime_ms() - start << endl;
|
|
|
|
|
// }, 300);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interval( [&] () { |
|
|
|
|
cout << "interval 4: " << rtime_ms() - start << endl; |
|
|
|
|
}, 400); |
|
|
|
|
// interval( [&] () {
|
|
|
|
|
// cout << "interval 4: " << rtime_ms() - start << endl;
|
|
|
|
|
// }, 400);
|
|
|
|
|
|
|
|
|
|
// interval inter1 ([&]() {
|
|
|
|
|
// cout << "interval prvi " << rtime_ms() - start << endl;
|
|
|
|
@ -127,13 +129,13 @@ int main () { |
|
|
|
|
|
|
|
|
|
// // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS -------------------------
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * Run an function asyncronic
|
|
|
|
|
// */
|
|
|
|
|
/**
|
|
|
|
|
* Run an function asyncronic |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
// asynco( []() {
|
|
|
|
|
// atask( []() {
|
|
|
|
|
// sleep_for(2s); // only for simulate log duration function
|
|
|
|
|
// cout << "asynco 1" << endl;
|
|
|
|
|
// cout << "atask 1" << endl;
|
|
|
|
|
// return 5;
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
@ -141,11 +143,11 @@ int main () { |
|
|
|
|
// * Call not lambda function
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
// asynco (notLambdaFunction);
|
|
|
|
|
// atask (notLambdaFunction);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// wait (
|
|
|
|
|
// asynco (
|
|
|
|
|
// atask (
|
|
|
|
|
// notLambdaFunction
|
|
|
|
|
// )
|
|
|
|
|
// );
|
|
|
|
@ -155,7 +157,7 @@ int main () { |
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
// clm classes;
|
|
|
|
|
// asynco( [&classes] () {
|
|
|
|
|
// atask( [&classes] () {
|
|
|
|
|
// classes.classMethode();
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
@ -165,20 +167,20 @@ int main () { |
|
|
|
|
// * Wait after runned as async
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
// auto a = asynco( []() {
|
|
|
|
|
// auto a = atask( []() {
|
|
|
|
|
// sleep_for(2s); // only for simulate log duration function
|
|
|
|
|
// cout << "asynco 2" << endl;
|
|
|
|
|
// cout << "atask 2" << endl;
|
|
|
|
|
// return 5;
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// cout << wait(a) << endl;
|
|
|
|
|
// cout << "print after asynco 2" << endl;
|
|
|
|
|
// cout << "print after atask 2" << endl;
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * Wait async function call and use i cout
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
// cout << wait(asynco( [] () {
|
|
|
|
|
// cout << wait(atask( [] () {
|
|
|
|
|
// sleep_for(chrono::seconds(1)); // only for simulate log duration function
|
|
|
|
|
// cout << "wait end" << endl;
|
|
|
|
|
// return 4;
|
|
|
|
@ -191,9 +193,9 @@ int main () { |
|
|
|
|
// sleep_to(3000);
|
|
|
|
|
// cout << "sleep_to " << rtime_ms() - start << endl;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Catch promise reject |
|
|
|
|
*/ |
|
|
|
|
// /**
|
|
|
|
|
// * Catch promise reject
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
// promise_reject(3000);
|
|
|
|
@ -209,110 +211,84 @@ int main () { |
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// asynco( [] {
|
|
|
|
|
// atask( [] {
|
|
|
|
|
// cout << "idemo ..." << endl;
|
|
|
|
|
// asynco( [] {
|
|
|
|
|
// atask( [] {
|
|
|
|
|
// cout << "ugdnježdena async funkcija " << endl;
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // --------------- EVENTS -------------------
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * initialization of typed events
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
// event<int, int> ev2int;
|
|
|
|
|
// event<int, string> evintString;
|
|
|
|
|
// event<> evoid;
|
|
|
|
|
// --------------- EVENTS -------------------
|
|
|
|
|
|
|
|
|
|
// ev2int.on("sum", [](int a, int b) {
|
|
|
|
|
// cout << "Sum " << a+b << endl;
|
|
|
|
|
// });
|
|
|
|
|
/**
|
|
|
|
|
* initialization of typed events |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
// ev2int.on("sum", [](int a, int b) {
|
|
|
|
|
// cout << "Sum done" << endl;
|
|
|
|
|
// });
|
|
|
|
|
event<int, int> ev2int; |
|
|
|
|
event<int, string> evintString; |
|
|
|
|
event<> evoid; |
|
|
|
|
|
|
|
|
|
// evintString.on("substract", [](int a, string b) {
|
|
|
|
|
// cout << "Substract " << a-stoi(b) << endl;
|
|
|
|
|
// });
|
|
|
|
|
ev2int.on("sum", [](int a, int b) { |
|
|
|
|
cout << "Sum " << a+b << endl; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// evoid.on("void", []() {
|
|
|
|
|
// cout << "Void emited" << endl;
|
|
|
|
|
// });
|
|
|
|
|
ev2int.on("sum", [](int a, int b) { |
|
|
|
|
cout << "Sum done" << endl; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// string emited2 = "2";
|
|
|
|
|
evintString.on("substract", [](int a, string b) { |
|
|
|
|
cout << "Substract " << a-stoi(b) << endl; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// evoid.on("void", [&]() {
|
|
|
|
|
// cout << "Void emited " << emited2 << endl;
|
|
|
|
|
// });
|
|
|
|
|
evoid.on("void", []() { |
|
|
|
|
cout << "Void emited" << endl; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// evoid.emit("void");
|
|
|
|
|
// sleep(1);
|
|
|
|
|
string emited2 = "2"; |
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * Emit
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
// ev2int.emit("sum", 5, 8);
|
|
|
|
|
evoid.on("void", [&]() { |
|
|
|
|
cout << "Void emited " << emited2 << endl; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
evoid.emit("void"); |
|
|
|
|
sleep(1); |
|
|
|
|
|
|
|
|
|
// sleep(1);
|
|
|
|
|
// evintString.emit("substract", 3, to_string(2));
|
|
|
|
|
/**
|
|
|
|
|
* Emit |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
// sleep(1);
|
|
|
|
|
// evoid.off("void");
|
|
|
|
|
// evoid.emit("void");
|
|
|
|
|
ev2int.emit("sum", 5, 8); |
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * Own class
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
|
|
// myOwnClass myclass;
|
|
|
|
|
sleep(1); |
|
|
|
|
evintString.emit("substract", 3, to_string(2)); |
|
|
|
|
|
|
|
|
|
// timeout t( [&] {
|
|
|
|
|
// myclass.emit("constructed", 1);
|
|
|
|
|
// }, 200);
|
|
|
|
|
sleep(1); |
|
|
|
|
evoid.off("void"); |
|
|
|
|
evoid.emit("void"); |
|
|
|
|
|
|
|
|
|
// myclass.on("constructed", [] (int i) {
|
|
|
|
|
// cout << "Constructed " << i << endl;
|
|
|
|
|
// });
|
|
|
|
|
/**
|
|
|
|
|
* Own class
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
string data_; |
|
|
|
|
myOwnClass myclass; |
|
|
|
|
|
|
|
|
|
// asynco_read("test.txt", [&data_] (string data, exception* error) {
|
|
|
|
|
// if (error) {
|
|
|
|
|
// cout << "Error " << error->what() << endl;
|
|
|
|
|
// } else {
|
|
|
|
|
// cout << "Data " << endl << data << endl;
|
|
|
|
|
// data_ = data;
|
|
|
|
|
// cout << "Data_" << data_ << endl;
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
timeout t( [&] { |
|
|
|
|
myclass.emit("constructed", 1); |
|
|
|
|
}, 200); |
|
|
|
|
|
|
|
|
|
// auto data = asynco_read("test4.txt");
|
|
|
|
|
myclass.on("constructed", [] (int i) { |
|
|
|
|
cout << "Constructed " << i << endl; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
// data_ = wait(data);
|
|
|
|
|
// cout << "data" << data_ << endl;
|
|
|
|
|
// } catch (exception& err) {
|
|
|
|
|
// cout << err.what() << endl;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// asynco_write("test1.txt", "Hello night", [] (exception* error) {
|
|
|
|
|
// if (error) {
|
|
|
|
|
// cout << "Error " << error->what() << endl;
|
|
|
|
|
// } else {
|
|
|
|
|
// cout << "Write successfuly" << endl;
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
auto status = asynco_write("test1.txt", "Hello night"); |
|
|
|
|
// auto status = fs::read("test1.txt");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
// wait(status);
|
|
|
|
|
// auto data = wait(status);
|
|
|
|
|
// cout << data;
|
|
|
|
|
// } catch (exception& err) {
|
|
|
|
|
// cout << err.what() << endl;
|
|
|
|
|
// }
|
|
|
|
|