1 
2 module hunt.service.util.UrlInfo;
3 
4 import hunt.collection.Map;
5 import hunt.collection.HashMap;
6 import hunt.service.util.UrlHelper;
7 import hunt.Integer;
8 import std.conv;
9 
10 public class UrlInfo  {
11 
12     private enum long                             serialVersionUID = -6438690329875954051L;
13 
14     /**
15      * 原始地址
16      */
17     private  string                              originUrl;
18 
19     /**
20      * The Protocol type.
21      */
22     private string                                        protocolType     = "TCP";
23     /**
24      * The Ip.
25      */
26     private string                                        host;
27 
28     /**
29      * The Port.
30      */
31     private int                                           port             = 80;
32 
33     /**
34      * The path
35      */
36     private string                                        path;
37 
38     /**
39      * 序列化方式,服务端指定,以服务端的为准
40      */
41     private string                                        serializationType;
42 
43     /**
44      * The rpc Version
45      */
46     private int                                           rpcVersion;
47 
48     /**
49      * 权重
50      *
51      * @see UrlInfoAttrs#ATTR_WEIGHT 原始权重
52      * @see UrlInfoAttrs#ATTR_WARMUP_WEIGHT 预热权重
53      */
54     private   int                        weight           = 100;
55 
56     /**
57      * 服务状态
58      */
59     // private  ProviderStatus             status           = ProviderStatus.AVAILABLE;
60 
61     /**
62      * 静态属性,不会变的
63      */
64     private  Map!(string,string)           staticAttrs   ;
65 
66     /**
67      * 动态属性,会动态变的 <br />
68      * <p>
69      * 例如动态权重,是否启用,预热标记等  invocationOptimizing
70      */
71     private   Map!(string,Object) dynamicAttrs ;
72 
73     public void init()
74     {
75         staticAttrs      = new HashMap!(string,string)();
76         dynamicAttrs     = new HashMap!(string,Object)();
77     }
78 
79     /**
80      * Instantiates a new Provider.
81      */
82     public this() {
83         init();
84     }
85     
86     /**
87      * Instantiates a new Provider.
88      *
89      * @param host the host
90      * @param port the port
91      */
92     public this(string host, int port) {
93         init();
94         this.host = host;
95         this.port = port;
96     }
97 
98     /**
99      * Instantiates a new Provider.
100      *
101      * @param host      the host
102      * @param port      the port
103      * @param originUrl the Origin url
104      */
105     public this(string host, int port, string originUrl) {
106         init();
107         this.host = host;
108         this.port = port;
109         this.originUrl = originUrl;
110     }
111 
112     /**
113      * 从URL里解析Provider
114      *
115      * @param url url地址
116      * @return Provider对象 provider
117      * @deprecated use {@link UrlHelper#toProviderInfo(string)}
118      */
119     
120     public static UrlInfo valueOf(string url) {
121         return UrlHelper.toProviderInfo(url);
122     }
123 
124     /**
125      * 序列化到url.
126      *
127      * @return the string
128      * @deprecated use {@link UrlHelper#toUrl(UrlInfo)}
129      */
130     
131     public string toUrl() {
132         return UrlHelper.toUrl(this);
133     }
134 
135     override
136     public bool opEquals(Object o) {
137         if (this is o) {
138             return true;
139         }
140         if (o is null || typeid(this) != typeid(o)) {
141             return false;
142         }
143 
144         UrlInfo that = cast(UrlInfo) o;
145 
146         if (port != that.port) {
147             return false;
148         }
149         if (rpcVersion != that.rpcVersion) {
150             return false;
151         }
152         if (protocolType !is null ? !(protocolType == (that.protocolType)) : that.protocolType !is null) {
153             return false;
154         }
155         if (host !is null ? !(host == (that.host)) : that.host !is null) {
156             return false;
157         }
158         if (path !is null ? !(path == (that.path)) : that.path !is null) {
159             return false;
160         }
161         if (serializationType !is null ? !(serializationType == (that.serializationType))
162             : that.serializationType !is null) {
163             return false;
164         }
165         // return staticAttrs !is null ? staticAttrs.equals(that.staticAttrs) : that.staticAttrs is null;
166         return true;
167     }
168 
169     override
170     public nothrow @trusted size_t toHash() {
171         size_t result = (protocolType !is null ? protocolType.hashOf() : 0);
172         result = 31 * result + (host !is null ? host.hashOf() : 0);
173         result = 31 * result + port;
174         result = 31 * result + (path !is null ? path.hashOf() : 0);
175         result = 31 * result + (serializationType !is null ? serializationType.hashOf() : 0);
176         result = 31 * result + rpcVersion;
177         // result = 31 * result + (staticAttrs !is null ? staticAttrs.hashCode() : 0);
178         return result;
179     }
180 
181     /**
182      * Gets origin url.
183      *
184      * @return the origin url
185      */
186     public string getOriginUrl() {
187         return originUrl;
188     }
189 
190     /**
191      * Sets origin url.
192      *
193      * @param originUrl the origin url
194      * @return the origin url
195      */
196     public UrlInfo setOriginUrl(string originUrl) {
197         this.originUrl = originUrl;
198         return this;
199     }
200 
201     /**
202      * Gets protocol type.
203      *
204      * @return the protocol type
205      */
206     public string getProtocolType() {
207         return protocolType;
208     }
209 
210     /**
211      * Sets protocol type.
212      *
213      * @param protocolType the protocol type
214      * @return the protocol type
215      */
216     public UrlInfo setProtocolType(string protocolType) {
217         this.protocolType = protocolType;
218         return this;
219     }
220 
221     /**
222      * Gets host.
223      *
224      * @return the host
225      */
226     public string getHost() {
227         return host;
228     }
229 
230     /**
231      * Sets host.
232      *
233      * @param host the host
234      * @return the host
235      */
236     public UrlInfo setHost(string host) {
237         this.host = host;
238         return this;
239     }
240 
241     /**
242      * Gets port.
243      *
244      * @return the port
245      */
246     public int getPort() {
247         return port;
248     }
249 
250     /**
251      * Sets port.
252      *
253      * @param port the port
254      * @return the port
255      */
256     public UrlInfo setPort(int port) {
257         this.port = port;
258         return this;
259     }
260 
261     /**
262      * Gets path.
263      *
264      * @return the path
265      */
266     public string getPath() {
267         return path;
268     }
269 
270     /**
271      * Sets path.
272      *
273      * @param path the path
274      * @return the path
275      */
276     public UrlInfo setPath(string path) {
277         this.path = path;
278         return this;
279     }
280 
281     /**
282      * Gets serialization type.
283      *
284      * @return the serialization type
285      */
286     public string getSerializationType() {
287         return serializationType;
288     }
289 
290     /**
291      * Sets serialization type.
292      *
293      * @param serializationType the serialization type
294      * @return the serialization type
295      */
296     public UrlInfo setSerializationType(string serializationType) {
297         this.serializationType = serializationType;
298         return this;
299     }
300 
301     /**
302      * Gets weight.
303      *
304      * @return the weight
305      */
306     public int getWeight() {
307         // ProviderStatus status = getStatus();
308         // if (status == ProviderStatus.WARMING_UP) {
309         //     try {
310         //         // 还处于预热时间中
311         //         Integer warmUpWeight = cast(Integer) getDynamicAttr(UrlInfoAttrs.ATTR_WARMUP_WEIGHT);
312         //         if (warmUpWeight !is null) {
313         //             return warmUpWeight;
314         //         }
315         //     } catch (Exception e) {
316         //         return weight;
317         //     }
318         // }
319         return weight;
320     }
321 
322     /**
323      * Sets weight.
324      *
325      * @param weight the weight
326      * @return the weight
327      */
328     public UrlInfo setWeight(int weight) {
329         this.weight = weight;
330         return this;
331     }
332 
333     /**
334      * Gets sofa version.
335      *
336      * @return the sofa version
337      */
338     public int getRpcVersion() {
339         return rpcVersion;
340     }
341 
342     /**
343      * Sets sofa version.
344      *
345      * @param rpcVersion the sofa version
346      * @return the sofa version
347      */
348     public UrlInfo setRpcVersion(int rpcVersion) {
349         this.rpcVersion = rpcVersion;
350         return this;
351     }
352 
353     /**
354      * Gets status.
355      *
356      * @return the status
357      */
358     // public ProviderStatus getStatus() {
359     //     if (status == ProviderStatus.WARMING_UP) {
360     //         if (System.currentTimeMillis() > (Long) getDynamicAttr(UrlInfoAttrs.ATTR_WARM_UP_END_TIME)) {
361     //             // 如果已经过了预热时间,恢复为正常
362     //             status = ProviderStatus.AVAILABLE;
363     //             setDynamicAttr(UrlInfoAttrs.ATTR_WARM_UP_END_TIME, null);
364     //         }
365     //     }
366     //     return status;
367     // }
368 
369     /**
370      * Sets status.
371      *
372      * @param status the status
373      * @return the status
374      */
375     // public UrlInfo setStatus(ProviderStatus status) {
376     //     this.status = status;
377     //     return this;
378     // }
379 
380     /**
381      * Gets static attribute.
382      *
383      * @return the static attribute
384      */
385     public Map!(string, string) getStaticAttrs() {
386         return staticAttrs;
387     }
388 
389     /**
390      * Sets static attribute.
391      *
392      * @param staticAttrs the static attribute
393      * @return the static attribute
394      */
395     public UrlInfo setStaticAttrs(Map!(string, string) staticAttrs) {
396         this.staticAttrs.clear();
397         this.staticAttrs.putAll(staticAttrs);
398         return this;
399     }
400 
401     /**
402      * Gets dynamic attribute.
403      *
404      * @return the dynamic attribute
405      */
406     public Map!(string, Object) getDynamicAttrs() {
407         return dynamicAttrs;
408     }
409 
410     /**
411      * Sets dynamic attribute.
412      *
413      * @param dynamicAttrs the dynamic attribute
414      * @return this
415      */
416     public UrlInfo setDynamicAttrs(Map!(string, Object) dynamicAttrs) {
417         this.dynamicAttrs.clear();
418         this.dynamicAttrs.putAll(dynamicAttrs);
419         return this;
420     }
421 
422     /**
423      * gets static attribute.
424      *
425      * @param staticAttrKey the static attribute key
426      * @return the static attribute Value
427      */
428     public string getStaticAttr(string staticAttrKey) {
429         return staticAttrs.get(staticAttrKey);
430     }
431 
432     /**
433      * Sets static attribute.
434      *
435      * @param staticAttrKey   the static attribute key
436      * @param staticAttrValue the static attribute value
437      * @return the static attribute
438      */
439     public UrlInfo setStaticAttr(string staticAttrKey, string staticAttrValue) {
440         if (staticAttrValue is null) {
441             staticAttrs.remove(staticAttrKey);
442         } else {
443             staticAttrs.put(staticAttrKey, staticAttrValue);
444         }
445         return this;
446     }
447 
448     /**
449      * gets dynamic attribute.
450      *
451      * @param dynamicAttrKey the dynamic attribute key
452      * @return the dynamic attribute Value
453      */
454     public Object getDynamicAttr(string dynamicAttrKey) {
455         return dynamicAttrs.get(dynamicAttrKey);
456     }
457 
458     /**
459      * Sets dynamic attribute.
460      *
461      * @param dynamicAttrKey   the dynamic attribute key
462      * @param dynamicAttrValue the dynamic attribute value
463      * @return the dynamic attribute
464      */
465     public UrlInfo setDynamicAttr(string dynamicAttrKey, Object dynamicAttrValue) {
466         if (dynamicAttrValue is null) {
467             dynamicAttrs.remove(dynamicAttrKey);
468         } else {
469             dynamicAttrs.put(dynamicAttrKey, dynamicAttrValue);
470         }
471         return this;
472     }
473 
474     override
475     public string toString() {
476         return originUrl is null ? host ~ ":" ~ port.to!string : originUrl;
477     }
478 
479     /**
480      * 得到属性值,先去动态属性,再取静态属性
481      *
482      * @param key 属性Key
483      * @return 属性值
484      */
485     public string getAttr(string key) {
486         string val =  dynamicAttrs.get(key).toString;
487         return val is null ? staticAttrs.get(key) : val;
488     }
489 }