1 /*
2  * Copyright (c) 2021 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 #ifndef COMMUNICATOR_TYPE_DEFINE_H
17 #define COMMUNICATOR_TYPE_DEFINE_H
18 
19 #include <string>
20 #include <vector>
21 #include <cstdint>
22 #include <functional>
23 #include "db_errno.h"
24 
25 namespace DistributedDB {
26 using LabelType = std::vector<uint8_t>;
27 using Finalizer = std::function<void(void)>;
28 using OnSendEnd = std::function<void(int result, bool isDirectEnd)>;
29 using OnConnectCallback = std::function<void(const std::string &target, bool isConnect)>;
30 constexpr unsigned int COMM_LABEL_LENGTH = 32; // Using SHA256 which length is 32
31 constexpr uint32_t MAX_TOTAL_LEN = 104857600; // 100M Limitation For Max Total Length
32 
33 template<typename T>
RegCallBack(const T & newCallback,T & oldCallback,const Finalizer & newFinalizer,Finalizer & oldFinalizer)34 int RegCallBack(const T &newCallback, T &oldCallback, const Finalizer &newFinalizer, Finalizer &oldFinalizer)
35 {
36     if (newCallback && oldCallback) {
37         // Already registered, not allowed
38         return -E_ALREADY_REGISTER;
39     }
40     if (newCallback && !oldCallback) {
41         // Do register
42         oldCallback = newCallback;
43         oldFinalizer = newFinalizer;
44         return E_OK;
45     }
46     if (!newCallback && oldCallback) {
47         // Do unregister
48         if (oldFinalizer) {
49             oldFinalizer();
50         }
51         oldCallback = nullptr;
52         oldFinalizer = nullptr;
53         return E_OK;
54     }
55     return -E_NOT_PERMIT;
56 }
57 
58 enum class Priority {
59     LOW = 0,        // Usually for datasync and its response
60     NORMAL = 1,     // Usually for timesync and its response
61     HIGH = 2,       // Only for communicator inside
62 };
63 
64 enum class FrameType {
65     EMPTY = 0,      // Used for gossip or help version negotiation
66     APPLICATION_MESSAGE = 1,
67     COMMUNICATION_LABEL_EXCHANGE = 2,
68     COMMUNICATION_LABEL_EXCHANGE_ACK = 3,
69     INVALID_MAX_FRAME_TYPE = 4,
70 };
71 } // namespace DistributedDB
72 
73 #endif // COMMUNICATOR_TYPE_DEFINE_H
74