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 ICOMMUNICATOR_H
17 #define ICOMMUNICATOR_H
18
19 #include <string>
20 #include <functional>
21 #include "message.h"
22 #include "ref_object.h"
23 #include "communicator_type_define.h"
24 #include "iprocess_communicator.h"
25 #include "db_properties.h"
26
27 namespace DistributedDB {
28 // inMsg is heap memory, its ownership transfers by calling OnMessageCallback
29 using OnMessageCallback = std::function<void(const std::string &srcTarget, Message *inMsg)>;
30 constexpr uint32_t SEND_TIME_OUT = 3000; // 3s
31
32 struct SendConfig {
33 bool nonBlock = false;
34 bool isNeedExtendHead = false;
35 uint32_t timeout = SEND_TIME_OUT;
36 ExtendInfo paramInfo;
37 };
38
SetSendConfigParam(const DBProperties & dbProperty,const std::string & dstTarget,bool nonBlock,uint32_t timeout,SendConfig & sendConf)39 inline void SetSendConfigParam(const DBProperties &dbProperty, const std::string &dstTarget, bool nonBlock,
40 uint32_t timeout, SendConfig &sendConf)
41 {
42 sendConf.nonBlock = nonBlock;
43 sendConf.timeout = timeout;
44 sendConf.isNeedExtendHead = dbProperty.GetBoolProp(DBProperties::SYNC_DUAL_TUPLE_MODE,
45 false);
46 sendConf.paramInfo.appId = dbProperty.GetStringProp(DBProperties::APP_ID, "");
47 sendConf.paramInfo.userId = dbProperty.GetStringProp(DBProperties::USER_ID, "");
48 sendConf.paramInfo.storeId = dbProperty.GetStringProp(DBProperties::STORE_ID, "");
49 sendConf.paramInfo.dstTarget = dstTarget;
50 }
51
52 class ICommunicator : public virtual RefObject {
53 public:
54 // Message heap memory
55 // Return 0 as success. Return negative as error
56 virtual int RegOnMessageCallback(const OnMessageCallback &onMessage, const Finalizer &inOper) = 0;
57 virtual int RegOnConnectCallback(const OnConnectCallback &onConnect, const Finalizer &inOper) = 0;
58 virtual int RegOnSendableCallback(const std::function<void(void)> &onSendable, const Finalizer &inOper) = 0;
59
60 virtual void Activate() = 0;
61
62 // return optimal allowed data size(Some header is taken into account and subtract)
63 virtual uint32_t GetCommunicatorMtuSize() const = 0;
64 virtual uint32_t GetCommunicatorMtuSize(const std::string &target) const = 0;
65
66 // return timeout in range [5s, 60s]
67 virtual uint32_t GetTimeout() const = 0;
68 virtual uint32_t GetTimeout(const std::string &target) const = 0;
69
70 virtual bool IsDeviceOnline(const std::string &device) const = 0;
71
72 // Get local target name for identify self
73 virtual int GetLocalIdentity(std::string &outTarget) const = 0;
74
75 // Get the protocol version of remote target. Return -E_NOT_FOUND if no record.
76 virtual int GetRemoteCommunicatorVersion(const std::string &target, uint16_t &outVersion) const = 0;
77
78 // inMsg is heap memory, its ownership transfers by calling SendMessage
79 // If send fail in SendMessage, nonBlock true will return, nonBlock false will block and retry
80 // timeout is ignore if nonBlock true. OnSendEnd won't always be called such as when in finalize stage.
81 // Return 0 as success. Return negative as error
82 virtual int SendMessage(const std::string &dstTarget, const Message *inMsg, const SendConfig &config) = 0;
83 virtual int SendMessage(const std::string &dstTarget, const Message *inMsg, const SendConfig &config,
84 const OnSendEnd &onEnd) = 0; // HW Code Regulation do not allow to use default parameters on virtual function
85
~ICommunicator()86 virtual ~ICommunicator() {};
87 };
88 } // namespace DistributedDB
89
90 #endif // ICOMMUNICATOR_H
91