log0.1
This commit is contained in:
91
main.cpp
91
main.cpp
@@ -1,8 +1,10 @@
|
||||
#include <QCoreApplication>
|
||||
#include <QCommandLineParser>
|
||||
#include <QDBusConnection>
|
||||
#include <signal.h>
|
||||
#include "src/utils/logs/logging.h"
|
||||
#include "src/core/CoreService.h"
|
||||
#include "src/api/ipc_dbus/IpcDBus.h"
|
||||
|
||||
void signalHandler(int signal)
|
||||
{
|
||||
@@ -12,24 +14,109 @@ void signalHandler(int signal)
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// ==================================
|
||||
// 创建并配置 Qt 应用程序对象
|
||||
// ==================================
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
app.setApplicationName("soft_bus_daemon");
|
||||
app.setApplicationVersion("1.0.0");
|
||||
app.setOrganizationName("soft_bus");
|
||||
|
||||
// ==================================
|
||||
// 设置 Qt 应用程序信息
|
||||
// ==================================
|
||||
QCommandLineParser parser;
|
||||
parser.setApplicationDescription(QStringLiteral("Soft Bus Daemon"));
|
||||
parser.addHelpOption();
|
||||
parser.addVersionOption();
|
||||
QCommandLineOption roleOption(QStringLiteral("role"), QStringLiteral("Daemon role (CoreRouter, CollectorSerial, CollectorCan, Collector, SqlServer)"));
|
||||
parser.addOption(roleOption);
|
||||
parser.process(app);
|
||||
|
||||
LOG_INFO() << "Starting Soft Bus Daemon";
|
||||
|
||||
const QString roleString = parser.value(roleOption).trimmed();
|
||||
CoreService::Role role = CoreService::Role::CoreRouter; // 默认角色
|
||||
if (roleString == QStringLiteral("CoreRouter")) {
|
||||
role = CoreService::Role::CoreRouter;
|
||||
} else if (roleString == QStringLiteral("CollectorSerial")) {
|
||||
role = CoreService::Role::CollectorSerial;
|
||||
} else if (roleString == QStringLiteral("CollectorCan")) {
|
||||
role = CoreService::Role::CollectorCan;
|
||||
} else if (roleString == QStringLiteral("Collector")) {
|
||||
role = CoreService::Role::Collector;
|
||||
} else if (roleString == QStringLiteral("SqlServer")) {
|
||||
role = CoreService::Role::SqlServer;
|
||||
} else {
|
||||
LOG_ERROR() << "Invalid role:" << roleString
|
||||
<< "valid roles are: CoreRouter, CollectorSerial, CollectorCan, Collector, SqlServer";
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// 创建并注册 D-Bus 服务(仅在 core_router 角色中启用)
|
||||
// ========================================================================
|
||||
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();
|
||||
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,守护进程无法提供服务
|
||||
}
|
||||
|
||||
// 注册 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 接口
|
||||
}
|
||||
|
||||
// 注册 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 接口无法使用
|
||||
}
|
||||
|
||||
// 打印成功启动信息,便于调试和监控
|
||||
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";
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ==================================
|
||||
// 处理系统信号,确保在接收到 SIGINT 或 SIGTERM 时能够优雅地退出
|
||||
// ==================================
|
||||
|
||||
signal(SIGINT, signalHandler);
|
||||
signal(SIGTERM, signalHandler);
|
||||
|
||||
CoreService::instance()->initialize();
|
||||
int ret = app.exec();
|
||||
CoreService::instance()->shutdown();
|
||||
|
||||
int ret = app.exec();
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user