Browse Source

实现了动态更新 init 和 start 配置的功能

jcsyshc 2 years ago
parent
commit
3eb4fa228a

+ 24 - 0
src/core/external_controller.cpp

@@ -2,6 +2,7 @@
 #include "core/global_defs.h"
 #include "core/sophiar_manager.h"
 #include "core/tristate_obj.h"
+#include "third_party/base64.h"
 #include "utility/config_utility.hpp"
 #include "utility/coro_worker.hpp"
 #include "utility/name_translator.hpp"
@@ -93,6 +94,14 @@ namespace sophiar {
         static uint8_t query_object_state(std::string_view obj_name) {
             return global_sophiar_manager->query_object_state(obj_name);
         }
+
+        static bool patch_init_config(std::string_view obj_name, const nlohmann::json &patch) {
+            return global_sophiar_manager->patch_init_config(obj_name, patch);
+        }
+
+        static bool patch_start_config(std::string_view obj_name, const nlohmann::json &patch) {
+            return global_sophiar_manager->patch_start_config(obj_name, patch);
+        }
     };
 
     struct external_controller::impl::client_instance {
@@ -110,6 +119,13 @@ namespace sophiar {
             s.set_option(tcp::no_delay(true)); // decrease latency
         }
 
+        static nlohmann::json decode_b64_json(std::string_view buf) {
+            auto json_length = macaron::Base64::Decode(buf, nullptr);
+            auto json_buf = dynamic_memory::new_instance(json_length);
+            macaron::Base64::Decode(buf, json_buf->data());
+            return nlohmann::json::parse(json_buf->data(), json_buf->data() + json_length);
+        }
+
         awaitable<bool> work_once() {
             // receive command
             auto length = co_await async_read_value<sophiar_endian, header_type>(s);
@@ -152,6 +168,14 @@ namespace sophiar {
                     auto state = query_object_state(obj);
                     writer << state_name_translator->translate(state) << ',';
                 }
+            } else if (cmd == "INITCONF") {
+                assert(params.size() == 2);
+                auto ok = patch_init_config(params[0], decode_b64_json(params[1]));
+                writer << (ok ? "True" : "False") << ',';
+            } else if (cmd == "STARTCONF") {
+                assert(params.size() == 2);
+                auto ok = patch_start_config(params[0], decode_b64_json(params[1]));
+                writer << (ok ? "True" : "False") << ',';
             } else {
                 writer << "Unknown command.";
             }

+ 1 - 0
src/core/external_variable_io.cpp

@@ -202,6 +202,7 @@ namespace sophiar {
                   buf(dynamic_memory::new_instance()),
                   signal_group(coro_signal_any_group::new_instance()),
                   watcher(signal_group->new_watcher()) {
+            s.set_option(tcp::no_delay{true});
             for (auto &info: infos) {
                 signal_group->add_watcher(REQUIRE_VARIABLE_WATCHER(info.var_index));
                 info_pool.push_back(

+ 21 - 0
src/core/sophiar_manager.cpp

@@ -444,6 +444,27 @@ namespace sophiar {
         return (uint8_t) pimpl->query_object_state(obj_index);
     }
 
+    bool sophiar_manager::patch_init_config(std::string_view obj_name, const nlohmann::json &patch) {
+        if (query_object_state(obj_name) !=
+            (uint8_t) tristate_obj::state_type::INITIAL) {
+            return false;
+        }
+        auto &obj_info = pimpl->obj_pool[obj_name];
+        obj_info.init_config.merge_patch(patch);
+        return true;
+    }
+
+    bool sophiar_manager::patch_start_config(std::string_view obj_name, const nlohmann::json &patch) {
+        auto obj_state = (tristate_obj::state_type) query_object_state(obj_name);
+        if (!tristate_obj::is_state_stable(obj_state) ||
+            obj_state == tristate_obj::state_type::RUNNING) {
+            return false;
+        }
+        auto &obj_info = pimpl->obj_pool[obj_name];
+        obj_info.start_config.merge_patch(patch);
+        return true;
+    }
+
     sophiar_manager::~sophiar_manager() = default;
 
 }

+ 3 - 0
src/core/sophiar_manager.h

@@ -83,6 +83,9 @@ namespace sophiar {
 
         uint8_t query_object_state(std::string_view obj_name);
 
+        bool patch_init_config(std::string_view obj_name, const nlohmann::json &patch);
+
+        bool patch_start_config(std::string_view obj_name, const nlohmann::json &patch);
     };
 
 #define REGISTER_TYPE(DerivedT) \

+ 123 - 0
src/third_party/base64.h

@@ -0,0 +1,123 @@
+#ifndef _MACARON_BASE64_H_
+#define _MACARON_BASE64_H_
+
+/**
+ * The MIT License (MIT)
+ * Copyright (c) 2016 tomykaira
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <string_view>
+
+namespace macaron {
+
+    class Base64 {
+    public:
+
+        static size_t Encode(std::string_view data, char *p) {
+            static constexpr char sEncodingTable[] = {
+                    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
+                    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+                    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
+                    'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
+                    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+                    'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+                    'w', 'x', 'y', 'z', '0', '1', '2', '3',
+                    '4', '5', '6', '7', '8', '9', '+', '/'
+            };
+
+            size_t in_len = data.size();
+            size_t out_len = 4 * ((in_len + 2) / 3);
+
+            if (p == nullptr) return out_len;
+
+            size_t i;
+            for (i = 0; i < in_len - 2; i += 3) {
+                *p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
+                *p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
+                *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | ((int) (data[i + 2] & 0xC0) >> 6)];
+                *p++ = sEncodingTable[data[i + 2] & 0x3F];
+            }
+            if (i < in_len) {
+                *p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
+                if (i == (in_len - 1)) {
+                    *p++ = sEncodingTable[((data[i] & 0x3) << 4)];
+                    *p++ = '=';
+                } else {
+                    *p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
+                    *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)];
+                }
+                *p++ = '=';
+            }
+
+            return out_len;
+        }
+
+        static size_t Decode(std::string_view input, char *out) {
+            static constexpr unsigned char kDecodingTable[] = {
+                    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+                    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+                    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
+                    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
+                    64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
+                    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
+                    64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
+                    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
+                    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+                    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+                    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+                    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+                    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+                    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+                    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+                    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
+            };
+
+            size_t in_len = input.size();
+            if (in_len % 4 != 0) return 0;
+
+            size_t out_len = in_len / 4 * 3;
+            if (input[in_len - 1] == '=') out_len--;
+            if (input[in_len - 2] == '=') out_len--;
+
+            if (out == nullptr) return out_len;
+
+            for (size_t i = 0, j = 0; i < in_len;) {
+                uint32_t a = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
+                uint32_t b = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
+                uint32_t c = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
+                uint32_t d = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
+
+                uint32_t triple = (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6);
+
+                if (j < out_len) out[j++] = (triple >> 2 * 8) & 0xFF;
+                if (j < out_len) out[j++] = (triple >> 1 * 8) & 0xFF;
+                if (j < out_len) out[j++] = (triple >> 0 * 8) & 0xFF;
+            }
+
+            return out_len;
+        }
+
+    };
+
+}
+
+#endif /* _MACARON_BASE64_H_ */