// Path: src/main/java/com/thenetcircle/comsumerdispatcher/util/HttpUtil.java
// public class HttpUtil {
// 	private static Log _logger = LogFactory.getLog(HttpUtil.class);
// 	
// 	public static String sendHttpPost(String url, String host, Map<String, String> parameters, int timeout) throws Exception {
// 		DefaultHttpClient httpclient = getDefaultHttpClient(host);
// 		UrlEncodedFormEntity formEntity = null;
// 		try {
// 			formEntity = new UrlEncodedFormEntity(getParamsList(parameters), HTTP.UTF_8);
// 		} catch (UnsupportedEncodingException e) {
// 			throw e;
// 		}
// 		HttpPost hp = new HttpPost(url);
// 		hp.setEntity(formEntity);
// 		if(host !=null && host.length() > 0)
// 			hp.setHeader("Host", host);
// 
// 		String responseStr = null;
// 		try {
// 			HttpResponse response = httpclient.execute(hp);
// 			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 				HttpEntity entity = response.getEntity();
// 				responseStr = new String(EntityUtils.toByteArray(entity));
// 			} else {
// 				responseStr = "get the response status:" + response.getStatusLine().getStatusCode();
// 			}
// 		} catch (Exception e) {
// 			_logger.error(e, e);
// 			return null;
// 		} finally {
// 			abortConnection(hp, httpclient);
// 		}
// 		return responseStr;
// 	}
// 
// 	private static void abortConnection(final HttpRequestBase hrb, final HttpClient httpclient) {
// 		if (hrb != null) {
// 			hrb.abort();
// 		}
// 		if (httpclient != null) {
// 			httpclient.getConnectionManager().shutdown();
// 		}
// 	}
// 
// 	private static DefaultHttpClient getDefaultHttpClient(String host) {
// 		DefaultHttpClient httpclient = new DefaultHttpClient();
// 		httpclient.getParams().setParameter(
// 				CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
// 		httpclient.getParams().setParameter("Host", host);
// 
// 		return httpclient;
// 	}
// 
// 	private static List<NameValuePair> getParamsList(Map<String, String> paramsMap) {
// 		if (paramsMap == null || paramsMap.size() == 0) {
// 			return null;
// 		}
// 		List<NameValuePair> params = new ArrayList<NameValuePair>();
// 		for (Map.Entry<String, String> map : paramsMap.entrySet()) {
// 			params.add(new BasicNameValuePair(map.getKey(), map.getValue()));
// 		}
// 		return params;
// 	}
// 	
// 	public static String convertUrlToHostNameAsNodeName(String url) throws MalformedURLException {
// 		String host = new URL(url).getHost().replace('.', '-');
// 		return host;
// 	}
// 	
// 	public static String getLocalHostName() {
// 		String hostname = null;
// 		try {
// 		    InetAddress addr = InetAddress.getLocalHost();
// 		    //byte[] ipAddr = addr.getAddress();
// 		    hostname = addr.getHostName();
// 		} catch (UnknownHostException e) {
// 			_logger.error(e, e);
// 		}
// 		return hostname;
// 	}
// }


import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.server.ServerConfig;
import org.apache.zookeeper.server.ZooKeeperServerMain;
import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
import com.thenetcircle.comsumerdispatcher.util.HttpUtil;
		if (clientConfigPath != null) {
			_logger.info("[Distribution] starts up as client, will laod data from distribution server....");
			runType = RUNTYPE_DIST_CLIENT;
			initZkConnection(clientConfigPath);
			
			while(getLivingJoinedMemberNum() < 1) {
				_logger.info("[Distribution] seems master is not up. wait for master to finishe starting up....");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
				}
			}
			
		} else if (serverConfigPath != null) {
			_logger.info("[Distribution] starts up as master, needs to start distribution server and populate data to server ....");
			runType = RUNTYPE_DIST_SERVER; // this is master, need to start distribution server and populate data to server
			loadDistributionServerConfig(serverConfigPath);
			
			startupServer();
			
			initZkConnection(sc.getClientPortAddress().getHostName() + ":" + sc.getClientPortAddress().getPort());
			
			// populate data to server
			populateDataTree();
		}
		
		updateJoinedMemberNode();
	}
	
	protected void updateJoinedMemberNode() {
// Path: src/main/java/com/thenetcircle/comsumerdispatcher/util/HttpUtil.java
// public class HttpUtil {
// 	private static Log _logger = LogFactory.getLog(HttpUtil.class);
// 	
// 	public static String sendHttpPost(String url, String host, Map<String, String> parameters, int timeout) throws Exception {
// 		DefaultHttpClient httpclient = getDefaultHttpClient(host);
// 		UrlEncodedFormEntity formEntity = null;
// 		try {
// 			formEntity = new UrlEncodedFormEntity(getParamsList(parameters), HTTP.UTF_8);
// 		} catch (UnsupportedEncodingException e) {
// 			throw e;
// 		}
// 		HttpPost hp = new HttpPost(url);
// 		hp.setEntity(formEntity);
// 		if(host !=null && host.length() > 0)
// 			hp.setHeader("Host", host);
// 
// 		String responseStr = null;
// 		try {
// 			HttpResponse response = httpclient.execute(hp);
// 			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 				HttpEntity entity = response.getEntity();
// 				responseStr = new String(EntityUtils.toByteArray(entity));
// 			} else {
// 				responseStr = "get the response status:" + response.getStatusLine().getStatusCode();
// 			}
// 		} catch (Exception e) {
// 			_logger.error(e, e);
// 			return null;
// 		} finally {
// 			abortConnection(hp, httpclient);
// 		}
// 		return responseStr;
// 	}
// 
// 	private static void abortConnection(final HttpRequestBase hrb, final HttpClient httpclient) {
// 		if (hrb != null) {
// 			hrb.abort();
// 		}
// 		if (httpclient != null) {
// 			httpclient.getConnectionManager().shutdown();
// 		}
// 	}
// 
// 	private static DefaultHttpClient getDefaultHttpClient(String host) {
// 		DefaultHttpClient httpclient = new DefaultHttpClient();
// 		httpclient.getParams().setParameter(
// 				CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
// 		httpclient.getParams().setParameter("Host", host);
// 
// 		return httpclient;
// 	}
// 
// 	private static List<NameValuePair> getParamsList(Map<String, String> paramsMap) {
// 		if (paramsMap == null || paramsMap.size() == 0) {
// 			return null;
// 		}
// 		List<NameValuePair> params = new ArrayList<NameValuePair>();
// 		for (Map.Entry<String, String> map : paramsMap.entrySet()) {
// 			params.add(new BasicNameValuePair(map.getKey(), map.getValue()));
// 		}
// 		return params;
// 	}
// 	
// 	public static String convertUrlToHostNameAsNodeName(String url) throws MalformedURLException {
// 		String host = new URL(url).getHost().replace('.', '-');
// 		return host;
// 	}
// 	
// 	public static String getLocalHostName() {
// 		String hostname = null;
// 		try {
// 		    InetAddress addr = InetAddress.getLocalHost();
// 		    //byte[] ipAddr = addr.getAddress();
// 		    hostname = addr.getHostName();
// 		} catch (UnknownHostException e) {
// 			_logger.error(e, e);
// 		}
// 		return hostname;
// 	}
// }


// Path: src/main/java/com/thenetcircle/comsumerdispatcher/distribution/DistributionManager.java
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.server.ServerConfig;
import org.apache.zookeeper.server.ZooKeeperServerMain;
import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
import com.thenetcircle.comsumerdispatcher.util.HttpUtil;

		if (clientConfigPath != null) {
			_logger.info("[Distribution] starts up as client, will laod data from distribution server....");
			runType = RUNTYPE_DIST_CLIENT;
			initZkConnection(clientConfigPath);
			
			while(getLivingJoinedMemberNum() < 1) {
				_logger.info("[Distribution] seems master is not up. wait for master to finishe starting up....");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
				}
			}
			
		} else if (serverConfigPath != null) {
			_logger.info("[Distribution] starts up as master, needs to start distribution server and populate data to server ....");
			runType = RUNTYPE_DIST_SERVER; // this is master, need to start distribution server and populate data to server
			loadDistributionServerConfig(serverConfigPath);
			
			startupServer();
			
			initZkConnection(sc.getClientPortAddress().getHostName() + ":" + sc.getClientPortAddress().getPort());
			
			// populate data to server
			populateDataTree();
		}
		
		updateJoinedMemberNode();
	}
	
	protected void updateJoinedMemberNode() {
		String hostname = HttpUtil.getLocalHostName();