File indexing completed on 2025-03-09 05:01:39

0001 /*
0002     SPDX-FileCopyrightText: 2018 Ivan Čukić <ivan.cukic(at)kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include <iostream>
0008 
0009 #include <voy/operations/merge.h>
0010 #include <voy/operations/slice.h>
0011 #include <voy/operations/transform.h>
0012 #include <voy/operations/filter.h>
0013 #include <voy/operations/identity.h>
0014 
0015 #include <voy/basic/delayed.h>
0016 #include <voy/basic/values.h>
0017 #include <voy/basic/sink.h>
0018 
0019 #include <voy/wrappers/process.h>
0020 #include <voy/wrappers/tcp_service.h>
0021 #include <voy/wrappers/zmq_service.h>
0022 
0023 #include <voy/engine/event_loop.h>
0024 
0025 #include <voy/dsl.h>
0026 
0027 
0028 int main(int argc, char *argv[])
0029 {
0030     auto cout = [] (auto&& value) {
0031         std::cout << "Out: " << voy_fwd(value) << std::endl;
0032     };
0033 
0034     using voy::dsl::operator|;
0035     using namespace std::literals::string_literals;
0036     using namespace std::literals::chrono_literals;
0037 
0038 // #define VALUES_TEST
0039 #ifdef VALUES_TEST
0040     auto pipeline_values =
0041         voy::values{42, 6} | voy::sink{cout};
0042 #endif
0043 
0044 // #define PROCESS_TEST
0045 #ifdef PROCESS_TEST
0046     auto pipeline_process =
0047         voy::system_cmd("task"s) | voy::sink{cout};
0048 #endif
0049 
0050 // #define DELAYED_TEST
0051 #ifdef DELAYED_TEST
0052     auto pipeline_delayed =
0053         voy::delayed(5s, "I'm finally here"s) | voy::sink{cout};
0054 #endif
0055 
0056 // #define FILTER_TEST
0057 #ifdef FILTER_TEST
0058     auto pipeline_filter =
0059         voy::system_cmd("task"s)
0060         | voy::transform([] (std::string in) {
0061                 std::transform(std::begin(in), std::end(in),
0062                                std::begin(in), toupper);
0063                 return in;
0064             })
0065         | voy::sink{cout};
0066 #endif
0067 
0068 // #define DELAYED_VALS_TEST
0069 #ifdef DELAYED_VALS_TEST
0070     auto pipeline_delayed_values =
0071         voy::delayed_values(2s, {"I'm running late"s, "sorry..."s})
0072         | voy::filter([] (const auto& s) {
0073                 return isupper(s[0]);
0074             })
0075         | voy::sink{cout};
0076 #endif
0077 
0078 // #define SLICE_TEST
0079 #ifdef SLICE_TEST
0080     auto pipeline_slice =
0081         voy::system_cmd("ping"s, "1.1.1.1"s)
0082         | voy::slice(1,3)
0083         | voy::sink{cout};
0084 #endif
0085 
0086 // #define MERGE_TEST
0087 #ifdef MERGE_TEST
0088     auto pipeline_merge =
0089         voy::merge(
0090             voy::system_cmd("task"s)
0091             | voy::transform([] (std::string in) {
0092                     std::transform(std::begin(in), std::end(in),
0093                                    std::begin(in), toupper);
0094                     return in;
0095                 })
0096             ,
0097 
0098             voy::delayed_values(2s, {"I'm running late"s, "sorry..."s})
0099             | voy::filter([] (const auto& s) {
0100                     return isupper(s[0]);
0101                 })
0102             ,
0103 
0104             voy::system_cmd("ping"s, "1.1.1.1"s)
0105             | voy::slice(1,3)
0106         )
0107         | voy::transform([] (auto&& s) {
0108             return ">> " + voy_fwd(s);
0109           })
0110         | voy::identity<>()
0111         | voy::sink{cout};
0112 #endif
0113 
0114 // #define TCP_TEST
0115 #ifdef TCP_TEST
0116     auto pipeline_tcp =
0117         voy::tcp::service<>(42042)
0118         | voy::filter(
0119             [] (const auto& value)
0120             {
0121                 auto copy = *value;
0122                 boost::algorithm::trim(copy);
0123                 return !copy.empty();
0124             })
0125         | voy::transform(
0126             [] (auto&& value)
0127             {
0128                 // value.reply((*value) + " (echoed)\n");
0129                 value.reply("Got a message: "s + *value + "\n"s);
0130                 return "TCP message: ["s + *value + "]"s;
0131             })
0132         | voy::sink{cout};
0133 #endif
0134 
0135 // #define ZMQ_SUB_TEST
0136 #ifdef ZMQ_SUB_TEST
0137     auto pipeline_zmq_sub =
0138         voy::zmq::subscriber<>("ipc:///tmp/ivan-zmq-voy-socket-in"s)
0139         | voy::sink{cout};
0140 #endif
0141 
0142 // #define ZMQ_PUB_TEST
0143 #ifdef ZMQ_PUB_TEST
0144     auto pipeline_zmq_pub =
0145         voy::system_cmd("ping"s, "1.1.1.1"s)
0146         | voy::zmq::publisher<>("ipc:///tmp/ivan-zmq-voy-socket-out"s);
0147 #endif
0148 
0149 // #define ZMQ_PUBSUB_TEST
0150 #ifdef ZMQ_PUBSUB_TEST
0151     auto pipeline_zmq_pub =
0152         voy::zmq::subscriber<>("ipc:///tmp/ivan-zmq-voy-socket-in"s)
0153         | voy::zmq::publisher<>("ipc:///tmp/ivan-zmq-voy-socket-out"s);
0154 #endif
0155 
0156 // #define ASSOCIATIVITY_TEST
0157 #ifdef ASSOCIATIVITY_TEST
0158     auto pipeline_associativity =
0159         voy::delayed_values(2s, {"I'm running late"s, "sorry..."s})
0160         | ( voy::filter([] (const auto& s) {
0161                 return isupper(s[0]);
0162             })
0163           | voy::filter([] (const auto& s) {
0164                 return isupper(s[0]);
0165             })
0166           )
0167         | ( voy::transform([] (auto s) {
0168                 std::transform(s.begin(), s.end(), s.begin(), toupper);
0169                 return s;
0170             })
0171         | voy::filter([] (const auto& s) {
0172                 return isupper(s[0]);
0173             })
0174           )
0175         | voy::sink{cout};
0176 
0177 
0178 #endif
0179 
0180     voy::event_loop::run();
0181 
0182     return 0;
0183 }
0184