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 "dms_sdk_demo.h"
17 
18 #include <iostream>
19 #include <string>
20 #include <vector>
21 
22 using namespace OHOS;
23 using namespace DistributedSchedule;
24 using namespace std;
25 
26 Business g_business;
27 namespace {
28 DmsHandler &dmsSourceHandlerdemo = DmsHandler::GetInstance();
29 sptr<IDSchedEventListener> listener = sptr<IDSchedEventListener>(new Business());
30 ContinueInfo g_continueInfo;
31 }
32 
Register(DSchedEventType type)33 void Business::Register(DSchedEventType type)
34 {
35     int32_t result = 0;
36     result = dmsSourceHandlerdemo.RegisterDSchedEventListener(type, listener);
37     if (result < 0) {
38         cout << "RegisterDSchedEventListener failed.CODE = " << result << endl;
39     } else {
40         cout << "RegisterDSchedEventListener succeed.CODE = " << result << endl;
41     }
42 }
43 
UnRegister(DSchedEventType type)44 void Business::UnRegister(DSchedEventType type)
45 {
46     int32_t result = 0;
47     result = dmsSourceHandlerdemo.UnRegisterDSchedEventListener(type, listener);
48     if (result < 0) {
49         cout << "UnRegisterDSchedEventListener failed.CODE = " << result << endl;
50     } else {
51         cout << "UnRegisterDSchedEventListener succeed.CODE = " << result << endl;
52     }
53 }
54 
GetContinueDeviceInfo()55 void Business::GetContinueDeviceInfo()
56 {
57     int32_t result = 0;
58     result = dmsSourceHandlerdemo.GetContinueInfo(g_continueInfo);
59     if (result < 0) {
60         cout << "GetContinueInfo failed.CODE = " << result << endl;
61     } else {
62         cout << "continueInfo.dstNetworkId_ : " << g_continueInfo.dstNetworkId_ << endl;
63         cout << "continueInfo.srcNetworkId_ : " << g_continueInfo.srcNetworkId_ << endl;
64     }
65 }
66 
GetDSchedEventInfo(DSchedEventType type)67 void Business::GetDSchedEventInfo(DSchedEventType type)
68 {
69     vector<EventNotify> notifys;
70     int32_t result = dmsSourceHandlerdemo.GetDSchedEventInfo(type, notifys);
71     if (result < 0) {
72         cout << "GetContinueInfo failed.CODE = " << result << endl;
73     } else {
74         for (auto notify : notifys) {
75             cout << endl << "DSchedEventInfo:" << endl;
76             cout << "eventResult: " << notify.eventResult_ << endl;
77             cout << "srcNetworkId: " << notify.srcNetworkId_ << endl;
78             cout << "dstNetworkId: " << notify.dstNetworkId_ << endl;
79             cout << "srcBundleName: " << notify.srcBundleName_ << endl;
80             cout << "srcModuleName: " << notify.srcModuleName_ << endl;
81             cout << "srcAbilityName: " << notify.srcAbilityName_ << endl;
82             cout << "destBundleName: " << notify.destBundleName_ << endl;
83             cout << "destModuleName: " << notify.destModuleName_ << endl;
84             cout << "destAbilityName: " << notify.destAbilityName_ << endl;
85             cout << "developerId: " << notify.developerId_ << endl;
86             cout << "dSchedEventType: " << notify.dSchedEventType_ << endl;
87             cout << "state: " << notify.state_ << endl << endl;
88         }
89     }
90     notifys.clear();
91 }
92 
DSchedEventNotify(EventNotify & notify)93 void Business::DSchedEventNotify(EventNotify& notify)
94 {
95     cout << endl << "DSchedEventNotify Start." << endl;
96     cout << "eventResult: " << notify.eventResult_ << endl;
97     cout << "srcNetworkId: " << notify.srcNetworkId_ << endl;
98     cout << "dstNetworkId: " << notify.dstNetworkId_ << endl;
99     cout << "srcBundleName: " << notify.srcBundleName_ << endl;
100     cout << "srcModuleName: " << notify.srcModuleName_ << endl;
101     cout << "srcAbilityName: " << notify.srcAbilityName_ << endl;
102     cout << "destBundleName: " << notify.destBundleName_ << endl;
103     cout << "destModuleName: " << notify.destModuleName_ << endl;
104     cout << "destAbilityName: " << notify.destAbilityName_ << endl;
105     cout << "developerId: " << notify.developerId_ << endl;
106     cout << "dSchedEventType: " << notify.dSchedEventType_ << endl;
107     cout << "state: " << notify.state_ << endl;
108     cout << "DSchedEventNotify Success." << endl;
109 }
main()110 int main()
111 {
112     cout << "Please select an option to test the interface:" << endl;
113     cout << "A.RegisterContinueListener       B.UnRegisterContinueListener       C.GetContinueInfo" << endl;
114     cout << "D.RegisterCollaborationListener  E.UnRegisterCollaborationListener  F.GetCollaborationInfo" << endl;
115     cout << "G.RegisterAllListener            H.UnRegisterAllListener            I.GetAllInfo" << endl;
116     cout << "J.GetContinueDeviceInfo          X.exit" << endl;
117     cout << "\n" << endl;
118 
119     char cmd;
120     while (cin >> cmd) {
121         if (cmd <= 'z' && cmd >= 'a') {
122             cmd = cmd + 'A' - 'a';
123         }
124         switch (cmd) {
125             case 'A' : g_business.Register(DMS_CONTINUE);
126                 break;
127             case 'B' : g_business.UnRegister(DMS_CONTINUE);
128                 break;
129             case 'C' : g_business.GetDSchedEventInfo(DMS_CONTINUE);
130                 break;
131             case 'D' : g_business.Register(DMS_COLLABORATION);
132                 break;
133             case 'E' : g_business.UnRegister(DMS_COLLABORATION);
134                 break;
135             case 'F' : g_business.GetDSchedEventInfo(DMS_COLLABORATION);
136                 break;
137             case 'G' : g_business.Register(DMS_ALL);
138                 break;
139             case 'H' : g_business.UnRegister(DMS_ALL);
140                 break;
141             case 'I' : g_business.GetDSchedEventInfo(DMS_ALL);
142                 break;
143             case 'J' : g_business.GetContinueDeviceInfo();
144                 break;
145             case 'X' :
146                 return 0;
147             default:
148                 cout << "unknown cmd, please input again" << endl;
149         }
150     }
151 
152     return 0;
153 }