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() 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     void OnBufferReceive(const std::string &srcTarget, const SerialBuffer *inBuf);
60 
61     // Call by CommunicatorAggregator directly
62     void OnConnectChange(const std::string &target, bool isConnect);
63 
64     // Call by CommunicatorAggregator directly
65     void OnSendAvailable();
66 
67     // Call by CommunicatorAggregator directly
68     LabelType GetCommunicatorLabel() const;
69 
70 private:
71     void TriggerVersionNegotiation(const std::string &dstTarget);
72     void TriggerUnknownMessageFeedback(const std::string &dstTarget, Message* &oriMsg);
73 
74     DECLARE_OBJECT_TAG(Communicator);
75 
76     CommunicatorAggregator *commAggrHandle_ = nullptr;
77     LabelType commLabel_;
78 
79     std::set<std::string> onlineTargets_; // Actually protected by connectHandleMutex_
80 
81     OnMessageCallback onMessageHandle_;
82     OnConnectCallback onConnectHandle_;
83     std::function<void(void)> onSendableHandle_;
84     Finalizer onMessageFinalizer_;
85     Finalizer onConnectFinalizer_;
86     Finalizer onSendableFinalizer_;
87     std::mutex messageHandleMutex_;
88     std::mutex connectHandleMutex_;
89     std::mutex sendableHandleMutex_;
90 };
91 } // namespace DistributedDB
92 
93 #endif // COMMUNICATOR_H
94