Exemplo n.º 1
0
Arquivo: Program.cs Projeto: kutagh/CC
        private static void ProcessClient(Port port, TcpClient client)
        {
            Console.WriteLine("Processing port {0}.", port);
            var nb = new Neighbor(port, client);
            while (Global.RoutingTable == null) { Thread.Sleep(10); Console.WriteLine("Sleep"); }
            while (Global.Threads == null) { Thread.Sleep(10); Console.WriteLine("Sleep"); }
            while (Global.Neighbors == null) { Thread.Sleep(10); Console.WriteLine("Sleep"); }
            lock (Global.Neighbors) {
                if(Global.Neighbors.ContainsKey(port))
                    Global.Neighbors[port] = nb;
                else Global.Neighbors.Add(port, nb);
                Console.WriteLine("Neighbor added");
            }
            lock (Global.RoutingTable) {
                if (!Global.RoutingTable.ContainsKey(port))
                    Global.RoutingTable.Add(port, new Row() { NBu = port, Du = 1 });
                else {
                    Global.RoutingTable[port].NBu = port;
                    Global.RoutingTable[port].Du = 1;
                    Console.WriteLine("Routing Table row updated");
                }
                if(Global.RoutingTable[port].NDISu.ContainsKey(port))
                    Global.RoutingTable[port].NDISu[port] = 0;
                else
                    Global.RoutingTable[port].NDISu.Add(port, 0);
            }
            lock (Global.Threads) {
                var listenForMessages = new Thread(() => ListenTo(port, client));
                Global.Threads.Add(port, listenForMessages);
                listenForMessages.Start();
                Console.WriteLine("Listener added");
            }

            if (Global.Verbose)
                Console.WriteLine("Nieuwe verbinding met node {0}".Formatter(port));

            RoutingTable.AddDirty(port);
            RoutingTable.AddNewPort(port);
        }
Exemplo n.º 2
0
Arquivo: Program.cs Projeto: kutagh/CC
        static void Main(string[] args)
        {
            #if DEBUG
            Console.WriteLine("Debugging mode");
            #endif
            if (args.Length == 0) {
                args = new string[] { "1002", "1001" };
            }
            int iterator = 0; // For easier handling of the args array, since we have two optional parameters but we always know when they can appear
            if (args[0][0] == 'p') {
                // Set console position
                int x = int.Parse(args[iterator++].Substring(1)), y = int.Parse(args[iterator++].Substring(1));
                SetWindowPos(MyConsole, 0, x, y, 0, 0, SWP_NOSIZE);
                Console.Title = "port = {0}, x = {1}, y = {2}".Formatter(args[iterator], x, y); // Port number
            } // In both cases, set the proper requested console title
            else {
                Console.Title = "port = {0}".Formatter(args[iterator]); // Port number
            }
            // Initialize routing table
            Global.LocalPort = Port.Parse(args[iterator]);
            var local = new Neighbor(Global.LocalPort, null);
            Global.RoutingTable.Add(Global.LocalPort, new Row() { NBu = Global.LocalPort, Du = 0 });
            // Start listener service first
            Thread listener = new Thread(() => ListenAt(Global.LocalPort));
            listener.Start();

            // Connect to other ports
            while (++iterator < args.Length) {
                Port port = Port.Parse(args[iterator]);
            #if DEBUG
                Console.WriteLine(port);
            #endif
                if (port > Global.LocalPort) ConnectTo(port);
            #if DEBUG
                else Console.WriteLine("Skipped");
            #endif
            }
            #if DEBUG
            Console.WriteLine("Connected to them all, time to broadcast");
            #endif
            // Input handling

            while (true) {
                var input = Console.ReadLine();
                if (input.StartsWith("S")) {
                    var prev = Global.Slowdown;
                    if(int.TryParse(input.Substring(2), out Global.Slowdown)) {
                        if (Global.Slowdown < 0) {
                            Global.Slowdown = prev;
                            Console.WriteLine(Global.Strings.ParameterError, "slowdown", "n", "a positive number");
                        }
                    }
                    Console.WriteLine(Global.Strings.ParameterError, "slowdown", "n", "a positive number");
                    continue;
                }
                if (input.StartsWith("R")) {
                    PrintRoutingTable();
                    continue;
                }
                if (input.StartsWith("M")) {
                    Console.WriteLine("Total number of distance estimates sent by this node: {0}".Formatter(Global.DistanceEstimates));
                    continue;
                }
                if (input.StartsWith("T")) {
                    input = input.Substring(2).Trim();
                    if (input.Equals("off", StringComparison.InvariantCultureIgnoreCase))
                        Global.Verbose = false;
                    else if (input.Equals("on", StringComparison.InvariantCultureIgnoreCase))
                        Global.Verbose = true;
                    else
                        Console.WriteLine(Global.Strings.ParameterError, "toggle", "state", "on or off");
                    continue;
                }
                if (input.StartsWith("D")) {
                    Port target;
                    if (Port.TryParse(input.Split(' ')[1], out target)) {
                        lock (Global.RoutingTable)
                            lock (Global.Neighbors)
                                if (Global.Neighbors.ContainsKey(target)) {
                                    Global.RoutingTable[target].SendMessage(Global.CreatePackage(Global.PackageNames.Disconnect, target, Global.LocalPort.ToString()));
                                    Disconnect(target);
                                }
                                else
                                    Console.WriteLine(Global.Strings.ParameterError, "delete", "port", "a valid port number that is connected to this node");
                    }
                    else
                        Console.WriteLine(Global.Strings.ParameterError, "delete", "port", "a valid port number");
                    continue;
                }
                if (input.StartsWith("C")) {
                    Port target;
                    if (Port.TryParse(input.Split(' ')[1], out target)) {
                        ConnectTo(target);
                    }
                    else
                        Console.WriteLine(Global.Strings.ParameterError, "connect", "port", "a valid port number");

                    continue;
                }
                if (input.StartsWith("B")) {
                    var split = input.Split(' ');
                    Port target;
                    if (split.Length > 2 && Port.TryParse(split[1], out target)) {
                        if (!IsInPartition(target) || target == Global.LocalPort) {
                            Console.WriteLine(Global.Strings.ParameterError, "broadcast", "port", "a valid port number that is connected to the current node");
                            continue;
                        }
                        var message = new StringBuilder(split[2]);
                        for (int i = 3; i < split.Length; i++)
                            message.AppendFormat(" {0}", split[i]);
                        var msg = Global.CreatePackage("Broadcast", target, message.ToString());
                        Port sendTo;
                        lock (Global.RoutingTable)
                            sendTo = Global.RoutingTable[target].NBu;
            #if DEBUG
                        Console.WriteLine("About to broadcast {0} to port {1} via port {2}", msg, target, sendTo);
            #endif
                        lock (Global.Neighbors)
                            Global.Neighbors[sendTo].SendMessage(msg);
            #if DEBUG
                        Console.WriteLine("Sent message to {0}", target);
            #endif
                        continue;
                    }
                    if (split.Length > 2)
                        Console.WriteLine(Global.Strings.ParameterError, "broadcast", "port", "a valid port number");
                    else
                        Console.WriteLine(Global.Strings.ParameterError, "broadcast", "message", "a valid message");
                    continue;
                }
                Console.WriteLine("You entered an invalid command. Please retry");
            }
        }