package org.kne.cloud.network.tun;

import java.io.IOException;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;


public class TestTUN {
	public static void main(String[] args) throws IOException, InterruptedException {
		TUNNetworkDevice tun = TUNNetworkDevice.createDevice("klalb1", "KLALB Decentralized SRv6 Network");
		InetAddressRow row4=new InetAddressRow(InetAddress.getByName("2486::1111"),120);
		InetAddressRow row6=new InetAddressRow(InetAddress.getByName("3.3.3.3"), 32);
		tun.setIPAddress(List.of(row4,row6));
		List<InetAddress> alist=new ArrayList<>();
		alist.add(InetAddress.getByName("2486:1::8888"));
		alist.add(InetAddress.getByName("8.8.8.8"));
		tun.setDNSAddress(alist );
		System.out.println(tun.getDNSAddress());
		//System.out.println(tun.getMTU());
		tun.setMTU(9000);
		TUNChannel channel=tun.openSession();
		new Thread(() -> {
			try {
				Thread.sleep(1000000);
				tun.close();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}).start();
		new Thread(() -> {
			try {
				ByteBuffer bb = ByteBuffer.allocate(9000);
				while (true) {
					long t2 = 0,t3=0;
					long t=System.nanoTime();
					channel.read(bb);
					long t1=System.nanoTime();
					
					bb.flip();
					/*try {
						long tc=System.nanoTime();
						IpV6Packet ipp=IpV6Packet.newPacket(bb.array(), 0, bb.limit());
						if(ipp.getHeader().getVersion().equals(IpVersion.IPV6)) {
							Packet pl=ipp.getPayload();
							if(pl instanceof IcmpV6CommonPacket) {
								Packet req=pl.getPayload();
								if(req instanceof IcmpV6EchoRequestPacket) {
									//System.out.println("recv:");
						//System.out.println(ipp+"\n");
							IcmpV6EchoReplyPacket.Builder ibdr=new IcmpV6EchoReplyPacket.Builder();
							ibdr.sequenceNumber(((IcmpV6EchoRequestPacket)req).getHeader().getSequenceNumber());
							ibdr.identifier(((IcmpV6EchoRequestPacket)req).getHeader().getIdentifier());
							ibdr.payloadBuilder(req.getPayload().getBuilder());
							IcmpV6CommonPacket.Builder icbd=new IcmpV6CommonPacket.Builder();
							icbd.type(IcmpV6Type.ECHO_REPLY);
							icbd.code(IcmpV6Code.NO_CODE);
							icbd.srcAddr(ipp.getHeader().getDstAddr());
							icbd.dstAddr(ipp.getHeader().getSrcAddr());
							icbd.correctChecksumAtBuild(true);
							icbd.payloadBuilder(ibdr);
							IpV6Packet.Builder ipv=new IpV6Packet.Builder();
							ipv.srcAddr(ipp.getHeader().getDstAddr());
							ipv.dstAddr(ipp.getHeader().getSrcAddr());
							ipv.trafficClass(ipp.getHeader().getTrafficClass());
							ipv.payloadBuilder(icbd);
							ipv.version(IpVersion.IPV6);
							ipv.flowLabel(IpV6SimpleFlowLabel.newInstance(0));
							ipv.nextHeader(IpNumber.ICMPV6);
							ipv.correctLengthAtBuild(true);
							ipv.hopLimit((byte) 20);
							//System.out.println("send:");
							Packet resp=ipv.build();
							//System.out.println(resp+"\n");
							byte[]b=resp.getRawData();
							ByteBuffer bbe=ByteBuffer.wrap(b);
							t2=System.nanoTime();
							channel.write(bbe);
					t3=System.nanoTime();
					System.out.println("writetime:"+(t3-t2));
					System.out.println("decodetime:"+(t2-tc));
								}
							}
						//IcmpV6EchoReplyPacket.Builder ib=new IcmpV6EchoReplyPacket.Builder();
						//ib.sequenceNumber(0)
						}
							
						
					} catch (IllegalRawDataException e) {
						e.printStackTrace();
					}*/

					System.out.println("readtime:"+(t1-t));
					bb.clear();
				}

			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}).start();
	}
}
