• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "process_msg.h"
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <cstdint>
20 #include <iostream>
21 #include "types.h"
22 #include "distributed_kv_data_manager.h"
23 #include "string.h"
24 #include<sstream> // 使用stringstream
25 #include <string.h>
26 #include <cstring>
27 #include <securec.h>
28 using namespace OHOS::DistributedKv;
29 using namespace std;
30 
31 class DisKvTest {
32 public:
33     static DistributedKvDataManager manager;
34     static std::shared_ptr<SingleKvStore> KvStorePtr;  // declare kvstore instance.
35     static Status statusGetKvStore;
36     static Status statusCloseKvStore;
37     static Status statusDeleteKvStore;
38     static Options create;
39     static UserId userId;
40     static AppId appId;
41     static StoreId storeIdTest;
42 };
43 
44 DistributedKvDataManager DisKvTest::manager;
45 std::shared_ptr<SingleKvStore> DisKvTest::KvStorePtr = nullptr; // declare kvstore instance.
46 Status DisKvTest::statusGetKvStore = Status::ERROR;
47 Status DisKvTest::statusCloseKvStore = Status::ERROR;
48 Status DisKvTest::statusDeleteKvStore = Status::ERROR;
49 UserId DisKvTest::userId;
50 AppId DisKvTest::appId;
51 StoreId DisKvTest::storeIdTest;
52 
initKvstoreId()53 void initKvstoreId()
54 {
55    DisKvTest::userId.userId = "account0";
56     DisKvTest::appId.appId = "com.ohos.kvdatamanager3.test";
57     DisKvTest::storeIdTest.storeId = "test3";
58 
59     //1.创建数据库
60     Options options {
61         .createIfMissing = true,
62             .encrypt = false,     //   .persistant = true,
63             .autoSync = false,
64             .backup = false,
65             .kvStoreType = KvStoreType::SINGLE_VERSION
66     };
67 
68     DisKvTest::statusGetKvStore = DisKvTest::manager.GetSingleKvStore(options, { DisKvTest::appId }, { DisKvTest::storeIdTest }, DisKvTest::KvStorePtr);
69 
70 
71 }
72 
getRealData(char * str,char * delims)73 char* getRealData(char* str, char* delims)
74 {
75     char *result = strtok( str, delims );
76     char *second = NULL;
77    // result = strtok( str, delims );
78     while( result != NULL ) {
79         //printf( "result is \"%s\"\n", result );
80       second = result;
81         result = strtok( NULL,delims );
82     }
83     return second;
84 }
85 
getParam(char * putData,char ret[][MAX_DATA_LENGTH])86 void getParam(char* putData, char ret[] [MAX_DATA_LENGTH])
87 {
88     char str[1024] = {":"};
89     memset_s(str,1024,0,1024);
90     strcpy_s(str, strlen(putData)+1, putData);
91     char delims[2] = {":"};
92     //memset_s(delims,0,2);
93     //strcpy_s(delims,strlen()+1, ":");
94 
95     char *result = strtok( str, delims );
96     //char *second = NULL;
97    // result = strtok( str, delims );
98     int i = 0;
99     while( result != NULL ) {
100         printf( "result is \"%s\"\n", result);
101         strcpy_s(ret[i], strlen(result)+1, result);
102         //second = result;
103         result = strtok( NULL, delims );
104         i++;
105        // printf("i= %d", i);
106         if(i==3)
107         {
108             return;
109         }
110 
111     }
112     return;
113 }
114 
str2int(char * str,int base)115 int str2int(char *str, int base) {
116     char sign;
117     int rv = 0;
118     int newbase = -1;
119 
120     if ((base < 2 && base !=0) || base > 36)
121         return 0;
122 
123     //跳过str开头的空白字符
124     while(*str && isspace(*str))
125         str++;
126 
127     //取得符号位
128     sign = (*str == '-' || *str == '+') ? *str++ : '+';
129 
130     //猜测需要转换的类型
131     if (*str == '0' && ++str) {
132         if ((*str == 'x' || *str == 'X') && ++str) {
133             newbase = 16;
134         } else if ((*str == 'b' || *str == 'B') &&++str) {
135             newbase = 2;
136         } else {
137             newbase = 8;
138         }
139     }
140 
141     //默认10进制
142     if (base == 0) {
143         base = newbase == -1 ? 10 : newbase;
144     }
145 
146     for (;*str; str++) {
147         //大写转小写
148         char c = *str | 0x20;
149         if (c >= 'a' && c<= 'z') {
150             rv = rv * base + c - 'a' + 10;
151         } else if (c >= '0' && c <= '9') {
152             rv = rv * base + c - '0';
153         } else {
154             break;
155         }
156         //printf("rv = %d\n", rv);
157     }
158 
159     return sign == '-' ? -rv : rv;
160 }
161 
ProcessSoftBus(int code,char * recvData)162 int ProcessSoftBus(int code, char* recvData)
163 {
164     return RESULT_OK;
165 }
166 
ProcessDataMgr(int code,char * recvData)167 int ProcessDataMgr(int code, char* recvData)
168 {
169     LOG("ProcessDataMgr, begin");
170 
171     initKvstoreId();
172     std::cout << "create status=" << static_cast<int>(DisKvTest::statusGetKvStore) << std::endl;
173     if ( Status::SUCCESS != DisKvTest::statusGetKvStore)
174     {
175         std::cout << "ERR:statusGetKvStore"<< std::endl;
176         return RESULT_ERR;
177     }
178 
179      switch(code)
180     {
181         case CTRL_CODE_DATAMGR_GET_DATA:
182         {
183             return processGetData(recvData);
184         }
185         break;
186         case CTRL_CODE_DATAMGR_GET_DATA_REPLY:
187         {
188             return RESULT_OK;
189         }
190         break;
191         case CTRL_CODE_DATAMGR_CREATE_KV:
192         {
193             return processCreateKv(recvData);
194         }
195         break;
196         case CTRL_CODE_DATAMGR_DELETE_KV:
197         {
198             return processDeleteKv(recvData);
199         }
200         break;
201         case CTRL_CODE_DATAMGR_PUT_DATA:
202         {
203             return processPutData(recvData);
204         }
205         case CTRL_CODE_DATAMGR_DELETE_DATA:
206         {
207             return processDeletetData(recvData);
208         }
209         break;
210         default:
211             break;
212     }
213     return -1;
214 }
215 
216 
processDeletetData(char * putData)217 int processDeletetData(char* putData)
218 {
219     LOG("LOGdisDataTest---processDeletetData,  begin");
220     //解析远端发来的数据 result[0]=code result[1]=key result[2]=value
221     char result[5][MAX_DATA_LENGTH];
222     memset_s(result,5*MAX_DATA_LENGTH,0,5*MAX_DATA_LENGTH);
223     getParam(putData, result);
224     for(int i=0;i<3;i++)
225     {
226         LOG("LOGdisDataTest---processGetData %s", result[i]);
227     }
228 
229 
230     //put data删除数据
231     Value valueInt;
232     Key keyInt=result[1];
233     Status status = DisKvTest::KvStorePtr->Delete(keyInt);
234     if (Status::SUCCESS == status )
235     {
236         LOG("LOGdisDataTest---Delete:RESULT_OK");
237         return RESULT_OK;
238     }
239     LOG("LOGdisDataTest---Delete:RESULT_ERR");
240     return RESULT_ERR;
241 }
242 
243 
processPutData(char * putData)244 int processPutData(char* putData)
245 {
246     LOG("LOGdisDataTest-processPutData,  begin");
247     //解析远端发来的数据 result[0]=code result[1]=key result[2]=value
248     char result[3][MAX_DATA_LENGTH];
249     memset_s(result,3*MAX_DATA_LENGTH,0,3*MAX_DATA_LENGTH);
250     getParam(putData, result);
251     for(int i=0;i<3;i++)
252     {
253         LOG("LOGdisDataTest-processPutData %s", result[i]);
254     }
255 
256 
257     //put data修改数据
258     Value valueInt;
259     Key keyInt=result[1];
260     if(strcmp(result[1], "math_score_int") == 0)
261     {
262         int numInt = atoi(result[2]);
263         valueInt =  Value(TransferTypeToByteArray<int>(numInt));
264     }
265     else  if(strcmp(result[1], "math_score_float") == 0)
266     {
267         float f2 = atof(result[2]);
268         valueInt =  Value(TransferTypeToByteArray<float>(f2));
269     }
270     else  if(strcmp(result[1], "math_score_double") == 0)
271     {
272         double f2 = atof(result[2]);
273         valueInt =  Value(TransferTypeToByteArray<double>(f2));
274     }
275     else  if(strcmp(result[1], "math_score_int64_t") == 0)
276     {
277         int64_t numInt = atoi(result[2]);
278         valueInt =  Value(TransferTypeToByteArray<int64_t>(numInt));
279     }
280     else  if(strcmp(result[1], "math_score_size_t") == 0)
281     {
282         size_t numInt = atoi(result[2]);
283         valueInt =  Value(TransferTypeToByteArray<size_t>(numInt));
284     }
285 
286     else  if(strcmp(result[1], "math_score_string") == 0)
287     {
288 
289         // result[2]解析的不完整
290         string ss ;
291         string ss2 ;
292         //strcpy_s(ss,str);
293         ss=putData;
294         ss2 = ss.substr(23);
295         cout<<"LOGdisDataTest2--ss = "<< ss<<endl;
296        cout<<"LOGdisDataTest2--ss2 = "<< ss2<<endl;
297        LOG("LOGdisDataTest2--ss2.c_str() =  %s", ss2.c_str());
298         valueInt =  Value(ss2);
299 
300     }
301     else  if(strcmp(result[1], "math_score_vector") == 0)
302     {
303         std::string str = result[2];
304         std::vector<uint8_t> vect;
305         vect.assign(str.begin(), str.end());
306         valueInt =  Value(vect);
307     }
308     Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt);
309     if (Status::SUCCESS == status )
310     {
311         LOG("LOGdisDataTest2--putData:RESULT_OK");
312         return RESULT_OK;
313     }
314     LOG("LOGdisDataTest2--putData:RESULT_ERR");
315     return RESULT_ERR;
316 }
317 
processGetData(char * putData)318 int processGetData(char* putData)
319 {
320     LOG("LOGdisDataTest-processGetData,  begin");
321 
322     //解析远端发来的数据 result[0]=code result[1]=key result[2]=value
323     char result[3][MAX_DATA_LENGTH];
324     memset_s(result,3*MAX_DATA_LENGTH,0,3*MAX_DATA_LENGTH);
325     getParam(putData, result);
326    // LOG("LOGdisDataTest--putData= %s", putData);
327     for(int i=0;i<3;i++)
328     {
329         LOG("for result[i] %s", result[i]);
330     }
331 
332 
333     //获取到本端数据
334     Value valueRetInt;
335     Key keyInt=result[1];
336     Status status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt);
337     if (Status::SUCCESS != status )
338     {
339         LOG("LOGdisDataTest--ERROR: Get(keyInt, valueRetInt)");
340         return RESULT_ERR;
341     }
342 
343     std::string ret = valueRetInt.ToString();
344     LOG("LOGdisDataTest--result[2]=  %s", result[2]);
345     LOG("LOGdisDataTest--ret.c_str() =  %s", ret.c_str());
346 
347     if(strcmp(result[1], "math_score_int") == 0)
348     {
349         LOG("LOGdisDataTest--math_score_int ");
350         int aaa = TransferByteArrayToType<int>(valueRetInt.Data());
351         int i2 = atoi(result[2]);
352         LOG("LOGdisDataTest--aaa=  %d", aaa);
353         LOG("LOGdisDataTest--i2 =  %d", i2);
354         if ( aaa == i2)
355         {
356             return RESULT_OK;
357         }
358 
359     }
360       else  if(strcmp(result[1], "math_score_float") == 0)
361       {
362         LOG("LOGdisDataTest--math_score_float ");
363         float aaa = TransferByteArrayToType<float>(valueRetInt.Data());
364 
365        // float fret = atof(ret.c_str());
366         float f2 = atof(result[2]);
367         float delta = f2 - aaa;
368         LOG("LOGdisDataTest--aaa=  %f", aaa);
369         LOG("LOGdisDataTest--f2 =  %f", f2);
370         LOG("LOGdisDataTest--delta =  %f", delta);
371         if ( std::abs(delta) <= 0.00001)
372         {
373             return RESULT_OK;
374         }
375     }
376       else  if(strcmp(result[1], "math_score_double") == 0)
377       {
378         LOG("LOGdisDataTest--math_score_double ");
379         double aaa = TransferByteArrayToType<double>(valueRetInt.Data());
380         double d2 = atof(result[2]);
381         double delta = d2 - aaa;
382        LOG("LOGdisDataTest--aaa=  %f", aaa);
383        LOG("LOGdisDataTest--d2 =  %f", d2);
384        LOG("LOGdisDataTest--delta =  %f", delta);
385         if ( std::abs(delta) <= 0.00001)
386         {
387             return RESULT_OK;
388         }
389       }
390     else   if(strcmp(result[1], "math_score_int64_t") == 0)
391     {
392         LOG("LOGdisDataTest--math_score_int64_t ");
393         int64_t aaa = TransferByteArrayToType<int64_t>(valueRetInt.Data());
394         int64_t i2 = atoi(result[2]);
395        //LOG("LOGdisDataTest--aaa=  %ld", aaa);
396        // LOG("LOGdisDataTest--i2 =  %ld", i2);
397         if ( aaa == i2)
398         {
399             return RESULT_OK;
400         }
401     }
402     else   if(strcmp(result[1], "math_score_size_t") == 0)
403     {
404         LOG("LOGdisDataTest--math_score_size_t ");
405         size_t aaa = TransferByteArrayToType<size_t>(valueRetInt.Data());
406         size_t i2 = atoi(result[2]);
407        // LOG("LOGdisDataTest--aaa=  %lu", aaa);
408        // LOG("LOGdisDataTest--i2 =  %lu", i2);
409         if ( aaa == i2)
410         {
411             return RESULT_OK;
412         }
413     }
414     else  if(strcmp(result[1], "math_score_string") == 0)
415     {
416         // result[2]解析的不完整
417         string ss ;
418         string ss2 ;
419         //strcpy_s(ss,str);
420         ss=putData;
421         ss2 = ss.substr(23);
422         cout<<"LOGdisDataTest--ss = "<< ss<<endl;
423         cout<<"LOGdisDataTest--ss2 = "<< ss2<<endl;
424         LOG("LOGdisDataTest--ss2.c_str() =  %s", ss2.c_str());
425         LOG("LOGdisDataTest--ret.c_str() =  %s", ret.c_str()); //数据库
426         if(strcmp(ss2.c_str(), ret.c_str()) == 0)
427         {
428             return RESULT_OK;
429         }
430     }
431     else  if(strcmp(result[1], "math_score_vector") == 0)
432     {
433        LOG("LOGdisDataTest--result[2]=  %s", result[2]);
434        LOG("LOGdisDataTest--ret.c_str() =  %s", ret.c_str());
435         if(strcmp(result[2], ret.c_str()) == 0)
436         {
437             return RESULT_OK;
438         }
439     }
440     return RESULT_ERR;
441 }
442 
443 
processCreateKv(char * putData)444 int processCreateKv(char* putData)
445 {
446 
447     LOG("processCreateKv,  begin");
448     //initKvstoreId();
449     std::cout << "create status=" << static_cast<int>(DisKvTest::statusGetKvStore) << std::endl;
450     if ( Status::SUCCESS == DisKvTest::statusGetKvStore)
451     {
452         std::cout << "SUCCESS:statusGetKvStore"<< std::endl;
453         return RESULT_OK;
454     }
455     else
456     {
457         std::cout << "ERR:statusGetKvStore"<< std::endl;
458         return RESULT_ERR;
459     }
460 
461 }
462 
processDeleteKv(char * putData)463 int processDeleteKv(char* putData)
464 {
465 
466     LOG("processDeleteKv,  begin");
467     //initKvstoreId();
468     DisKvTest::statusCloseKvStore = DisKvTest::manager.CloseAllKvStore(DisKvTest::appId);
469     DisKvTest::statusDeleteKvStore = DisKvTest::manager.DeleteAllKvStore(DisKvTest::appId);
470     if ( (Status::SUCCESS == DisKvTest::statusCloseKvStore)  && (Status::SUCCESS == DisKvTest::statusDeleteKvStore) )
471     {
472         std::cout << "SUCCESS:statusDeleteKvStore"<< std::endl;
473         return RESULT_OK;
474     }
475     else
476     {
477         std::cout << "ERR:statusDeleteKvStore"<< std::endl;
478         return RESULT_ERR;
479     }
480 }
481 
ProcessDP(int code,char * recvData)482 int ProcessDP(int code, char* recvData)
483 {
484     return RESULT_OK;
485 }
486 
ProcessDM(int code,char * recvData)487 int ProcessDM(int code, char* recvData)
488 {
489     return RESULT_OK;
490 }
491 
ProcessFileMgr(int code,char * recvData)492 int ProcessFileMgr(int code, char* recvData)
493 {
494     return RESULT_OK;
495 }
496 
ProcessSecMgr(int code,char * recvData)497 int ProcessSecMgr(int code, char* recvData)
498 {
499     return RESULT_OK;
500 }
501 
ProcessMediaMgr(int code,char * recvData)502 int ProcessMediaMgr(int code, char* recvData)
503 {
504     return RESULT_OK;
505 }
506 
507