File indexing completed on 2025-02-09 06:39:41

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 <sys/types.h>
0010 #include <unistd.h>
0011 
0012 #include <voy/operations/merge.h>
0013 #include <voy/operations/slice.h>
0014 #include <voy/operations/transform.h>
0015 #include <voy/operations/filter.h>
0016 #include <voy/operations/identity.h>
0017 
0018 #include <voy/basic/delayed.h>
0019 #include <voy/basic/values.h>
0020 #include <voy/basic/sink.h>
0021 
0022 #include <voy/wrappers/process.h>
0023 #include <voy/wrappers/tcp_service.h>
0024 #include <voy/wrappers/zmq_service.h>
0025 
0026 #include <voy/engine/event_loop.h>
0027 
0028 #include <voy/dsl.h>
0029 #include <voy/dsl/multiprocess.h>
0030 
0031 #include "../../utils/debug.h"
0032 
0033 using namespace std::literals::string_literals;
0034 using namespace std::literals::chrono_literals;
0035 
0036 
0037 #if defined TEST_FRONTEND
0038 voy_declare_bridge_spread(frontend_to_backend_1)
0039 voy_declare_bridge_ignored(backend_1_to_backend_2)
0040 voy_declare_bridge_in(backend_2_to_frontend)
0041 
0042 #elif defined TEST_BACKEND_1
0043 voy_declare_bridge_accept(frontend_to_backend_1)
0044 voy_declare_bridge_post(backend_1_to_backend_2)
0045 voy_declare_bridge_ignored(backend_2_to_frontend)
0046 
0047 #else // TEST_BACKEND_2
0048 voy_declare_bridge_ignored(frontend_to_backend_1)
0049 voy_declare_bridge_collect(backend_1_to_backend_2)
0050 voy_declare_bridge_out(backend_2_to_frontend)
0051 
0052 #endif
0053 
0054 inline std::string pid_s()
0055 {
0056     return " // pid:" + debug::colorize(getpid());
0057 }
0058 
0059 int main(int argc, char *argv[])
0060 {
0061     using voy::dsl::operator|;
0062     using voy::dsl::operator||;
0063     using debug::color;
0064 
0065     auto cout = [] (auto&& value) {
0066         std::cout << "Out: " << voy_fwd(value) << std::endl;
0067     };
0068 
0069     auto append_pid = voy::transform([] (std::string&& value) {
0070                 debug::out(color::gray) << "sending " << value << " from " << pid_s();
0071                 return std::move(value) + pid_s();
0072             });
0073 
0074     debug::out(color::red) << pid_s();
0075 
0076     auto pipeline =
0077         voy::system_cmd("ping"s, "localhost"s)
0078         | voy::transform([] (std::string&& value) {
0079               std::transform(value.begin(), value.end(), value.begin(), toupper);
0080               debug::out(color::gray) << value << pid_s();
0081               return value;
0082           })
0083         | append_pid
0084         | voy_bridge(frontend_to_backend_1)
0085 
0086         | voy::transform([] (std::string&& value) {
0087               const auto pos = value.find_last_of('=');
0088               debug::out(color::gray) << value << " found = at " << pos << pid_s();
0089               return std::make_pair(std::move(value), pos);
0090           })
0091         | voy::transform([] (std::pair<std::string, size_t>&& pair) {
0092               const auto& [ value, pos ] = pair;
0093               debug::out(color::gray) << value << " found = at " << pos << " - extracting ms" << pid_s();
0094               return pos == std::string::npos
0095                           ? value
0096                           : std::string(value.cbegin() + pos + 1, value.cend());
0097           })
0098         | append_pid
0099         | voy_bridge(backend_1_to_backend_2)
0100 
0101         | voy::filter([] (const std::string& value) {
0102               debug::out(color::gray) << value << " - filtering" << pid_s();
0103               return value < "0.145"s;
0104           })
0105         | append_pid
0106         | voy_bridge(backend_2_to_frontend)
0107 
0108         | voy::sink{cout};
0109 
0110     voy::event_loop::run();
0111 
0112     return 0;
0113 }
0114