194 lines
7.6 KiB
C++
194 lines
7.6 KiB
C++
// 示例 IO_BATCH 插件(VDE 微内核 P4 / ABI 1.5;纯 C ABI,不链接内核)。
|
||
//
|
||
// mock 批量导入导出:不读写真实文件,按请求字节数返回 processed/failed JSON,
|
||
// 用于验证 ABI 1.5 VdeIoBatchPlugin 的注册/回调/JSON 输入输出/错误隔离。
|
||
#include <cstdint>
|
||
#include <cstdio>
|
||
#include <cstdlib>
|
||
#include <cstring>
|
||
#include <string>
|
||
|
||
#include "vde/plugin/vde_plugin_api.h"
|
||
|
||
namespace {
|
||
|
||
int fail(const VdePluginHost* host, int code, const char* message) {
|
||
if (host && host->set_error) host->set_error(message);
|
||
return code;
|
||
}
|
||
|
||
const char* find_string(const char* json, const char* key) {
|
||
if (!json) return nullptr;
|
||
const std::string pattern = "\"" + std::string(key) + "\"";
|
||
const char* pos = std::strstr(json, pattern.c_str());
|
||
return pos;
|
||
}
|
||
|
||
int run(const VdePluginHost* host, const char* options_json, const char* request_json,
|
||
size_t request_size, VdeBuffer* response_json, char* error_message,
|
||
size_t error_message_size) {
|
||
const auto set_error = [&](const char* text) {
|
||
if (host && host->set_error) host->set_error(text);
|
||
if (error_message && error_message_size > 0)
|
||
std::snprintf(error_message, error_message_size, "%s", text);
|
||
};
|
||
if (!host || !host->alloc || !host->free_buffer) {
|
||
set_error("io_batch: host services incomplete");
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
if (!request_json || request_size == 0) {
|
||
set_error("io_batch: request JSON is empty");
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
if (!response_json) {
|
||
set_error("io_batch: response buffer is null");
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
if (options_json && std::strstr(options_json, "\"flags\"")) {
|
||
const char* flags = std::strstr(options_json, "\"flags\"");
|
||
const char* colon = flags ? std::strchr(flags, ':') : nullptr;
|
||
if (colon && std::strtol(colon + 1, nullptr, 10) != 0) {
|
||
set_error("io_batch: options.flags must be 0");
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
}
|
||
const char* format = find_string(options_json, "format");
|
||
const std::uint32_t processed =
|
||
static_cast<std::uint32_t>(request_size / 16) + 1;
|
||
std::string json;
|
||
char buffer[384];
|
||
std::snprintf(buffer, sizeof(buffer),
|
||
"{\"batch_id\":\"io_batch.mock\",\"ok\":true,\"processed\":%u,"
|
||
"\"failed\":0,\"request_bytes\":%zu,\"format\":\"mock\"}",
|
||
processed, request_size);
|
||
json += buffer;
|
||
void* out = host->alloc(json.size() + 1);
|
||
if (!out) {
|
||
set_error("io_batch: cannot allocate response buffer");
|
||
return VDE_PLUGIN_ERR_BUFFER;
|
||
}
|
||
std::memcpy(out, json.c_str(), json.size() + 1);
|
||
response_json->data = out;
|
||
response_json->size = json.size();
|
||
(void)format;
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
|
||
// ABI 1.6:结果回填版 run。旧 run 保留用于旧宿主(1.5)回退。
|
||
int run_v2(const VdePluginHost* host, const char* options_json, const char* request_json,
|
||
size_t request_size, VdeIoBatchResult* result, VdeBuffer* response_json,
|
||
char* error_message, size_t error_message_size) {
|
||
const auto set_error = [&](const char* text) {
|
||
if (host && host->set_error) host->set_error(text);
|
||
if (error_message && error_message_size > 0)
|
||
std::snprintf(error_message, error_message_size, "%s", text);
|
||
};
|
||
if (response_json) {
|
||
response_json->data = nullptr;
|
||
response_json->size = 0;
|
||
}
|
||
if (!host || !host->alloc || !host->free_buffer) {
|
||
set_error("io_batch: host services incomplete");
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
if (!result || result->struct_size < sizeof(VdeIoBatchResult) || !response_json) {
|
||
set_error("io_batch: result/response buffer invalid");
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
if (!request_json || request_size == 0) {
|
||
set_error("io_batch: request JSON is empty");
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
if (options_json && std::strstr(options_json, "\"flags\"")) {
|
||
const char* flags = std::strstr(options_json, "\"flags\"");
|
||
const char* colon = flags ? std::strchr(flags, ':') : nullptr;
|
||
if (colon && std::strtol(colon + 1, nullptr, 10) != 0) {
|
||
set_error("io_batch: options.flags must be 0");
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
}
|
||
const bool partial = (std::strstr(request_json, "\"partial\":true") != nullptr) ||
|
||
(options_json && std::strstr(options_json, "\"partial\":true"));
|
||
const std::uint32_t processed =
|
||
static_cast<std::uint32_t>(request_size / 16) + 1;
|
||
const std::uint32_t failed = partial ? 1u : 0u;
|
||
result->status = partial ? static_cast<std::uint32_t>(VDE_IO_BATCH_ERR_PARTIAL)
|
||
: static_cast<std::uint32_t>(VDE_IO_BATCH_OK);
|
||
result->processed = processed;
|
||
result->failed = failed;
|
||
std::string json;
|
||
char buffer[448];
|
||
std::snprintf(buffer, sizeof(buffer),
|
||
"{\"batch_id\":\"io_batch.mock\",\"api\":\"run_v2\","
|
||
"\"status\":%d,\"ok\":%s,\"processed\":%u,\"failed\":%u,"
|
||
"\"request_bytes\":%zu,\"format\":\"mock\"}",
|
||
static_cast<int>(result->status), partial ? "false" : "true", processed,
|
||
failed, request_size);
|
||
json += buffer;
|
||
void* out = host->alloc(json.size() + 1);
|
||
if (!out) {
|
||
set_error("io_batch: cannot allocate response buffer");
|
||
return VDE_PLUGIN_ERR_BUFFER;
|
||
}
|
||
std::memcpy(out, json.c_str(), json.size() + 1);
|
||
response_json->data = out;
|
||
response_json->size = json.size();
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
const char* describe() {
|
||
return "VDE sample io_batch (mock copy; ABI 1.6)";
|
||
}
|
||
|
||
const VdeIoBatchPlugin kIoBatch = [] {
|
||
VdeIoBatchPlugin plugin{};
|
||
plugin.struct_size = sizeof(VdeIoBatchPlugin);
|
||
plugin.batch_id = "io_batch.mock";
|
||
plugin.capabilities_json = "{\"formats\":[\"mock\"],\"directions\":[\"import\",\"export\"]}";
|
||
plugin.run = &run;
|
||
plugin.run_v2 = &run_v2;
|
||
plugin.describe = &describe;
|
||
return plugin;
|
||
}();
|
||
|
||
} // namespace
|
||
|
||
extern "C" {
|
||
|
||
VDE_PLUGIN_EXPORT int vde_plugin_query(std::uint32_t host_abi_major, VdePluginInfo* info) {
|
||
if (!info || info->struct_size < sizeof(VdePluginInfo)) return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
if (host_abi_major != VDE_PLUGIN_ABI_MAJOR) return VDE_PLUGIN_ERR_ABI;
|
||
info->id = "com.vde.sample.io_batch";
|
||
info->name = "VDE Sample IO Batch (ABI 1.6)";
|
||
info->version = "0.1.0";
|
||
info->kind = VDE_PLUGIN_KIND_IO_BATCH;
|
||
info->abi_major = VDE_PLUGIN_ABI_MAJOR;
|
||
info->abi_minor = VDE_PLUGIN_ABI_MINOR;
|
||
info->capabilities = "io_batch:mock";
|
||
info->license = "Apache-2.0";
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
VDE_PLUGIN_EXPORT int vde_plugin_init(const VdePluginHost* host, VdePluginRegistration* reg) {
|
||
if (!host || !reg) return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
const std::size_t io_batch_field_end =
|
||
offsetof(VdePluginRegistration, io_batch) + sizeof(reg->io_batch);
|
||
if (reg->struct_size < io_batch_field_end) return VDE_PLUGIN_ERR_ABI;
|
||
if (!host->alloc || !host->free_buffer || !host->set_error)
|
||
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "io_batch: host services incomplete");
|
||
reg->kind = VDE_PLUGIN_KIND_IO_BATCH;
|
||
reg->format = nullptr;
|
||
reg->generic = nullptr;
|
||
reg->cae_solver = nullptr;
|
||
reg->cam_strategy = nullptr;
|
||
reg->gcs_solver = nullptr;
|
||
reg->mesher = nullptr;
|
||
reg->optimizer = nullptr;
|
||
reg->io_batch = &kIoBatch;
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {}
|
||
|
||
} // extern "C"
|