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(int sessionId,int result)44 static int OnSessionOpened(int sessionId, int 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(int sessionId)51 static void OnSessionClosed(int sessionId)
52 {
53     printf("session closed, session id = %d\r\n", sessionId);
54 }
55 
56 // Notify that the byte data is received.
OnBytesReceived(int sessionId,const void * data,unsigned int len)57 static void OnBytesReceived(int 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(int sessionId,const void * data,unsigned int len)63 static void OnMessageReceived(int 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(int sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)69 static void OnStreamReceived(int sessionId, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param)
70 {
71     printf("session stream received, session id = %d\r\n", sessionId);
72 }
73 
74 static ISessionListener g_sessionlistenerA = {
75     .OnSessionOpened = OnSessionOpened,
76     .OnSessionClosed = OnSessionClosed,
77     .OnBytesReceived = OnBytesReceived,
78     .OnMessageReceived = OnMessageReceived,
79     .OnStreamReceived = OnStreamReceived,
80 };
81 
main(void)82 int main(void)
83 {
84     /*
85      * 1. Device A calls CreateSessionServer() to create a session service based on the application bundle name and
86      * session name, and registers the callbacks for session opened, session closed, byte received, and message
87      * received.
88      */
89     int ret = CreateSessionServer(g_pkgNameA, g_sessionNameA, &g_sessionlistenerA);
90     printf("create session server result = %d\n", ret);
91 
92     /*
93      * 2. Device A calls OpenSession() to open a session based on the local session name, peer session name, and peer
94      * network ID, and then determines the session channel based on the session type.
95      * When the session is open, a callback will be invoked to notify devices A and B.
96      * A session ID is returned for subsequent operations. In addition, sessionName must be DistributedFileService.
97      */
98     int sessionId = OpenSession(g_sessionNameA, g_sessionNameB, g_networkidB, g_groupid, &g_sessionAttr);
99     printf("open session result = %d\n", sessionId);
100 
101     /* 3. Device A calls the APIs provided by the distributed file service to perform operations. */
102     char *key = (char *)g_testData;
103     unsigned int len = strlen(key);
104     ret = GetSessionKey(sessionId, key, len);
105     printf("get session key result = %d\n", ret);
106 
107     int handle = 1;
108     ret = GetSessionHandle(sessionId, &handle);
109     printf("get session handle result = %d\n", ret);
110 
111     ret = DisableSessionListener(sessionId);
112     printf("disable session listener result = %d\n", ret);
113 }
114