Example #1
0
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                MijnPoort = int.Parse(args[0]);
            }
            new Server(MijnPoort);

            Console.Title = "MultiClientServer " + MijnPoort;

            int[] buren = new int[args.Length - 1];
            table = new RoutingTable(MijnPoort);

            for (int i = 1; i < args.Length; i++)
            {
                int poort = int.Parse(args[i]);
                buren[i - 1] = poort;
                if (poort > MijnPoort)
                {
                    Buren.Add(poort, new Connection(poort));
                }
            }

            foreach (KeyValuePair <int, Connection> buur in Buren)
            {
                for (int j = 0; j < buren.Length; j++)
                {
                    UpdateDictionary(buren[j] + " 0 " + buren[j] + " " + buren[j]);
                }
            }

            lock (table.Table)
            {
                foreach (KeyValuePair <int, Entry> entry in table.Table)
                {
                    foreach (KeyValuePair <int, Connection> buur in Buren)
                    {
                        SendMessage(buur.Key, "UpdateRoute " + entry.Value.ToString() + " " + MijnPoort);
                    }
                }
            }

            while (true)
            {
                string input = Console.ReadLine();

                switch (input.Split()[0])
                {
                case "C":
                    int poort = int.Parse(input.Split()[1]);
                    if (Buren.ContainsKey(poort))
                    {
                        Console.WriteLine("Hier is al verbinding naar!");
                    }
                    else
                    {
                        // Leg verbinding aan (als client)
                        Buren.Add(poort, new Connection(poort));
                        Console.WriteLine("Verbonden: " + poort);
                        UpdateDictionary(poort + " 0 0 " + poort);

                        lock (table.Table)
                        {
                            foreach (KeyValuePair <int, Entry> entry in table.Table)
                            {
                                SendMessage(poort, "UpdateRoute " + entry.Value.ToString() + " " + MijnPoort);
                            }
                        }

                        foreach (KeyValuePair <int, Connection> buur in Buren)
                        {
                            SendMessage(buur.Key, "UpdateRoute " + MijnPoort + " 0 " + MijnPoort + " " + poort);
                        }
                    }
                    break;

                case "R":
                    Console.WriteLine(table.showTable());
                    break;

                case "B":
                    // Stuur berichtje
                    string[] delen  = input.Split(new char[] { ' ' }, 3);
                    int      poort2 = int.Parse(delen[1]);

                    SendMessage(poort2, delen[2]);

                    break;

                case "D":
                    // Verwijder connectie
                    string[] delen2 = input.Split(' ');
                    int      port   = int.Parse(delen2[1]);
                    SendMessage(port, "PlsRemoveMe " + MijnPoort);
                    UpdateRemoval(port);
                    break;
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            bool cmdInput = (args.Length != 0);

            if (cmdInput)
            {
                MijnPoort = int.Parse(args[0]);
            }
            else
            {
                MijnPoort = int.Parse(Console.ReadLine());
            }

            new Server(MijnPoort);
            Console.Title = "NetChange " + MijnPoort.ToString();
            routingtable  = new RoutingTable(MijnPoort, new Node(MijnPoort, 0, MijnPoort));
            if (cmdInput)
            {
                for (int i = 1; i < args.Length; i++)
                {
                    int  newPoort       = int.Parse(args[i]);
                    Node newNode        = new Node(newPoort, 1, newPoort);
                    bool connectSucceed = false;
                    while (!connectSucceed)
                    {
                        try
                        {
                            AddConnection(newPoort, newNode);
                            connectSucceed = true;
                            Console.WriteLine("Verbonden: " + newPoort);
                        }
                        catch
                        {
                            System.Threading.Thread.Sleep(50);
                        }
                    }
                }
            }

            while (true)
            {
                string input = Console.ReadLine();
                try
                {
                    switch (input.Split()[0])
                    {
                    // R: reveal dictionary
                    case "R":
                    {
                        Print();
                        break;
                    }

                    // B: bericht naar poort
                    case "B":
                    {
                        input = input.Substring(input.IndexOf(" ") + 1);
                        SendMessage(input);
                        break;
                    }

                    // C: connect met poort
                    case "C":
                    {
                        int poort = int.Parse(input.Split()[1]);
                        Console.WriteLine("Adding connection to {0} in routing table", poort);
                        Node newNode = new Node(poort, 1, poort);
                        AddConnection(poort, newNode);
                        break;
                    }

                    // D: destroy verbinding met poort
                    case "D":
                    {
                        int poort = int.Parse(input.Split()[1]);
                        if (routingtable.containskey(poort))
                        {
                            Node destroyNode = new Node(poort, 1, poort);
                            routingtable.RemoveConnection(poort, destroyNode);
                        }
                        else
                        {
                            Console.WriteLine("Poort " + poort + " is niet bekend");
                        }
                        break;
                    }
                    }
                }
                catch (Exception e)
                { Console.WriteLine(e); }
            }
        }