AllJoyn 框架採用總線協作模式,PC 端模塊化關鍵在於接口定義分離、服務實現解耦、總線附件生命週期管理。
AllJoynProject/
├── interfaces/ // .xml 接口定義(introspection XML)
│ ├── org.example.Control.xml
│ └── org.example.Sensor.xml
├── generated/ // alljoyn-codegen 生成的 C++ 抽象類
│ ├── ControlInterface.h/.cpp
│ └── SensorInterface.h/.cpp
├── services/ // 具體業務實現
│ ├── ControlService.h/.cpp // 繼承 ControlInterface
│ └── SensorService.h/.cpp // 繼承 SensorInterface
├── bus/ // 總線初始化、會話管理、信號處理
│ ├── BusAttachmentWrapper.h/.cpp
│ └── SessionManager.h/.cpp
├── main.cpp // 入口:註冊 BusObject、連接總線、進入事件循環
└── CMakeLists.txt
alljoyn-codegen -l cpp 生成骨架,禁止手改生成代碼。BusObject 只實現一組邏輯接口,狀態封裝在私有成員。BusAttachment 全程唯一,封裝連接、斷開、重連邏輯。MessageReceiver::MethodHandler 回調,避免阻塞主循環。CMake 鏈接 alljoyn、alljoyn_about、crypto 庫,啟用 C++17,單元測試用 gtest 模擬總線消息。