2 Copyright (c) 2018-2019 Nokia.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
22 #ifndef SHAREDDATALAYER_REDIS_ASYNCHIREDISCOMMANDDISPATCHER_HPP_
23 #define SHAREDDATALAYER_REDIS_ASYNCHIREDISCOMMANDDISPATCHER_HPP_
25 #include "private/redis/asynccommanddispatcher.hpp"
32 #include "private/logger.hpp"
33 #include "private/timer.hpp"
38 struct redisAsyncContext;
41 namespace shareddatalayer
48 class HiredisEpollAdapter;
51 class AsyncHiredisCommandDispatcher: public AsyncCommandDispatcher
54 AsyncHiredisCommandDispatcher(const AsyncHiredisCommandDispatcher&) = delete;
56 AsyncHiredisCommandDispatcher& operator = (const AsyncHiredisCommandDispatcher&) = delete;
58 AsyncHiredisCommandDispatcher(Engine& engine,
59 const std::string& address,
61 std::shared_ptr<ContentsBuilder> contentsBuilder,
62 bool usePermanentCommandCallbacks,
63 std::shared_ptr<Logger> logger,
64 bool usedForSentinel);
66 AsyncHiredisCommandDispatcher(Engine& engine,
67 const std::string& address,
69 std::shared_ptr<ContentsBuilder> contentsBuilder,
70 bool usePermanentCommandCallbacks,
71 HiredisSystem& hiredisSystem,
72 std::shared_ptr<HiredisEpollAdapter> adapter,
73 std::shared_ptr<Logger> logger,
74 bool usedForSentinel);
76 ~AsyncHiredisCommandDispatcher() override;
78 void waitConnectedAsync(const ConnectAck& connectAck) override;
80 void registerDisconnectCb(const DisconnectCb& disconnectCb) override;
82 void dispatchAsync(const CommandCb& commandCb, const AsyncConnection::Namespace& ns,
83 const Contents& contents) override;
85 void disableCommandCallbacks() override;
89 void setDisconnected();
91 void handleReply(const CommandCb& commandCb,
92 const std::error_code& error,
93 const redisReply* rr);
95 bool isClientCallbacksEnabled() const;
97 void verifyConnection();
99 void disconnectHiredis();
101 void armConnectionRetryTimer(Timer::Duration duration,
102 std::function<void()> retryAction);
105 enum class ServiceState
107 CONNECTION_VERIFICATION,
111 using Callback = std::function<void(const Reply&)>;
116 std::shared_ptr<ContentsBuilder> contentsBuilder;
117 bool usePermanentCommandCallbacks;
118 HiredisSystem& hiredisSystem;
119 std::shared_ptr<HiredisEpollAdapter> adapter;
120 redisAsyncContext* ac;
121 ConnectAck connectAck;
122 DisconnectCb disconnectCallback;
123 ServiceState serviceState;
124 std::list<CommandCb> cbs;
125 bool clientCallbacksEnabled;
126 Timer connectionRetryTimer;
127 Timer::Duration connectionRetryTimerDuration;
128 Timer::Duration connectionVerificationRetryTimerDuration;
129 std::shared_ptr<Logger> logger;
130 bool usedForSentinel;
134 bool isValidCb(const CommandCb& commandCb);
136 void removeCb(const CommandCb& commandCb);
138 void callCommandCbWithError(const CommandCb& commandCb, const std::error_code& error);
140 void dispatchAsync(const CommandCb& commandCb, const Contents& contents, bool checkConnectionState);
142 void verifyConnectionReply(const std::error_code& error, const redis::Reply& reply);