Beispiel #1
0
        public void UpdateRoutingTable(LSDB LinkStateDB, bool InfoOnly)
        {
            if (!InfoOnly)
            {
                RoutingTable.Clear();
            }

            foreach (Host ht in LinkStateDB.Hosts)
            {
                try
                {
                    List <String> Routes = new List <String>(LinkStateDB.g.Shortest_path(RouterName, ht.HostName));
                    Routes.Reverse();

                    if (InfoOnly)
                    {
                        Console.Write("Source>> {0} ", RouterName);
                        foreach (String hop in Routes)
                        {
                            Console.Write("> {0} ", hop);
                        }
                        Console.Write("  <<Destination\n");
                    }
                    else
                    {
                        foreach (DataRow row in Neighbours.Rows)
                        {
                            try     // Can't reach hosts
                            {
                                if (Routes.First() == row["Destination"].ToString())
                                {
                                    RoutingTable.Rows.Add(ht.HostName, Routes.First(), Int32.Parse(row["Metric cost"].ToString()));
                                    break;
                                }
                            }
                            catch (Exception)
                            {
                                LinkStateDB.AllOK = false;
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    LinkStateDB.AllOK = false;      // Host disappears while in mid-transaction
                }
            }
            if (InfoOnly)
            {
                Console.WriteLine();
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            LinkStateDB = new LSDB();
            AddTestTopologyData();

            for (;;)
            {
                if (Command == -1)
                {
                    break;
                }

                ShowMenu();
                bool valid = Int32.TryParse(Console.ReadLine(), out Command);

                if (valid)
                {
                    switch (Command)
                    {
                    case 0:         // Exit
                        Command = -1;
                        break;

                    case 1:         // Add Router
                        Console.WriteLine("Router's name: ");
                        Name = Console.ReadLine();
                        if (LinkStateDB.CheckIfExistsRouter(Name))
                        {
                            Console.WriteLine("ERR> Name already taken!");
                        }
                        else
                        {
                            LinkStateDB.Routers.Add(new Router(Name));
                            Console.WriteLine("Router Added");
                        }
                        break;

                    case 2:         // Remove Router
                        Console.WriteLine("Router's name: ");
                        Name = Console.ReadLine();
                        LinkStateDB.RemoveRouter(Name);
                        Console.WriteLine("Router Removed");
                        break;

                    case 3:         // Add Host
                        Console.WriteLine("Host's name: ");
                        Name = Console.ReadLine();
                        Console.WriteLine("Host's default message: ");
                        PacketMessage = Console.ReadLine();
                        if (LinkStateDB.CheckIfExistsHost(Name))
                        {
                            Console.WriteLine("ERR> Name already taken!");
                        }
                        else
                        {
                            LinkStateDB.Hosts.Add(new Host(Name, PacketMessage));
                            Console.WriteLine("Host Added");
                        }
                        break;

                    case 4:         // Remove Host
                        Console.WriteLine("Host's name: ");
                        Name = Console.ReadLine();
                        LinkStateDB.RemoveHost(Name);
                        Console.WriteLine("Host Removed");
                        break;

                    case 5:         // Change Host's Message
                        Console.WriteLine("Host's name: ");
                        Name = Console.ReadLine();
                        Console.WriteLine("Host's new default message: ");
                        PacketMessage = Console.ReadLine();

                        foreach (Host obj in LinkStateDB.Hosts)
                        {
                            if (obj.HostName == Name)
                            {
                                obj.EditPacketMessage(PacketMessage);
                                Console.WriteLine("Message changed");
                            }
                        }
                        break;

                    case 6:         // Add Router's Neighbour ROUTERS ONLY

                        Console.Write("Router's name: ");
                        Name = Console.ReadLine();
                        Console.Write("Neighbour's name: ");
                        NName = Console.ReadLine();
                        Console.Write("Metric cost: ");

                        if (Name == NName)
                        {
                            Console.WriteLine("ERR> already connected!");
                            break;
                        }

                        if (!LinkStateDB.CheckIfExistsRouter(Name))
                        {
                            Console.WriteLine("ERR> Router {0} does not exist!", Name);
                            break;
                        }
                        else
                        {
                            if (!LinkStateDB.CheckIfExistsRouter(NName))
                            {
                                Console.WriteLine("ERR> Router {0} does not exist!", NName);
                                break;
                            }
                            else
                            {
                                if (Int32.TryParse(Console.ReadLine(), out Metric))
                                {
                                    foreach (Router obj in LinkStateDB.Routers)
                                    {
                                        if (obj.RouterName == Name)
                                        {
                                            obj.AddNeighbours(NName, Metric);
                                        }
                                        if (obj.RouterName == NName)
                                        {
                                            obj.AddNeighbours(Name, Metric);
                                        }
                                    }

                                    LinkStateDB.UpdateLSDB();
                                    Console.WriteLine("Routers Connected");
                                    break;
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }


                    case 7:         // Add Host's Neighbour ROUTER-HOST ONLY
                        Console.Write("Host's name: ");
                        Name = Console.ReadLine();
                        Console.Write("Router's name: ");
                        NName = Console.ReadLine();
                        Console.Write("Metric cost: ");

                        if (Name == NName)     // foreach check if exists such connection
                        {
                            Console.WriteLine("ERR> already connected!");
                            break;
                        }

                        if (!LinkStateDB.CheckIfExistsHost(Name))
                        {
                            Console.WriteLine("ERR> Host {0} does not exist!", Name);
                            break;
                        }
                        else
                        {
                            if (!LinkStateDB.CheckIfExistsRouter(NName))
                            {
                                Console.WriteLine("ERR> Router {0} does not exist!", NName);
                                break;
                            }
                            else
                            {
                                if (Int32.TryParse(Console.ReadLine(), out Metric))
                                {
                                    foreach (Host obj in LinkStateDB.Hosts)
                                    {
                                        if (obj.HostName == Name)
                                        {
                                            obj.AddNeighbours(NName, Metric);
                                        }
                                    }

                                    foreach (Router obj in LinkStateDB.Routers)
                                    {
                                        if (obj.RouterName == NName)
                                        {
                                            obj.AddNeighbours(Name, Metric);
                                        }
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }

                        LinkStateDB.UpdateLSDB();
                        Console.WriteLine("Host <---> Router Connected");
                        break;


                    case 8:         // Remove Neighbour's Connection
                        Console.Write("Name #1: ");
                        Name = Console.ReadLine();
                        Console.Write("Name #2: ");
                        NName = Console.ReadLine();
                        LinkStateDB.RemoveConnection(Name, NName);
                        Console.WriteLine("Connection Removed");
                        break;

                    case 9:         // Show Network Topology
                        LinkStateDB.ShowNetworkTopology();
                        break;

                    case 10:        // Send Packet From 1 Host to Another

                        LinkStateDB.AllOK = true;
                        LinkStateDB.UpdateLSDB();
                        if (!LinkStateDB.AllOK)
                        {
                            Console.WriteLine("ERR> Connect all Hosts to a network or delete them!");
                            break;
                        }

                        Console.Write("Sender: ");      // must be hosts
                        Sender = Console.ReadLine();
                        Console.Write("Receiver: ");
                        Receiver = Console.ReadLine();

                        if (!LinkStateDB.CheckIfExistsHost(Sender) || !LinkStateDB.CheckIfExistsHost(Receiver))
                        {
                            Console.WriteLine("ERR> Sender and Receiver must be hosts!");
                            break;
                        }

                        PacketLocation = Sender;
                        bool   PacketArrived = false;
                        String NextHop       = "";

                        if (PacketLocation == Receiver)
                        {
                            PacketArrived = true;
                        }

                        while (!PacketArrived)
                        {
                            if (PacketLocation == Receiver)
                            {
                                PacketArrived = true;
                            }
                            else
                            {
                                //---------------------------------Existing Route
                                NextHop = "";
                                Console.WriteLine("in-proccess> Packet located in {0}", PacketLocation);

                                if (LinkStateDB.CheckIfExistsHost(PacketLocation))     //show next hop
                                {
                                    List <String> Route = new List <String>(LinkStateDB.g.Shortest_path(PacketLocation, Receiver));
                                    Route.Reverse();
                                    try
                                    {
                                        NextHop = Route.First();
                                    }
                                    catch (System.InvalidOperationException)
                                    {
                                        Console.WriteLine("ERR> Can't reach one of the hosts!");
                                        PacketArrived = false;
                                        break;
                                    }
                                    Console.WriteLine("in-proccess> Host does not have Routing table. Closest Router --> {0}", NextHop);
                                }
                                else if (LinkStateDB.CheckIfExistsRouter(PacketLocation))       // show routing table + next hop
                                {
                                    foreach (Router rt in LinkStateDB.Routers)
                                    {
                                        if (rt.RouterName == PacketLocation)
                                        {
                                            Console.WriteLine("in-proccess> From {0} Routing Table: ", rt.RouterName);
                                            foreach (DataRow row in rt.RoutingTable.Rows)
                                            {
                                                if (row["Destination"].ToString() == Receiver)
                                                {
                                                    Console.WriteLine("Dest> {0}  > Next Hop> {1}  > Distance> {2}   <<<<< Router \"{3}\"", row["Destination"], row["Next Hop"], row["Distance"], rt.RouterName);
                                                    NextHop = row["Next Hop"].ToString();
                                                    Console.WriteLine("in-proccess> Expected Next Hop = {0}.", NextHop);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }

                                //------------------------------------Change Route?
                                Console.WriteLine("in-proccess> Change Network topology? [Y/<anything else>]");
                                if (Console.ReadKey().KeyChar == 'y')
                                {
                                    Console.WriteLine("\nin-proccess> Edit Network Topology Before Sending Out The Packet:");

                                    bool StillWantToChange = true;
                                    while (StillWantToChange)
                                    {
                                        ShowChangeMenu();
                                        bool ChoiceValid = Int32.TryParse(Console.ReadLine(), out Command2);

                                        if (ChoiceValid)
                                        {
                                            switch (Command2)
                                            {
                                            case 0:             // Exit
                                                StillWantToChange = false;
                                                break;

                                            case 1:             // Add Router
                                                Console.WriteLine("Router's name: ");
                                                Name = Console.ReadLine();
                                                if (LinkStateDB.CheckIfExistsRouter(Name))
                                                {
                                                    Console.WriteLine("ERR> Name already taken!");
                                                }
                                                else
                                                {
                                                    LinkStateDB.Routers.Add(new Router(Name));
                                                    Console.WriteLine("Router Added");
                                                }
                                                break;

                                            case 2:             // Add Host
                                                Console.WriteLine("Host's name: ");
                                                Name = Console.ReadLine();
                                                Console.WriteLine("Host's default message: ");
                                                PacketMessage = Console.ReadLine();
                                                if (LinkStateDB.CheckIfExistsHost(Name))
                                                {
                                                    Console.WriteLine("ERR> Name already taken!");
                                                }
                                                else
                                                {
                                                    LinkStateDB.Hosts.Add(new Host(Name, PacketMessage));
                                                    Console.WriteLine("Host Added");
                                                }
                                                break;

                                            case 3:             // Add Router's Neighbour ROUTERS ONLY

                                                Console.Write("in-proccess> Router's name: ");
                                                Name = Console.ReadLine();
                                                Console.Write("in-proccess> Neighbour's name: ");
                                                NName = Console.ReadLine();
                                                Console.Write("in-proccess> Metric cost: ");

                                                if (Name == NName)
                                                {
                                                    Console.WriteLine("in-proccess> ERR> already connected!");
                                                    break;
                                                }

                                                if (!LinkStateDB.CheckIfExistsRouter(Name))
                                                {
                                                    Console.WriteLine("in-proccess> ERR> Router {0} does not exist!", Name);
                                                    break;
                                                }
                                                else
                                                {
                                                    if (!LinkStateDB.CheckIfExistsRouter(NName))
                                                    {
                                                        Console.WriteLine("in-proccess> ERR> Router {0} does not exist!", NName);
                                                        break;
                                                    }
                                                    else
                                                    {
                                                        if (Int32.TryParse(Console.ReadLine(), out Metric))
                                                        {
                                                            foreach (Router obj in LinkStateDB.Routers)
                                                            {
                                                                if (obj.RouterName == Name)
                                                                {
                                                                    obj.AddNeighbours(NName, Metric);
                                                                }
                                                                if (obj.RouterName == NName)
                                                                {
                                                                    obj.AddNeighbours(Name, Metric);
                                                                }
                                                            }

                                                            LinkStateDB.UpdateLSDB();
                                                            Console.WriteLine("Routers Connected");
                                                            break;
                                                        }
                                                        else
                                                        {
                                                            break;
                                                        }
                                                    }
                                                }


                                            case 4:             // Add Host's Neighbour ROUTER-HOST ONLY
                                                Console.Write("in-proccess> Host's name: ");
                                                Name = Console.ReadLine();
                                                Console.Write("in-proccess> Router's name: ");
                                                NName = Console.ReadLine();
                                                Console.Write("in-proccess> Metric cost: ");

                                                if (Name == NName)         // foreach check if exists such connection
                                                {
                                                    Console.WriteLine("ERR> already connected!");
                                                    break;
                                                }

                                                if (!LinkStateDB.CheckIfExistsHost(Name))
                                                {
                                                    Console.WriteLine("in-proccess> ERR> Host {0} does not exist!", Name);
                                                    break;
                                                }
                                                else
                                                {
                                                    if (!LinkStateDB.CheckIfExistsRouter(NName))
                                                    {
                                                        Console.WriteLine("in-proccess> ERR> Router {0} does not exist!", NName);
                                                        break;
                                                    }
                                                    else
                                                    {
                                                        if (Int32.TryParse(Console.ReadLine(), out Metric))
                                                        {
                                                            foreach (Host obj in LinkStateDB.Hosts)
                                                            {
                                                                if (obj.HostName == Name)
                                                                {
                                                                    obj.AddNeighbours(NName, Metric);
                                                                }
                                                            }

                                                            foreach (Router obj in LinkStateDB.Routers)
                                                            {
                                                                if (obj.RouterName == NName)
                                                                {
                                                                    obj.AddNeighbours(Name, Metric);
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                            break;
                                                        }
                                                    }
                                                }

                                                LinkStateDB.UpdateLSDB();
                                                Console.WriteLine("Host <---> Router Connected");
                                                break;


                                            case 5:             // Remove Neighbour's Connection
                                                Console.Write("in-proccess> Name #1: ");
                                                Name = Console.ReadLine();
                                                Console.Write("in-proccess> Name #2: ");
                                                NName = Console.ReadLine();
                                                LinkStateDB.RemoveConnection(Name, NName);
                                                Console.WriteLine("Connection Removed");
                                                break;

                                            case 6:             // Show Network Topology
                                                LinkStateDB.ShowNetworkTopology();
                                                break;

                                            case 7:             //Show Routing Tables
                                                LinkStateDB.ShowRoutes();
                                                break;

                                            default:
                                                Console.WriteLine("in-proccess> ERR >> Wrong input!");
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            Console.WriteLine("in-proccess> ERR >> Wrong input!");
                                        }
                                    }
                                    //------------------------------Edit if Route was Changed
                                    if (LinkStateDB.CheckIfExistsHost(PacketLocation))
                                    {
                                        List <String> Route = new List <String>(LinkStateDB.g.Shortest_path(PacketLocation, Receiver));
                                        Route.Reverse();
                                        try
                                        {
                                            NextHop = Route.First();
                                        }
                                        catch (System.InvalidOperationException)
                                        {
                                            Console.WriteLine("ERR> Can't reach one of the hosts!");
                                            PacketArrived = false;
                                            break;
                                        }
                                        Console.WriteLine("in-proccess> Host does not have Routing table. Closest Router --> {0}", NextHop);
                                    }
                                    else if (LinkStateDB.CheckIfExistsRouter(PacketLocation))
                                    {
                                        foreach (Router rt in LinkStateDB.Routers)
                                        {
                                            if (rt.RouterName == PacketLocation)
                                            {
                                                Console.WriteLine("in-proccess> From {0} Routing Table: ", rt.RouterName);
                                                foreach (DataRow row in rt.RoutingTable.Rows)
                                                {
                                                    if (row["Destination"].ToString() == Receiver)
                                                    {
                                                        Console.WriteLine("\n|| Dest> {0}  || Next Hop> {1} || Distance> {2} ||  <<<<< Router \"{3}\"", row["Destination"], row["Next Hop"], row["Distance"], rt.RouterName);
                                                        NextHop = row["Next Hop"].ToString();
                                                        Console.WriteLine("in-proccess> Expected Next Hop = {0}.", NextHop);
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            Console.WriteLine("\n[HOP COMPLETED]");
                            Console.WriteLine("in-proccess> Packet Hop'ed successfully: {0} --> {1}. Route: From {2} to {3}.", PacketLocation, NextHop, Sender, Receiver);
                            PacketLocation = NextHop;
                        }

                        //------------------PacketArrived

                        if (PacketArrived)
                        {
                            Console.WriteLine("in-proccess> Packet arrived successfully: {0} --> {1}.", Sender, Receiver);

                            List <String> Route = new List <String>(LinkStateDB.g.Shortest_path(Sender, Receiver));
                            Console.Write("Route Travelled  {0}", Sender);
                            Route.Reverse();
                            foreach (String str in Route)
                            {
                                Console.Write(" > {0} ", str);
                            }

                            foreach (Host ht in LinkStateDB.Hosts)
                            {
                                if (ht.HostName == Sender)
                                {
                                    Console.WriteLine("\nMessage Arrived: {0}", ht.PacketMessage);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("in-proccess>  Packet was dropped at {0}.", PacketLocation);
                        }
                        break;

                    case 11:
                        LinkStateDB.ShowRoutes();
                        break;

                    default:
                        Console.WriteLine("ERR >> Wrong input!");
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("ERR >> Wrong input!");
                }
            }
        }