1 /*
2  * Copyright (c) 2023-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 #include "network/softbus/softbus_file_send_listener.h"
17 
18 #include <cinttypes>
19 
20 #include "dfs_error.h"
21 #include "network/softbus/softbus_handler.h"
22 #include "network/softbus/softbus_session_pool.h"
23 #include "trans_mananger.h"
24 #include "utils_log.h"
25 
26 namespace OHOS {
27 namespace Storage {
28 namespace DistributedFile {
OnFile(int32_t socket,FileEvent * event)29 void SoftBusFileSendListener::OnFile(int32_t socket, FileEvent *event)
30 {
31     if (event == nullptr) {
32         LOGE("invalid paramter");
33         return;
34     }
35     switch (event->type) {
36         case FILE_EVENT_SEND_PROCESS:
37             OnSendFileProcess(socket, event->bytesProcessed, event->bytesTotal);
38             break;
39         case FILE_EVENT_SEND_FINISH:
40             OnSendFileFinished(socket);
41             break;
42         case FILE_EVENT_SEND_ERROR:
43             OnFileTransError(socket, event->errorCode);
44             break;
45         case FILE_EVENT_TRANS_STATUS:
46             OnSendFileReport(socket, event->statusList, event->errorCode);
47             break;
48         default:
49             LOGI("Other situations");
50             break;
51     }
52 }
53 
GetLocalSessionName(int32_t sessionId)54 std::string SoftBusFileSendListener::GetLocalSessionName(int32_t sessionId)
55 {
56     std::string sessionName = "";
57     sessionName = SoftBusHandler::GetSessionName(sessionId);
58     return sessionName;
59 }
60 
OnSendFileProcess(int32_t sessionId,uint64_t bytesUpload,uint64_t bytesTotal)61 void SoftBusFileSendListener::OnSendFileProcess(int32_t sessionId, uint64_t bytesUpload, uint64_t bytesTotal)
62 {
63     LOGI("OnSendFileProcess, sessionId = %{public}d bytesUpload = %{public}" PRIu64 "bytesTotal = %{public}" PRIu64 "",
64          sessionId, bytesUpload, bytesTotal);
65 }
66 
OnSendFileFinished(int32_t sessionId)67 void SoftBusFileSendListener::OnSendFileFinished(int32_t sessionId)
68 {
69     LOGI("OnSendFileFinished, sessionId = %{public}d", sessionId);
70     std::string sessionName = GetLocalSessionName(sessionId);
71     if (sessionName.empty()) {
72         LOGE("sessionName is empty");
73         return;
74     }
75     SoftBusHandler::GetInstance().CloseSession(sessionId, sessionName);
76 }
77 
OnFileTransError(int32_t sessionId,int32_t errorCode)78 void SoftBusFileSendListener::OnFileTransError(int32_t sessionId, int32_t errorCode)
79 {
80     LOGE("OnFileTransError");
81     std::string sessionName = GetLocalSessionName(sessionId);
82     if (sessionName.empty()) {
83         LOGE("sessionName is empty");
84         return;
85     }
86     SoftBusHandler::GetInstance().CloseSession(sessionId, sessionName);
87 }
88 
OnSendFileReport(int32_t sessionId,FileStatusList statusList,int32_t errorCode)89 void SoftBusFileSendListener::OnSendFileReport(int32_t sessionId, FileStatusList statusList, int32_t errorCode)
90 {
91     LOGE("OnSendFileReport");
92 }
93 
OnSendFileShutdown(int32_t sessionId,ShutdownReason reason)94 void SoftBusFileSendListener::OnSendFileShutdown(int32_t sessionId, ShutdownReason reason)
95 {
96     LOGE("OnSendFileShutdown, sessionId is %{public}d", sessionId);
97     std::string sessionName = GetLocalSessionName(sessionId);
98     if (sessionName.empty()) {
99         LOGE("sessionName is empty");
100         return;
101     }
102     SoftBusHandler::GetInstance().CloseSession(sessionId, sessionName);
103 }
104 } // namespace DistributedFile
105 } // namespace Storage
106 } // namespace OHOS