1 /* 2 * Copyright (c) 2021-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 NETWORK_AGENT_TEMPLATE_H 17 #define NETWORK_AGENT_TEMPLATE_H 18 19 #include <list> 20 #include <memory> 21 #include <mutex> 22 #include <vector> 23 #include <string_view> 24 25 #include "device/device_info.h" 26 #include "dfsu_actor.h" 27 #include "dfsu_startable.h" 28 #include "dfsu_thread.h" 29 #include "mountpoint/mount_point.h" 30 #include "network/kernel_talker.h" 31 #include "network/session_pool.h" 32 #include "network/softbus/softbus_session_dispatcher.h" 33 #include "nlohmann/json.hpp" 34 #include "utils_directory.h" 35 36 namespace OHOS { 37 namespace Storage { 38 namespace DistributedFile { 39 constexpr uint8_t LINK_TYPE_AP = 1; 40 constexpr uint8_t LINK_TYPE_P2P = 2; 41 constexpr std::string_view GROUP_TYPE_AP = "hmdfs_WifiGroup"; 42 constexpr std::string_view GROUP_TYPE_P2P = "hmdfs_P2PGroup"; 43 44 class NetworkAgentTemplate : public DfsuStartable, public DfsuActor<NetworkAgentTemplate> { 45 public: NetworkAgentTemplate(std::weak_ptr<MountPoint> mountPoint)46 explicit NetworkAgentTemplate(std::weak_ptr<MountPoint> mountPoint) 47 : DfsuActor<NetworkAgentTemplate>(this), 48 mountPoint_(mountPoint), 49 kernerlTalker_(std::make_shared<KernelTalker>( 50 mountPoint, 51 [&](NotifyParam ¶m) { GetSessionProcess(param); }, 52 [&](const std::string &cid) { CloseSessionForOneDevice(cid); })), 53 sessionPool_(kernerlTalker_) 54 { 55 } ~NetworkAgentTemplate()56 virtual ~NetworkAgentTemplate() {} 57 void Start(); 58 void Stop(); 59 void ConnectOnlineDevices(); 60 void DisconnectAllDevices(); 61 void DisconnectDeviceByP2P(const DeviceInfo info); 62 void DisconnectDeviceByP2PHmdfs(const DeviceInfo info); 63 void AcceptSession(std::shared_ptr<BaseSession> session, const std::string backStage); 64 void ConnectDeviceByP2PAsync(const DeviceInfo info); 65 bool FindSocketId(int32_t socketId); GetMountPoint()66 std::shared_ptr<MountPoint> GetMountPoint() 67 { 68 return mountPoint_.lock(); 69 }; 70 protected: 71 virtual void JoinDomain() = 0; 72 virtual void QuitDomain() = 0; 73 virtual void StopTopHalf() = 0; 74 virtual void StopBottomHalf() = 0; 75 virtual int32_t OpenSession(const DeviceInfo &info, const uint8_t &linkType) = 0; 76 virtual void CloseSession(std::shared_ptr<BaseSession> session) = 0; 77 78 std::weak_ptr<MountPoint> mountPoint_; 79 80 private: 81 void HandleAllNotify(int fd); 82 void NotifyHandler(NotifyParam ¶m); 83 void GetSessionProcess(NotifyParam ¶m); 84 void GetSession(const std::string &cid); 85 void CloseSessionForOneDevice(const std::string &cid); 86 void GetSessionProcessInner(NotifyParam param); 87 88 std::mutex taskMut_; 89 std::list<Utils::DfsuThread> tasks_; 90 std::shared_ptr<KernelTalker> kernerlTalker_; 91 SessionPool sessionPool_; 92 }; 93 } // namespace DistributedFile 94 } // namespace Storage 95 } // namespace OHOS 96 #endif // NETWORK_AGENT_TEMPLATE_H