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