• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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}zu 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     (void)cid;
78     LOGI("session closed!");
79 }
80 
AcceptSession(shared_ptr<BaseSession> session)81 void NetworkAgentTemplate::AcceptSession(shared_ptr<BaseSession> session)
82 {
83     auto cmd = make_unique<DfsuCmd<NetworkAgentTemplate, shared_ptr<BaseSession>>>(
84         &NetworkAgentTemplate::AcceptSessionInner, session);
85     cmd->UpdateOption({.tryTimes_ = 1});
86     Recv(move(cmd));
87 }
88 
AcceptSessionInner(shared_ptr<BaseSession> session)89 void NetworkAgentTemplate::AcceptSessionInner(shared_ptr<BaseSession> session)
90 {
91     auto cid = session->GetCid();
92     LOGI("AcceptSesion, cid:%{public}s", cid.c_str());
93     sessionPool_.HoldSession(session);
94 }
95 
GetSessionProcess(NotifyParam & param)96 void NetworkAgentTemplate::GetSessionProcess(NotifyParam &param)
97 {
98     auto cmd = make_unique<DfsuCmd<NetworkAgentTemplate, NotifyParam>>(
99         &NetworkAgentTemplate::GetSessionProcessInner, param);
100     cmd->UpdateOption({.tryTimes_ = 1});
101     Recv(move(cmd));
102 }
103 
GetSessionProcessInner(NotifyParam param)104 void NetworkAgentTemplate::GetSessionProcessInner(NotifyParam param)
105 {
106     string cidStr(param.remoteCid, CID_MAX_LEN);
107     int fd = param.fd;
108     LOGI("NOTIFY_GET_SESSION, old fd %{public}d, remote cid %{public}s", fd, cidStr.c_str());
109     sessionPool_.ReleaseSession(fd);
110     GetSession(cidStr);
111 }
112 
GetSession(const string & cid)113 void NetworkAgentTemplate::GetSession(const string &cid)
114 {
115     DeviceInfo deviceInfo;
116     deviceInfo.SetCid(cid);
117     try {
118         OpenSession(deviceInfo);
119     } catch (const DfsuException &e) {
120         LOGE("reget session failed, code: %{public}d", e.code());
121     }
122 }
123 } // namespace DistributedFile
124 } // namespace Storage
125 } // namespace OHOS
126