1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ark_communication_provider.h"
17 #include "device_manager_adapter.h"
18 #include "log_print.h"
19 
20 #undef LOG_TAG
21 #define LOG_TAG "ArkCommunicationProvider"
22 
23 namespace OHOS {
24 namespace AppDistributedKv {
25 using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter;
Init()26 CommunicationProvider &ArkCommunicationProvider::Init()
27 {
28     static ArkCommunicationProvider instance;
29     if (instance.isInited) {
30         return instance;
31     }
32     ZLOGI("begin");
33     std::lock_guard<std::mutex> lock(mutex_);
34     if (!instance.isInited) {
35         instance.Initialize();
36     }
37     instance.isInited = true;
38     ZLOGI("normal end");
39     return instance;
40 }
ArkCommunicationProvider()41 ArkCommunicationProvider::ArkCommunicationProvider()
42     : CommunicationProviderImpl(appPipeMgrImpl_)
43 {
44 }
45 
GetLocalDevice() const46 DeviceInfo ArkCommunicationProvider::GetLocalDevice() const
47 {
48     if (deviceQuery_ == nullptr) {
49         return DmAdapter::GetInstance().GetLocalDevice();
50     }
51     return deviceQuery_->GetLocalDevice();
52 }
53 
GetRemoteDevices() const54 std::vector<DeviceInfo> ArkCommunicationProvider::GetRemoteDevices() const
55 {
56     if (deviceQuery_ == nullptr) {
57         return DmAdapter::GetInstance().GetRemoteDevices();
58     }
59     return deviceQuery_->GetRemoteDevices();
60 }
61 
GetDeviceInfo(const std::string & networkId) const62 DeviceInfo ArkCommunicationProvider::GetDeviceInfo(const std::string &networkId) const
63 {
64     if (deviceQuery_ == nullptr) {
65         return DmAdapter::GetInstance().GetDeviceInfo(networkId);
66     }
67     return deviceQuery_->GetDeviceInfo(networkId);
68 }
69 
SetDeviceQuery(std::shared_ptr<IDeviceQuery> deviceQuery)70 void ArkCommunicationProvider::SetDeviceQuery(std::shared_ptr<IDeviceQuery> deviceQuery)
71 {
72     ZLOGI("set device query");
73     deviceQuery_ = deviceQuery;
74 }
75 }  // namespace AppDistributedKv
76 }  // namespace OHOS
77