Red Hat Data Grid HotRod C++ Client  7.2.0
ConfigurationBuilder.h
Go to the documentation of this file.
1 #ifndef ISPN_HOTROD_CONFIGURATION_CONFIGURATIONBUILDER_H
2 #define ISPN_HOTROD_CONFIGURATION_CONFIGURATIONBUILDER_H
3 
4 #include <string>
5 #include <vector>
6 #include <sstream>
7 #include <fstream>
8 #include <iostream>
9 
10 #include "infinispan/hotrod/defs.h"
13 #include "Configuration.h"
17 #include "NearCacheConfiguration.h"
18 
19 using namespace infinispan::hotrod::event;
20 
21 namespace infinispan {
22 namespace hotrod {
23 
29 {
30 public:
31  ClusterConfigurationBuilder(std::vector<ServerConfigurationBuilder>& servers, ConfigurationBuilder &parent) : servers(servers), m_parent(parent) {}
40  ClusterConfigurationBuilder& addClusterNode(const std::string host, const int port)
41  {
42  servers.push_back(ServerConfigurationBuilder(m_parent).host(host).port(port));
43  return *this;
44  }
45 private:
46  std::vector<ServerConfigurationBuilder>& servers;
47  ConfigurationBuilder &m_parent;
48 };
49 
54 {
55 public:
57 
61  int getMaxEntries() const {
62  return m_maxEntries;
63  }
64 
72  NearCacheConfigurationBuilder& maxEntries(unsigned int maxEntries = 0) {
73  this->m_maxEntries = maxEntries;
74  return *this;
75  }
76 
81  return m_mode;
82  }
83 
92  this->m_mode = mode;
93  return *this;
94  }
95 
97  {
98  return NearCacheConfiguration(m_mode,m_maxEntries);
99  }
100 
101  private:
102  NearCacheMode m_mode=DISABLED;
103  unsigned int m_maxEntries=0;
104 };
105 
112 {
113  public:
115  m_connectionTimeout(60000),
116  m_forceReturnValue(false),
117  m_keySizeEstimate(64),
118  m_protocolVersion(Configuration::PROTOCOL_VERSION_24),
119  m_socketTimeout(60000),
120  m_tcpNoDelay(true),
121  m_valueSizeEstimate(512),
122  m_maxRetries(10),
123  m_balancingStrategyProducer(nullptr),
124  __pragma(warning(suppress:4355)) // passing uninitialized 'this'
125  connectionPoolConfigurationBuilder(*this),
126  __pragma(warning(suppress:4355))
127  securityConfigurationBuilder(*this),
128  nearCacheConfigurationBuilder(*this)
129  {}
130 
131  void validate() {}
132 
140  ClusterConfigurationBuilder addCluster(const std::string& clusterName) {
141  return ClusterConfigurationBuilder(m_serversMap[clusterName],*this);
142  }
143 
151  if (m_serversMap.find(Configuration::DEFAULT_CLUSTER_NAME) == m_serversMap.end())
152  {
154  }
155  auto& servers = m_serversMap[Configuration::DEFAULT_CLUSTER_NAME];
156  servers.push_back(ServerConfigurationBuilder(*this));
157  return servers[servers.size() - 1];
158  }
159 
160 
167  ConfigurationBuilder& addServers(std::string servers) {
168  std::istringstream originalStream(servers);
169  std::string stringHelper;
170  std::string hostHelper;
171  std::string portHelper;
172  int portInt;
173 
174  // TODO: handle IPv6 addresses
175  while(std::getline(originalStream,stringHelper,';')) {
176  std::istringstream singleServerStream(stringHelper);
177  std::getline(singleServerStream,hostHelper,':');
178  std::getline(singleServerStream,portHelper,':');
179  std::istringstream portStream(portHelper);
180  portStream >> portInt;
181  addServer().host(hostHelper).port(portInt);
182  }
183 
184  return *this;
185  }
186 
193  return connectionPoolConfigurationBuilder;
194  }
195 
201  ConfigurationBuilder& connectionTimeout(int connectionTimeout_) {
202  m_connectionTimeout = connectionTimeout_;
203  return *this;
204  }
205 
212  ConfigurationBuilder& forceReturnValues(bool forceReturnValues_) {
213  m_forceReturnValue = forceReturnValues_;
214  return *this;
215  }
216 
222  ConfigurationBuilder& keySizeEstimate(int keySizeEstimate_) {
223  m_keySizeEstimate = keySizeEstimate_;
224  return *this;
225  }
226 
234  ConfigurationBuilder& protocolVersion(const std::string &protocolVersion_) {
235  m_protocolVersion = protocolVersion_;
236  return *this;
237  }
238 
245  ConfigurationBuilder& socketTimeout(int socketTimeout_) {
246  m_socketTimeout = socketTimeout_;
247  return *this;
248  }
249 
256  return securityConfigurationBuilder.getSslConfigurationBuilder();
257  }
258 
265  return nearCacheConfigurationBuilder;
266  }
267 
273  ConfigurationBuilder& tcpNoDelay(bool tcpNoDelay_) {
274  m_tcpNoDelay = tcpNoDelay_;
275  return *this;
276  }
277 
283  ConfigurationBuilder& valueSizeEstimate(int valueSizeEstimate_) {
284  m_valueSizeEstimate = valueSizeEstimate_;
285  return *this;
286  }
287 
294  ConfigurationBuilder& maxRetries(int maxRetries_) {
295  m_maxRetries = maxRetries_;
296  return *this;
297  }
298 
300  m_balancingStrategyProducer = bsp;
301  return *this;
302  }
303 
311  return create();
312  }
313 
321  std::map<std::string,std::vector<ServerConfiguration>> serversMap;
322  for (auto p: m_serversMap)
323  {
324  std::vector<ServerConfiguration> scVec;
325  for (auto e : p.second)
326  {
327  scVec.push_back(e.create());
328  }
329  serversMap.insert(std::make_pair(p.first, scVec));
330  }
331  if (serversMap.size()==0)
332  {
333  std::vector<ServerConfiguration> scVec;
334  scVec.push_back(ServerConfigurationBuilder(*this).create());
335  serversMap.insert(std::make_pair(Configuration::DEFAULT_CLUSTER_NAME, scVec));
336  }
337 
338  return Configuration(m_protocolVersion,
339  connectionPoolConfigurationBuilder.create(),
340  m_connectionTimeout,
341  m_forceReturnValue,
342  m_keySizeEstimate,
343  serversMap,
344  m_socketTimeout,
345  securityConfigurationBuilder.create(),
346  m_tcpNoDelay,
347  m_valueSizeEstimate,
348  m_maxRetries,
349  nearCacheConfigurationBuilder.create(),
350  m_balancingStrategyProducer,
351  m_eventMarshaller);
352 
353  }
354 
362  // FIXME: read pool, ssl and server configs
363  m_protocolVersion = configuration.getProtocolVersionCString();
364  m_connectionTimeout = configuration.getConnectionTimeout();
365  m_forceReturnValue = configuration.isForceReturnValue();
366  m_socketTimeout = configuration.getSocketTimeout();
367  m_tcpNoDelay = configuration.isTcpNoDelay();
368  m_keySizeEstimate = configuration.getKeySizeEstimate();
369  m_valueSizeEstimate = configuration.getValueSizeEstimate();
370  m_maxRetries = configuration.getMaxRetries();
371  m_eventMarshaller = configuration.getEventMarshaller();
372  return *this;
373  }
380  {
381  return securityConfigurationBuilder;
382  }
383  private:
384  int m_connectionTimeout;
385  bool m_forceReturnValue;
386  int m_keySizeEstimate;
387  std::string m_protocolVersion;
388  std::map<std::string,std::vector<ServerConfigurationBuilder> >m_serversMap;
389  int m_socketTimeout;
390  bool m_tcpNoDelay;
391  int m_valueSizeEstimate;
392  unsigned int m_maxRetries;
393  FailOverRequestBalancingStrategy::ProducerFn m_balancingStrategyProducer;
394  ConnectionPoolConfigurationBuilder connectionPoolConfigurationBuilder;
395  SecurityConfigurationBuilder securityConfigurationBuilder;
396  JBasicEventMarshaller m_defaultEventMarshaller;
397  NearCacheConfigurationBuilder nearCacheConfigurationBuilder;
398 
399  EventMarshaller &m_eventMarshaller=m_defaultEventMarshaller;
400 };
401 
402 }} // namespace
403 
404 #endif // ISPN_HOTROD_CONFIGURATION_CONFIGURATIONBUILDER_H
ConfigurationBuilder & addServers(std::string servers)
Definition: ConfigurationBuilder.h:167
ConnectionPoolConfigurationBuilder & connectionPool()
Definition: ConfigurationBuilder.h:192
HR_EXTERN const event::EventMarshaller & getEventMarshaller() const
ConfigurationBuilder & keySizeEstimate(int keySizeEstimate_)
Definition: ConfigurationBuilder.h:222
HR_EXTERN const int & getSocketTimeout() const
void validate()
Definition: ConfigurationBuilder.h:131
NearCacheConfiguration create()
Definition: ConfigurationBuilder.h:96
HR_EXTERN const int & getValueSizeEstimate() const
NearCacheConfigurationBuilder & maxEntries(unsigned int maxEntries=0)
Definition: ConfigurationBuilder.h:72
ConfigurationBuilder()
Definition: ConfigurationBuilder.h:114
ConfigurationBuilder & valueSizeEstimate(int valueSizeEstimate_)
Definition: ConfigurationBuilder.h:283
ConfigurationBuilder & maxRetries(int maxRetries_)
Definition: ConfigurationBuilder.h:294
NearCacheConfigurationBuilder(ConfigurationBuilder &_builder)
Definition: ConfigurationBuilder.h:56
Definition: NearCacheConfiguration.h:32
ServerConfigurationBuilder & addServer()
Definition: ConfigurationBuilder.h:150
Definition: CacheClientListener.h:30
HR_EXTERN const int & getKeySizeEstimate() const
Definition: SecurityConfigurationBuilder.h:19
Definition: ConnectionPoolConfigurationBuilder.h:15
Definition: ConfigurationBuilder.h:53
Definition: NearCacheConfiguration.h:21
ConfigurationBuilder & socketTimeout(int socketTimeout_)
Definition: ConfigurationBuilder.h:245
Definition: SslConfigurationBuilder.h:12
Definition: AuthenticationConfiguration.h:10
HR_EXTERN const int & getMaxRetries() const
NearCacheMode
Definition: NearCacheConfiguration.h:20
ConfigurationBuilder & balancingStrategyProducer(FailOverRequestBalancingStrategy::ProducerFn bsp)
Definition: ConfigurationBuilder.h:299
Definition: ServerConfigurationBuilder.h:25
HR_EXTERN const bool & isForceReturnValue() const
Definition: ConfigurationBuilder.h:28
virtual ServerConfiguration create()
Definition: ServerConfigurationBuilder.h:62
ConfigurationBuilder & forceReturnValues(bool forceReturnValues_)
Definition: ConfigurationBuilder.h:212
HR_EXTERN const int & getConnectionTimeout() const
Definition: Configuration.h:28
FailOverRequestBalancingStrategy *(* ProducerFn)()
Definition: FailOverRequestBalancingStrategy.h:19
Configuration create()
Definition: ConfigurationBuilder.h:320
HR_EXTERN const char * getProtocolVersionCString() const
#define __pragma(...)
Definition: defs.h:106
int getMaxEntries() const
Definition: ConfigurationBuilder.h:61
NearCacheConfigurationBuilder & mode(NearCacheMode mode=DISABLED)
Definition: ConfigurationBuilder.h:91
NearCacheConfigurationBuilder & nearCache()
Definition: ConfigurationBuilder.h:264
Definition: ConfigurationBuilder.h:111
HR_EXTERN const bool & isTcpNoDelay() const
Definition: ConfigurationChildBuilder.h:18
NearCacheMode getMode() const
Definition: ConfigurationBuilder.h:80
static HR_EXTERN const char * DEFAULT_CLUSTER_NAME
Definition: Configuration.h:42
Configuration build()
Definition: ConfigurationBuilder.h:310
ConfigurationBuilder & protocolVersion(const std::string &protocolVersion_)
Definition: ConfigurationBuilder.h:234
SslConfigurationBuilder & ssl()
Definition: ConfigurationBuilder.h:255
SecurityConfigurationBuilder & security()
Definition: ConfigurationBuilder.h:379
ConfigurationBuilder & read(Configuration &configuration)
Definition: ConfigurationBuilder.h:361
ClusterConfigurationBuilder(std::vector< ServerConfigurationBuilder > &servers, ConfigurationBuilder &parent)
Definition: ConfigurationBuilder.h:31
ConfigurationBuilder & tcpNoDelay(bool tcpNoDelay_)
Definition: ConfigurationBuilder.h:273
ConfigurationBuilder & connectionTimeout(int connectionTimeout_)
Definition: ConfigurationBuilder.h:201
ClusterConfigurationBuilder addCluster(const std::string &clusterName)
Definition: ConfigurationBuilder.h:140
ClusterConfigurationBuilder & addClusterNode(const std::string host, const int port)
Definition: ConfigurationBuilder.h:40