package net.freertr.rtr;

import java.util.Comparator;
import net.freertr.addr.addrIP;
import net.freertr.ip.ipFwd;
import net.freertr.ip.ipFwdIface;
import net.freertr.pack.packHolder;
import net.freertr.pack.packLdp;
import net.freertr.pipe.pipeLine;
import net.freertr.pipe.pipeSide;
import net.freertr.prt.prtTcp;
import net.freertr.prt.prtUdp;
import net.freertr.util.bits;
import net.freertr.util.counter;
import net.freertr.util.debugger;
import net.freertr.util.logger;

/**
 * label distribution protocol (rfc5036) targeted
 *
 * @author matecsaba
 */
public class rtrLdpTrgtd implements Runnable, Comparator<rtrLdpTrgtd> {

    /**
     * peer address
     */
    public final addrIP peer;

    /**
     * source interface
     */
    public ipFwdIface ifc;

    /**
     * ldp interface
     */
    public rtrLdpIface ldp;

    /**
     * ip layer
     */
    public ipFwd ip;

    /**
     * udp layer
     */
    public prtUdp udp;

    /**
     * tcp layer
     */
    public prtTcp tcp;

    /**
     * connection
     */
    private pipeSide conn;

    private int need2run;

    /**
     * create ldp targeted
     *
     * @param adr address of peer
     */
    public rtrLdpTrgtd(addrIP adr) {
        peer = adr.copyBytes();
    }

    public int compare(rtrLdpTrgtd o1, rtrLdpTrgtd o2) {
        return o1.peer.compare(o1.peer, o2.peer);
    }

    public String toString() {
        return "ldp to " + peer;
    }

    /**
     * start connection
     */
    public void workStart() {
        if (debugger.rtrLdpEvnt) {
            logger.debug("starting targeted hello with " + peer);
        }
        keepWorking();
        new Thread(this).start();
    }

    /**
     * stop connection
     */
    public void workStop() {
        if (debugger.rtrLdpEvnt) {
            logger.debug("stopping targeted hello with " + peer);
        }
        if (conn != null) {
            conn.setClose();
        }
        rtrLdpNeigh ntry = ip.ldpNeighFind(null, peer, false);
        if (ntry == null) {
            return;
        }
        ntry.helloTrg = false;
        if (ntry.helloIfc) {
            return;
        }
        ip.ldpNeighDel(ntry);
    }

    /**
     * keep working
     */
    public void keepWorking() {
        need2run = ldp.trgtHelloIntrvl / 1000;
    }

    public void run() {
        try {
            conn = udp.streamConnect(new pipeLine(65536, true), ifc, packLdp.port, peer, packLdp.port, "ldp", -1, null, -1, -1);
            if (conn == null) {
                ip.ldpTargetDel(this);
                return;
            }
            conn.setTime(ldp.trgtHelloHldtm);
            int run = ldp.trgtHelloHldtm;
            for (;;) {
                bits.sleep(1000);
                run += 1000;
                if (conn == null) {
                    break;
                }
                if (conn.isClosed() != 0) {
                    break;
                }
                need2run--;
                if (need2run < 0) {
                    break;
                }
                if (run > ldp.trgtHelloIntrvl) {
                    if (debugger.rtrLdpEvnt) {
                        logger.debug("tx hello " + peer);
                    }
                    packLdp pk = new packLdp();
                    pk.conn = conn;
                    pk.cntr = new counter();
                    pk.lsrID = ifc.addr.toIPv4();
                    pk.transAddr = ifc.addr.copyBytes();
                    pk.msgTyp = packLdp.msgThello;
                    pk.holdTime = ldp.trgtHelloHldtm / 1000;
                    pk.targeted = true;
                    pk.putHelloParam();
                    pk.putTransAddr();
                    pk.createLDPheader();
                    pk.sendPack();
                    run = 0;
                }
                packLdp pk = new packLdp();
                pk.conn = conn;
                pk.pack.pipeRecv(conn, 0, packHolder.maxData, 142);
                if (pk.parseLDPheader()) {
                    continue;
                }
                if (pk.parseMSGheader()) {
                    continue;
                }
                if (pk.getHelloParam()) {
                    continue;
                }
                pk.transAddr = peer.copyBytes();
                pk.getTransAddr();
                if (debugger.rtrLdpEvnt) {
                    logger.debug("rx hello " + peer);
                }
                rtrLdpNeigh ntry = ip.ldpNeighFind(null, peer, true);
                if (ntry == null) {
                    continue;
                }
                ntry.helloTrg = true;
                if (ntry.ifc != null) {
                    continue;
                }
                ntry.ifc = ifc;
                ntry.udp = udp;
                ntry.tcp = tcp;
                ntry.trans = pk.transAddr;
                ntry.lsrID = pk.lsrID;
                ntry.sessHelloHldtm = ldp.sessHelloHldtm;
                ntry.sessHelloIntrvl = ldp.sessHelloIntrvl;
                ntry.sessionTTL = ldp.sessionTTL;
                ntry.sessionTOS = ldp.sessionTOS;
                ntry.filterIn = ldp.filterIn;
                ntry.filterOut = ldp.filterOut;
                ntry.labelPop = ldp.labelPop;
                ntry.startPeer();
            }
        } catch (Exception e) {
            logger.traceback(e);
        }
        if (debugger.rtrLdpEvnt) {
            logger.debug("stopping targeted hello with " + peer);
        }
        workStop();
        ip.ldpTargetDel(this);
    }

}
