1 /* 2 * Copyright (c) 2021-2023 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 33 namespace OHOS { 34 namespace Storage { 35 namespace DistributedFile { 36 constexpr uint8_t LINK_TYPE_AP = 1; 37 constexpr uint8_t LINK_TYPE_P2P = 2; 38 constexpr std::string_view GROUP_TYPE_AP = "hmdfs_WifiGroup"; 39 constexpr std::string_view GROUP_TYPE_P2P = "hmdfs_P2PGroup"; 40 41 class NetworkAgentTemplate : public DfsuStartable, public DfsuActor<NetworkAgentTemplate> { 42 public: NetworkAgentTemplate(std::weak_ptr<MountPoint> mountPoint)43 explicit NetworkAgentTemplate(std::weak_ptr<MountPoint> mountPoint) 44 : DfsuActor<NetworkAgentTemplate>(this), 45 mountPoint_(mountPoint), 46 kernerlTalker_(std::make_shared<KernelTalker>( 47 mountPoint, 48 [&](NotifyParam ¶m) { GetSessionProcess(param); }, 49 [&](const std::string &cid) { CloseSessionForOneDevice(cid); })), 50 sessionPool_(kernerlTalker_) 51 { 52 } ~NetworkAgentTemplate()53 virtual ~NetworkAgentTemplate() {} 54 void Start(); 55 void Stop(); 56 void ConnectOnlineDevices(); 57 void DisconnectAllDevices(); 58 void ConnectDeviceAsync(const DeviceInfo info); 59 void DisconnectDevice(const DeviceInfo info); 60 void AcceptSession(std::shared_ptr<BaseSession> session); 61 void ConnectDeviceByP2PAsync(const DeviceInfo info); GetMountPoint()62 std::shared_ptr<MountPoint> GetMountPoint() 63 { 64 return mountPoint_.lock(); 65 }; 66 protected: 67 virtual void JoinDomain() = 0; 68 virtual void QuitDomain() = 0; 69 virtual void StopTopHalf() = 0; 70 virtual void StopBottomHalf() = 0; 71 virtual void OpenSession(const DeviceInfo &info, const uint8_t &linkType) = 0; 72 virtual void CloseSession(std::shared_ptr<BaseSession> session) = 0; 73 74 std::weak_ptr<MountPoint> mountPoint_; 75 76 private: 77 void HandleAllNotify(int fd); 78 void NotifyHandler(NotifyParam ¶m); 79 void GetSessionProcess(NotifyParam ¶m); 80 void GetSession(const std::string &cid); 81 void CloseSessionForOneDevice(const std::string &cid); 82 void AcceptSessionInner(std::shared_ptr<BaseSession> session); 83 void GetSessionProcessInner(NotifyParam param); 84 85 std::mutex taskMut_; 86 std::list<Utils::DfsuThread> tasks_; 87 std::shared_ptr<KernelTalker> kernerlTalker_; 88 SessionPool sessionPool_; 89 }; 90 } // namespace DistributedFile 91 } // namespace Storage 92 } // namespace OHOS 93 #endif // NETWORK_AGENT_TEMPLATE_H