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