Пример #1
0
 public void SetRight(Leaf right)
 {
     this.right = right;
 }
Пример #2
0
        public List <int> ORTCIter(Leaf curLeaf, int nextHop)
        {
            List <int> leftVal, rightVal;

            //Atualizar valor do next hop a propagar
            if (curLeaf.GetNextHop() != -1)
            {
                nextHop = curLeaf.GetNextHop();
            }

            //Verificar se nos encontramos num nó só com um filho e adicionar um caso seja esse o caso
            if (curLeaf.GetLeft() == null && curLeaf.GetRight() != null)
            {
                if (nextHop != -1)
                {
                    curLeaf.SetLeft(new Leaf(nextHop));
                }
            }
            else if (curLeaf.GetLeft() != null && curLeaf.GetRight() == null)
            {
                if (nextHop != -1)
                {
                    curLeaf.SetRight(new Leaf(nextHop));
                }
            }

            if (curLeaf.GetLeft() != null && curLeaf.GetRight() != null)
            {
                List <int> intersect = new List <int>();

                leftVal  = ORTCIter(curLeaf.GetLeft(), nextHop);
                rightVal = ORTCIter(curLeaf.GetRight(), nextHop);

                // Caso não seja uma folha, é necessário eliminar o Next Hop do nó atual
                curLeaf.SetNextHop(-1);

                //Verificar se toda a lista contenha o mesmo nexthop, caso sim pode-se truncar por aqui
                //facilmente verificavel caso ambos só transmitão um valor e este seja igual
                if (leftVal[0] == rightVal[0] && leftVal.Count == 1 && rightVal.Count == 1 && leftVal[0] != -1)
                {
                    curLeaf.SetNextHop(leftVal[0]);
                    curLeaf.SetLeft(null);
                    curLeaf.SetRight(null);
                }

                //caso haja interseçao, manter a interseçao, caso nao manter a união
                intersect = leftVal.Intersect <int>(rightVal).ToList <int>();

                //Mudar valor do prefixo e caso esse seja o caso para um dos valores da interseçao

                /*if (curLeaf == root && intersect.Count != 0)
                 *  curLeaf.SetNextHop(intersect[0]);*/

                if (intersect.Count != 0)
                {
                    if (intersect[0] != -1)
                    {
                        curLeaf.SetNextHop(intersect[0]);
                        bool delete = DeleteDefault(curLeaf.GetLeft(), intersect[0]);
                        if (delete)
                        {
                            curLeaf.SetLeft(null);
                        }
                        delete = DeleteDefault(curLeaf.GetRight(), intersect[0]);
                        if (delete)
                        {
                            curLeaf.SetRight(null);
                        }
                    }
                }
                else
                {
                    intersect = leftVal.Union <int>(rightVal).ToList <int>();


                    // Para conseguirmos ter um default

                    /*if (curLeaf == root && intersect[0] != -1)
                     * {
                     *  curLeaf.SetNextHop(intersect[0]);
                     *  bool delete = DeleteDefault(curLeaf.GetLeft(), intersect[0]);
                     *  if (delete)
                     *      curLeaf.SetLeft(null);
                     *  delete = DeleteDefault(curLeaf.GetRight(), intersect[0]);
                     *  if (delete)
                     *      curLeaf.SetRight(null);
                     * }*/

                    if (curLeaf == root && !intersect.Contains(-1))
                    {
                        curLeaf.SetNextHop(intersect[0]);
                        bool delete = DeleteDefault(curLeaf.GetLeft(), intersect[0]);
                        if (delete)
                        {
                            curLeaf.SetLeft(null);
                        }
                        delete = DeleteDefault(curLeaf.GetRight(), intersect[0]);
                        if (delete)
                        {
                            curLeaf.SetRight(null);
                        }
                    }
                }

                return(intersect);
            }
            else
            {
                //List<int> newList = new List<int>();

                //newList.Add(nextHop);


                curLeaf.possibleNextHops = new List <int>();

                curLeaf.possibleNextHops.Add(nextHop);

                return(curLeaf.possibleNextHops);
            }
        }
Пример #3
0
 public void SetLeft(Leaf left)
 {
     this.left = left;
 }
Пример #4
0
        public void LookUp()
        {
            Console.WriteLine("Which address do you want to look up?");
            string address = Console.ReadLine();

            if (address.Length > 16)
            {
                Console.WriteLine("The address you provided is not valid.");
                return;
            }

            if (root == null)
            {
                Console.WriteLine("There is no tree. Please provide a file with a prefix table or insert prefixes.");
                return;
            }

            Leaf aux     = root;
            int  nextHop = aux.GetNextHop();

            for (int i = 0; i < address.Length; i++)
            {
                if (address[i] == '0')
                {
                    if (aux.GetLeft() == null)
                    {
                        if (nextHop == -1)
                        {
                            Console.WriteLine("Package discarded");
                        }
                        else
                        {
                            Console.WriteLine("The next hop is " + nextHop);
                        }

                        return;
                    }


                    aux = aux.GetLeft();
                }
                else
                {
                    if (aux.GetRight() == null)
                    {
                        if (nextHop == -1)
                        {
                            Console.WriteLine("Package discarded");
                        }
                        else
                        {
                            Console.WriteLine("The next hop is " + nextHop);
                        }

                        return;
                    }

                    aux = aux.GetRight();
                }


                if (aux.GetNextHop() != -1)
                {
                    nextHop = aux.GetNextHop();
                }
            }

            if (nextHop == -1)
            {
                Console.WriteLine("Package discarded");
            }
            else
            {
                Console.WriteLine("The next hop is " + nextHop);
            }
        }