log0.2
This commit is contained in:
@@ -51,6 +51,18 @@ qt_add_library(softbus_template
|
||||
qt_add_executable(softbus_daemon
|
||||
main.cpp
|
||||
src/core/CoreService.cpp
|
||||
|
||||
# api 接口
|
||||
|
||||
src/api/ipc_dbus/IpcDBus.h
|
||||
src/api/ipc_dbus/IpcDBus.cpp
|
||||
|
||||
|
||||
)
|
||||
|
||||
target_include_directories(softbus_daemon
|
||||
PRIVATE
|
||||
${CMAKE_SOURCE_DIR}
|
||||
)
|
||||
|
||||
|
||||
@@ -58,6 +70,9 @@ qt_add_executable(softbus_daemon
|
||||
target_link_libraries(softbus_daemon
|
||||
PRIVATE
|
||||
Qt::Core
|
||||
Qt6::DBus
|
||||
Qt6::SerialPort
|
||||
Qt6::Network
|
||||
)
|
||||
|
||||
add_subdirectory(plugins)
|
||||
|
||||
BIN
bin/softbus_daemon
Executable file
BIN
bin/softbus_daemon
Executable file
Binary file not shown.
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;
|
||||
}
|
||||
|
||||
75
src/api/ipc_dbus/IpcDBus.cpp
Normal file
75
src/api/ipc_dbus/IpcDBus.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "IpcDBus.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QDateTime>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include "src/core/CoreService.h"
|
||||
|
||||
IpcDBus::IpcDBus(CoreService* coreService, const QString& role, QObject* parent)
|
||||
: QObject(parent),
|
||||
m_coreService(coreService),
|
||||
m_role(role)
|
||||
{
|
||||
}
|
||||
|
||||
IpcDBus::~IpcDBus() = default;
|
||||
|
||||
bool IpcDBus::isRunning() const
|
||||
{
|
||||
if (!m_coreService) {
|
||||
return false;
|
||||
}
|
||||
return m_coreService->isInitialized();
|
||||
}
|
||||
|
||||
QString IpcDBus::ping() const
|
||||
{
|
||||
return QStringLiteral("pong");
|
||||
}
|
||||
|
||||
QString IpcDBus::getVersion() const
|
||||
{
|
||||
return QCoreApplication::applicationVersion();
|
||||
}
|
||||
|
||||
QString IpcDBus::getStatus() const
|
||||
{
|
||||
QJsonObject obj;
|
||||
obj.insert(QStringLiteral("service"), QStringLiteral("com.softbus.Daemon"));
|
||||
obj.insert(QStringLiteral("objectPath"), QStringLiteral("/com/softbus/Daemon"));
|
||||
obj.insert(QStringLiteral("interface"), QStringLiteral("com.softbus.Daemon"));
|
||||
obj.insert(QStringLiteral("pid"), static_cast<qint64>(QCoreApplication::applicationPid()));
|
||||
obj.insert(QStringLiteral("role"), m_role);
|
||||
obj.insert(QStringLiteral("running"), isRunning());
|
||||
obj.insert(QStringLiteral("timestampMs"), static_cast<qint64>(QDateTime::currentMSecsSinceEpoch()));
|
||||
|
||||
const QJsonDocument doc(obj);
|
||||
return QString::fromUtf8(doc.toJson(QJsonDocument::Compact));
|
||||
}
|
||||
|
||||
qint64 IpcDBus::getPid() const
|
||||
{
|
||||
return static_cast<qint64>(QCoreApplication::applicationPid());
|
||||
}
|
||||
|
||||
void IpcDBus::shutdown()
|
||||
{
|
||||
if (m_coreService) {
|
||||
m_coreService->shutdown();
|
||||
}
|
||||
QCoreApplication::quit();
|
||||
}
|
||||
|
||||
bool IpcDBus::reloadConfig()
|
||||
{
|
||||
// 占位:后续移植配置系统时在这里触发 reload
|
||||
return false;
|
||||
}
|
||||
|
||||
void IpcDBus::updateLoadInfo()
|
||||
{
|
||||
// 预留:后续可在此周期性采样 CPU/内存/队列长度等指标,并通过 getStatus() 输出
|
||||
}
|
||||
|
||||
@@ -8,7 +8,9 @@
|
||||
|
||||
#include <QObject>
|
||||
|
||||
|
||||
#include <QString>
|
||||
// 前向声明
|
||||
class CoreService;
|
||||
/**
|
||||
* @brief D-Bus 服务适配器 - 暴露核心服务接口
|
||||
*/
|
||||
@@ -18,7 +20,7 @@ class IpcDBus : public QObject
|
||||
Q_CLASSINFO("D-Bus Interface", "com.softbus.Daemon")
|
||||
|
||||
public:
|
||||
explicit IpcDBus(CoreService* coreService, QObject* parent = nullptr);
|
||||
explicit IpcDBus(CoreService* coreService, const QString& role, QObject* parent = nullptr);
|
||||
~IpcDBus();
|
||||
|
||||
public slots:
|
||||
@@ -28,6 +30,10 @@ public slots:
|
||||
*/
|
||||
bool isRunning() const;
|
||||
|
||||
QString ping() const;
|
||||
|
||||
QString getVersion() const;
|
||||
|
||||
/**
|
||||
* @brief 获取核心服务状态信息
|
||||
* @return JSON 格式的状态信息
|
||||
@@ -46,6 +52,8 @@ public slots:
|
||||
*/
|
||||
void shutdown();
|
||||
|
||||
bool reloadConfig();
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -54,8 +62,8 @@ private slots:
|
||||
void updateLoadInfo();
|
||||
|
||||
private:
|
||||
|
||||
|
||||
CoreService* m_coreService{nullptr}; // non-owning
|
||||
QString m_role;
|
||||
};
|
||||
|
||||
#endif // IPC_DBUS_H
|
||||
Reference in New Issue
Block a user