package org.kne.cloud.network.minecraft;

import java.io.File;
import java.io.IOException;
import java.net.Inet6Address;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

import org.kne.cloud.network.MultipurposeSocketAddress;
import org.kne.cloud.network.SocketBridge;
import org.kne.cloud.network.SocketListener;
import org.kne.cloud.network.SocketToSocketProxy;
import org.kne.cloud.network.klalb.CONST;
import org.kne.cloud.network.klalb.KLALBController;
import org.kne.cloud.network.klalb.KLALBRemoteLine;
import org.kne.util.AutoProperties;

public class SimpleKLALBMinecraftClient {
public static void main(String[] args) throws UnknownHostException, IOException {

	System.out.println(CONST.klalb+" V"+CONST.klalbver);
	Scanner scn=new Scanner(System.in);
	Properties def=new Properties();
	def.setProperty("server", "");
	def.setProperty("local", "");
	AutoProperties ap=new AutoProperties(new File("klalbclient.ini"),def);
	KLALBController kc=new KLALBController();
	kc.registerToProxyTypeAs("KLALB");
	MinecraftServerAddress msa=MinecraftServerAddress.findAddress(ap.getProperty("server"));
	MinecraftServerPinger msp=new MinecraftServerPinger(msa);
	if(msp.getKlalbversion()==null) {
		System.out.println("服务器不支持加速！！");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.exit(0);
	}
	Inet6Address vad= kc.getRemoteVaddrBySocketAddress(msa);
	MultipurposeSocketAddress vmsa=new MultipurposeSocketAddress("KLALB_Stream",new InetSocketAddress(vad, msp.getKlalbvport()));
	System.out.println("连接成功："+ap.getProperty("server"));
	new SocketToSocketProxy(new MultipurposeSocketAddress(ap.getProperty("local")), vmsa);
	System.out.println("提示：输入state并回车可以查看当前线路状态");
	while(true) {
		String s=scn.nextLine();
		switch(s) {
		case "state":
			System.out.println("状态\t上传流量\t下载流量\t上传速度\t下载速度\t延迟\t抖动\t下一次重试");
			for (Iterator<KLALBRemoteLine> iterator = kc.getLines().iterator(); iterator.hasNext();) {
				KLALBRemoteLine hostPort =  iterator.next();
				System.out.println(hostPort .toString2());
				//System.out.println();
			}
			break;
		default :
			System.out.println("未知命令！");
			
		}
	}
}
}
