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 ICOMMUNICATORAGGREGATOR_H
17 #define ICOMMUNICATORAGGREGATOR_H
18 
19 #include <cstdint>
20 #include "communicator_type_define.h"
21 #include "db_status_adapter.h"
22 #include "iadapter.h"
23 #include "ref_object.h"
24 
25 namespace DistributedDB {
26 class ICommunicator; // Forward Declaration
27 // Return E_OK to indicate to retain received frame. Do not block during callback.
28 using CommunicatorLackCallback = std::function<int(const LabelType &commLabel, const std::string &userId)>;
29 
30 class ICommunicatorAggregator : public virtual RefObject {
31 public:
32     // Return 0 as success. Return negative as error
33     // The caller is the owner of inAdapter and responsible for manage its lifecycle.
34     // The ICommunicatorAggregator is only the user of inAdapter
35     // If Initialize fail, the ICommunicatorAggregator will rollback what had done to inAdapter so it can be reuse.
36     virtual int Initialize(IAdapter *inAdapter, const std::shared_ptr<DBStatusAdapter> &statusAdapter) = 0;
37 
38     // Call this method after Initialize successfully and before destroy the ICommunicatorAggregator
39     // Emphasize again : DO NOT CALL Finalize IF Initialize FAIL.
40     // Must not call any other functions if Finalize had been called.
41     // More likely, The Finalize has no chance to be called. since it is process level.
42     virtual void Finalize() = 0;
43 
44     // If not success, return nullptr and set outErrorNo
45     virtual ICommunicator *AllocCommunicator(uint64_t commLabel, int &outErrorNo) = 0;
46     virtual ICommunicator *AllocCommunicator(const LabelType &commLabel, int &outErrorNo) = 0;
47     virtual void ReleaseCommunicator(ICommunicator *inCommunicator) = 0;
48     virtual int RegCommunicatorLackCallback(const CommunicatorLackCallback &onCommLack, const Finalizer &inOper) = 0;
49     virtual int RegOnConnectCallback(const OnConnectCallback &onConnect, const Finalizer &inOper) = 0;
50     virtual int GetLocalIdentity(std::string &outTarget) const = 0;
~ICommunicatorAggregator()51     virtual ~ICommunicatorAggregator() {};
52 };
53 } // namespace DistributedDB
54 
55 #endif // ICOMMUNICATORAGGREGATOR_H
56