47aeba54d1a95deda51709ff118a4b75eba85f0c
[ric-plt/sdl.git] / include / private / redis / asyncdatabasediscovery.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 #ifndef SHAREDDATALAYER_REDIS_ASYNCDATABASEDISCOVERY_HPP_
18 #define SHAREDDATALAYER_REDIS_ASYNCDATABASEDISCOVERY_HPP_
19
20 #include <functional>
21 #include <map>
22 #include <string>
23 #include <memory>
24 #include <vector>
25 #include <boost/optional.hpp>
26 #include "private/logger.hpp"
27
28 namespace shareddatalayer
29 {
30     class Engine;
31     class DatabaseConfiguration;
32
33     namespace redis
34     {
35         struct DatabaseInfo;
36
37         class AsyncDatabaseDiscovery
38         {
39         public:
40             AsyncDatabaseDiscovery(const AsyncDatabaseDiscovery&) = delete;
41
42             AsyncDatabaseDiscovery& operator = (const AsyncDatabaseDiscovery&) = delete;
43
44             virtual ~AsyncDatabaseDiscovery() = default;
45
46             using StateChangedCb = std::function<void(const DatabaseInfo&)>;
47
48             /**
49              * Register a callback to be invoked whenever connection information changes. Client would tear down
50              * its current connection and setup a new one using the information that was passed to the callback.
51              *
52              * @note connection state changes (e.g. connection down, switchover)) do not invoke callback if
53              *       connection information does not change.
54              *
55              * @note When a callback is set, it should be scheduled right away (not called inside this function)
56              *       if connection info is already available.
57              *
58              * @param stateChangedCb Callback to be invoked when the information of the database changes.
59              */
60             virtual void setStateChangedCb(const StateChangedCb& stateChangedCb) = 0;
61
62             virtual void clearStateChangedCb() = 0;
63
64             using Namespace = std::string;
65
66             static std::shared_ptr<AsyncDatabaseDiscovery> create(std::shared_ptr<Engine> engine,
67                                                                   const boost::optional<Namespace>& ns,
68                                                                   const DatabaseConfiguration& staticDatabaseConfiguration,
69                                                                   std::shared_ptr<Logger> logger);
70
71         protected:
72             AsyncDatabaseDiscovery() = default;
73         };
74     }
75 }
76
77 #endif