log0.2
This commit is contained in:
104
main.cpp
104
main.cpp
@@ -1,6 +1,7 @@
|
||||
#include <QCoreApplication>
|
||||
#include <QCommandLineParser>
|
||||
#include <QDBusConnection>
|
||||
#include <QDBusError>
|
||||
#include <signal.h>
|
||||
#include "src/utils/logs/logging.h"
|
||||
#include "src/core/CoreService.h"
|
||||
@@ -30,12 +31,21 @@ int main(int argc, char *argv[])
|
||||
parser.setApplicationDescription(QStringLiteral("Soft Bus Daemon"));
|
||||
parser.addHelpOption();
|
||||
parser.addVersionOption();
|
||||
QCommandLineOption roleOption(QStringLiteral("role"), QStringLiteral("Daemon role (CoreRouter, CollectorSerial, CollectorCan, Collector, SqlServer)"));
|
||||
QCommandLineOption roleOption(QStringLiteral("role"),
|
||||
QStringLiteral("Daemon role (CoreRouter, CollectorSerial, CollectorCan, Collector, SqlServer)"),
|
||||
QStringLiteral("role"),
|
||||
QStringLiteral("CoreRouter"));
|
||||
parser.addOption(roleOption);
|
||||
QCommandLineOption dbusBusOption(QStringLiteral("dbus-bus"),
|
||||
QStringLiteral("DBus bus to use: system|session|auto (default: auto)"),
|
||||
QStringLiteral("bus"),
|
||||
QStringLiteral("auto"));
|
||||
parser.addOption(dbusBusOption);
|
||||
parser.process(app);
|
||||
|
||||
|
||||
const QString roleString = parser.value(roleOption).trimmed();
|
||||
const QString dbusBus = parser.value(dbusBusOption).trimmed();
|
||||
CoreService::Role role = CoreService::Role::CoreRouter; // 默认角色
|
||||
if (roleString == QStringLiteral("CoreRouter")) {
|
||||
role = CoreService::Role::CoreRouter;
|
||||
@@ -53,56 +63,63 @@ int main(int argc, char *argv[])
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// 创建并注册 D-Bus 服务(仅在 core_router 角色中启用)
|
||||
CoreService* coreService = CoreService::instance();
|
||||
if (!coreService->initialize()) {
|
||||
LOG_ERROR() << "CoreService initialize failed";
|
||||
return 1;
|
||||
}
|
||||
QObject::connect(&app, &QCoreApplication::aboutToQuit, coreService, &CoreService::shutdown);
|
||||
|
||||
// ========================================================================
|
||||
// 创建并注册 D-Bus 服务(仅在 CoreRouter 角色中启用)
|
||||
// ========================================================================
|
||||
if (role == CoreService::Role::CoreRouter) {
|
||||
// D-Bus 是 Linux 系统的进程间通信机制,允许外部客户端访问守护进程
|
||||
// I 实现了具体的 D-Bus 接口,提供状态查询、硬件操作等功能
|
||||
|
||||
// 创建 DaemonService 实例,传入 CoreService 以便访问核心功能
|
||||
// 父对象设置为 app,确保在应用程序退出时自动销毁
|
||||
g_daemonService = new DaemonService(coreService, &app);
|
||||
|
||||
// 连接系统 D-Bus(systemBus)而不是会话 D-Bus(sessionBus)
|
||||
// 系统 D-Bus 是系统级服务,可以在所有用户会话中访问
|
||||
// 会话 D-Bus 只能在同一用户会话中访问
|
||||
QDBusConnection connection = QDBusConnection::systemBus();
|
||||
const QString serviceName = QStringLiteral("com.softbus.Daemon");
|
||||
const QString objectPath = QStringLiteral("/com/softbus/Daemon");
|
||||
|
||||
IpcDBus* ipc = new IpcDBus(coreService, roleString, &app);
|
||||
|
||||
auto pickConnection = [&]() -> QDBusConnection {
|
||||
if (dbusBus == QStringLiteral("system")) {
|
||||
return QDBusConnection::systemBus();
|
||||
}
|
||||
if (dbusBus == QStringLiteral("session")) {
|
||||
return QDBusConnection::sessionBus();
|
||||
}
|
||||
// auto: 优先 system(更符合 daemon),失败则退回 session(方便本地开发)
|
||||
QDBusConnection conn = QDBusConnection::systemBus();
|
||||
if (conn.isConnected()) {
|
||||
return conn;
|
||||
}
|
||||
return QDBusConnection::sessionBus();
|
||||
};
|
||||
|
||||
QDBusConnection connection = pickConnection();
|
||||
if (!connection.isConnected()) {
|
||||
LOG_ERROR() << "Cannot connect to system D-Bus";
|
||||
LOG_ERROR() << "Please ensure D-Bus is running and proper permissions are configured";
|
||||
return 1; // 无法连接 D-Bus,守护进程无法提供服务
|
||||
LOG_ERROR() << "Cannot connect to D-Bus. system error="
|
||||
<< QDBusConnection::systemBus().lastError().message()
|
||||
<< " session error=" << QDBusConnection::sessionBus().lastError().message();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 注册 D-Bus 服务名称
|
||||
// 服务名称格式:反向域名格式(如 "com.softbus.Daemon")
|
||||
// 其他进程可以通过此名称找到守护进程
|
||||
if (!connection.registerService("com.softbus.Daemon")) {
|
||||
LOG_ERROR() << "Failed to register D-Bus service:" << connection.lastError().message();
|
||||
LOG_ERROR() << "Possible causes:";
|
||||
LOG_ERROR() << " 1. Service name already in use";
|
||||
LOG_ERROR() << " 2. Insufficient D-Bus permissions";
|
||||
LOG_ERROR() << " 3. D-Bus configuration issue";
|
||||
return 1; // 服务注册失败,无法提供 D-Bus 接口
|
||||
if (!connection.registerService(serviceName)) {
|
||||
LOG_ERROR() << "Failed to register D-Bus service:" << serviceName
|
||||
<< "error=" << connection.lastError().message();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 注册 D-Bus 对象路径和方法
|
||||
// 对象路径:"/com/softbus/Daemon" - 类似于文件系统中的路径
|
||||
// ExportAllSlots: 导出所有 public slots 作为 D-Bus 方法
|
||||
// ExportAllSignals: 导出所有 signals 作为 D-Bus 信号
|
||||
// 这样外部客户端就可以调用守护进程的方法和接收信号
|
||||
if (!connection.registerObject("/com/softbus/Daemon", g_daemonService,
|
||||
QDBusConnection::ExportAllSlots | QDBusConnection::ExportAllSignals)) {
|
||||
LOG_ERROR() << "Failed to register D-Bus object:" << connection.lastError().message();
|
||||
return 1; // 对象注册失败,D-Bus 接口无法使用
|
||||
if (!connection.registerObject(objectPath,
|
||||
ipc,
|
||||
QDBusConnection::ExportAllSlots | QDBusConnection::ExportAllSignals)) {
|
||||
LOG_ERROR() << "Failed to register D-Bus object:" << objectPath
|
||||
<< "error=" << connection.lastError().message();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 打印成功启动信息,便于调试和监控
|
||||
LOG_INFO() << "Soft Bus Daemon started successfully";
|
||||
LOG_INFO() << "D-Bus service: com.softbus.Daemon";
|
||||
LOG_INFO() << "D-Bus object: /com/softbus/Daemon";
|
||||
LOG_INFO() << "Ready to accept D-Bus requests";
|
||||
|
||||
LOG_INFO() << "D-Bus ready. bus="
|
||||
<< (connection.name() == QDBusConnection::systemBus().name() ? "system" : "session")
|
||||
<< " service=" << serviceName
|
||||
<< " object=" << objectPath;
|
||||
}
|
||||
|
||||
|
||||
@@ -114,9 +131,6 @@ int main(int argc, char *argv[])
|
||||
signal(SIGINT, signalHandler);
|
||||
signal(SIGTERM, signalHandler);
|
||||
|
||||
CoreService::instance()->initialize();
|
||||
CoreService::instance()->shutdown();
|
||||
|
||||
int ret = app.exec();
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user