#include "pibytearray.h" #include "piclientserver_client.h" #include "piclientserver_server.h" #include "picodeparser.h" #include "piintrospection_server.h" #include "piiostream.h" #include "pijson.h" #include "pilog.h" #include "pimathbase.h" #include "pip.h" #include "pivaluetree_conversions.h" using namespace PICoutManipulators; enum MyEvent { meVoid, meInt, meString, meIntString, }; PIKbdListener kbd; class MyServerClient: public PIClientServer::ServerClient { public: ~MyServerClient() { send_thread.stopAndWait(); } protected: void readed(PIByteArray data) override { piCout << "readed" << (data.size()); } // void aboutDelete() override { piCout << "aboutDelete"; } // void disconnected() override { piCout << "disconnected"; } void connected() override { // piCout << "connected"; send_thread.start( [this] { // write((PIString::fromNumber(++counter)).toUTF8()); PIByteArray ba(64_KiB); write(ba); }, 2_Hz); } PIThread send_thread; int counter = 0; }; class MyClient: public PIClientServer::Client { public: ~MyClient() { send_thread.stopAndWait(); } protected: void readed(PIByteArray data) override { piCout << "readed" << (data.size()); } void disconnected() override { piCout << "disconnected"; } void connected() override { piCout << "connected"; send_thread.start( [this] { // write((PIString::fromNumber(++counter)).toUTF8()); write(PIByteArray(64_KiB)); }, 2_Hz); } PIThread send_thread; int counter = 0; }; #include int main(int argc, char * argv[]) { /*piCout << PIString("hello locale").entries("lo"); // 2 piCout << PIString("hello locale").entries("lo lo"); // 1 piCout << ("3hello4"_u8); piCout << ("3hello4"_u8).entries("hello1"); piCout << ("3hello4"_u8).entries("hello"); piCout << ("3hello4"_u8).entries("l"); piCout << ("3hello4"_u8).entries("ello"); piCout << ("3hello4"_u8).entries("hell"); piCout << ("2hellohello1"_u8); piCout << ("2hellohello1"_u8).entries("hello1"); piCout << ("2hellohello1"_u8).entries("hello"); piCout << ("2hellohello1"_u8).entries("l"); piCout << ("2hellohello1"_u8).entries("ello"); piCout << ("2hellohello1"_u8).entries("hell"); return 0;*/ PILog log; log.setColorConsole(false); log.setOutput(PILog::File, false); log.setLogName("test"); log.setDir("logs"); // log.setTimestampFormat("hh-mm-ss"); // log.setLineFormat("[c] m (t)"); log.start(); // log.enqueue("debug msg"); // log.enqueue("warn msg with ${c}", PILog::Category::Warning); // log.enqueue("ERROR${m}${t}", PILog::Category::Error); log.setLevel(PILog::Level::Info); log.debug() << "some msg"; piMSleep(50); log.info() << "information"; piMSleep(50); log.warning() << "another!"; piMSleep(50); log.error(&log) << "critical!"; // log.stop(); return 0; /*PIPeer p("123"); piCout << "start ..."; p.start(); piCout << "start ok"; piSleep(1.); piCout << "stop ..."; p.stopAndWait(); piCout << "stop ok"; piSleep(1.); piCout << "exit"; return 0;*/ if (argc > 1) { PIINTROSPECTION_START(server); } else { PIINTROSPECTION_START(client); } kbd.enableExitCapture(); PIClientServer::Server * s = nullptr; PIThread * s_thread = new PIThread(); PIVector cv; if (argc > 1) { piCout << "Server"; s = new PIClientServer::Server(); s->setClientFactory([] { return new MyServerClient(); }); s->configuration().enableSymmetricEncryption("1122334455667788"_hex); s->listenAll(12345); s_thread->start( [s] { piCout << "*** clients" << s->clientsCount(); int i = 0; s->forEachClient([&i](PIClientServer::ServerClient * c) { // piCout << "client" << ++i << c; c->write(PIByteArray(1_KiB)); // c->close(); // piMSleep(200); }); }, 1._Hz); } else { piCout << "Client"; piForTimes(2) { piMSleep(25); auto c = new MyClient(); c->configuration().enableSymmetricEncryption("1122334455667788"_hex); c->connect(PINetworkAddress::resolve("127.0.0.1", 12345)); cv << c; } } WAIT_FOR_EXIT; s_thread->stopAndWait(); piDeleteSafety(s); piDeleteAllAndClear(cv); PIINTROSPECTION_STOP return 0; /*PIPackedTCP * tcp_s = PIIODevice::createFromFullPath("ptcp://s::8000")->cast(); // new PIPackedTCP(PIPackedTCP::Server, {"0.0.0.0:8000"}); PIPackedTCP * tcp_c = PIIODevice::createFromFullPath("ptcp://c:127.0.0.1:8000") ->cast(); // new PIPackedTCP(PIPackedTCP::Client, {"127.0.0.1:8000"}); piCout << tcp_s << tcp_c; // CONNECTL(&tcp_s, opened, []() { piCout << "Srv opened"; }); // CONNECTL(&tcp_c, opened, []() { piCout << "Cli opened"; }); // CONNECTL(&tcp_s, closed, []() { piCout << "Srv closed"; }); // CONNECTL(&tcp_c, closed, []() { piCout << "Cli closed"; }); CONNECTL(tcp_s, connected, []() { piCout << "Srv conn"; }); CONNECTL(tcp_c, connected, []() { piCout << "Cli conn"; }); CONNECTL(tcp_s, disconnected, []() { piCout << "Srv disconn"; }); CONNECTL(tcp_c, disconnected, []() { piCout << "Cli disconn"; }); CONNECTL(tcp_s, threadedReadEvent, [](const uchar * readed, ssize_t size) { PIByteArray d(readed, size); piCout << "Srv readed" << d; }); CONNECTL(tcp_c, threadedReadEvent, [](const uchar * readed, ssize_t size) { PIByteArray d(readed, size); piCout << "Cli readed" << d; }); // tcp_s->open(); // tcp_c->open(); tcp_s->startThreadedRead(); tcp_c->startThreadedRead(); piForTimes(5) { piCout << "\n1"; piMSleep(200); piCout << "2"; // tcp_c->close(); piCout << "2+"; // tcp_s->close(); piCout << "3"; // tcp_c->open(); piCout << "3+"; // tcp_s->open(); piCout << "4"; piMSleep(500); // piCout << "5"; tcp_c->write("1234567890"_a.toByteArray()); // piCout << "6"; tcp_s->write("1234567890"_a.toByteArray()); // piCout << "7"; piMSleep(200); } // piCout << &tcp_s; // piCout << tcp_s->path(); // piCout << tcp_s->constructFullPath(); delete tcp_s; delete tcp_c; return 0;*/ /*PITimer timer(tfunc); // timer.addDelimiter(2); timer.addDelimiter(40, tfunc4); tm.reset(); timer.start(0.5_Hz); piMSleep(2200); piCout << tm.elapsed_m() << cnt; timer.stop(); timer.waitForFinish(); return 0;*/ bool posted; PIStateMachine * root = new PIStateMachine("Machine"); root->setOnFinish([] { piCout << "finish"; }); PIStateLambda * s1 = new PIStateLambda([] { piCout << "+ enter s1"; }, [] { piCout << "- exit s1"; }, "s1"); PIStateLambda * s2 = new PIStateLambda([] { piCout << "+ enter s2"; }, [] { piCout << "- exit s2"; }, "s2"); PIStateLambda * s3 = new PIStateLambda([] { piCout << "+ enter s3"; }, [] { piCout << "- exit s3"; }, "s3"); PIStateLambda * s4 = new PIStateLambda([] { piCout << "+ enter s4"; }, [] { piCout << "- exit s4"; }, "s4"); PIStateLambda * s11 = new PIStateLambda([] { piCout << " + enter s11"; }, [] { piCout << " - exit s11"; }, "s11"); PIStateLambda * s12 = new PIStateLambda([] { piCout << " + enter s12"; }, [] { piCout << " - exit s12"; }, "s12"); PIStateLambda * s13 = new PIStateLambda([] { piCout << " + enter s13"; }, [] { piCout << " - exit s13"; }, "s13"); PIStateLambda * s21 = new PIStateLambda([] { piCout << " + enter s21"; }, [] { piCout << " - exit s21"; }, "s21"); PIStateLambda * s22 = new PIStateLambda([] { piCout << " + enter s22"; }, [] { piCout << " - exit s22"; }, "s22"); PIStateLambda * s23 = new PIStateLambda([] { piCout << " + enter s23"; }, [] { piCout << " - exit s23"; }, "s23"); PIStateLambda * s211 = new PIStateLambda([] { piCout << " + enter s211"; }, [] { piCout << " - exit s211"; }, "s211"); PIStateLambda * s212 = new PIStateLambda([] { piCout << " + enter s212"; }, [] { piCout << " - exit s212"; }, "s212"); PIStateLambda * s213 = new PIStateLambda([] { piCout << " + enter s213"; }, [] { piCout << " - exit s213"; }, "s213"); PIStateFinal * s214 = new PIStateFinal([] { piCout << " + enter s214 final"; }, "s214f"); root->addStates({s1, s2, s3, s4}); // s1->addStates({s11, s12, s13}); // s2->addStates({s21, s22, s23}); // s21->addStates({s211, s212, s213}); // root->addState(s214); s2->setParallel(true); root->setInitialState(s1); s1->setInitialState(s11); s2->setInitialState(s21); s21->setInitialState(s213); // s213->addTransition(s13, meVoid)->addAction([] { piCout << "action transition s21 -> s22"; }); // s3->addTransition(s212, meVoid)->addAction([] { piCout << "action transition s1 -> s213"; }); s1->addTransition(s2, meVoid)->addAction([root] { root->postEvent(meInt, 1); }); s2->addTransition(s3, meInt)->addGuard([](int i) { return i == 1; })->addAction([root] { root->postEvent(meInt, 2); }); s3->addTransition(s4, meInt)->addGuard([](int i) { return i == 2; })->addAction([root] { root->postEvent(meInt, 3); }); // s2->addTimeoutTransition(s3, .5_s); // s3->addTransition(s1, meIntString)->addGuard([](int i, PIString str) { return i == 2 && str == "hello"; }); root->start(); piCout << "initial" << root->isRunning() << "\n"; // piCout << "active atomics" << root->activeAtomics(); root->print(); piCout << "\npost event"; posted = root->postEvent(meVoid); piCout << "posted" << posted << "\n"; // piCout << "active atomics" << root->activeAtomics(); piSleep(1.); root->print(); // piCout << "\npost event"; // posted = root->postEvent(meVoid); // piCout << "posted" << posted << "\n"; // root->print(); /*root->addStates({s1, s2, s3}); root->setInitialState(s1); root->start(); piCout << root->postEvent(meVoid); piCout << ""; piCout << root->postEvent(meInt, 0.5f); piCout << ""; piCout << root->postEvent(meInt, 0); piCout << ""; piCout << root->postEvent(meInt, 1); piCout << ""; piCout << root->postEvent(meIntString, 2, "hello"); piCout << ""; piCout << root->postEvent(meIntString, 2, PIString("hello")); piCout << "";*/ delete root; /*PISerial ser; ser.setSpeed(PISerial::S115200); piCout << ser.open("COM15", PIIODevice::ReadWrite); CONNECTL(&ser, threadedReadEvent, ([](const uchar * data, ssize_t size) { piCout << PIByteArray(data, size); })); ser.startThreadedRead(); (2_s).sleep(); ser.stopAndWait();*/ /*auto test = [](PISystemTime t) { piCout << "---------------"; piCout << t << t.toString(); piCout << PISystemTime::fromString(t.toString()) << PISystemTime::fromString(t.toString()).toString(); piCout << PIVariant(t); piCout << PIVariant(t).toString(); }; auto test_str = [](PIString s) { piCout << "---------------"; piCout << s; piCout << PISystemTime::fromString(s) << PISystemTime::fromString(s).toString(); }; test(PISystemTime::current()); test(1000.123_d); test(61_s); test(59_s); test(1234_us); test(5000001_ns); test_str(""); test_str("2020-05-04"); test_str("2020-05-04 11:12:13"); test_str("11:12:13"); test_str("11:12:13.405"); test_str("100"); test_str("5 s"); test_str("5s"); test_str("1000 d 2 h 57 m 7 s 200 ms 2 ns"); test_str("0.2 d"); test_str(".5d"); test_str("3E+2us"); test_str("0.1d 200millis"); test_str("2h 15 seconds"); test_str("4secs 2hours 100 ns 3m 5ms 1 day 6 micros "); test_str("1hz"); test_str("5.123khz"); test_str("5khz 123 hz"); test_str("2mhz"); test_str("1ghz"); test_str("1hz 1hz");*/ // PIPair p = createPIPair(0, ""); /*PIConfig conf("model.conf"); piCout << "****** config\n" << conf.allLeaves() << "******\n"; PIValueTree vt = PIValueTreeConversions::fromTextFile("model.conf"); piCout << "****** tree"; vt.forEachRecursive( [](const PIValueTree & v, const PIString & fn) { piCout << fn << "=" << v.value().toString() << "#" << v.comment(); }); piCout << "******";*/ return 0; }