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 sendstream_demo.c
18  *
19  * @brief Provides the sample code for sending stream data.
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 = "ohos.distributedschedule.dms.test";  // Session name of device A
33 
34 // Network ID generated by the peer end when devices A and B are networked
35 const char *g_networkidB = "ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00";
36 const char *g_groupid = "TEST_GROUP_ID";  // Group ID
37 static SessionAttribute g_sessionAttr = {
38     .dataType = TYPE_BYTES,  // Session type
39 };
40 
41 // Notify that the session is set up successfully.
OnSessionOpened(int sessionId,int result)42 static int OnSessionOpened(int sessionId, int result)
43 {
44     printf("session opened,sesison id = %d\r\n", sessionId);
45     return 0;
46 }
47 
48 // Notify that the session is closed.
OnSessionClosed(int sessionId)49 static void OnSessionClosed(int sessionId)
50 {
51     printf("session closed, session id = %d\r\n", sessionId);
52 }
53 
54 // Notify that the stream data is received.
OnStreamReceived(int sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)55 static void OnStreamReceived(int sessionId, const StreamData *data, const StreamData *ext,
56                              const StreamFrameInfo *param)
57 {
58     printf("session stream received, session id = %d\r\n", sessionId);
59 }
60 
61 static ISessionListener g_sessionlistenerA = {
62     .OnSessionOpened = OnSessionOpened,
63     .OnSessionClosed = OnSessionClosed,
64     .OnStreamReceived = OnStreamReceived,
65 };
66 
main(void)67 int main(void)
68 {
69     /*
70      * 1. Device A calls CreateSessionServer() to create a session server based on
71      * the application bundle name and session name, and registers the callbacks for
72      * session opened, session closed, byte received, and message received.
73      */
74     int ret = CreateSessionServer(g_pkgNameA, g_sessionNameA, &g_sessionlistenerA);
75     printf("create session server result = %d\n", ret);
76 
77     /*
78      * 2. Device A calls OpenSession() to open a session based on the local session name,
79      * peer session name, and peer network ID, and determine the session channel based on the session type.
80      * When the session is open, a callback will be invoked to notify devices A and B.
81      * A session ID is returned for subsequent data sending.
82      */
83     int sessionId = OpenSession(g_sessionNameA, g_sessionNameB, g_networkidB, g_groupid, &g_sessionAttr);
84     printf("open session result = %d\n", sessionId);
85 
86     /* 3. Device A calls SendStream() to send data to device B. */
87     const StreamData streamData = {0};
88     const StreamData ext = {0};
89     const StreamFrameInfo param = {0};
90 
91     ret = SendStream(sessionId, &streamData, &ext, &param);
92     printf("send stream result = %d\n", ret);
93 
94     /* 4. After data transmission is complete, device A calls CloseSession() to close the session
95      * and instructs device B to close the session.
96      */
97     CloseSession(sessionId);
98     printf("SOFTBUS_OK");
99 
100     /* 5. After the session is closed, devices A and B call RemoveSessionServer() to remove the session server. */
101     ret = RemoveSessionServer(g_pkgNameA, g_sessionNameA);
102     printf("remove session server result = %d\n", ret);
103 }
104 
105 // Device B:
106 
107 #include <stdio.h>
108 #include "session.h"
109 
110 const char *g_pkgNameB = "dmsB"; // Application bundle name of device B
111 const char *g_sessionNameB = "ohos.distributedschedule.dms.testB";  // Session name of device B
112 
OnSessionOpened(int sessionId,int result)113 static int OnSessionOpened(int sessionId, int result)
114 {
115     printf("session opened,sesison id = %d\r\n", sessionId);
116     return 0;
117 }
118 
OnSessionClosed(int sessionId)119 static void OnSessionClosed(int sessionId)
120 {
121     printf("session closed, session id = %d\r\n", sessionId);
122 }
123 
OnStreamReceived(int sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)124 static void OnStreamReceived(int sessionId, const StreamData *data, const StreamData *ext,
125                              const StreamFrameInfo *param)
126 {
127     printf("session stream received, session id = %d\r\n", sessionId);
128 }
129 
130 static ISessionListener g_sessionlistenerB = {
131     .OnSessionOpened = OnSessionOpened,
132     .OnSessionClosed = OnSessionClosed,
133     .OnStreamReceived = OnStreamReceived,
134 };
135 
main(void)136 int main(void)
137 {
138     /*
139      * 1. Device B calls CreateSessionServer() to create a session server based on
140      * the application bundle name and session name, and registers the callbacks for
141      * session opened, session closed, byte received, and message received.
142      */
143     int ret = CreateSessionServer(g_pkgNameB, g_sessionNameB, &g_sessionlistenerB);
144     printf("create session server result = %d\n", ret);
145 
146     /*
147      * 2. Upon receiving the session open notification via OnSessionOpened(), device B waits for device A to send data.
148      * When receiving data, device B returns the receiving status via OnStreamReceived().
149      */
150 
151     /* 3. When the data is received, device B closes the session and removes the session server. */
152     ret = RemoveSessionServer(g_pkgNameB, g_sessionNameB);
153     printf("remove session server result = %d\n", ret);
154 }
155