1 /*
2 * Copyright (c) 2024 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 #ifndef FILEMANAGEMENT_DFS_SERVICE_ALL_CONNECT_MANAGER_H
17 #define FILEMANAGEMENT_DFS_SERVICE_ALL_CONNECT_MANAGER_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 
23 #include "block_object.h"
24 #include "service_collaboration_manager_capi.h"
25 
26 namespace OHOS {
27 namespace Storage {
28 namespace DistributedFile {
29 class AllConnectManager {
30 public:
31     static AllConnectManager &GetInstance();
32     int32_t InitAllConnectManager();
33     int32_t UnInitAllConnectManager();
34     int32_t PublishServiceState(const std::string &peerNetworkId,
35                                 ServiceCollaborationManagerBussinessStatus state);
36     int32_t ApplyAdvancedResource(const std::string &peerNetworkId,
37                                   ServiceCollaborationManager_ResourceRequestInfoSets *resourceRequest);
38     std::shared_ptr<ServiceCollaborationManager_ResourceRequestInfoSets> BuildResourceRequest();
39 
40 private:
41     AllConnectManager();
42     ~AllConnectManager() = default;
43     int32_t GetAllConnectSoLoad();
44     int32_t RegisterLifecycleCallback();
45     int32_t UnRegisterLifecycleCallback();
46 
47     static int32_t OnStop(const char *peerNetworkId);
48     static int32_t ApplyResult(int32_t errorcode, int32_t result, const char *reason);
49 
50     std::mutex allConnectLock_;
51     void *dllHandle_ = nullptr;
52 
53     ServiceCollaborationManager_API allConnect_ = {
54         .ServiceCollaborationManager_PublishServiceState = nullptr,
55         .ServiceCollaborationManager_ApplyAdvancedResource = nullptr,
56         .ServiceCollaborationManager_RegisterLifecycleCallback = nullptr,
57         .ServiceCollaborationManager_UnRegisterLifecycleCallback = nullptr,
58     };
59     ServiceCollaborationManager_Callback allConnectCallback_;
60     std::shared_ptr<ServiceCollaborationManager_HardwareRequestInfo> remoteHardwareList_;
61     std::shared_ptr<ServiceCollaborationManager_HardwareRequestInfo> localHardwareList_;
62     std::shared_ptr<ServiceCollaborationManager_CommunicationRequestInfo> communicationRequest_;
63 
64     static std::shared_ptr<BlockObject<bool>> applyResultBlock_;
65     static constexpr uint32_t BLOCK_INTERVAL_ALLCONNECT = 60 * 1000;
66     static inline const std::string SERVICE_NAME {"Distributedfile"};
67 };
68 
69 } // namespace DistributedFile
70 } // namespace Storage
71 } // namespace OHOS
72 #endif // FILEMANAGEMENT_DFS_SERVICE_ALL_CONNECT_MANAGER_H