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 #ifndef OHOS_SHARING_DOMAIN_RPC_MANAGER_H
17 #define OHOS_SHARING_DOMAIN_RPC_MANAGER_H
18 
19 #include <mutex>
20 #include "domain_def.h"
21 #include "domain_rpc_client.h"
22 #include "domain_rpc_service.h"
23 #include "singleton.h"
24 
25 namespace OHOS {
26 namespace Sharing {
27 
28 class DomainRpcManager final : public ITransmitMgr,
29                                public DelayedSingleton<DomainRpcManager>,
30                                public std::enable_shared_from_this<DomainRpcManager>{
31     DECLARE_DELAYED_SINGLETON(DomainRpcManager);
32 public:
33     //impl ITransmitMgr
34     bool IsPeerExist(std::string peerId) override;
35     int32_t SendDomainRequest(std::string remoteId, std::shared_ptr<BaseDomainMsg> BaseMsg) override;
36 
37     int32_t DelRpcClient(std::string remoteId);
38     int32_t AddRpcClient(std::string remoteId);
39     int32_t AddDomainRpcService(DomainRpcService* service);
40     int32_t AddRpcClient(std::string remoteId, sptr<IDomainRpcService> peerProxy);
41 
42     void OnPeerConnected(std::string remoteId) final;
43     void OnPeerDisconnected(std::string remoteId) final;
44     void OnDomainRequest(std::string remoteId, std::shared_ptr<BaseDomainMsg> BaseMsg) final;
45 
46     int32_t Init();
47     int32_t DeInit();
48 
49 private:
50     int32_t ClearRpcClient();
51 
52 private:
53     std::mutex mutex_;
54     std::map<std::string, std::shared_ptr<DomainRpcClient>> rpcClients_;
55 
56     DomainRpcService *localService_ = nullptr;
57 };
58 
59 } // namespace Sharing
60 } // namespace OHOS
61 #endif