1 /*
2  * Hunt - A redis client library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11  
12 module hunt.redis.RedisShardInfo;
13 
14 import hunt.redis.Exceptions;
15 import hunt.redis.HostAndPort;
16 import hunt.redis.Protocol;
17 import hunt.redis.Redis;
18 import hunt.redis.util.RedisURIHelper;
19 import hunt.redis.util.ShardInfo;
20 import hunt.redis.util.Sharded;
21 
22 import hunt.net.util.HttpURI;
23 
24 import std.conv;
25 
26 class RedisShardInfo : ShardInfo!(Redis) {
27 
28     private int connectionTimeout;
29     private int soTimeout;
30     private string host;
31     private int port;
32     private string password = null;
33     private string name = null;
34     // Default Redis DB
35     private int db = 0;
36     private bool ssl;
37     // private SSLSocketFactory sslSocketFactory;
38     // private SSLParameters sslParameters;
39     // private HostnameVerifier hostnameVerifier;
40     
41     this(string host) {
42         super(DEFAULT_WEIGHT);
43         HttpURI uri = new HttpURI(host);
44         if (RedisURIHelper.isValid(uri)) {
45             this.host = uri.getHost();
46             this.port = uri.getPort();
47             this.password = RedisURIHelper.getPassword(uri);
48             this.db = RedisURIHelper.getDBIndex(uri);
49             this.ssl = RedisURIHelper.isRedisSSLScheme(uri);
50         } else {
51             this.host = host;
52             this.port = Protocol.DEFAULT_PORT;
53         }
54     }
55 
56     // this(string host, SSLSocketFactory sslSocketFactory,
57     //     SSLParameters sslParameters, HostnameVerifier hostnameVerifier) {
58     //   this(host);
59     //   this.sslSocketFactory = sslSocketFactory;
60     //   this.sslParameters = sslParameters;
61     //   this.hostnameVerifier = hostnameVerifier;
62     // }
63 
64     this(string host, string name) {
65         this(host, Protocol.DEFAULT_PORT, name);
66     }
67 
68     this(HostAndPort hp) {
69         this(hp.getHost(), hp.getPort());
70     }
71 
72     this(string host, int port) {
73         this(host, port, Protocol.DEFAULT_TIMEOUT);
74     }
75 
76     this(string host, int port, bool ssl) {
77         this(host, port, Protocol.DEFAULT_TIMEOUT, ssl);
78     }
79 
80     // this(string host, int port, bool ssl, SSLSocketFactory sslSocketFactory,
81     //     SSLParameters sslParameters, HostnameVerifier hostnameVerifier) {
82     //   this(host, port, Protocol.DEFAULT_TIMEOUT, ssl, sslSocketFactory, sslParameters,
83     //       hostnameVerifier);
84     // }
85 
86     this(string host, int port, string name) {
87         this(host, port, Protocol.DEFAULT_TIMEOUT, name);
88     }
89 
90     this(string host, int port, string name, bool ssl) {
91         this(host, port, Protocol.DEFAULT_TIMEOUT, name, ssl);
92     }
93 
94     // this(string host, int port, string name, bool ssl, SSLSocketFactory sslSocketFactory,
95     //     SSLParameters sslParameters, HostnameVerifier hostnameVerifier) {
96     //   this(host, port, Protocol.DEFAULT_TIMEOUT, name, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
97     // }
98 
99     this(string host, int port, int timeout) {
100         this(host, port, timeout, timeout, DEFAULT_WEIGHT);
101     }
102 
103     this(string host, int port, int timeout, bool ssl) {
104         this(host, port, timeout, timeout, DEFAULT_WEIGHT, ssl);
105     }
106 
107     // this(string host, int port, int timeout, bool ssl,
108     //     SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
109     //     HostnameVerifier hostnameVerifier) {
110     //   this(host, port, timeout, timeout, DEFAULT_WEIGHT, ssl, sslSocketFactory,
111     //       sslParameters, hostnameVerifier);
112     // }
113 
114     this(string host, int port, int timeout, string name) {
115         this(host, port, timeout, timeout, DEFAULT_WEIGHT);
116         this.name = name;
117     }
118 
119     this(string host, int port, int timeout, string name, bool ssl) {
120         this(host, port, timeout, timeout, DEFAULT_WEIGHT, ssl);
121         this.name = name;
122     }
123 
124     // this(string host, int port, int timeout, string name, bool ssl,
125     //     SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
126     //     HostnameVerifier hostnameVerifier) {
127     //   this(host, port, timeout, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
128     //   this.name = name;
129     // }
130 
131     this(string host, int port, int connectionTimeout, int soTimeout, int weight) {
132         super(weight);
133         this.host = host;
134         this.port = port;
135         this.connectionTimeout = connectionTimeout;
136         this.soTimeout = soTimeout;
137     }
138 
139     this(string host, int port, int connectionTimeout, int soTimeout, int weight,
140             bool ssl) {
141         super(weight);
142         this.host = host;
143         this.port = port;
144         this.connectionTimeout = connectionTimeout;
145         this.soTimeout = soTimeout;
146         this.ssl = ssl;
147     }
148 
149     // this(string host, int port, int connectionTimeout, int soTimeout, int weight,
150     //     bool ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
151     //     HostnameVerifier hostnameVerifier) {
152     //   this(host, port, connectionTimeout, soTimeout, weight, ssl);
153     //   this.sslSocketFactory = sslSocketFactory;
154     //   this.sslParameters = sslParameters;
155     //   this.hostnameVerifier = hostnameVerifier;
156     // }
157 
158     this(string host, string name, int port, int timeout, int weight) {
159         this(host, port, timeout, timeout, weight);
160         this.name = name;
161     }
162 
163     this(string host, string name, int port, int timeout, int weight, bool ssl) {
164         this(host, port, timeout, timeout, weight, ssl);
165         this.name = name;
166     }
167 
168     // this(string host, string name, int port, int timeout, int weight,
169     //     bool ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
170     //     HostnameVerifier hostnameVerifier) {
171     //   this(host, port, timeout, timeout, weight, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
172     //   this.name = name;
173     // }
174 
175     // this(HttpURI uri) {
176     //   super(DEFAULT_WEIGHT);
177     //   if (!RedisURIHelper.isValid(uri)) {
178     //     throw new InvalidURIException(format(
179     //       "Cannot open Redis connection due invalid HttpURI. %s", uri.toString()));
180     //   }
181 
182     //   this.host = uri.getHost();
183     //   this.port = uri.getPort();
184     //   this.password = RedisURIHelper.getPassword(uri);
185     //   this.db = RedisURIHelper.getDBIndex(uri);
186     //   this.ssl = RedisURIHelper.isRedisSSLScheme(uri);
187     // }
188 
189     // this(HttpURI uri, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
190     //     HostnameVerifier hostnameVerifier) {
191     //   this(uri);
192     //   this.sslSocketFactory = sslSocketFactory;
193     //   this.sslParameters = sslParameters;
194     //   this.hostnameVerifier = hostnameVerifier;
195     // }
196 
197     override
198     string toString() {
199         return host ~ ":" ~ port.to!string() ~ "*" ~ getWeight().to!string();
200     }
201 
202     string getHost() {
203         return host;
204     }
205 
206     int getPort() {
207         return port;
208     }
209 
210     string getPassword() {
211         return password;
212     }
213 
214     void setPassword(string auth) {
215         this.password = auth;
216     }
217 
218     int getConnectionTimeout() {
219         return connectionTimeout;
220     }
221 
222     void setConnectionTimeout(int connectionTimeout) {
223         this.connectionTimeout = connectionTimeout;
224     }
225 
226     int getSoTimeout() {
227         return soTimeout;
228     }
229 
230     void setSoTimeout(int soTimeout) {
231         this.soTimeout = soTimeout;
232     }
233 
234     override
235     string getName() {
236         return name;
237     }
238 
239     int getDb() {
240         return db;
241     }
242 
243     bool getSsl() {
244             return ssl;
245     }
246 
247     // SSLSocketFactory getSslSocketFactory() {
248     //   return sslSocketFactory;
249     // }
250 
251     // SSLParameters getSslParameters() {
252     //   return sslParameters;
253     // }
254 
255     // HostnameVerifier getHostnameVerifier() {
256     //   return hostnameVerifier;
257     // }
258 
259     override
260     Redis createResource() {
261         return new Redis(this);
262     }
263     
264 }