File size: 16,762 Bytes
3374e90 | 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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | // BEX C++ CLI Tool β Pure C ABI + Callback Architecture
//
// Full-featured CLI demonstrating the pure C FFI with async callback pattern.
// No bridge crate dependency β just bex_engine.h and the Rust static/shared library.
//
// Features:
// - Plugin management: install, uninstall, list, info, enable, disable
// - API key management: set-key, get-key, delete-key, list-keys
// - Media browsing: home, search, info, servers, stream
// - Async operations use std::promise/future for blocking wait
//
// Build with CMake:
// mkdir build && cd build
// cmake .. -DCMAKE_BUILD_TYPE=Release
// make -j$(nproc)
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <future>
#include <memory>
#include <sstream>
#include <iomanip>
#include <stdexcept>
#include "bex_engine.h"
// ββ Callback handler for async operations ββββββββββββββββββββββββββββββ
/// The C-callback triggered by Rust's Tokio thread when an async task finishes.
/// It casts user_data back to a std::promise and fulfills it.
extern "C" void on_bex_result(void* user_data, uint64_t req_id,
bool success, const uint8_t* payload, size_t len) {
auto* promise = static_cast<std::promise<std::string>*>(user_data);
if (success) {
std::string result(reinterpret_cast<const char*>(payload), len);
promise->set_value(std::move(result));
} else {
std::string error_msg(reinterpret_cast<const char*>(payload), len);
promise->set_exception(std::make_exception_ptr(std::runtime_error(error_msg)));
}
}
/// Submit an async request and block until the result arrives.
/// Uses std::promise/future to bridge the async callback to sync CLI.
std::string await_request(BexEngine* engine, uint64_t request_id,
std::promise<std::string>& promise, uint32_t timeout_ms = 30000) {
auto future = promise.get_future();
auto status = future.wait_for(std::chrono::milliseconds(timeout_ms));
if (status == std::future_status::timeout) {
bex_cancel_request(engine, request_id);
throw std::runtime_error("Request timed out after " + std::to_string(timeout_ms) + "ms");
}
return future.get();
}
// ββ Pretty-print JSON ββββββββββββββββββββββββββββββββββββββββββββββββββ
void print_json(const std::string& json_str) {
bool already_pretty = json_str.find('\n') != std::string::npos;
if (already_pretty) {
std::cout << json_str << std::endl;
return;
}
int indent = 0;
bool in_string = false;
bool escape = false;
for (size_t i = 0; i < json_str.size(); i++) {
char c = json_str[i];
if (escape) {
std::cout << c;
escape = false;
continue;
}
if (c == '\\' && in_string) {
std::cout << c;
escape = true;
continue;
}
if (c == '"') {
in_string = !in_string;
std::cout << c;
continue;
}
if (in_string) {
std::cout << c;
continue;
}
switch (c) {
case '{':
case '[':
std::cout << c << "\n";
indent += 2;
std::cout << std::string(indent, ' ');
break;
case '}':
case ']':
std::cout << "\n";
indent -= 2;
std::cout << std::string(indent, ' ') << c;
break;
case ',':
std::cout << c << "\n" << std::string(indent, ' ');
break;
case ':':
std::cout << c << " ";
break;
case ' ':
case '\n':
case '\r':
case '\t':
break;
default:
std::cout << c;
}
}
std::cout << std::endl;
}
/// Decode capability bits into a string
std::string capabilities_str(uint32_t caps) {
std::string result;
if (caps & (1 << 0)) result += "HOME ";
if (caps & (1 << 1)) result += "CATEGORY ";
if (caps & (1 << 2)) result += "SEARCH ";
if (caps & (1 << 3)) result += "INFO ";
if (caps & (1 << 4)) result += "SERVERS ";
if (caps & (1 << 5)) result += "STREAM ";
if (caps & (1 << 6)) result += "SUBTITLES ";
if (caps & (1 << 7)) result += "ARTICLES ";
if (!result.empty()) result.pop_back();
return result;
}
/// RAII wrapper for BexPluginInfoList
struct PluginListGuard {
BexPluginInfoList list;
explicit PluginListGuard(BexPluginInfoList l) : list(l) {}
~PluginListGuard() { bex_plugin_info_list_free(list); }
BexPluginInfo* begin() { return list.items; }
BexPluginInfo* end() { return list.items + list.count; }
size_t size() const { return list.count; }
};
/// RAII wrapper for C strings returned by the engine
struct CStrGuard {
char* ptr;
explicit CStrGuard(char* p) : ptr(p) {}
~CStrGuard() { if (ptr) bex_string_free(ptr); }
std::string str() const { return ptr ? std::string(ptr) : std::string(); }
};
// ββ Usage ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
void print_usage(const char* prog) {
std::cerr
<< "BEX C++ CLI v4.0 β WASM Plugin Engine (Pure C ABI + Callbacks)\n"
<< "\n"
<< "Usage:\n"
<< " " << prog << " install <path> Install a .bex plugin package\n"
<< " " << prog << " uninstall <id> Uninstall a plugin by ID\n"
<< " " << prog << " list List installed plugins\n"
<< " " << prog << " info-plugin <id> Show detailed plugin information\n"
<< " " << prog << " enable <id> Enable a plugin\n"
<< " " << prog << " disable <id> Disable a plugin\n"
<< "\n"
<< " API Key / Secret Management:\n"
<< " " << prog << " set-key <plugin_id> <key> <val> Set an API key for a plugin\n"
<< " " << prog << " get-key <plugin_id> <key> Get an API key value\n"
<< " " << prog << " delete-key <plugin_id> <key> Delete an API key\n"
<< " " << prog << " list-keys <plugin_id> List all keys for a plugin\n"
<< "\n"
<< " Media Browsing (async with callbacks):\n"
<< " " << prog << " home <plugin_id> Get home sections\n"
<< " " << prog << " search <plugin_id> <query> Search media\n"
<< " " << prog << " info <plugin_id> <id> Get media info\n"
<< " " << prog << " servers <plugin_id> <id> Get servers (id is self-describing)\n"
<< " " << prog << " stream <plugin_id> <json> Resolve stream from server JSON\n"
<< "\n"
<< " Debug:\n"
<< " " << prog << " stats Show engine stats\n"
<< "\n"
<< "Design: IDs are self-describing. The plugin knows how to parse its own IDs.\n"
<< "Example: bexcli servers com.gogoanime 'one-piece$ep=1$sub=1$dub=0'\n"
<< std::endl;
}
// ββ Main βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
int main(int argc, char* argv[]) {
if (argc < 2) {
print_usage(argv[0]);
return 1;
}
std::string data_dir = std::string(getenv("HOME") ? getenv("HOME") : ".") + "/.bex-data";
if (getenv("BEX_DATA_DIR")) {
data_dir = getenv("BEX_DATA_DIR");
}
std::string cmd = argv[1];
// Create the engine
BexEngine* engine = bex_engine_new(data_dir.c_str());
if (!engine) {
std::cerr << "Error: Failed to create BexEngine" << std::endl;
return 1;
}
try {
// ββ Plugin Management ββββββββββββββββββββββββββββββββββββββ
if (cmd == "install" && argc >= 3) {
int rc = bex_engine_install(engine, argv[2]);
if (rc != 0) {
CStrGuard err(bex_engine_last_error(engine));
throw std::runtime_error("Install failed: " + err.str());
}
std::cout << "Plugin installed from: " << argv[2] << std::endl;
}
else if (cmd == "uninstall" && argc >= 3) {
int rc = bex_engine_uninstall(engine, argv[2]);
if (rc != 0) {
CStrGuard err(bex_engine_last_error(engine));
throw std::runtime_error("Uninstall failed: " + err.str());
}
std::cout << "Plugin uninstalled: " << argv[2] << std::endl;
}
else if (cmd == "list") {
PluginListGuard list(bex_engine_list_plugins(engine));
if (list.size() == 0) {
std::cout << "No plugins installed." << std::endl;
} else {
std::cout << std::left
<< std::setw(40) << "ID"
<< std::setw(20) << "NAME"
<< std::setw(10) << "VERSION"
<< std::setw(10) << "STATUS"
<< "CAPABILITIES" << std::endl;
std::cout << std::string(100, '-') << std::endl;
for (size_t i = 0; i < list.size(); i++) {
auto& p = list.list.items[i];
std::cout << std::left
<< std::setw(40) << (p.id ? p.id : "")
<< std::setw(20) << (p.name ? p.name : "")
<< std::setw(10) << (p.version ? p.version : "")
<< std::setw(10) << (p.enabled ? "enabled" : "disabled")
<< capabilities_str(p.capabilities) << std::endl;
}
}
}
else if (cmd == "info-plugin" && argc >= 3) {
BexPluginInfo info;
int rc = bex_engine_plugin_info(engine, argv[2], &info);
if (rc != 0) {
CStrGuard err(bex_engine_last_error(engine));
throw std::runtime_error("Plugin info failed: " + err.str());
}
std::cout << "ID: " << (info.id ? info.id : "") << std::endl;
std::cout << "Name: " << (info.name ? info.name : "") << std::endl;
std::cout << "Version: " << (info.version ? info.version : "") << std::endl;
std::cout << "Enabled: " << (info.enabled ? "yes" : "no") << std::endl;
std::cout << "Capabilities: " << capabilities_str(info.capabilities) << std::endl;
// Show API keys for this plugin
CStrGuard keys(bex_engine_secret_keys(engine, argv[2]));
if (keys.ptr && strlen(keys.ptr) > 0) {
std::cout << "API Keys: " << keys.str() << std::endl;
} else {
std::cout << "API Keys: (none)" << std::endl;
}
bex_plugin_info_free(info);
}
else if (cmd == "enable" && argc >= 3) {
int rc = bex_engine_enable(engine, argv[2]);
if (rc != 0) {
CStrGuard err(bex_engine_last_error(engine));
throw std::runtime_error("Enable failed: " + err.str());
}
std::cout << "Enabled: " << argv[2] << std::endl;
}
else if (cmd == "disable" && argc >= 3) {
int rc = bex_engine_disable(engine, argv[2]);
if (rc != 0) {
CStrGuard err(bex_engine_last_error(engine));
throw std::runtime_error("Disable failed: " + err.str());
}
std::cout << "Disabled: " << argv[2] << std::endl;
}
// ββ API Key / Secret Management βββββββββββββββββββββββββββ
else if (cmd == "set-key" && argc >= 5) {
int rc = bex_engine_secret_set(engine, argv[2], argv[3], argv[4]);
if (rc != 0) {
CStrGuard err(bex_engine_last_error(engine));
throw std::runtime_error("Set key failed: " + err.str());
}
std::cout << "Key '" << argv[3] << "' set for plugin '" << argv[2] << "'" << std::endl;
}
else if (cmd == "get-key" && argc >= 4) {
char buf[4096];
size_t buf_len = sizeof(buf);
int rc = bex_engine_secret_get(engine, argv[2], argv[3], buf, &buf_len);
if (rc == 0) {
std::cout << buf << std::endl;
} else {
std::cout << "Key '" << argv[3] << "' not found for plugin '" << argv[2] << "'" << std::endl;
}
}
else if (cmd == "delete-key" && argc >= 4) {
int rc = bex_engine_secret_delete(engine, argv[2], argv[3]);
if (rc == 0) {
std::cout << "Key '" << argv[3] << "' deleted from plugin '" << argv[2] << "'" << std::endl;
} else if (rc == 1) {
std::cout << "Key '" << argv[3] << "' not found for plugin '" << argv[2] << "'" << std::endl;
} else {
CStrGuard err(bex_engine_last_error(engine));
throw std::runtime_error("Delete key failed: " + err.str());
}
}
else if (cmd == "list-keys" && argc >= 3) {
CStrGuard keys(bex_engine_secret_keys(engine, argv[2]));
if (keys.ptr && strlen(keys.ptr) > 0) {
std::cout << "Keys for plugin '" << argv[2] << "': " << keys.str() << std::endl;
} else {
std::cout << "No keys found for plugin '" << argv[2] << "'" << std::endl;
}
}
// ββ Media Browsing (async with callbacks) ββββββββββββββββ
else if (cmd == "home" && argc >= 3) {
std::promise<std::string> promise;
uint64_t req_id = bex_submit_home(engine, argv[2], on_bex_result, &promise);
std::string result = await_request(engine, req_id, promise);
print_json(result);
}
else if (cmd == "search" && argc >= 4) {
std::promise<std::string> promise;
uint64_t req_id = bex_submit_search(engine, argv[2], argv[3], on_bex_result, &promise);
std::string result = await_request(engine, req_id, promise);
print_json(result);
}
else if (cmd == "info" && argc >= 4) {
std::promise<std::string> promise;
uint64_t req_id = bex_submit_info(engine, argv[2], argv[3], on_bex_result, &promise);
std::string result = await_request(engine, req_id, promise);
print_json(result);
}
else if (cmd == "servers" && argc >= 4) {
// The ID is self-describing β the plugin knows how to parse its own IDs.
// Example: bexcli servers com.gogoanime 'one-piece$ep=1$sub=1$dub=0'
std::promise<std::string> promise;
uint64_t req_id = bex_submit_servers(engine, argv[2], argv[3], on_bex_result, &promise);
std::string result = await_request(engine, req_id, promise);
print_json(result);
}
else if (cmd == "stream" && argc >= 4) {
std::promise<std::string> promise;
uint64_t req_id = bex_submit_stream(engine, argv[2], argv[3], on_bex_result, &promise);
std::string result = await_request(engine, req_id, promise);
print_json(result);
}
// ββ Debug βββββββββββββββββββββββββββββββββββββββββββββββββ
else if (cmd == "stats") {
CStrGuard stats(bex_engine_stats(engine));
if (stats.ptr) {
print_json(stats.str());
} else {
std::cout << "Unable to get stats" << std::endl;
}
}
else {
print_usage(argv[0]);
bex_engine_free(engine);
return 1;
}
bex_engine_free(engine);
}
catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
bex_engine_free(engine);
return 1;
}
return 0;
}
|