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.
17 #ifndef SHAREDDATALAYER_REDIS_ASYNCHIREDISCOMMANDDISPATCHER_HPP_
18 #define SHAREDDATALAYER_REDIS_ASYNCHIREDISCOMMANDDISPATCHER_HPP_
20 #include "private/redis/asynccommanddispatcher.hpp"
27 #include "private/logger.hpp"
28 #include "private/timer.hpp"
33 struct redisAsyncContext;
36 namespace shareddatalayer
43 class HiredisEpollAdapter;
46 class AsyncHiredisCommandDispatcher: public AsyncCommandDispatcher
49 AsyncHiredisCommandDispatcher(const AsyncHiredisCommandDispatcher&) = delete;
51 AsyncHiredisCommandDispatcher& operator = (const AsyncHiredisCommandDispatcher&) = delete;
53 AsyncHiredisCommandDispatcher(Engine& engine,
54 const std::string& address,
56 std::shared_ptr<ContentsBuilder> contentsBuilder,
57 bool usePermanentCommandCallbacks,
58 std::shared_ptr<Logger> logger);
60 AsyncHiredisCommandDispatcher(Engine& engine,
61 const std::string& address,
63 std::shared_ptr<ContentsBuilder> contentsBuilder,
64 bool usePermanentCommandCallbacks,
65 HiredisSystem& hiredisSystem,
66 std::shared_ptr<HiredisEpollAdapter> adapter,
67 std::shared_ptr<Logger> logger);
69 ~AsyncHiredisCommandDispatcher() override;
71 void waitConnectedAsync(const ConnectAck& connectAck) override;
73 void registerDisconnectCb(const DisconnectCb& disconnectCb) override;
75 void dispatchAsync(const CommandCb& commandCb, const AsyncConnection::Namespace& ns,
76 const Contents& contents) override;
78 void disableCommandCallbacks() override;
82 void setDisconnected();
84 void handleReply(const CommandCb& commandCb,
85 const std::error_code& error,
86 const redisReply* rr);
88 bool isClientCallbacksEnabled() const;
90 void verifyConnection();
92 void disconnectHiredis();
94 void armConnectionRetryTimer(Timer::Duration duration,
95 std::function<void()> retryAction);
98 enum class ServiceState
100 CONNECTION_VERIFICATION,
104 using Callback = std::function<void(const Reply&)>;
109 std::shared_ptr<ContentsBuilder> contentsBuilder;
110 bool usePermanentCommandCallbacks;
111 HiredisSystem& hiredisSystem;
112 std::shared_ptr<HiredisEpollAdapter> adapter;
113 redisAsyncContext* ac;
114 ConnectAck connectAck;
115 DisconnectCb disconnectCallback;
116 ServiceState serviceState;
117 std::list<CommandCb> cbs;
118 bool clientCallbacksEnabled;
119 Timer connectionRetryTimer;
120 Timer::Duration connectionRetryTimerDuration;
121 Timer::Duration connectionVerificationRetryTimerDuration;
122 std::shared_ptr<Logger> logger;
126 bool isValidCb(const CommandCb& commandCb);
128 void removeCb(const CommandCb& commandCb);
130 void callCommandCbWithError(const CommandCb& commandCb, const std::error_code& error);
132 void dispatchAsync(const CommandCb& commandCb, const Contents& contents, bool checkConnectionState);
134 void verifyConnectionReply(const std::error_code& error, const redis::Reply& reply);