1 module elasticsearch.transport.sniffer;
2 
3 import elasticsearch.transport.transport;
4 
5 import std.regex;
6 
7 class Sniffer {
8 	static regexURL = ctRegex!`\/([^:]*):([0-9]+)\]`;
9 
10 	private {
11 		Transport _transport;
12 	}
13 
14 	this(Transport t) {
15 		_transport = t;
16 	}
17 
18 	@property Host[] hosts() {
19 		import std.conv;
20 		Host[] returnHosts;
21 
22 		auto nodes = _transport.performRequest(RequestMethod.GET, "_nodes/http").jsonBody;
23 		foreach(string id, info; nodes["nodes"]) {
24 			auto key = _transport.protocol ~ "_address";
25 			if (key in info) {
26 				auto address = info[key].to!string;
27 				auto matches = matchFirst(address, regexURL);
28 				if (matches) {
29 					returnHosts ~= Host(matches[1], matches[2].to!int, _transport.protocol);
30 				}
31 			}
32 		}
33 
34 		return returnHosts;
35 	}
36 }
37 
38 
39 /*
40 // Should be reimplementd as a feature test
41 unittest {
42 	import std.exception;
43 	import elasticsearch.transport.http.vibe;
44 	
45 	auto host = Host("localhost");
46 	auto t = new VibeTransport();
47 	t.hosts ~= host;
48 	t.hosts ~= Host();
49 
50 	auto sniffer = new Sniffer(t);
51 
52 	auto hosts = sniffer.hosts();
53 
54 	assert(hosts.length == 1);
55 	assert(host.hostName == "localhost");
56 	assert(host.port == 9200);
57 }
58 */