1 /* 2 * Copyright (c) 2021 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 24 #include "device/device_info.h" 25 #include "dfsu_actor.h" 26 #include "dfsu_startable.h" 27 #include "dfsu_thread.h" 28 #include "mountpoint/mount_point.h" 29 #include "network/kernel_talker.h" 30 #include "network/session_pool.h" 31 32 namespace OHOS { 33 namespace Storage { 34 namespace DistributedFile { 35 class NetworkAgentTemplate : public DfsuStartable, public DfsuActor<NetworkAgentTemplate> { 36 public: NetworkAgentTemplate(std::weak_ptr<MountPoint> mountPoint)37 explicit NetworkAgentTemplate(std::weak_ptr<MountPoint> mountPoint) 38 : DfsuActor<NetworkAgentTemplate>(this), 39 mountPoint_(mountPoint), 40 kernerlTalker_(std::make_shared<KernelTalker>( 41 mountPoint, 42 [&](NotifyParam ¶m) { GetSessionProcess(param); }, 43 [&](const std::string &cid) { CloseSessionForOneDevice(cid); })), 44 sessionPool_(kernerlTalker_) 45 { 46 } ~NetworkAgentTemplate()47 virtual ~NetworkAgentTemplate() {} 48 void Start(); 49 void Stop(); 50 void ConnectOnlineDevices(); 51 void DisconnectAllDevices(); 52 void ConnectDeviceAsync(const DeviceInfo info); 53 void DisconnectDevice(const DeviceInfo info); 54 void AcceptSession(std::shared_ptr<BaseSession> session); GetMountPoint()55 std::shared_ptr<MountPoint> GetMountPoint() 56 { 57 return mountPoint_.lock(); 58 }; 59 protected: 60 virtual void JoinDomain() = 0; 61 virtual void QuitDomain() = 0; 62 virtual void StopTopHalf() = 0; 63 virtual void StopBottomHalf() = 0; 64 virtual void OpenSession(const DeviceInfo &info) = 0; 65 virtual void CloseSession(std::shared_ptr<BaseSession> session) = 0; 66 67 std::weak_ptr<MountPoint> mountPoint_; 68 69 private: 70 void HandleAllNotify(int fd); 71 void NotifyHandler(NotifyParam ¶m); 72 void GetSessionProcess(NotifyParam ¶m); 73 void GetSession(const std::string &cid); 74 void CloseSessionForOneDevice(const std::string &cid); 75 void AcceptSessionInner(std::shared_ptr<BaseSession> session); 76 void GetSessionProcessInner(NotifyParam param); 77 78 std::mutex taskMut_; 79 std::list<Utils::DfsuThread> tasks_; 80 std::shared_ptr<KernelTalker> kernerlTalker_; 81 SessionPool sessionPool_; 82 }; 83 } // namespace DistributedFile 84 } // namespace Storage 85 } // namespace OHOS 86 #endif // NETWORK_AGENT_TEMPLATE_H