• 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 COMMUNICATOR_H
17 #define COMMUNICATOR_H
18 
19 #include <chrono>
20 #include <condition_variable>
21 #include <cstdint>
22 #include <functional>
23 #include <mutex>
24 #include <set>
25 #include <string>
26 #include "communicator_aggregator.h"
27 #include "icommunicator.h"
28 #include "serial_buffer.h"
29 
30 namespace DistributedDB {
31 class Communicator : public ICommunicator {
32 public:
33     Communicator(CommunicatorAggregator *inCommAggregator, const LabelType &inLabel);
34     ~Communicator() override;
35 
36     DISABLE_COPY_ASSIGN_MOVE(Communicator);
37 
38     int RegOnMessageCallback(const OnMessageCallback &onMessage, const Finalizer &inOper) override;
39     int RegOnConnectCallback(const OnConnectCallback &onConnect, const Finalizer &inOper) override;
40     int RegOnSendableCallback(const std::function<void(void)> &onSendable, const Finalizer &inOper) override;
41 
42     void Activate(const std::string &userId = "") override;
43 
44     uint32_t GetCommunicatorMtuSize() const override;
45     uint32_t GetCommunicatorMtuSize(const std::string &target) const override;
46 
47     uint32_t GetTimeout() const override;
48     uint32_t GetTimeout(const std::string &target) const override;
49     bool IsDeviceOnline(const std::string &device) const override;
50     int GetLocalIdentity(std::string &outTarget) const override;
51     // Get the protocol version of remote target. Return -E_NOT_FOUND if no record.
52     int GetRemoteCommunicatorVersion(const std::string &target, uint16_t &outVersion) const override;
53 
54     int SendMessage(const std::string &dstTarget, const Message *inMsg, const SendConfig &config) override;
55     int SendMessage(const std::string &dstTarget, const Message *inMsg, const SendConfig &config,
56         const OnSendEnd &onEnd) override;
57 
58     // Call by CommunicatorAggregator directly
59     int OnBufferReceive(const std::string &srcTarget, const SerialBuffer *inBuf, const std::string &sendUser,
60         uint16_t remoteDbVersion);
61 
62     // Call by CommunicatorAggregator directly
63     void OnConnectChange(const std::string &target, bool isConnect);
64 
65     // Call by CommunicatorAggregator directly
66     void OnSendAvailable();
67 
68     // Call by CommunicatorAggregator directly
69     LabelType GetCommunicatorLabel() const;
70 
71     std::string GetTargetUserId(const ExtendInfo &paramInfo) const override;
72 
73     bool ExchangeClosePending(bool expected) override;
74 private:
75     void TriggerVersionNegotiation(const std::string &dstTarget);
76     void TriggerUnknownMessageFeedback(const std::string &dstTarget, Message* &oriMsg);
77 
78     DECLARE_OBJECT_TAG(Communicator);
79 
80     CommunicatorAggregator *commAggrHandle_ = nullptr;
81     LabelType commLabel_;
82 
83     std::set<std::string> onlineTargets_; // Actually protected by connectHandleMutex_
84 
85     OnMessageCallback onMessageHandle_;
86     OnConnectCallback onConnectHandle_;
87     std::function<void(void)> onSendableHandle_;
88     std::atomic<bool> dbClosePending_;
89     Finalizer onMessageFinalizer_;
90     Finalizer onConnectFinalizer_;
91     Finalizer onSendableFinalizer_;
92     std::mutex messageHandleMutex_;
93     std::mutex connectHandleMutex_;
94     std::mutex sendableHandleMutex_;
95 };
96 } // namespace DistributedDB
97 
98 #endif // COMMUNICATOR_H
99