Add extra line about src files are part of RIC platform project
[ric-plt/sdl.git] / include / private / redis / asynchirediscommanddispatcher.hpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
3
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
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20 */
21
22 #ifndef SHAREDDATALAYER_REDIS_ASYNCHIREDISCOMMANDDISPATCHER_HPP_
23 #define SHAREDDATALAYER_REDIS_ASYNCHIREDISCOMMANDDISPATCHER_HPP_
24
25 #include "private/redis/asynccommanddispatcher.hpp"
26 #include <string>
27 #include <list>
28 #include <vector>
29 #include <map>
30 #include <memory>
31 #include <queue>
32 #include "private/logger.hpp"
33 #include "private/timer.hpp"
34
35 extern "C"
36 {
37     struct redisReply;
38     struct redisAsyncContext;
39 }
40
41 namespace shareddatalayer
42 {
43     class Engine;
44
45     namespace redis
46     {
47         class HiredisSystem;
48         class HiredisEpollAdapter;
49         class Reply;
50
51         class AsyncHiredisCommandDispatcher: public AsyncCommandDispatcher
52         {
53         public:
54             AsyncHiredisCommandDispatcher(const AsyncHiredisCommandDispatcher&) = delete;
55
56             AsyncHiredisCommandDispatcher& operator = (const AsyncHiredisCommandDispatcher&) = delete;
57
58             AsyncHiredisCommandDispatcher(Engine& engine,
59                                           const std::string& address,
60                                           uint16_t port,
61                                           std::shared_ptr<ContentsBuilder> contentsBuilder,
62                                           bool usePermanentCommandCallbacks,
63                                           std::shared_ptr<Logger> logger,
64                                           bool usedForSentinel);
65
66             AsyncHiredisCommandDispatcher(Engine& engine,
67                                           const std::string& address,
68                                           uint16_t port,
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);
75
76             ~AsyncHiredisCommandDispatcher() override;
77
78             void waitConnectedAsync(const ConnectAck& connectAck) override;
79
80             void registerDisconnectCb(const DisconnectCb& disconnectCb) override;
81
82             void dispatchAsync(const CommandCb& commandCb, const AsyncConnection::Namespace& ns,
83                                const Contents& contents) override;
84
85             void disableCommandCallbacks() override;
86
87             void setConnected();
88
89             void setDisconnected();
90
91             void handleReply(const CommandCb& commandCb,
92                              const std::error_code& error,
93                              const redisReply* rr);
94
95             bool isClientCallbacksEnabled() const;
96
97             void verifyConnection();
98
99             void disconnectHiredis();
100
101             void armConnectionRetryTimer(Timer::Duration duration,
102                                          std::function<void()> retryAction);
103
104         private:
105             enum class ServiceState
106             {   DISCONNECTED,
107                 CONNECTION_VERIFICATION,
108                 CONNECTED
109             };
110
111             using Callback = std::function<void(const Reply&)>;
112
113             Engine& engine;
114             std::string address;
115             uint16_t port;
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;
131
132             void connect();
133
134             bool isValidCb(const CommandCb& commandCb);
135
136             void removeCb(const CommandCb& commandCb);
137
138             void callCommandCbWithError(const CommandCb& commandCb, const std::error_code& error);
139
140             void dispatchAsync(const CommandCb& commandCb, const Contents& contents, bool checkConnectionState);
141
142             void verifyConnectionReply(const std::error_code& error, const redis::Reply& reply);
143         };
144     }
145 }
146
147 #endif