public AddUpdateRequest(string senderID, int senderPort, int seq, NHLFEntry entry)
 {
     messageType     = "NHLF.AddUpdateRequest";
     this.senderID   = senderID;
     this.senderPort = senderPort;
     this.seq        = seq;
     this.entry      = entry;
 }
Esempio n. 2
0
        // Keyboard input handling -each keypress can be interpreted as
        // a control command.
        void KeyboardControl()
        {
            while (true)
            {
                switch (Console.ReadKey(true).Key)
                {
                case ConsoleKey.T:
                    transportFunction.PrintRouteTable();
                    break;

                case ConsoleKey.A:
                    LRM.PrintAssignments();
                    break;

                case ConsoleKey.P:
                    if (Log.IsPaused)
                    {
                        Log.Unpause();
                    }
                    else
                    {
                        Log.Pause();
                    }
                    break;

#if DEBUG
                case ConsoleKey.Enter:
                    MPLSPacket testPacket = new MPLSPacket(new int[] { 2137 }, "This is a test MPLSMessage.");
                    transportFunction.EnqueuePacketOnFirstQueue(testPacket);
                    break;

                case ConsoleKey.U:
                    NHLFEntry        entry         = new NHLFEntry(10, 1, 17, true, 2, new int[] { 35 });
                    AddUpdateRequest testUpdateReq = new AddUpdateRequest("Helo it me", mgmtLocalPort, 2137, entry);
                    SendManagementMsg(mgmtLocalPort, testUpdateReq);
                    break;

                case ConsoleKey.R:
                    RemoveRequest testRemoveReq = new RemoveRequest("Helo it me", mgmtLocalPort, 2137, 10);
                    SendManagementMsg(mgmtLocalPort, testRemoveReq);
                    break;

                case ConsoleKey.L:
                    AllocateRequest testAllocReq = new AllocateRequest(id, mgmtLocalPort, allocCounter++, 1, 30, 1);
                    SendManagementMsg(mgmtLocalPort, testAllocReq);
                    break;

                case ConsoleKey.D:
                    DeallocateRequest testDeallocReq = new DeallocateRequest(id, mgmtLocalPort, allocCounter--, 1);
                    SendManagementMsg(mgmtLocalPort, testDeallocReq);
                    break;
#endif
                default:
                    break;
                }
            }
        }
        /*
         * ParseRoutingTable(string)
         *
         * This method parses routing information from a specially formatted
         * text file. The data format is as follows:
         *
         *  interface_in    label_in    add/swap(bool)  interface_out   label(s)_out
         *
         * Row elements are tab-separated (\t) and labels (if adding more) are comma-separated.
         *
         * @returns RouteEntry[] - "list of rows" in the routing table
         */
        public void ParseRoutingTable(string pathToFile)
        {
            List <NHLFEntry> newEntryList = new List <NHLFEntry>();

            // If the filepath is empty, return an empty object (the table is clear)
            if (pathToFile == "")
            {
                return;
            }

            try
            {
                string[] tableRows = File.ReadAllLines(pathToFile, Encoding.UTF8);

                // Iterate over each line in routing config file
                foreach (string row in tableRows)
                {
                    if (row[0] == '#') // Check if a row is a comment - if true, skip it
                    {
                        continue;
                    }
                    string[]  attributes = row.Split('\t');
                    NHLFEntry newEntry   = new NHLFEntry(
                        int.Parse(attributes[0]),
                        byte.Parse(attributes[1]),
                        int.Parse(attributes[2]),
                        bool.Parse(attributes[3]),
                        byte.Parse(attributes[4]),
                        Array.ConvertAll(attributes[5].Split(','), int.Parse) // Convert labels as CSVs into int[]
                        );
                    newEntryList.Add(newEntry);
                }
                routingTable.Concat(newEntryList); // Concat - instead of assignment, not to erase existing entries
            }
            catch (FileNotFoundException)
            {
                throw new FileNotFoundException();
            }
        }
        // Adds, swaps or removes (depending on situation) an entry in the
        // routing table. Returns a textual representation of the operation
        // undertaken - "Added", "Swapped" or "Removed".
        public string UpdateRoutingTable(NHLFEntry newEntry, bool addOrSwap)
        {
            NHLFEntry existingEntry = null;
            bool      entrySwapped  = true;
            string    actionTaken;

            try
            {
                var queryResults = from entry in routingTable
                                   where entry.interface_in == newEntry.interface_in && entry.label_in == newEntry.label_in
                                   select entry;

                existingEntry = queryResults.Single();
                routingTable.Remove(existingEntry);
            }
            catch (InvalidOperationException)
            {
                entrySwapped = false;
            }

            if (addOrSwap)
            {
                routingTable.Add(newEntry);
                if (entrySwapped)
                {
                    actionTaken = "Swapped";
                }
                else
                {
                    actionTaken = "Added";
                }
            }
            else
            {
                actionTaken = "Removed";
            }

            return(actionTaken);
        }