1e0e9324cSopenharmony_ci/*
2e0e9324cSopenharmony_ci * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3e0e9324cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e0e9324cSopenharmony_ci * you may not use this file except in compliance with the License.
5e0e9324cSopenharmony_ci * You may obtain a copy of the License at
6e0e9324cSopenharmony_ci *
7e0e9324cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e0e9324cSopenharmony_ci *
9e0e9324cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e0e9324cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e0e9324cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e0e9324cSopenharmony_ci * See the License for the specific language governing permissions and
13e0e9324cSopenharmony_ci * limitations under the License.
14e0e9324cSopenharmony_ci */
15e0e9324cSopenharmony_ci
16e0e9324cSopenharmony_ci#include "domain_manager.h"
17e0e9324cSopenharmony_ci#include "common/common_macro.h"
18e0e9324cSopenharmony_ci#include "common/sharing_log.h"
19e0e9324cSopenharmony_ci#include "interaction/domain/rpc/domain_rpc_manager.h"
20e0e9324cSopenharmony_ci#include "interaction/interaction_manager.h"
21e0e9324cSopenharmony_ci#include "interaction/ipc_codec/ipc_msg.h"
22e0e9324cSopenharmony_ci
23e0e9324cSopenharmony_cinamespace OHOS {
24e0e9324cSopenharmony_cinamespace Sharing {
25e0e9324cSopenharmony_ci
26e0e9324cSopenharmony_ciDomainManager::DomainManager()
27e0e9324cSopenharmony_ci{
28e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
29e0e9324cSopenharmony_ci    DmKit::InitDeviceManager();
30e0e9324cSopenharmony_ci    localDeviceInfo_ = DmKit::GetLocalDevicesInfo();
31e0e9324cSopenharmony_ci    trustedDeviceInfos_ = DmKit::GetTrustedDevicesInfo();
32e0e9324cSopenharmony_ci}
33e0e9324cSopenharmony_ci
34e0e9324cSopenharmony_ciDomainManager::~DomainManager()
35e0e9324cSopenharmony_ci{
36e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
37e0e9324cSopenharmony_ci    std::unique_lock lock(mutex_);
38e0e9324cSopenharmony_ci    transmitMgrs_.clear();
39e0e9324cSopenharmony_ci    peerTypeMap_.clear();
40e0e9324cSopenharmony_ci}
41e0e9324cSopenharmony_ci
42e0e9324cSopenharmony_ciint32_t DomainManager::SendDomainRequest(std::string remoteId, std::shared_ptr<BaseDomainMsg> BaseMsg)
43e0e9324cSopenharmony_ci{
44e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
45e0e9324cSopenharmony_ci    RETURN_INVALID_IF_NULL(BaseMsg);
46e0e9324cSopenharmony_ci    auto mgr = FindMgrByRemoteId(BaseMsg->toDevId);
47e0e9324cSopenharmony_ci    if (mgr != nullptr) {
48e0e9324cSopenharmony_ci        SHARING_LOGD("mgr exist.");
49e0e9324cSopenharmony_ci        mgr->SendDomainRequest(BaseMsg->toDevId, BaseMsg);
50e0e9324cSopenharmony_ci    } else {
51e0e9324cSopenharmony_ci        if (!BaseMsg->toDevId.empty()) {
52e0e9324cSopenharmony_ci            SHARING_LOGD("AddRpcClient.");
53e0e9324cSopenharmony_ci            // first send  need  to create  RPC client
54e0e9324cSopenharmony_ci            DomainRpcManager::GetInstance()->AddRpcClient(BaseMsg->toDevId);
55e0e9324cSopenharmony_ci            DomainRpcManager::GetInstance()->SendDomainRequest(BaseMsg->toDevId, BaseMsg);
56e0e9324cSopenharmony_ci        } else {
57e0e9324cSopenharmony_ci            SHARING_LOGE("remote device is not exist.");
58e0e9324cSopenharmony_ci        }
59e0e9324cSopenharmony_ci    }
60e0e9324cSopenharmony_ci
61e0e9324cSopenharmony_ci    return 0;
62e0e9324cSopenharmony_ci}
63e0e9324cSopenharmony_ci
64e0e9324cSopenharmony_civoid DomainManager::OnDomainRequest(std::string remoteId, std::shared_ptr<BaseDomainMsg> BaseMsg)
65e0e9324cSopenharmony_ci{
66e0e9324cSopenharmony_ci    SHARING_LOGD("redirect to interactionMgr.");
67e0e9324cSopenharmony_ci    RETURN_IF_NULL(BaseMsg);
68e0e9324cSopenharmony_ci    auto listener = listener_.lock();
69e0e9324cSopenharmony_ci    if (listener != nullptr) {
70e0e9324cSopenharmony_ci        listener->OnDomainMsg(BaseMsg);
71e0e9324cSopenharmony_ci    } else {
72e0e9324cSopenharmony_ci        SHARING_LOGE("listener is null.");
73e0e9324cSopenharmony_ci    }
74e0e9324cSopenharmony_ci}
75e0e9324cSopenharmony_ci
76e0e9324cSopenharmony_ciint32_t DomainManager::AddServiceManager(std::shared_ptr<ITransmitMgr> mgr)
77e0e9324cSopenharmony_ci{
78e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
79e0e9324cSopenharmony_ci    RETURN_INVALID_IF_NULL(mgr);
80e0e9324cSopenharmony_ci    std::unique_lock lock(mutex_);
81e0e9324cSopenharmony_ci    mgr->SetListener(shared_from_this());
82e0e9324cSopenharmony_ci    transmitMgrs_.insert(std::make_pair(mgr->GetDomainType(), mgr));
83e0e9324cSopenharmony_ci
84e0e9324cSopenharmony_ci    return 0;
85e0e9324cSopenharmony_ci}
86e0e9324cSopenharmony_ci
87e0e9324cSopenharmony_ciint32_t DomainManager::AddPeer(std::shared_ptr<ITransmitMgr> mgr, std::shared_ptr<IDomainPeer> caller)
88e0e9324cSopenharmony_ci{
89e0e9324cSopenharmony_ci    RETURN_INVALID_IF_NULL(mgr);
90e0e9324cSopenharmony_ci    RETURN_INVALID_IF_NULL(caller);
91e0e9324cSopenharmony_ci    SHARING_LOGD("trace, remoteId: %{public}s.", caller->GetRemoteId().c_str());
92e0e9324cSopenharmony_ci    std::unique_lock lock(mutex_);
93e0e9324cSopenharmony_ci    peerTypeMap_.insert(std::make_pair(caller->GetRemoteId(), caller->GetDomainType()));
94e0e9324cSopenharmony_ci
95e0e9324cSopenharmony_ci    return 0;
96e0e9324cSopenharmony_ci}
97e0e9324cSopenharmony_ci
98e0e9324cSopenharmony_ciint32_t DomainManager::DelPeer(std::string remoteId)
99e0e9324cSopenharmony_ci{
100e0e9324cSopenharmony_ci    SHARING_LOGD("trace, remoteId: %{public}s.", remoteId.c_str());
101e0e9324cSopenharmony_ci    std::unique_lock lock(mutex_);
102e0e9324cSopenharmony_ci    if (peerTypeMap_.find(remoteId) != peerTypeMap_.end()) {
103e0e9324cSopenharmony_ci        peerTypeMap_.erase(remoteId);
104e0e9324cSopenharmony_ci    } else {
105e0e9324cSopenharmony_ci        SHARING_LOGE("remoteId is not exist.");
106e0e9324cSopenharmony_ci        return -1;
107e0e9324cSopenharmony_ci    }
108e0e9324cSopenharmony_ci
109e0e9324cSopenharmony_ci    return 0;
110e0e9324cSopenharmony_ci}
111e0e9324cSopenharmony_ci
112e0e9324cSopenharmony_cistd::shared_ptr<ITransmitMgr> DomainManager::FindMgrByRemoteId(std::string remoteId)
113e0e9324cSopenharmony_ci{
114e0e9324cSopenharmony_ci    SHARING_LOGD("trace, peerTypeMap size: %{public}zu.", peerTypeMap_.size());
115e0e9324cSopenharmony_ci    std::unique_lock lock(mutex_);
116e0e9324cSopenharmony_ci    auto iter = peerTypeMap_.find(remoteId);
117e0e9324cSopenharmony_ci    if (iter != peerTypeMap_.end()) {
118e0e9324cSopenharmony_ci        auto mgrIter = transmitMgrs_.find(iter->second);
119e0e9324cSopenharmony_ci        if (mgrIter != transmitMgrs_.end()) {
120e0e9324cSopenharmony_ci            return mgrIter->second;
121e0e9324cSopenharmony_ci        } else {
122e0e9324cSopenharmony_ci            SHARING_LOGE("mgr is null.");
123e0e9324cSopenharmony_ci        }
124e0e9324cSopenharmony_ci    } else {
125e0e9324cSopenharmony_ci        SHARING_LOGE("remoteId is not exist.");
126e0e9324cSopenharmony_ci    }
127e0e9324cSopenharmony_ci
128e0e9324cSopenharmony_ci    return nullptr;
129e0e9324cSopenharmony_ci}
130e0e9324cSopenharmony_ci
131e0e9324cSopenharmony_civoid DomainManager::SetListener(DomainManagerListener::Ptr listener)
132e0e9324cSopenharmony_ci{
133e0e9324cSopenharmony_ci    listener_ = listener;
134e0e9324cSopenharmony_ci}
135e0e9324cSopenharmony_ci
136e0e9324cSopenharmony_ci} // namespace Sharing
137e0e9324cSopenharmony_ci} // namespace OHOS