1 /*
2  * Copyright (c) 2022 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 /**
17  * @file dfs_demo.c
18  *
19  * @brief Provides the sample code for the distributed file service.
20  *
21  * @since 1.0
22  * @version 1.0
23  */
24 
25 // Device A:
26 
27 #include <stdio.h>
28 #include "session.h"
29 #include "softbus_config_type.h"
30 
31 const char *g_pkgNameA = "dms"; // Application bundle name of device A
32 const char *g_sessionNameA = "DistributedFileService"; // Session name of the distributed file service of device A
33 const char *g_sessionNameB = "DistributedFileService"; // Session name of the distributed file service of device B
34 
35 // Network ID generated by device B after devices A and B are networked.
36 const char *g_networkidB = "ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00";
37 const char *g_groupid = "TEST_GROUP_ID";  // Group ID
38 static SessionAttribute g_sessionAttr = {
39     .dataType = TYPE_FILE, // Session type
40 };
41 const char *g_testData = "TranSessionTest_GetSessionKeyTestData";
42 
43 // Notify that the session is set up successfully.
OnSessionOpened(int32_t sessionId, int32_t result)44 static int32_t OnSessionOpened(int32_t sessionId, int32_t result)
45 {
46     printf("session opened,sesison id = %d\r\n", sessionId);
47     return 0;
48 }
49 
50 // Notify that the session is closed.
OnSessionClosed(int32_t sessionId)51 static void OnSessionClosed(int32_t sessionId)
52 {
53     printf("session closed, session id = %d\r\n", sessionId);
54 }
55 
56 // Notify that the byte data is received.
OnBytesReceived(int32_t sessionId, const void *data, unsigned int len)57 static void OnBytesReceived(int32_t sessionId, const void *data, unsigned int len)
58 {
59     printf("session bytes received, session id = %d\r\n", sessionId);
60 }
61 
62 // Notify that the message is received.
OnMessageReceived(int32_t sessionId, const void *data, unsigned int len)63 static void OnMessageReceived(int32_t sessionId, const void *data, unsigned int len)
64 {
65     printf("session msg received, session id = %d\r\n", sessionId);
66 }
67 
68 // Notify that the stream data is received.
OnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param)69 static void OnStreamReceived(int32_t sessionId, const StreamData *data,
70                              const StreamData *ext, const StreamFrameInfo *param)
71 {
72     printf("session stream received, session id = %d\r\n", sessionId);
73 }
74 
75 static ISessionListener g_sessionlistenerA = {
76     .OnSessionOpened = OnSessionOpened,
77     .OnSessionClosed = OnSessionClosed,
78     .OnBytesReceived = OnBytesReceived,
79     .OnMessageReceived = OnMessageReceived,
80     .OnStreamReceived = OnStreamReceived,
81 };
82 
main(void)83 int32_t main(void)
84 {
85     /*
86      * 1. Device A calls CreateSessionServer() to create a session service based on the application bundle name and
87      * session name, and registers the callbacks for session opened, session closed, byte received, and message
88      * received.
89      */
90     int32_t ret = CreateSessionServer(g_pkgNameA, g_sessionNameA, &g_sessionlistenerA);
91     printf("create session server result = %d\n", ret);
92 
93     /*
94      * 2. Device A calls OpenSession() to open a session based on the local session name, peer session name, and peer
95      * network ID, and then determines the session channel based on the session type.
96      * When the session is open, a callback will be invoked to notify devices A and B.
97      * A session ID is returned for subsequent operations. In addition, sessionName must be DistributedFileService.
98      */
99     int32_t sessionId = OpenSession(g_sessionNameA, g_sessionNameB, g_networkidB, g_groupid, &g_sessionAttr);
100     printf("open session result = %d\n", sessionId);
101 
102     /* 3. Device A calls the APIs provided by the distributed file service to perform operations. */
103     char *key = (char *)g_testData;
104     unsigned int len = strlen(key);
105     ret = GetSessionKey(sessionId, key, len);
106     printf("get session key result = %d\n", ret);
107 
108     int32_t handle = 1;
109     ret = GetSessionHandle(sessionId, &handle);
110     printf("get session handle result = %d\n", ret);
111 
112     ret = DisableSessionListener(sessionId);
113     printf("disable session listener result = %d\n", ret);
114 }
115