Erixonich commented on code in PR #7607:
URL: https://github.com/apache/ignite-3/pull/7607#discussion_r2932795500


##########
modules/platforms/cpp/tests/fake_server/proxy/asio_proxy.h:
##########
@@ -0,0 +1,248 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#pragma once
+
+#include <atomic>
+#include <iostream>
+#include <map>
+#include <memory>
+#include <queue>
+#include <thread>
+#include <vector>
+
+#include <asio.hpp>
+#include <asio/ts/internet.hpp>
+
+#include "message_listener.h"
+
+#include <list>
+
+namespace ignite::proxy {
+
+using asio::ip::tcp;
+
+static constexpr size_t BUFF_SIZE = 4096;
+
+struct configuration {
+    asio::ip::port_type m_in_port;
+    std::string m_out_host_and_port;
+    std::shared_ptr<message_listener> m_listener;
+
+    configuration(
+        asio::ip::port_type m_in_port,
+        const std::string &m_out_host_and_port,
+        std::shared_ptr<message_listener> m_listener)
+        : m_in_port(m_in_port)
+        , m_out_host_and_port(m_out_host_and_port)
+        , m_listener(std::move(m_listener)) { }
+};
+
+enum class direction { forward, reverse };
+
+template<direction DIRECTION>
+class session_part: public 
std::enable_shared_from_this<session_part<DIRECTION>> {
+public:
+    session_part(std::shared_ptr<tcp::socket> m_src, 
std::shared_ptr<tcp::socket> m_dst, std::shared_ptr<message_listener> listener)
+        : m_src(std::move(m_src))
+        , m_dst(std::move(m_dst))
+        , m_listener(std::move(listener)) {}
+
+    void do_read() {
+        std::cout << "Reading from socket\n";
+
+        m_src->async_read_some(asio::buffer(m_buf, BUFF_SIZE),
+        [self = std::move(this->shared_from_this())](const asio::error_code& 
ec, size_t len) {
+            if (ec) {
+                if (ec == asio::error::eof) {
+                    return;
+                }
+                throw std::runtime_error("Error while reading from socket " + 
ec.message());
+            }
+
+            message m{self->m_buf.begin(), self->m_buf.begin() + len};
+
+            if (self->m_listener) {
+                if constexpr (DIRECTION == direction::forward) {
+                    self->m_listener->register_out_message(m);
+                } else {
+                    self->m_listener->register_in_message(m);
+                }
+            }
+
+            self->do_write(std::move(m));
+        });
+    }
+
+    void do_write(message&& msg) {
+        asio::async_write(
+            *m_dst, asio::buffer(msg.data(), msg.size()),
+            [self = std::move(this->shared_from_this())](asio::error_code ec, 
size_t) {
+                if (ec) {
+                    if (ec == asio::error::eof) {
+                        return;
+                    }
+                    throw std::runtime_error("Error while writing to socket " 
+ ec.message());
+                }
+
+                self->do_read();
+            });
+    }
+
+private:
+    std::shared_ptr<tcp::socket> m_src;
+    std::shared_ptr<tcp::socket> m_dst;
+    std::array<char, BUFF_SIZE> m_buf{};
+    std::shared_ptr<message_listener> m_listener{nullptr};
+
+};
+
+class session : public std::enable_shared_from_this<session> {
+public:
+    session(
+        std::shared_ptr<tcp::socket> in_sock,
+        std::shared_ptr<tcp::socket> out_sock,
+        std::shared_ptr<message_listener> listener)
+        : m_in_sock(std::move(in_sock))
+        , m_out_sock(std::move(out_sock))
+    {
+        m_forward_part = 
std::make_shared<session_part<direction::forward>>(m_in_sock, m_out_sock, 
listener);
+        m_reverse_part = 
std::make_shared<session_part<direction::reverse>>(m_out_sock, m_in_sock, 
listener);
+    }
+
+    void start() { do_serve(); }
+
+    tcp::socket &get_out_sock() { return *m_out_sock; }
+
+private:
+    void do_serve() {
+        m_forward_part->do_read();
+        m_reverse_part->do_read();
+    }
+
+    std::shared_ptr<tcp::socket> m_in_sock;
+    std::shared_ptr<tcp::socket> m_out_sock;
+
+    std::shared_ptr<session_part<direction::forward>> m_forward_part;
+    std::shared_ptr<session_part<direction::reverse>> m_reverse_part;
+};
+
+class asio_proxy {
+public:
+    asio_proxy(std::vector<configuration> configurations)
+        : m_resolver(m_io_context)
+    {
+        for (auto &cfg : configurations) {
+            m_conn_map.emplace(
+                cfg.m_in_port,
+                proxy_entry{m_io_context, cfg.m_in_port, 
cfg.m_out_host_and_port, cfg.m_listener}
+            );
+        }
+
+        do_serve();
+
+        m_executor = std::make_unique<std::thread>([this]() {
+            m_io_context.run();
+        });
+    }
+
+    ~asio_proxy() {
+        m_stopped.store(true);
+        m_io_context.stop();
+
+        m_executor->join();
+    }
+
+private:
+    struct proxy_entry {
+        tcp::acceptor m_in_acceptor;
+        std::string m_out_host;
+        std::string m_out_port;
+        std::shared_ptr<message_listener> m_listener;
+
+        proxy_entry(
+            asio::io_context& io_context,
+            asio::ip::port_type in_port,
+            const std::string& out_host_and_port,

Review Comment:
   This is done for usability purposes. User of this class can reuse address 
list for ignite_client.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to