mirror of
https://github.com/logos-storage/logos-storage-app-skeleton.git
synced 2026-06-13 20:09:28 +00:00
complete downloader, print spr on uploader
This commit is contained in:
parent
1bfbb42774
commit
cb6aec92c1
@ -2,8 +2,26 @@
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QDir>
|
||||
#include <QEventLoop>
|
||||
#include <QTimer>
|
||||
#include <iostream>
|
||||
|
||||
#define DEFAULT_TIMEOUT 10000
|
||||
|
||||
int app_main(LogosModules* modules, int argc, char* argv[]);
|
||||
|
||||
bool await(QEventLoop* loop, int timeoutMs) {
|
||||
QTimer::singleShot(timeoutMs, loop, [loop]() {
|
||||
std::cerr << "Call timed out." << std::endl;
|
||||
loop->exit(1);
|
||||
});
|
||||
return loop->exec() == 0;
|
||||
}
|
||||
|
||||
void notify(QEventLoop* loop, bool successValue) {
|
||||
loop->exit(successValue ? 0 : 1);
|
||||
}
|
||||
|
||||
int app_main(LogosModules* modules, int argc, char* argv[]);
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
@ -14,10 +32,10 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
QCoreApplication app(argc, argv);
|
||||
QCoreApplication::setOrganizationName("Logos");
|
||||
QCoreApplication::setApplicationName("LogosStorageCLI");
|
||||
QCoreApplication::setApplicationName("LogosDownloader");
|
||||
|
||||
Logos logos(QCoreApplication::applicationDirPath() + "/../modules");
|
||||
if (!logos.init()) {
|
||||
if (!logos.init("LogosDownloader")) {
|
||||
std::cerr << "Failed to initialize Logos" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
@ -36,7 +54,9 @@ int app_main(LogosModules* modules, int argc, char* argv[]) {
|
||||
const QString jsonConfig = "{"
|
||||
"\"listen-addrs\": [\"/ip4/0.0.0.0/tcp/8001\"],"
|
||||
"\"disc-port\": 9001,"
|
||||
"\"nat\": \"none\""
|
||||
"\"nat\": \"none\","
|
||||
"\"data-dir\": \"./downloader-data\","
|
||||
"\"bootstrap-node\": [\"" + QString(argv[1]) + "\"]"
|
||||
"}";
|
||||
|
||||
if (!modules->storage_module.init(jsonConfig)) {
|
||||
@ -44,5 +64,50 @@ int app_main(LogosModules* modules, int argc, char* argv[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
QUrl url = QUrl::fromLocalFile(argv[3]);
|
||||
QString cid = argv[2];
|
||||
|
||||
if (!modules->storage_module.init(jsonConfig)) {
|
||||
std::cerr << "Failed to initialize storage module" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
{
|
||||
QEventLoop loop;
|
||||
|
||||
modules->storage_module.on("storageStart", [&loop](const QVariantList& data) {
|
||||
bool success = data[0].toBool();
|
||||
if (!success) {
|
||||
std::cerr << "Failed to start storage module: " << data[1].toString().toStdString() << std::endl;
|
||||
}
|
||||
notify(&loop, success);
|
||||
});
|
||||
|
||||
modules->storage_module.start();
|
||||
|
||||
if (!await(&loop, DEFAULT_TIMEOUT)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
QEventLoop loop;
|
||||
|
||||
modules->storage_module.on("storageDownloadDone", [&loop](const QVariantList& data) {
|
||||
bool success = data[0].toBool();
|
||||
if (!success) {
|
||||
std::cerr << "Failed to download file: " << data[2].toString().toStdString() << std::endl;
|
||||
}
|
||||
notify(&loop, success);
|
||||
});
|
||||
|
||||
modules->storage_module.downloadToUrl(cid, url);
|
||||
|
||||
if (!await(&loop, DEFAULT_TIMEOUT)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << "Download completed successfully." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
@ -22,7 +22,7 @@ Logos::~Logos()
|
||||
}
|
||||
}
|
||||
|
||||
bool Logos::init()
|
||||
bool Logos::init(const QString& name)
|
||||
{
|
||||
std::cout << "Setting plugins directory to: " << m_pluginsDir.toStdString() << std::endl;
|
||||
logos_core_set_plugins_dir(m_pluginsDir.toUtf8().constData());
|
||||
@ -37,7 +37,7 @@ bool Logos::init()
|
||||
}
|
||||
std::cout << "Successfully loaded storage_module plugin" << std::endl;
|
||||
|
||||
m_api = new LogosAPI("cli");
|
||||
m_api = new LogosAPI(name);
|
||||
m_modules = new LogosModules(m_api);
|
||||
m_initialized = true;
|
||||
return true;
|
||||
|
||||
@ -24,7 +24,7 @@ public:
|
||||
|
||||
// Initialize the Logos Core: set plugins directory, start, and load required plugins.
|
||||
// Returns true on success.
|
||||
bool init();
|
||||
bool init(const QString& name);
|
||||
|
||||
// Cleanup the Logos Core. Called automatically in destructor.
|
||||
void cleanup();
|
||||
|
||||
@ -30,10 +30,10 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
QCoreApplication app(argc, argv);
|
||||
QCoreApplication::setOrganizationName("Logos");
|
||||
QCoreApplication::setApplicationName("LogosStorageCLI");
|
||||
QCoreApplication::setApplicationName("LogosUploader");
|
||||
|
||||
Logos logos(QCoreApplication::applicationDirPath() + "/../modules");
|
||||
if (!logos.init()) {
|
||||
if (!logos.init("LogosUploader")) {
|
||||
std::cerr << "Failed to initialize Logos" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
@ -53,6 +53,7 @@ int app_main(LogosModules* modules, int argc, char* argv[]) {
|
||||
const QString jsonConfig = "{"
|
||||
"\"listen-addrs\": [\"/ip4/0.0.0.0/tcp/8000\"],"
|
||||
"\"disc-port\": 9000,"
|
||||
"\"data-dir\": \"./uploader-data\","
|
||||
"\"nat\": \"none\""
|
||||
"}";
|
||||
|
||||
@ -78,6 +79,14 @@ int app_main(LogosModules* modules, int argc, char* argv[]) {
|
||||
}
|
||||
}
|
||||
|
||||
LogosResult spr = modules->storage_module.spr();
|
||||
if (!spr.success) {
|
||||
std::cerr << "Failed to get SPR: " << spr.getValue<QString>().toStdString() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cerr << "SPR: " << spr.getValue<QString>().toStdString() << std::endl;
|
||||
|
||||
{
|
||||
QEventLoop loop;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user