1 /*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "domain_manager.h"
17 #include "common/sharing_log.h"
18 #include "interaction/domain/rpc/domain_rpc_manager.h"
19 #include "interaction/interaction_manager.h"
20 #include "interaction/ipc_codec/ipc_msg.h"
21
22 namespace OHOS {
23 namespace Sharing {
24
DomainManager()25 DomainManager::DomainManager()
26 {
27 SHARING_LOGD("trace.");
28 DmKit::InitDeviceManager();
29 localDeviceInfo_ = DmKit::GetLocalDevicesInfo();
30 trustedDeviceInfos_ = DmKit::GetTrustedDevicesInfo();
31 }
32
~DomainManager()33 DomainManager::~DomainManager()
34 {
35 SHARING_LOGD("trace.");
36 std::unique_lock lock(mutex_);
37 transmitMgrs_.clear();
38 peerTypeMap_.clear();
39 listener_ = nullptr;
40 }
41
SendDomainRequest(std::string remoteId,std::shared_ptr<BaseDomainMsg> BaseMsg)42 int32_t DomainManager::SendDomainRequest(std::string remoteId, std::shared_ptr<BaseDomainMsg> BaseMsg)
43 {
44 SHARING_LOGD("trace.");
45 auto mgr = FindMgrByRemoteId(BaseMsg->toDevId);
46 if (mgr != nullptr) {
47 SHARING_LOGD("mgr exist.");
48 mgr->SendDomainRequest(BaseMsg->toDevId, BaseMsg);
49 } else {
50 if (!BaseMsg->toDevId.empty()) {
51 SHARING_LOGD("AddRpcClient.");
52 // first send need to create RPC client
53 DomainRpcManager::GetInstance()->AddRpcClient(BaseMsg->toDevId);
54 DomainRpcManager::GetInstance()->SendDomainRequest(BaseMsg->toDevId, BaseMsg);
55 } else {
56 SHARING_LOGE("remote device is not exist.");
57 }
58 }
59
60 return 0;
61 }
62
OnDomainRequest(std::string remoteId,std::shared_ptr<BaseDomainMsg> BaseMsg)63 void DomainManager::OnDomainRequest(std::string remoteId, std::shared_ptr<BaseDomainMsg> BaseMsg)
64 {
65 SHARING_LOGD("redirect to interactionMgr.");
66 if (listener_!= nullptr) {
67 listener_->OnDomainMsg(BaseMsg);
68 } else {
69 SHARING_LOGE("listener is null.");
70 }
71 }
72
AddServiceManager(std::shared_ptr<ITransmitMgr> mgr)73 int32_t DomainManager::AddServiceManager(std::shared_ptr<ITransmitMgr> mgr)
74 {
75 SHARING_LOGD("trace.");
76 std::unique_lock lock(mutex_);
77 mgr->SetListener(shared_from_this());
78 transmitMgrs_.insert(std::make_pair(mgr->GetDomainType(), mgr));
79
80 return 0;
81 }
82
AddPeer(std::shared_ptr<ITransmitMgr> mgr,std::shared_ptr<IDomainPeer> caller)83 int32_t DomainManager::AddPeer(std::shared_ptr<ITransmitMgr> mgr, std::shared_ptr<IDomainPeer> caller)
84 {
85 SHARING_LOGD("trace, remoteId: %{public}s.", caller->GetRemoteId().c_str());
86 std::unique_lock lock(mutex_);
87 peerTypeMap_.insert(std::make_pair(caller->GetRemoteId(), caller->GetDomainType()));
88
89 return 0;
90 }
91
DelPeer(std::string remoteId)92 int32_t DomainManager::DelPeer(std::string remoteId)
93 {
94 SHARING_LOGD("trace, remoteId: %{public}s.", remoteId.c_str());
95 std::unique_lock lock(mutex_);
96 if (peerTypeMap_.find(remoteId) != peerTypeMap_.end()) {
97 peerTypeMap_.erase(remoteId);
98 } else {
99 SHARING_LOGE("remoteId is not exist.");
100 return -1;
101 }
102
103 return 0;
104 }
105
FindMgrByRemoteId(std::string remoteId)106 std::shared_ptr<ITransmitMgr> DomainManager::FindMgrByRemoteId(std::string remoteId)
107 {
108 SHARING_LOGD("trace, peerTypeMap size: %{public}zu.", peerTypeMap_.size());
109 std::unique_lock lock(mutex_);
110 auto iter = peerTypeMap_.find(remoteId);
111 if (iter != peerTypeMap_.end()) {
112 auto mgrIter = transmitMgrs_.find(iter->second);
113 if (mgrIter != transmitMgrs_.end()) {
114 return mgrIter->second;
115 } else {
116 SHARING_LOGE("mgr is null.");
117 }
118 } else {
119 SHARING_LOGE("remoteId is not exist.");
120 }
121
122 return nullptr;
123 }
124
SetListener(DomainManagerListener * listener)125 void DomainManager::SetListener(DomainManagerListener *listener)
126 {
127 listener_ = listener;
128 }
129
130 } // namespace Sharing
131 } // namespace OHOS