package org.i3xx.util.client.dns;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.NoSuchElementException;

import org.xbill.DNS.DClass;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.SRVRecord;
import org.xbill.DNS.SimpleResolver;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;

public class CloudRegInfo {
	
	/**  */
	private int port;
	
	/**  */
	private String host;
	
	/**  */
	private InetAddress[] addr;
	
	/**
	 * @param domain
	 * @throws TextParseException
	 * @throws UnknownHostException
	 * @throws NoSuchElementException
	 */
	public CloudRegInfo(String domain) throws TextParseException, UnknownHostException, NoSuchElementException {
		
		Lookup lookup = new Lookup("_cloudreg._tcp."+domain, Type.SRV, DClass.IN);
		lookup.setResolver(new SimpleResolver());
		
		lookup.run();
		Record[] answers = lookup.getAnswers();
		
		if(answers!=null && answers.length!=0) {
			Record r = answers[0];
			SRVRecord srv = (SRVRecord)r;
			
			port = srv.getPort();
			host = srv.getTarget().toString();
			
			while(host.endsWith("."))
				host = host.substring(0, host.length()-1);
			
			addr = InetAddress.getAllByName(host);
		}else{
			throw new NoSuchElementException("The service 'cloudreg._tcp."+domain+"' is not available in the DNS.");
		}
		
	}

	/**
	 * @return the port
	 */
	public int getPort() {
		return port;
	}

	/**
	 * @return the host
	 */
	public String getHost() {
		return host;
	}
	
	/**
	 * @return the addresses
	 */
	public InetAddress[] getAddr() {
		return addr;
	}
}

@@ -430,16 +430,18 @@
 vate int
+%5B%5D
  port;%0D%0A
@@ -468,16 +468,18 @@
 e String
+%5B%5D
  host;%0D%0A
@@ -513,16 +513,18 @@
 ddress%5B%5D
+%5B%5D
  addr;%0D%0A
@@ -1009,16 +1009,183 @@
 !=0) %7B%0D%0A
+%09%09%09port = new int%5Banswers.length%5D;%0D%0A%09%09%09host = new String%5Banswers.length%5D;%0D%0A%09%09%09addr = new InetAddress%5Banswers.length%5D%5B%5D;%0D%0A%09%09%09%0D%0A%09%09%09for(int i=0;i%3Canswers.length;i++) %7B%0D%0A%09
 %09%09%09Recor
@@ -1203,16 +1203,17 @@
 rs%5B0%5D;%0D%0A
+%09
 %09%09%09SRVRe
@@ -1241,25 +1241,30 @@
 )r;%0D%0A%09%09%09
+%09
 %0D%0A
+%09
 %09%09%09port
+%5Bi%5D
  = srv.g
@@ -1277,20 +1277,25 @@
 ();%0D%0A%09%09%09
-host
+%09String h
  = srv.g
@@ -1325,23 +1325,22 @@
 %0A%09%09%09
+%09
 %0D%0A%09%09%09
+%09
 while(h
-ost
 .end
@@ -1360,19 +1360,14 @@
 %09%09%09%09
+%09
 h
-ost
  = h
-ost
 .sub
@@ -1377,19 +1377,16 @@
 ing(0, h
-ost
 .length(
@@ -1395,25 +1395,48 @@
 1);%0D%0A%09%09%09
+%09
 %0D%0A
+%09%09%09%09host%5Bi%5D = h;%0D%0A%09
 %09%09%09addr
+%5Bi%5D
  = InetA
@@ -1460,13 +1460,21 @@
 me(h
-ost);
+);%0D%0A%09%09%09%7D//for
 %0D%0A%09%09
@@ -1650,16 +1650,18 @@
 blic int
+%5B%5D
  getPort
@@ -1736,16 +1736,18 @@
 c String
+%5B%5D
  getHost
@@ -1835,16 +1835,18 @@
 ddress%5B%5D
+%5B%5D
  getAddr

Add multiple DNS entries