isapego commented on code in PR #7080:
URL: https://github.com/apache/ignite-3/pull/7080#discussion_r2589567802


##########
modules/platforms/cpp/ignite/client/detail/node_connection.h:
##########
@@ -49,6 +49,23 @@ class node_connection : public 
std::enable_shared_from_this<node_connection> {
 public:
     typedef std::function<void(protocol::writer&, const 
protocol::protocol_context&)> writer_function_type;
 
+    struct pending_request {
+        /** Handler function for request */
+        std::shared_ptr<response_handler> handler = nullptr;

Review Comment:
   ```suggestion
           std::shared_ptr<response_handler> handler{};
   ```



##########
modules/platforms/cpp/ignite/client/detail/node_connection.h:
##########
@@ -49,6 +49,23 @@ class node_connection : public 
std::enable_shared_from_this<node_connection> {
 public:
     typedef std::function<void(protocol::writer&, const 
protocol::protocol_context&)> writer_function_type;
 
+    struct pending_request {
+        /** Handler function for request */
+        std::shared_ptr<response_handler> handler = nullptr;
+
+        /**
+         * Optional request timeout.
+         * When provided contains time point after which request would be 
considered as timed out.
+         */
+        std::optional<std::chrono::time_point<std::chrono::steady_clock>> 
timeouts_at = std::nullopt;

Review Comment:
   ```suggestion
           std::optional<std::chrono::time_point<std::chrono::steady_clock>> 
timeouts_at{};
   ```



##########
modules/platforms/cpp/ignite/client/detail/node_connection.cpp:
##########
@@ -203,7 +207,46 @@ std::shared_ptr<response_handler> 
node_connection::find_handler_unsafe(std::int6
     if (it == m_request_handlers.end())
         return {};
 
-    return it->second;
+    return it->second.handler;
+}
+
+void node_connection::handle_timeouts() {
+    auto now = std::chrono::steady_clock::now();
+
+    {
+        std::lock_guard lock(m_request_handlers_mutex);
+
+        for (auto it = m_request_handlers.begin(); it != 
m_request_handlers.end();) {
+            auto &id = it->first;
+            auto &req = it->second;
+
+            if (req.timeouts_at.has_value() && req.timeouts_at < now) {
+                std::stringstream ss;
+                ss << "Operation timeout [req_id=" << id << "]";
+                auto res = 
req.handler->set_error(ignite_error(error::code::CLIENT_OPERATION_TIMEOUT, 
ss.str()));
+
+                this->m_logger->log_warning(ss.str());

Review Comment:
   Minor, but: method `ss.str()` allocates new string, you are not supposed to 
call it more than once. Also, I think basic string concatenation would work 
faster and look simpler here:
   ```suggestion
                   auto err_message = std::string("Operation timeout [req_id=") 
+ std::to_string(id) + ']';
                   auto res = 
req.handler->set_error(ignite_error(error::code::CLIENT_OPERATION_TIMEOUT, 
err_message));
   
                   this->m_logger->log_warning(err_message);
   ```



##########
modules/platforms/cpp/tests/fake_server/tcp_client_channel.cpp:
##########
@@ -0,0 +1,94 @@
+/*
+* 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.
+ */
+
+#include "tcp_client_channel.h"
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <unistd.h>
+
+std::vector<std::byte> ignite::tcp_client_channel::read_next_n_bytes(size_t n) 
{

Review Comment:
   Let's move namespace from the method name to surround all the methods.



##########
modules/platforms/cpp/tests/fake_server/tcp_client_channel.cpp:
##########
@@ -0,0 +1,94 @@
+/*
+* 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.
+ */
+
+#include "tcp_client_channel.h"
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <unistd.h>
+
+std::vector<std::byte> ignite::tcp_client_channel::read_next_n_bytes(size_t n) 
{
+    std::vector<std::byte> res;
+    res.reserve(n);
+
+    while (res.size() < n && !m_stopped) {
+        if (m_remaining == 0) {
+            receive_next_packet();
+        }
+
+        size_t bytes_to_consume = std::min(m_remaining, n);
+
+        std::copy_n(m_buf + m_pos, bytes_to_consume, std::back_inserter(res));
+
+        m_pos += bytes_to_consume;
+        m_remaining -= bytes_to_consume;
+    }
+
+    if (m_stopped) {
+        return {};
+    }
+
+    return res;
+}
+
+void ignite::tcp_client_channel::send_message(std::vector<std::byte> msg) {
+    ::send(m_cl_fd, msg.data(), msg.size(), 0);
+}
+
+void ignite::tcp_client_channel::receive_next_packet() {
+    int received = ::recv(m_cl_fd, m_buf, sizeof(m_buf), 0);
+
+    if (received == 0) {
+        std::cout << "connection was closed" << std::endl;

Review Comment:
   Let's use the logger in server instead of `std::cout`



##########
modules/platforms/cpp/ignite/client/detail/node_connection.cpp:
##########
@@ -203,7 +207,46 @@ std::shared_ptr<response_handler> 
node_connection::find_handler_unsafe(std::int6
     if (it == m_request_handlers.end())
         return {};
 
-    return it->second;
+    return it->second.handler;
+}
+
+void node_connection::handle_timeouts() {
+    auto now = std::chrono::steady_clock::now();
+
+    {
+        std::lock_guard lock(m_request_handlers_mutex);
+
+        for (auto it = m_request_handlers.begin(); it != 
m_request_handlers.end();) {
+            auto &id = it->first;
+            auto &req = it->second;
+
+            if (req.timeouts_at.has_value() && req.timeouts_at < now) {

Review Comment:
   Let's use explicit value access. This comparison is kind of C++17 feature 
and I don't like how it makes you think really hard for such an easy operation 
:).
   ```suggestion
               if (req.timeouts_at && *req.timeouts_at < now) {
   ```



##########
modules/platforms/cpp/tests/fake_server/fake_server.h:
##########
@@ -0,0 +1,98 @@
+/*
+* 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 "ignite/protocol/bitmask_feature.h"
+#include "ignite/protocol/client_operation.h"
+#include "ignite/protocol/reader.h"
+#include "ignite/protocol/writer.h"
+#include "response_action.h"
+#include "tcp_client_channel.h"
+
+#include <atomic>
+#include <thread>
+#include <unistd.h>
+
+using namespace ignite;
+
+using raw_msg = std::vector<std::byte>;
+
+class fake_server {
+public:
+    explicit fake_server(
+        int srv_port = 10800,
+        
std::function<std::unique_ptr<response_action>(protocol::client_operation)> 
op_type_handler = nullptr
+        )
+        : m_srv_port(srv_port)
+        , m_op_type_handler(op_type_handler)
+    {}
+
+    ~fake_server() {
+        m_stopped.store(true);
+
+        if (m_started)
+            m_client_channel->stop();
+
+        if (m_srv_fd > 0) {
+            ::close(m_srv_fd);
+        }
+
+        m_io_thread->join();
+    }
+
+    void start();
+
+private:
+    /** Server socket FD. */
+    int m_srv_fd = -1;
+    /** Flag is up when server initialization was complete. */
+    std::atomic_bool m_started{false};
+
+    /** Flag is up when server destruction initiated. */
+    std::atomic_bool m_stopped{false};
+
+    /** Port number server listens to. Default is 10800. */
+    const int m_srv_port;
+
+    /** Pointer to the thread which handles client requests. */
+    std::unique_ptr<std::thread> m_io_thread{};
+
+    /** Objects which owns client socket FD and handles reads and writes to 
it. */
+    std::unique_ptr<tcp_client_channel> m_client_channel{};
+
+    /**
+     * Allows developer to define custom action on requests according to their 
type.
+     */
+    
std::function<std::unique_ptr<response_action>(protocol::client_operation)> 
m_op_type_handler;

Review Comment:
   Methods go to the very bottom.



##########
modules/platforms/cpp/tests/fake_server/fake_server.h:
##########
@@ -0,0 +1,98 @@
+/*
+* 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 "ignite/protocol/bitmask_feature.h"
+#include "ignite/protocol/client_operation.h"
+#include "ignite/protocol/reader.h"
+#include "ignite/protocol/writer.h"
+#include "response_action.h"
+#include "tcp_client_channel.h"
+
+#include <atomic>
+#include <thread>
+#include <unistd.h>
+
+using namespace ignite;
+
+using raw_msg = std::vector<std::byte>;
+
+class fake_server {
+public:
+    explicit fake_server(
+        int srv_port = 10800,
+        
std::function<std::unique_ptr<response_action>(protocol::client_operation)> 
op_type_handler = nullptr
+        )
+        : m_srv_port(srv_port)
+        , m_op_type_handler(op_type_handler)
+    {}
+
+    ~fake_server() {
+        m_stopped.store(true);
+
+        if (m_started)
+            m_client_channel->stop();
+
+        if (m_srv_fd > 0) {
+            ::close(m_srv_fd);
+        }
+
+        m_io_thread->join();
+    }
+
+    void start();
+
+private:
+    /** Server socket FD. */
+    int m_srv_fd = -1;
+    /** Flag is up when server initialization was complete. */
+    std::atomic_bool m_started{false};
+
+    /** Flag is up when server destruction initiated. */
+    std::atomic_bool m_stopped{false};
+
+    /** Port number server listens to. Default is 10800. */
+    const int m_srv_port;
+
+    /** Pointer to the thread which handles client requests. */
+    std::unique_ptr<std::thread> m_io_thread{};
+
+    /** Objects which owns client socket FD and handles reads and writes to 
it. */
+    std::unique_ptr<tcp_client_channel> m_client_channel{};
+
+    /**
+     * Allows developer to define custom action on requests according to their 
type.
+     */
+    
std::function<std::unique_ptr<response_action>(protocol::client_operation)> 
m_op_type_handler;
+
+    void start_socket();
+
+    void bind_address_port() const;
+
+    void start_socket_listen() const;
+
+    void accept_client_connection();
+
+    void handle_client_handshake() const;
+
+    void send_server_handshake() const;
+
+    void write_response(std::vector<std::byte>& resp, int64_t req_id, 
std::function<void(protocol::writer &wr)> body);

Review Comment:
   Nitpick:
   ```suggestion
       void write_response(std::vector<std::byte>& resp, std::int64_t req_id, 
std::function<void(protocol::writer &wr)> body);
   ```



-- 
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