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 #include "network/network_agent_template.h"
17 #include "device/device_manager_agent.h"
18 #include "dfsu_exception.h"
19 #include "utils_log.h"
20
21 namespace OHOS {
22 namespace Storage {
23 namespace DistributedFile {
24 using namespace std;
25 namespace {
26 constexpr int MAX_RETRY_COUNT = 7;
27 constexpr int OPEN_SESSSION_DELAY_TIME = 100;
28 } // namespace
29
Start()30 void NetworkAgentTemplate::Start()
31 {
32 JoinDomain();
33 kernerlTalker_->CreatePollThread();
34 ConnectOnlineDevices();
35 }
36
Stop()37 void NetworkAgentTemplate::Stop()
38 {
39 StopTopHalf();
40 StopBottomHalf();
41 kernerlTalker_->WaitForPollThreadExited();
42 }
43
ConnectDeviceAsync(const DeviceInfo info)44 void NetworkAgentTemplate::ConnectDeviceAsync(const DeviceInfo info)
45 {
46 std::this_thread::sleep_for(std::chrono::milliseconds(
47 OPEN_SESSSION_DELAY_TIME)); // Temporary workaround for time sequence issues(offline-onSessionOpened)
48 OpenSession(info);
49 }
50
ConnectOnlineDevices()51 void NetworkAgentTemplate::ConnectOnlineDevices()
52 {
53 auto dma = DeviceManagerAgent::GetInstance();
54 auto infos = dma->GetRemoteDevicesInfo();
55 LOGI("Have %{public}d devices Online", infos.size());
56 for (const auto &info : infos) {
57 auto cmd = make_unique<DfsuCmd<NetworkAgentTemplate, const DeviceInfo>>(
58 &NetworkAgentTemplate::ConnectDeviceAsync, info);
59 cmd->UpdateOption({.tryTimes_ = MAX_RETRY_COUNT});
60 Recv(move(cmd));
61 }
62 }
63
DisconnectAllDevices()64 void NetworkAgentTemplate::DisconnectAllDevices()
65 {
66 sessionPool_.ReleaseAllSession();
67 }
68
DisconnectDevice(const DeviceInfo info)69 void NetworkAgentTemplate::DisconnectDevice(const DeviceInfo info)
70 {
71 LOGI("DeviceOffline, cid:%{public}s", info.GetCid().c_str());
72 sessionPool_.ReleaseSession(info.GetCid());
73 }
74
CloseSessionForOneDevice(const string & cid)75 void NetworkAgentTemplate::CloseSessionForOneDevice(const string &cid)
76 {
77 LOGI("session closed!");
78 }
79
AcceptSession(shared_ptr<BaseSession> session)80 void NetworkAgentTemplate::AcceptSession(shared_ptr<BaseSession> session)
81 {
82 auto cmd = make_unique<DfsuCmd<NetworkAgentTemplate, shared_ptr<BaseSession>>>(
83 &NetworkAgentTemplate::AcceptSessionInner, session);
84 cmd->UpdateOption({.tryTimes_ = 1});
85 Recv(move(cmd));
86 }
87
AcceptSessionInner(shared_ptr<BaseSession> session)88 void NetworkAgentTemplate::AcceptSessionInner(shared_ptr<BaseSession> session)
89 {
90 auto cid = session->GetCid();
91 LOGI("AcceptSesion, cid:%{public}s", cid.c_str());
92 sessionPool_.HoldSession(session);
93 }
94
GetSessionProcess(NotifyParam & param)95 void NetworkAgentTemplate::GetSessionProcess(NotifyParam ¶m)
96 {
97 auto cmd = make_unique<DfsuCmd<NetworkAgentTemplate, NotifyParam>>(
98 &NetworkAgentTemplate::GetSessionProcessInner, param);
99 cmd->UpdateOption({.tryTimes_ = 1});
100 Recv(move(cmd));
101 }
102
GetSessionProcessInner(NotifyParam param)103 void NetworkAgentTemplate::GetSessionProcessInner(NotifyParam param)
104 {
105 string cidStr(param.remoteCid, CID_MAX_LEN);
106 int fd = param.fd;
107 LOGI("NOTIFY_GET_SESSION, old fd %{public}d, remote cid %{public}s", fd, cidStr.c_str());
108 sessionPool_.ReleaseSession(fd);
109 GetSession(cidStr);
110 }
111
GetSession(const string & cid)112 void NetworkAgentTemplate::GetSession(const string &cid)
113 {
114 DeviceInfo deviceInfo;
115 deviceInfo.SetCid(cid);
116 try {
117 OpenSession(deviceInfo);
118 } catch (const DfsuException &e) {
119 LOGE("reget session failed, code: %{public}d", e.code());
120 }
121 }
122 } // namespace DistributedFile
123 } // namespace Storage
124 } // namespace OHOS