From 22cccf9a24d6ce62a8c3e46bd5357e78848b1342 Mon Sep 17 00:00:00 2001 From: majiajun Date: Thu, 23 Jun 2022 01:13:29 -0700 Subject: [PATCH] distributedsche process memory optimization Signed-off-by: majiajun Change-Id: I7112242a7124d7fc352165c168a3b26dcc8d9ab6 --- bundle.json | 18 + interfaces/innerkits/dmsbase/BUILD.gn | 46 ++ .../include/adapter/dnetwork_adapter.h | 0 .../include/deviceManager/dms_device_info.h | 0 .../deviceManager/dms_device_listener.h | 32 + .../distributed_device_node_listener.h | 0 .../include/dtbschedmgr_device_info_storage.h | 195 +++--- .../dmsbase}/include/dtbschedmgr_log.h | 0 .../dmsbase}/include/parcel_helper.h | 0 sa_profile/1401.xml | 4 +- .../src/adapter/dnetwork_adapter.cpp | 0 .../src/deviceManager/dms_device_info.cpp | 0 .../src/distributed_device_node_listener.cpp | 0 .../src/dtbschedmgr_device_info_storage.cpp | 646 +++++++++--------- services/dtbabilitymgr/BUILD.gn | 46 ++ .../distributed_ability_manager_interface.h | 31 + .../distributed_ability_manager_service.h | 59 ++ .../distributed_ability_manager_stub.h | 35 + .../distributed_ability_manager_service.cpp | 121 ++++ .../src/distributed_ability_manager_stub.cpp | 26 + services/dtbschedmgr/BUILD.gn | 5 +- .../include/distributed_sched_service.h | 7 +- .../include/distributed_sched_stub.h | 1 + .../src/distributed_sched_service.cpp | 52 +- services/dtbschedmgr/test/BUILD.gn | 5 +- 25 files changed, 873 insertions(+), 456 deletions(-) create mode 100644 interfaces/innerkits/dmsbase/BUILD.gn rename {services/dtbschedmgr => interfaces/innerkits/dmsbase}/include/adapter/dnetwork_adapter.h (100%) rename {services/dtbschedmgr => interfaces/innerkits/dmsbase}/include/deviceManager/dms_device_info.h (100%) mode change 100755 => 100644 create mode 100644 interfaces/innerkits/dmsbase/include/deviceManager/dms_device_listener.h rename {services/dtbschedmgr => interfaces/innerkits/dmsbase}/include/distributed_device_node_listener.h (100%) mode change 100755 => 100644 rename {services/dtbschedmgr => interfaces/innerkits/dmsbase}/include/dtbschedmgr_device_info_storage.h (92%) rename {services/dtbschedmgr => interfaces/innerkits/dmsbase}/include/dtbschedmgr_log.h (100%) rename {services/dtbschedmgr => interfaces/innerkits/dmsbase}/include/parcel_helper.h (100%) rename services/{dtbschedmgr => base}/src/adapter/dnetwork_adapter.cpp (100%) rename services/{dtbschedmgr => base}/src/deviceManager/dms_device_info.cpp (100%) rename services/{dtbschedmgr => base}/src/distributed_device_node_listener.cpp (100%) rename services/{dtbschedmgr => base}/src/dtbschedmgr_device_info_storage.cpp (87%) create mode 100644 services/dtbabilitymgr/BUILD.gn create mode 100644 services/dtbabilitymgr/include/distributed_ability_manager_interface.h create mode 100644 services/dtbabilitymgr/include/distributed_ability_manager_service.h create mode 100644 services/dtbabilitymgr/include/distributed_ability_manager_stub.h create mode 100644 services/dtbabilitymgr/src/distributed_ability_manager_service.cpp create mode 100644 services/dtbabilitymgr/src/distributed_ability_manager_stub.cpp mode change 100755 => 100644 services/dtbschedmgr/src/distributed_sched_service.cpp diff --git a/bundle.json b/bundle.json index cec47d5e..ce5c0a29 100644 --- a/bundle.json +++ b/bundle.json @@ -42,8 +42,26 @@ "//foundation/distributedschedule/dmsfwk/etc/init:etc", "//foundation/distributedschedule/dmsfwk/etc/profile:distributedsched_trust", "//foundation/distributedschedule/dmsfwk/sa_profile:dms_sa_profile", + "//foundation/distributedschedule/dmsfwk/services/dtbabilitymgr:distributed_ability_manager_svr", "//foundation/distributedschedule/dmsfwk/services/dtbschedmgr:distributedschedsvr" ], + "inner_kits": [ + { + "header": { + "header_base": "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/dmsbase/include", + "header_files": [ + "adapter/dnetwork_adapter.h", + "deviceManager/dms_device_info.h", + "deviceManager/dms_device_listener.h", + "distributed_device_node_listener.h", + "dtbschedmgr_device_info_storage.h", + "dtbschedmgr_log.h", + "parcel_helper.h" + ] + }, + "name": "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/dmsbase:dmsbaseinner" + } + ], "test": [ "//foundation/distributedschedule/dmsfwk/services/dtbschedmgr:unittest" ] diff --git a/interfaces/innerkits/dmsbase/BUILD.gn b/interfaces/innerkits/dmsbase/BUILD.gn new file mode 100644 index 00000000..24e9b365 --- /dev/null +++ b/interfaces/innerkits/dmsbase/BUILD.gn @@ -0,0 +1,46 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") + +config("distributed_sched_config") { + visibility = [ ":*" ] + include_dirs = [ "include" ] +} + +ohos_shared_library("dmsbaseinner") { + install_enable = true + sources = [ + "../../../services/base/src/adapter/dnetwork_adapter.cpp", + "../../../services/base/src/deviceManager/dms_device_info.cpp", + "../../../services/base/src/distributed_device_node_listener.cpp", + "../../../services/base/src/dtbschedmgr_device_info_storage.cpp", + ] + + configs = [ ":distributed_sched_config" ] + public_configs = [ ":distributed_sched_config" ] + + external_deps = [ + "device_auth:deviceauth_sdk", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "utils_base:utils", + ] + part_name = "dmsfwk_standard" + subsystem_name = "distributedschedule" +} diff --git a/services/dtbschedmgr/include/adapter/dnetwork_adapter.h b/interfaces/innerkits/dmsbase/include/adapter/dnetwork_adapter.h similarity index 100% rename from services/dtbschedmgr/include/adapter/dnetwork_adapter.h rename to interfaces/innerkits/dmsbase/include/adapter/dnetwork_adapter.h diff --git a/services/dtbschedmgr/include/deviceManager/dms_device_info.h b/interfaces/innerkits/dmsbase/include/deviceManager/dms_device_info.h old mode 100755 new mode 100644 similarity index 100% rename from services/dtbschedmgr/include/deviceManager/dms_device_info.h rename to interfaces/innerkits/dmsbase/include/deviceManager/dms_device_info.h diff --git a/interfaces/innerkits/dmsbase/include/deviceManager/dms_device_listener.h b/interfaces/innerkits/dmsbase/include/deviceManager/dms_device_listener.h new file mode 100644 index 00000000..8c9dd42f --- /dev/null +++ b/interfaces/innerkits/dmsbase/include/deviceManager/dms_device_listener.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_DISTRIBUTED_DMS_DEVICE_LISTENER_H +#define OHOS_DISTRIBUTED_DMS_DEVICE_LISTENER_H +#include + +namespace OHOS { +namespace DistributedSchedule { + +class DmsDeviceListener : public virtual RefBase { +public: + DmsDeviceListener() = default; + virtual ~DmsDeviceListener() = default; + + virtual void DeviceOnlineNotify(const std::string& deviceId) = 0; + virtual void DeviceOfflineNotify(const std::string& deviceId) = 0; +}; +} // namespace DistributedSchedule +} // namespace OHOS +#endif /* OHOS_DISTRIBUTED_DMS_DEVICE_LISTENER_H */ diff --git a/services/dtbschedmgr/include/distributed_device_node_listener.h b/interfaces/innerkits/dmsbase/include/distributed_device_node_listener.h old mode 100755 new mode 100644 similarity index 100% rename from services/dtbschedmgr/include/distributed_device_node_listener.h rename to interfaces/innerkits/dmsbase/include/distributed_device_node_listener.h diff --git a/services/dtbschedmgr/include/dtbschedmgr_device_info_storage.h b/interfaces/innerkits/dmsbase/include/dtbschedmgr_device_info_storage.h similarity index 92% rename from services/dtbschedmgr/include/dtbschedmgr_device_info_storage.h rename to interfaces/innerkits/dmsbase/include/dtbschedmgr_device_info_storage.h index 397714c2..c9a61614 100644 --- a/services/dtbschedmgr/include/dtbschedmgr_device_info_storage.h +++ b/interfaces/innerkits/dmsbase/include/dtbschedmgr_device_info_storage.h @@ -1,96 +1,99 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef OHOS_DISTRIBUTED_DTBSCHEDMGR_DEVICE_INFO_INTERFACE_H -#define OHOS_DISTRIBUTED_DTBSCHEDMGR_DEVICE_INFO_INTERFACE_H - -#include - -#include "adapter/dnetwork_adapter.h" -#include "deviceManager/dms_device_info.h" -#include "distributed_device_node_listener.h" -#include "event_handler.h" -#include "iremote_object.h" -#include "single_instance.h" - -namespace OHOS { -namespace DistributedSchedule { -class DnetServiceDeathRecipient : public IRemoteObject::DeathRecipient { -public: - DnetServiceDeathRecipient() = default; - ~DnetServiceDeathRecipient() override = default; - void OnRemoteDied(const wptr& remote) override; -}; - -class DtbschedmgrDeviceInfoStorage { - DECLARE_SINGLE_INSTANCE(DtbschedmgrDeviceInfoStorage); - -public: - bool Init(); - void Stop(); - bool GetLocalDeviceId(std::string& deviceId); - void DeviceOnlineNotify(const std::shared_ptr devInfo); - void DeviceOfflineNotify(const std::string& deviceId); - void OnDeviceInfoChanged(const std::string& deviceId, DeviceInfoType type); - - /** - * get device info by device id - * - * @param deviceId, string - * @return shared_ptr - */ - std::shared_ptr GetDeviceInfoById(const std::string& deviceId); - - /** - * get uuid by networkId - * - * @param networkId - */ - std::string GetUuidByNetworkId(const std::string& networkId); - - /** - * get networkId by uuid - * - * @param uuid - */ - std::string GetNetworkIdByUuid(const std::string& uuid); - - /** - * GetDeviceIdSet get all of the device Id in same network - * - * @param deviceIdSet Returns the device set. - */ - void GetDeviceIdSet(std::set& deviceIdSet); - -private: - bool InitNetworkIdManager(std::shared_ptr dnetworkAdapter); - bool ConnectSoftbus(); - void ClearAllDevices(); - bool WaitForDnetworkReady(); - bool GetLocalDeviceFromDnet(std::string& deviceId); - void RegisterUuidNetworkIdMap(const std::string& networkId); - void UnregisterUuidNetworkIdMap(const std::string& networkId); - std::mutex deviceLock_; - std::shared_ptr deviceNodeListener_; - std::map> remoteDevices_; - std::string deviceId_; - std::map uuidNetworkIdMap_; - std::mutex uuidNetworkIdLock_; - std::shared_ptr initHandler_; - std::shared_ptr networkIdMgrHandler_; -}; -} // namespace DistributedSchedule -} // namespace OHOS -#endif // OHOS_DISTRIBUTED_DTBSCHEDMGR_DEVICE_INFO_INTERFACE_H \ No newline at end of file +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_DTBSCHEDMGR_DEVICE_INFO_INTERFACE_H +#define OHOS_DISTRIBUTED_DTBSCHEDMGR_DEVICE_INFO_INTERFACE_H + +#include + +#include "adapter/dnetwork_adapter.h" +#include "deviceManager/dms_device_info.h" +#include "deviceManager/dms_device_listener.h" +#include "distributed_device_node_listener.h" +#include "event_handler.h" +#include "iremote_object.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedSchedule { +class DnetServiceDeathRecipient : public IRemoteObject::DeathRecipient { +public: + DnetServiceDeathRecipient() = default; + ~DnetServiceDeathRecipient() override = default; + void OnRemoteDied(const wptr& remote) override; +}; + +class DtbschedmgrDeviceInfoStorage { + DECLARE_SINGLE_INSTANCE(DtbschedmgrDeviceInfoStorage); + +public: + bool Init(); + bool Init(sptr listener); + void Stop(); + bool GetLocalDeviceId(std::string& deviceId); + void DeviceOnlineNotify(const std::shared_ptr devInfo); + void DeviceOfflineNotify(const std::string& deviceId); + void OnDeviceInfoChanged(const std::string& deviceId, DeviceInfoType type); + + /** + * get device info by device id + * + * @param deviceId, string + * @return shared_ptr + */ + std::shared_ptr GetDeviceInfoById(const std::string& deviceId); + + /** + * get uuid by networkId + * + * @param networkId + */ + std::string GetUuidByNetworkId(const std::string& networkId); + + /** + * get networkId by uuid + * + * @param uuid + */ + std::string GetNetworkIdByUuid(const std::string& uuid); + + /** + * GetDeviceIdSet get all of the device Id in same network + * + * @param deviceIdSet Returns the device set. + */ + void GetDeviceIdSet(std::set& deviceIdSet); + +private: + bool InitNetworkIdManager(std::shared_ptr dnetworkAdapter); + bool ConnectSoftbus(); + void ClearAllDevices(); + bool WaitForDnetworkReady(); + bool GetLocalDeviceFromDnet(std::string& deviceId); + void RegisterUuidNetworkIdMap(const std::string& networkId); + void UnregisterUuidNetworkIdMap(const std::string& networkId); + sptr listener_; + std::mutex deviceLock_; + std::shared_ptr deviceNodeListener_; + std::map> remoteDevices_; + std::string deviceId_; + std::map uuidNetworkIdMap_; + std::mutex uuidNetworkIdLock_; + std::shared_ptr initHandler_; + std::shared_ptr networkIdMgrHandler_; +}; +} // namespace DistributedSchedule +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_DTBSCHEDMGR_DEVICE_INFO_INTERFACE_H diff --git a/services/dtbschedmgr/include/dtbschedmgr_log.h b/interfaces/innerkits/dmsbase/include/dtbschedmgr_log.h similarity index 100% rename from services/dtbschedmgr/include/dtbschedmgr_log.h rename to interfaces/innerkits/dmsbase/include/dtbschedmgr_log.h diff --git a/services/dtbschedmgr/include/parcel_helper.h b/interfaces/innerkits/dmsbase/include/parcel_helper.h similarity index 100% rename from services/dtbschedmgr/include/parcel_helper.h rename to interfaces/innerkits/dmsbase/include/parcel_helper.h diff --git a/sa_profile/1401.xml b/sa_profile/1401.xml index b0b67be0..dc5c1d65 100644 --- a/sa_profile/1401.xml +++ b/sa_profile/1401.xml @@ -16,11 +16,11 @@ distributedsched 1401 - libdistributedschedsvr.z.so + libdistributed_ability_manager_svr.z.so true true 1 - \ No newline at end of file + diff --git a/services/dtbschedmgr/src/adapter/dnetwork_adapter.cpp b/services/base/src/adapter/dnetwork_adapter.cpp similarity index 100% rename from services/dtbschedmgr/src/adapter/dnetwork_adapter.cpp rename to services/base/src/adapter/dnetwork_adapter.cpp diff --git a/services/dtbschedmgr/src/deviceManager/dms_device_info.cpp b/services/base/src/deviceManager/dms_device_info.cpp similarity index 100% rename from services/dtbschedmgr/src/deviceManager/dms_device_info.cpp rename to services/base/src/deviceManager/dms_device_info.cpp diff --git a/services/dtbschedmgr/src/distributed_device_node_listener.cpp b/services/base/src/distributed_device_node_listener.cpp similarity index 100% rename from services/dtbschedmgr/src/distributed_device_node_listener.cpp rename to services/base/src/distributed_device_node_listener.cpp diff --git a/services/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp b/services/base/src/dtbschedmgr_device_info_storage.cpp similarity index 87% rename from services/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp rename to services/base/src/dtbschedmgr_device_info_storage.cpp index 60477f77..bcda7d75 100644 --- a/services/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp +++ b/services/base/src/dtbschedmgr_device_info_storage.cpp @@ -1,325 +1,321 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "dtbschedmgr_device_info_storage.h" - -#include -#include - -#include "distributed_device_node_listener.h" -#include "distributed_sched_adapter.h" -#include "dtbschedmgr_log.h" - -#include "ipc_object_proxy.h" -#include "ipc_skeleton.h" -#include "iservice_registry.h" -#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER -#include "mission/distributed_sched_mission_manager.h" -#endif -#include "system_ability_definition.h" - -using namespace std; -namespace OHOS { -namespace DistributedSchedule { -using namespace std::chrono_literals; - -namespace { -constexpr int32_t RETRY_TIMES = 30; -constexpr int32_t CONNECT_SOFTBUS_RETRY_TIMES = 60; -const std::string TAG = "DtbschedmgrDeviceInfoStorage"; -} - -IMPLEMENT_SINGLE_INSTANCE(DtbschedmgrDeviceInfoStorage); - -bool DtbschedmgrDeviceInfoStorage::Init() -{ - if (initHandler_ == nullptr) { - auto deviceInfoStorageRunner = AppExecFwk::EventRunner::Create("DmsDeviceInfoStorageManager"); - initHandler_ = std::make_shared(deviceInfoStorageRunner); - } - - auto func = [this]() { - HILOGI("begin connect softbus"); - for (int32_t retryTimes = 0; retryTimes <= CONNECT_SOFTBUS_RETRY_TIMES; retryTimes++) { - if (ConnectSoftbus()) { - return; - } - HILOGE("retry connect softbus %{public}d times", retryTimes); - std::this_thread::sleep_for(1s); - } - HILOGE("connect softbus 60times * 30s, error!!"); - }; - if (!initHandler_->PostTask(func)) { - HILOGE("Init handler postTask failed"); - return false; - } - return true; -} - -bool DtbschedmgrDeviceInfoStorage::ConnectSoftbus() -{ - ClearAllDevices(); - bool isReady = WaitForDnetworkReady(); - if (!isReady) { - HILOGE("ConnectSoftbus wait Dnetwork failed!"); - return false; - } - std::shared_ptr dnetworkAdapter = DnetworkAdapter::GetInstance(); - if (dnetworkAdapter == nullptr) { - HILOGE("DnetworkAdapter::GetInstance() null"); - return false; - } - if (!InitNetworkIdManager(dnetworkAdapter)) { - HILOGE("InitNetworkIdManager failed"); - return false; - } - HILOGI("ConnectSoftbus success"); - return true; -} - -bool DtbschedmgrDeviceInfoStorage::InitNetworkIdManager(std::shared_ptr dnetworkAdapter) -{ - if (networkIdMgrHandler_ == nullptr) { - auto runner = AppExecFwk::EventRunner::Create("DmsNetworkIdManager"); - networkIdMgrHandler_ = std::make_shared(runner); - } - - deviceNodeListener_ = std::make_shared(); - if (!dnetworkAdapter->AddDeviceChangeListener(deviceNodeListener_)) { - deviceNodeListener_ = nullptr; - HILOGE("AddDeviceChangeListener failed!"); - return false; - } - return true; -} - -void DtbschedmgrDeviceInfoStorage::Stop() -{ - ClearAllDevices(); - if (deviceNodeListener_ != nullptr) { - DnetworkAdapter::GetInstance()->RemoveDeviceChangeListener(deviceNodeListener_); - deviceNodeListener_ = nullptr; - } -} - -bool DtbschedmgrDeviceInfoStorage::WaitForDnetworkReady() -{ - auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (samgr == nullptr) { - HILOGE("WaitForDnetworkReady failed to get samgr!"); - return false; - } - int32_t retryTimeout = RETRY_TIMES; - do { - auto dnetwork = samgr->CheckSystemAbility(SOFTBUS_SERVER_SA_ID); - if (dnetwork != nullptr) { - IPCObjectProxy* proxy = reinterpret_cast(dnetwork.GetRefPtr()); - // make sure the proxy is not dead - if (proxy != nullptr && !proxy->IsObjectDead()) { - return true; - } - } - HILOGI("Waiting for dnentwork service..."); - std::this_thread::sleep_for(1s); - if (--retryTimeout <= 0) { - HILOGI("Waiting for dnentwork service timeout(30)s"); - return false; - } - } while (true); - return false; -} - -void DtbschedmgrDeviceInfoStorage::RegisterUuidNetworkIdMap(const std::string& networkId) -{ - std::string uuid = DnetworkAdapter::GetInstance()->GetUuidByNetworkId(networkId); - { - std::lock_guard autoLock(uuidNetworkIdLock_); - uuidNetworkIdMap_[uuid] = networkId; - } -} - -void DtbschedmgrDeviceInfoStorage::UnregisterUuidNetworkIdMap(const std::string& networkId) -{ - std::string uuid = DnetworkAdapter::GetInstance()->GetUuidByNetworkId(networkId); - { - std::lock_guard autoLock(uuidNetworkIdLock_); - uuidNetworkIdMap_.erase(uuid); - } -} - -void DtbschedmgrDeviceInfoStorage::GetDeviceIdSet(std::set& deviceIdSet) -{ - deviceIdSet.clear(); - lock_guard autoLock(deviceLock_); - for (const auto& device : remoteDevices_) { - deviceIdSet.emplace(device.first); - } -} - -bool DtbschedmgrDeviceInfoStorage::GetLocalDeviceId(std::string& deviceId) -{ - return GetLocalDeviceFromDnet(deviceId); -} - -bool DtbschedmgrDeviceInfoStorage::GetLocalDeviceFromDnet(std::string& deviceId) -{ - auto dnetworkAdapter = DnetworkAdapter::GetInstance(); - if (dnetworkAdapter == nullptr) { - HILOGE("GetLocalDeviceFromDnet dnetworkAdapter null"); - return false; - } - std::shared_ptr localNode = dnetworkAdapter->GetLocalBasicInfo(); - if (localNode != nullptr) { - deviceId = localNode->networkId; - HILOGI("get local deviceId from DnetworkAdapter, deviceId = %{public}s", - DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str()); - return true; - } - HILOGE("GetLocalDeviceFromDnet localNode null"); - return false; -} - -void DtbschedmgrDeviceInfoStorage::ClearAllDevices() -{ - lock_guard autoLock(deviceLock_); - remoteDevices_.clear(); -} - -std::shared_ptr DtbschedmgrDeviceInfoStorage::GetDeviceInfoById(const string& deviceId) -{ - lock_guard autoLock(deviceLock_); - auto iter = remoteDevices_.find(deviceId); - if (iter == remoteDevices_.end()) { - return nullptr; - } - return iter->second; -} - -std::string DtbschedmgrDeviceInfoStorage::GetUuidByNetworkId(const std::string& networkId) -{ - if (networkId.empty()) { - HILOGW("GetUuidByNetworkId networkId empty!"); - return ""; - } - { - std::lock_guard autoLock(uuidNetworkIdLock_); - auto iter = uuidNetworkIdMap_.begin(); - while (iter != uuidNetworkIdMap_.end()) { - if (iter->second == networkId) { - return iter->first; - } else { - ++iter; - } - } - } - std::string uuid = DnetworkAdapter::GetInstance()->GetUuidByNetworkId(networkId); - return uuid; -} - -std::string DtbschedmgrDeviceInfoStorage::GetNetworkIdByUuid(const std::string& uuid) -{ - if (uuid.empty()) { - HILOGW("GetNetworkIdByUuid uuid empty!"); - return ""; - } - { - std::lock_guard autoLock(uuidNetworkIdLock_); - auto iter = uuidNetworkIdMap_.find(uuid); - if (iter != uuidNetworkIdMap_.end()) { - return iter->second; - } - return ""; - } -} -void DtbschedmgrDeviceInfoStorage::DeviceOnlineNotify(const std::shared_ptr devInfo) -{ - if (devInfo == nullptr) { - HILOGE("DeviceOnlineNotify devInfo null"); - return; - } - std::string devId = devInfo->GetDeviceId(); - HILOGI("DeviceOnlineNotify deviceId = %{public}s", - DnetworkAdapter::AnonymizeDeviceId(devId).c_str()); - - if (networkIdMgrHandler_ == nullptr) { - HILOGE("DeviceOnlineNotify networkIdMgrHandler null"); - return; - } - auto nodeOnline = [this, devInfo]() { - std::string deviceId = devInfo->GetDeviceId(); - RegisterUuidNetworkIdMap(deviceId); - std::string uuid = GetUuidByNetworkId(deviceId); - HILOGI("DeviceOnlineNotify process deviceId = %{public}s, uuid = %{public}s, deviceName = %{public}s", - DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str(), - DnetworkAdapter::AnonymizeDeviceId(uuid).c_str(), devInfo->GetDeviceName().c_str()); - DistributedSchedAdapter::GetInstance().DeviceOnline(deviceId); -#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER - DistributedSchedMissionManager::GetInstance().UpdateConnCapSupportOsd(deviceId); - DistributedSchedMissionManager::GetInstance().DeviceOnlineNotify(deviceId); -#endif - { - lock_guard autoLock(deviceLock_); - remoteDevices_[deviceId] = devInfo; - } -#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER - DistributedSchedMissionManager::GetInstance().UpdateSwitchValueToRemote(); -#endif - }; - if (!networkIdMgrHandler_->PostTask(nodeOnline)) { - HILOGE("DeviceOnlineNotify handler postTask failed"); - } -} - -void DtbschedmgrDeviceInfoStorage::DeviceOfflineNotify(const std::string& deviceId) -{ - if (deviceId.empty()) { - HILOGE("DeviceOfflineNotify deviceId empty"); - return; - } - HILOGI("DeviceOfflineNotify deviceId = %{public}s", - DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str()); - if (networkIdMgrHandler_ == nullptr) { - HILOGE("DeviceOfflineNotify networkIdMgrHandler null"); - return; - } - auto nodeOffline = [this, deviceId]() { - std::string uuid = GetUuidByNetworkId(deviceId); - HILOGI("DeviceOfflineNotify process deviceId = %{public}s, uuid = %{public}s", - DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str(), DnetworkAdapter::AnonymizeDeviceId(uuid).c_str()); - DistributedSchedAdapter::GetInstance().DeviceOffline(deviceId); -#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER - DistributedSchedMissionManager::GetInstance().DeviceOfflineNotify(deviceId); -#endif - UnregisterUuidNetworkIdMap(deviceId); - lock_guard autoLock(deviceLock_); - remoteDevices_.erase(deviceId); - }; - if (!networkIdMgrHandler_->PostTask(nodeOffline)) { - HILOGE("DeviceOfflineNotify handler postTask failed"); - } -} - -void DtbschedmgrDeviceInfoStorage::OnDeviceInfoChanged(const std::string& deviceId, DeviceInfoType type) -{ - HILOGI("OnDeviceInfoChanged called"); -} - -void DnetServiceDeathRecipient::OnRemoteDied(const wptr& remote) -{ - HILOGI("OnRemoteDied dnetwork service died"); - DtbschedmgrDeviceInfoStorage::GetInstance().Init(); -} -} -} +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dtbschedmgr_device_info_storage.h" + +#include +#include + +#include "distributed_device_node_listener.h" +#include "dtbschedmgr_log.h" + +#include "ipc_object_proxy.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" + +using namespace std; +namespace OHOS { +namespace DistributedSchedule { +using namespace std::chrono_literals; + +namespace { +constexpr int32_t RETRY_TIMES = 30; +constexpr int32_t CONNECT_SOFTBUS_RETRY_TIMES = 60; +const std::string TAG = "DtbschedmgrDeviceInfoStorage"; +} + +IMPLEMENT_SINGLE_INSTANCE(DtbschedmgrDeviceInfoStorage); + +bool DtbschedmgrDeviceInfoStorage::Init() +{ + if (initHandler_ == nullptr) { + auto deviceInfoStorageRunner = AppExecFwk::EventRunner::Create("DmsDeviceInfoStorageManager"); + initHandler_ = std::make_shared(deviceInfoStorageRunner); + } + + auto func = [this]() { + HILOGI("begin connect softbus"); + for (int32_t retryTimes = 0; retryTimes <= CONNECT_SOFTBUS_RETRY_TIMES; retryTimes++) { + if (ConnectSoftbus()) { + return; + } + HILOGE("retry connect softbus %{public}d times", retryTimes); + std::this_thread::sleep_for(1s); + } + HILOGE("connect softbus 60times * 30s, error!!"); + }; + if (!initHandler_->PostTask(func)) { + HILOGE("Init handler postTask failed"); + return false; + } + return true; +} + +bool DtbschedmgrDeviceInfoStorage::Init(sptr listener) +{ + listener_ = listener; + return Init(); +} + +bool DtbschedmgrDeviceInfoStorage::ConnectSoftbus() +{ + ClearAllDevices(); + bool isReady = WaitForDnetworkReady(); + if (!isReady) { + HILOGE("ConnectSoftbus wait Dnetwork failed!"); + return false; + } + std::shared_ptr dnetworkAdapter = DnetworkAdapter::GetInstance(); + if (dnetworkAdapter == nullptr) { + HILOGE("DnetworkAdapter::GetInstance() null"); + return false; + } + if (!InitNetworkIdManager(dnetworkAdapter)) { + HILOGE("InitNetworkIdManager failed"); + return false; + } + HILOGI("ConnectSoftbus success"); + return true; +} + +bool DtbschedmgrDeviceInfoStorage::InitNetworkIdManager(std::shared_ptr dnetworkAdapter) +{ + if (networkIdMgrHandler_ == nullptr) { + auto runner = AppExecFwk::EventRunner::Create("DmsNetworkIdManager"); + networkIdMgrHandler_ = std::make_shared(runner); + } + + deviceNodeListener_ = std::make_shared(); + if (!dnetworkAdapter->AddDeviceChangeListener(deviceNodeListener_)) { + deviceNodeListener_ = nullptr; + HILOGE("AddDeviceChangeListener failed!"); + return false; + } + return true; +} + +void DtbschedmgrDeviceInfoStorage::Stop() +{ + ClearAllDevices(); + if (deviceNodeListener_ != nullptr) { + DnetworkAdapter::GetInstance()->RemoveDeviceChangeListener(deviceNodeListener_); + deviceNodeListener_ = nullptr; + } +} + +bool DtbschedmgrDeviceInfoStorage::WaitForDnetworkReady() +{ + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + HILOGE("WaitForDnetworkReady failed to get samgr!"); + return false; + } + int32_t retryTimeout = RETRY_TIMES; + do { + auto dnetwork = samgr->CheckSystemAbility(SOFTBUS_SERVER_SA_ID); + if (dnetwork != nullptr) { + IPCObjectProxy* proxy = reinterpret_cast(dnetwork.GetRefPtr()); + // make sure the proxy is not dead + if (proxy != nullptr && !proxy->IsObjectDead()) { + return true; + } + } + HILOGI("Waiting for dnentwork service..."); + std::this_thread::sleep_for(1s); + if (--retryTimeout <= 0) { + HILOGI("Waiting for dnentwork service timeout(30)s"); + return false; + } + } while (true); + return false; +} + +void DtbschedmgrDeviceInfoStorage::RegisterUuidNetworkIdMap(const std::string& networkId) +{ + std::string uuid = DnetworkAdapter::GetInstance()->GetUuidByNetworkId(networkId); + { + std::lock_guard autoLock(uuidNetworkIdLock_); + uuidNetworkIdMap_[uuid] = networkId; + } +} + +void DtbschedmgrDeviceInfoStorage::UnregisterUuidNetworkIdMap(const std::string& networkId) +{ + std::string uuid = DnetworkAdapter::GetInstance()->GetUuidByNetworkId(networkId); + { + std::lock_guard autoLock(uuidNetworkIdLock_); + uuidNetworkIdMap_.erase(uuid); + } +} + +void DtbschedmgrDeviceInfoStorage::GetDeviceIdSet(std::set& deviceIdSet) +{ + deviceIdSet.clear(); + lock_guard autoLock(deviceLock_); + for (const auto& device : remoteDevices_) { + deviceIdSet.emplace(device.first); + } +} + +bool DtbschedmgrDeviceInfoStorage::GetLocalDeviceId(std::string& deviceId) +{ + return GetLocalDeviceFromDnet(deviceId); +} + +bool DtbschedmgrDeviceInfoStorage::GetLocalDeviceFromDnet(std::string& deviceId) +{ + auto dnetworkAdapter = DnetworkAdapter::GetInstance(); + if (dnetworkAdapter == nullptr) { + HILOGE("GetLocalDeviceFromDnet dnetworkAdapter null"); + return false; + } + std::shared_ptr localNode = dnetworkAdapter->GetLocalBasicInfo(); + if (localNode != nullptr) { + deviceId = localNode->networkId; + HILOGI("get local deviceId from DnetworkAdapter, deviceId = %{public}s", + DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str()); + return true; + } + HILOGE("GetLocalDeviceFromDnet localNode null"); + return false; +} + +void DtbschedmgrDeviceInfoStorage::ClearAllDevices() +{ + lock_guard autoLock(deviceLock_); + remoteDevices_.clear(); +} + +std::shared_ptr DtbschedmgrDeviceInfoStorage::GetDeviceInfoById(const string& deviceId) +{ + lock_guard autoLock(deviceLock_); + auto iter = remoteDevices_.find(deviceId); + if (iter == remoteDevices_.end()) { + return nullptr; + } + return iter->second; +} + +std::string DtbschedmgrDeviceInfoStorage::GetUuidByNetworkId(const std::string& networkId) +{ + if (networkId.empty()) { + HILOGW("GetUuidByNetworkId networkId empty!"); + return ""; + } + { + std::lock_guard autoLock(uuidNetworkIdLock_); + auto iter = uuidNetworkIdMap_.begin(); + while (iter != uuidNetworkIdMap_.end()) { + if (iter->second == networkId) { + return iter->first; + } else { + ++iter; + } + } + } + std::string uuid = DnetworkAdapter::GetInstance()->GetUuidByNetworkId(networkId); + return uuid; +} + +std::string DtbschedmgrDeviceInfoStorage::GetNetworkIdByUuid(const std::string& uuid) +{ + if (uuid.empty()) { + HILOGW("GetNetworkIdByUuid uuid empty!"); + return ""; + } + { + std::lock_guard autoLock(uuidNetworkIdLock_); + auto iter = uuidNetworkIdMap_.find(uuid); + if (iter != uuidNetworkIdMap_.end()) { + return iter->second; + } + return ""; + } +} +void DtbschedmgrDeviceInfoStorage::DeviceOnlineNotify(const std::shared_ptr devInfo) +{ + if (devInfo == nullptr) { + HILOGE("DeviceOnlineNotify devInfo null"); + return; + } + std::string devId = devInfo->GetDeviceId(); + HILOGI("deviceId = %{public}s", + DnetworkAdapter::AnonymizeDeviceId(devId).c_str()); + + if (networkIdMgrHandler_ == nullptr) { + HILOGE("networkIdMgrHandler null"); + return; + } + auto nodeOnline = [this, devInfo]() { + std::string deviceId = devInfo->GetDeviceId(); + RegisterUuidNetworkIdMap(deviceId); + std::string uuid = GetUuidByNetworkId(deviceId); + HILOGI("deviceId = %{public}s, uuid = %{public}s, deviceName = %{public}s", + DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str(), + DnetworkAdapter::AnonymizeDeviceId(uuid).c_str(), devInfo->GetDeviceName().c_str()); + { + lock_guard autoLock(deviceLock_); + remoteDevices_[deviceId] = devInfo; + } + if (listener_!= nullptr) { + listener_->DeviceOnlineNotify(deviceId); + } + }; + if (!networkIdMgrHandler_->PostTask(nodeOnline)) { + HILOGE("DeviceOnlineNotify handler postTask failed"); + } +} + +void DtbschedmgrDeviceInfoStorage::DeviceOfflineNotify(const std::string& deviceId) +{ + if (deviceId.empty()) { + HILOGE("DeviceOfflineNotify deviceId empty"); + return; + } + HILOGI("DeviceOfflineNotify deviceId = %{public}s", + DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str()); + if (networkIdMgrHandler_ == nullptr) { + HILOGE("DeviceOfflineNotify networkIdMgrHandler null"); + return; + } + auto nodeOffline = [this, deviceId]() { + std::string uuid = GetUuidByNetworkId(deviceId); + HILOGI("DeviceOfflineNotify process deviceId = %{public}s, uuid = %{public}s", + DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str(), DnetworkAdapter::AnonymizeDeviceId(uuid).c_str()); + if (listener_!= nullptr) { + listener_->DeviceOfflineNotify(deviceId); + } + UnregisterUuidNetworkIdMap(deviceId); + lock_guard autoLock(deviceLock_); + remoteDevices_.erase(deviceId); + }; + if (!networkIdMgrHandler_->PostTask(nodeOffline)) { + HILOGE("DeviceOfflineNotify handler postTask failed"); + } +} + +void DtbschedmgrDeviceInfoStorage::OnDeviceInfoChanged(const std::string& deviceId, DeviceInfoType type) +{ + HILOGI("OnDeviceInfoChanged called"); +} + +void DnetServiceDeathRecipient::OnRemoteDied(const wptr& remote) +{ + HILOGI("OnRemoteDied dnetwork service died"); + DtbschedmgrDeviceInfoStorage::GetInstance().Init(); +} +} +} diff --git a/services/dtbabilitymgr/BUILD.gn b/services/dtbabilitymgr/BUILD.gn new file mode 100644 index 00000000..a0473779 --- /dev/null +++ b/services/dtbabilitymgr/BUILD.gn @@ -0,0 +1,46 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") + +config("distributed_ability_manager_config") { + visibility = [ ":*" ] + include_dirs = [ "include" ] +} + +ohos_shared_library("distributed_ability_manager_svr") { + install_enable = true + sources = [ + "src/distributed_ability_manager_service.cpp", + "src/distributed_ability_manager_stub.cpp", + ] + + configs = [ ":distributed_ability_manager_config" ] + + ldflags = [ "-rdynamic" ] + + external_deps = [ + "dmsfwk_standard:dmsbaseinner", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "utils_base:utils", + ] + + part_name = "dmsfwk_standard" + subsystem_name = "distributedschedule" +} diff --git a/services/dtbabilitymgr/include/distributed_ability_manager_interface.h b/services/dtbabilitymgr/include/distributed_ability_manager_interface.h new file mode 100644 index 00000000..56faf606 --- /dev/null +++ b/services/dtbabilitymgr/include/distributed_ability_manager_interface.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_ABILITY_MANAGER_INTERFACE_H +#define OHOS_DISTRIBUTED_ABILITY_MANAGER_INTERFACE_H + +#include "iremote_broker.h" + +namespace OHOS { +namespace DistributedSchedule { +class IDistributedAbilityManager : public OHOS::IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedSchedule.IDistributedAbilityManager"); + virtual int32_t SendRequestToImpl(uint32_t code, MessageParcel& data, MessageParcel& reply, + MessageOption& option) = 0; +}; +} // namespace DistributedSchedule +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_ABILITY_MANAGER_INTERFACE_H \ No newline at end of file diff --git a/services/dtbabilitymgr/include/distributed_ability_manager_service.h b/services/dtbabilitymgr/include/distributed_ability_manager_service.h new file mode 100644 index 00000000..006a4a6f --- /dev/null +++ b/services/dtbabilitymgr/include/distributed_ability_manager_service.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_ABILITY_MANAGER_SERVICE_H +#define OHOS_DISTRIBUTED_ABILITY_MANAGER_SERVICE_H + +#include "distributed_ability_manager_stub.h" +#include "deviceManager/dms_device_listener.h" +#include "single_instance.h" +#include "system_ability.h" + +namespace OHOS { +namespace DistributedSchedule { +class DistributedAbilityManagerService : public SystemAbility, public DistributedAbilityManagerStub, + public DmsDeviceListener { +DECLARE_SYSTEM_ABILITY(DistributedAbilityManagerService); + +public: + DistributedAbilityManagerService(int32_t systemAbilityId, bool runOnCreate); + ~DistributedAbilityManagerService() = default; + void OnStart() override; + void OnStop() override; + int32_t SendRequestToImpl(uint32_t code, MessageParcel& data, MessageParcel& reply, + MessageOption& option) override; + void DeviceOnlineNotify(const std::string& deviceId) override; + void DeviceOfflineNotify(const std::string& deviceId) override; + +private: + bool InitDmsImplFunc(); + using LibHandle = void*; + LibHandle dmsImplHandle_ = nullptr; + + using OnStartFunc = void(*)(); + using OnRemoteRequestFunc = int32_t(*)(uint32_t code, MessageParcel& data, MessageParcel& reply, + MessageOption& option); + using DeviceOnlineNotifyFunc = void(*)(const std::string& deviceId); + using DeviceOfflineNotifyFunc = void(*)(const std::string& deviceId); + OnRemoteRequestFunc onRemoteRequestFunc_ = nullptr; + DeviceOnlineNotifyFunc deviceOnlineNotifyFunc_ = nullptr; + DeviceOfflineNotifyFunc deviceOfflineNotifyFunc_ = nullptr; + + std::atomic_bool isLoaded_ = false; + std::mutex libLoadLock_; +}; +} // namespace DistributedSchedule +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_ABILITY_MANAGER_SERVICE_H diff --git a/services/dtbabilitymgr/include/distributed_ability_manager_stub.h b/services/dtbabilitymgr/include/distributed_ability_manager_stub.h new file mode 100644 index 00000000..01217ca3 --- /dev/null +++ b/services/dtbabilitymgr/include/distributed_ability_manager_stub.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_ABILITY_MANAGER_STUB_H +#define OHOS_DISTRIBUTED_ABILITY_MANAGER_STUB_H + +#include "distributed_ability_manager_interface.h" +#include "ipc_skeleton.h" +#include "iremote_stub.h" +#include "message_parcel.h" + +namespace OHOS { +namespace DistributedSchedule { +class DistributedAbilityManagerStub : public IRemoteStub { +public: + DistributedAbilityManagerStub() = default; + ~DistributedAbilityManagerStub() = default; + int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, + MessageParcel& reply, MessageOption& option) override; +}; +} // namespace DistributedSchedule +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_ABILITY_MANAGER_STUB_H \ No newline at end of file diff --git a/services/dtbabilitymgr/src/distributed_ability_manager_service.cpp b/services/dtbabilitymgr/src/distributed_ability_manager_service.cpp new file mode 100644 index 00000000..a9387a70 --- /dev/null +++ b/services/dtbabilitymgr/src/distributed_ability_manager_service.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "distributed_ability_manager_service.h" + +#include +#include + +#include "adapter/dnetwork_adapter.h" +#include "dlfcn.h" +#include "dtbschedmgr_device_info_storage.h" +#include "dtbschedmgr_log.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace DistributedSchedule { +const std::string TAG = "DistributedAbilityManagerService"; + +REGISTER_SYSTEM_ABILITY_BY_ID(DistributedAbilityManagerService, DISTRIBUTED_SCHED_SA_ID, true); + +DistributedAbilityManagerService::DistributedAbilityManagerService( + int32_t systemAbilityId, bool runOnCreate) : SystemAbility(systemAbilityId, runOnCreate) +{ +} + +void DistributedAbilityManagerService::OnStart() +{ + HILOGI("begin"); + DnetworkAdapter::GetInstance()->Init(); + if (!DtbschedmgrDeviceInfoStorage::GetInstance().Init(this)) { + HILOGW("Init DtbschedmgrDeviceInfoStorage init failed."); + } + Publish(this); +} + +void DistributedAbilityManagerService::OnStop() +{ + HILOGI("begin"); +} + +bool DistributedAbilityManagerService::InitDmsImplFunc() +{ + std::lock_guard lock(libLoadLock_); + if (isLoaded_) { + return true; + } + HILOGI("begin"); + dmsImplHandle_ = dlopen("libdistributedschedsvr.z.so", RTLD_NOW); + if (dmsImplHandle_ == nullptr) { + HILOGE("dlopen libdistributedschedsvr failed with errno:%s!", dlerror()); + return false; + } + + onRemoteRequestFunc_ = reinterpret_cast(dlsym(dmsImplHandle_, "OnRemoteRequest")); + if (onRemoteRequestFunc_ == nullptr) { + dlclose(dmsImplHandle_); + HILOGE("get OnRemoteRequest function error"); + return false; + } + deviceOnlineNotifyFunc_ = reinterpret_cast(dlsym(dmsImplHandle_, "DeviceOnlineNotify")); + if (deviceOnlineNotifyFunc_ == nullptr) { + dlclose(dmsImplHandle_); + HILOGE("get DeviceOnlineNotify function error"); + return false; + } + deviceOfflineNotifyFunc_ = reinterpret_cast(dlsym(dmsImplHandle_, "DeviceOfflineNotify")); + if (deviceOfflineNotifyFunc_ == nullptr) { + dlclose(dmsImplHandle_); + HILOGE("get DeviceOfflineNotify function error"); + return false; + } + OnStartFunc onStartFunc = reinterpret_cast(dlsym(dmsImplHandle_, "OnStart")); + if (onStartFunc == nullptr) { + dlclose(dmsImplHandle_); + HILOGE("get OnStart function error"); + return false; + } + onStartFunc(); + isLoaded_ = true; + HILOGI("end"); + return true; +} + +int32_t DistributedAbilityManagerService::SendRequestToImpl(uint32_t code, MessageParcel& data, + MessageParcel& reply, MessageOption& option) +{ + if (InitDmsImplFunc()) { + return onRemoteRequestFunc_(code, data, reply, option); + } + return INVALID_PARAMETERS_ERR; +} + +void DistributedAbilityManagerService::DeviceOnlineNotify(const std::string& deviceId) +{ + if (InitDmsImplFunc()) { + deviceOnlineNotifyFunc_(deviceId); + } +} + +void DistributedAbilityManagerService::DeviceOfflineNotify(const std::string& deviceId) +{ + if (InitDmsImplFunc()) { + deviceOfflineNotifyFunc_(deviceId); + } +} +} // namespace DistributedSchedule +} // namespace OHOS diff --git a/services/dtbabilitymgr/src/distributed_ability_manager_stub.cpp b/services/dtbabilitymgr/src/distributed_ability_manager_stub.cpp new file mode 100644 index 00000000..0bab09fc --- /dev/null +++ b/services/dtbabilitymgr/src/distributed_ability_manager_stub.cpp @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "distributed_ability_manager_stub.h" + +namespace OHOS { +namespace DistributedSchedule { +int32_t DistributedAbilityManagerStub::OnRemoteRequest(uint32_t code, + MessageParcel& data, MessageParcel& reply, MessageOption& option) +{ + return SendRequestToImpl(code, data, reply, option); +} +} // namespace DistributedSchedule +} // namespace OHOS diff --git a/services/dtbschedmgr/BUILD.gn b/services/dtbschedmgr/BUILD.gn index 9e5b310b..148dc6a6 100644 --- a/services/dtbschedmgr/BUILD.gn +++ b/services/dtbschedmgr/BUILD.gn @@ -33,11 +33,8 @@ ohos_shared_library("distributedschedsvr") { sources = [ "src/ability_connection_wrapper_proxy.cpp", "src/ability_connection_wrapper_stub.cpp", - "src/adapter/dnetwork_adapter.cpp", "src/bundle/bundle_manager_internal.cpp", "src/connect_death_recipient.cpp", - "src/deviceManager/dms_device_info.cpp", - "src/distributed_device_node_listener.cpp", "src/distributed_sched_adapter.cpp", "src/distributed_sched_continuation.cpp", "src/distributed_sched_dumper.cpp", @@ -50,7 +47,6 @@ ohos_shared_library("distributedschedsvr") { "src/dms_free_install_callback_proxy.cpp", "src/dms_free_install_callback_stub.cpp", "src/dms_token_callback.cpp", - "src/dtbschedmgr_device_info_storage.cpp", ] configs = [ ":distributed_sched_config" ] @@ -64,6 +60,7 @@ ohos_shared_library("distributedschedsvr") { "bundle_framework:appexecfwk_core", "device_auth:deviceauth_sdk", "distributeddatamgr:distributeddata_inner", + "dmsfwk_standard:dmsbaseinner", "dsoftbus:softbus_client", "eventhandler:libeventhandler", "hiviewdfx_hilog_native:libhilog", diff --git a/services/dtbschedmgr/include/distributed_sched_service.h b/services/dtbschedmgr/include/distributed_sched_service.h index 08cdc563..64e6cd7c 100755 --- a/services/dtbschedmgr/include/distributed_sched_service.h +++ b/services/dtbschedmgr/include/distributed_sched_service.h @@ -31,7 +31,6 @@ #include "nocopyable.h" #endif #include "single_instance.h" -#include "system_ability.h" namespace OHOS { namespace DistributedSchedule { @@ -58,15 +57,13 @@ struct ProcessDiedNotifyInfo { TargetComponent targetComponent; }; -class DistributedSchedService : public SystemAbility, public DistributedSchedStub { -DECLARE_SYSTEM_ABILITY(DistributedSchedService); +class DistributedSchedService : public DistributedSchedStub { DECLARE_SINGLE_INSTANCE_BASE(DistributedSchedService); public: ~DistributedSchedService() = default; - void OnStart() override; - void OnStop() override; + void OnStart(); int32_t StartRemoteAbility(const OHOS::AAFwk::Want& want, int32_t callerUid, int32_t requestCode, uint32_t accessToken) override; int32_t StartAbilityFromRemote(const OHOS::AAFwk::Want& want, diff --git a/services/dtbschedmgr/include/distributed_sched_stub.h b/services/dtbschedmgr/include/distributed_sched_stub.h index f400fe62..d48f607c 100644 --- a/services/dtbschedmgr/include/distributed_sched_stub.h +++ b/services/dtbschedmgr/include/distributed_sched_stub.h @@ -30,6 +30,7 @@ public: ~DistributedSchedStub(); int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; + protected: #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER virtual std::unique_ptr GetRemoteSnapshotInfo(const std::u16string& deviceId, int32_t missionId) diff --git a/services/dtbschedmgr/src/distributed_sched_service.cpp b/services/dtbschedmgr/src/distributed_sched_service.cpp old mode 100755 new mode 100644 index 4d5ee661..5ed787eb --- a/services/dtbschedmgr/src/distributed_sched_service.cpp +++ b/services/dtbschedmgr/src/distributed_sched_service.cpp @@ -77,11 +77,39 @@ constexpr int32_t DISTRIBUTED_COMPONENT_REMOVE = 2; constexpr int32_t REPORT_DISTRIBUTED_COMPONENT_CHANGE_CODE = 1; } -IMPLEMENT_SINGLE_INSTANCE(DistributedSchedService); +extern "C" { +void OnStart() +{ + DistributedSchedService::GetInstance().OnStart(); +} + +int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, + MessageOption& option) +{ + return DistributedSchedService::GetInstance().OnRemoteRequest(code, data, reply, option); +} + +void DeviceOnlineNotify(const std::string& deviceId) +{ + DistributedSchedAdapter::GetInstance().DeviceOnline(deviceId); +#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER + DistributedSchedMissionManager::GetInstance().DeviceOnlineNotify(deviceId); +#endif +} + +void DeviceOfflineNotify(const std::string& deviceId) +{ + DistributedSchedAdapter::GetInstance().DeviceOffline(deviceId); +#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER + DistributedSchedMissionManager::GetInstance().DeviceOfflineNotify(deviceId); +#endif +} +} -const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(&DistributedSchedService::GetInstance()); +IMPLEMENT_SINGLE_INSTANCE(DistributedSchedService); +static const sptr INSTANCE = &DistributedSchedService::GetInstance(); -DistributedSchedService::DistributedSchedService() : SystemAbility(DISTRIBUTED_SCHED_SA_ID, true) +DistributedSchedService::DistributedSchedService() { } @@ -114,18 +142,8 @@ bool DistributedSchedService::Init() #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER DistributedSchedMissionManager::GetInstance().Init(); #endif - bool ret = Publish(this); - if (!ret) { - HILOGE("Init Publish failed!"); - return false; - } - - if (!DtbschedmgrDeviceInfoStorage::GetInstance().Init()) { - HILOGE("Init DtbschedmgrDeviceInfoStorage init failed."); - } HILOGD("Init init success."); DistributedSchedAdapter::GetInstance().Init(); - DnetworkAdapter::GetInstance()->Init(); #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER DistributedSchedMissionManager::GetInstance().InitDataStorage(); #endif @@ -140,11 +158,6 @@ bool DistributedSchedService::Init() return true; } -void DistributedSchedService::OnStop() -{ - HILOGD("OnStop ready to stop service."); -} - int32_t DistributedSchedService::StartRemoteAbility(const OHOS::AAFwk::Want& want, int32_t callerUid, int32_t requestCode, uint32_t accessToken) { @@ -758,8 +771,7 @@ int32_t DistributedSchedService::StartRemoteAbilityByCall(const OHOS::AAFwk::Wan HILOGE("StartRemoteAbilityByCall check deviceId failed"); return INVALID_PARAMETERS_ERR; } - CallerInfo callerInfo; - callerInfo = { callerUid, callerPid }; + CallerInfo callerInfo = { callerUid, callerPid }; callerInfo.sourceDeviceId = localDeviceId; callerInfo.accessToken = accessToken; if (!BundleManagerInternal::GetCallerAppIdFromBms(callerInfo.uid, callerInfo.callerAppId)) { diff --git a/services/dtbschedmgr/test/BUILD.gn b/services/dtbschedmgr/test/BUILD.gn index f1efbb0e..3c0037cd 100644 --- a/services/dtbschedmgr/test/BUILD.gn +++ b/services/dtbschedmgr/test/BUILD.gn @@ -37,6 +37,7 @@ dsched_external_deps = [ "bundle_framework:appexecfwk_core", "device_auth:deviceauth_sdk", "distributeddatamgr:distributeddata_inner", + "dmsfwk_standard:dmsbaseinner", "dsoftbus:softbus_client", "eventhandler:libeventhandler", "hiviewdfx_hilog_native:libhilog", @@ -60,11 +61,8 @@ dsched_public_deps = [ dtbschedmgr_sources = [ "${distributed_service}/dtbschedmgr/src/ability_connection_wrapper_proxy.cpp", "${distributed_service}/dtbschedmgr/src/ability_connection_wrapper_stub.cpp", - "${distributed_service}/dtbschedmgr/src/adapter/dnetwork_adapter.cpp", "${distributed_service}/dtbschedmgr/src/bundle/bundle_manager_internal.cpp", "${distributed_service}/dtbschedmgr/src/connect_death_recipient.cpp", - "${distributed_service}/dtbschedmgr/src/deviceManager/dms_device_info.cpp", - "${distributed_service}/dtbschedmgr/src/distributed_device_node_listener.cpp", "${distributed_service}/dtbschedmgr/src/distributed_sched_adapter.cpp", "${distributed_service}/dtbschedmgr/src/distributed_sched_continuation.cpp", "${distributed_service}/dtbschedmgr/src/distributed_sched_dumper.cpp", @@ -77,7 +75,6 @@ dtbschedmgr_sources = [ "${distributed_service}/dtbschedmgr/src/dms_free_install_callback_proxy.cpp", "${distributed_service}/dtbschedmgr/src/dms_free_install_callback_stub.cpp", "${distributed_service}/dtbschedmgr/src/dms_token_callback.cpp", - "${distributed_service}/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp", ] if (dmsfwk_standard_mission_manager) { -- Gitee