示例#1
0
        static void Main(string[] args)
        {
            /************ Create node object ************/
            RegisterType type = RegisterType.TaskManager;
            byte parallelThreads = 5;
            string[] problems = { "DVRP"};

            NetworkNode node = new NetworkNode(type, parallelThreads, problems);
            //NetworkNode node = new NetworkNode();

            /************ Setup connection ************/
            string inputLine = "";
            foreach (string arg in args)
                inputLine += arg + " ";

            InputParser inputParser = new InputParser(inputLine);
            inputParser.ParseInput();

            IPAddress address = inputParser.Address;
            int port = inputParser.Port;

            SmartConsole.PrintLine("I'm a " + node.Type, SmartConsole.DebugLevel.Advanced);
            NetworkClient client = new NetworkClient(address, port);

            /************ Setup Logic modules ************/

            // system tracker
            SystemTracker systemTracker = new SystemTracker(node);

            MessageHandler messageHandler = new MessageHandler(systemTracker, client);

            MessageProcessor messageProcessor = new MessageProcessor(messageHandler, client, node);

            node.MessageProcessor = messageProcessor;

            /************ Init all threads ************/
            for (int i = 0; i < parallelThreads; i++)
            {
                node.TaskThreads[i] = new TaskThread(i, problems[0], messageProcessor, (int)node.Id);
            }

            /************ Register ************/
            client.Connect();
            SmartConsole.PrintLine("Sending Register message", SmartConsole.DebugLevel.Advanced);
            messageProcessor.Communicate(node.ToRegisterMessage());

            KeepAliveTimer keepAliveTimer = new KeepAliveTimer(messageProcessor, systemTracker);
            /************ Start Logic modules ************/
            keepAliveTimer.Start();

            Object mutex = new Object();
            // TODO Thread pool waiting

            lock (mutex)
            {
                Monitor.Wait(mutex);
            }
            
        }
示例#2
0
        private static void registerToServer(NetworkClient client, NetworkNode node)
        {
            Console.Write(" >> Press enter to connect to server... \n");
            Console.ReadLine();

            Console.Write(" >> Client connecting to server... " + client.Address + ":" + client.Port + "\n");
            Console.Write(" >> \n\n");
            client.Connect();

            RegisterMessage registerMessage = node.ToRegisterMessage();

            Console.Write(" >> Sending Register message... \n\n");
            client.Send(registerMessage);

            Console.Write(" >> Waiting for response... \n\n");
            Message response = client.Receive();

            Console.Write(" >> The response is: \n\n" + response.ToString() + "\n\n");
        }
        public void InitiateBackup(IPAddress myAddress, int myPort, IPAddress masterAddress, int masterPort)
        {
            SmartConsole.PrintHeader("Starting backup server");
            SmartConsole.PrintLine("Address: " + myAddress.ToString() + ":" + myPort, SmartConsole.DebugLevel.Advanced);

            // Create overall system tracker
            SystemTracker systemTracker = new SystemTracker();

            // Create list of all clients
            ClientTracker clientTracker = new ClientTracker();

            // Task Tracker
            TaskTracker taskTracker = new TaskTracker();

            // Start network connection
            NetworkServer server = new NetworkServer(myAddress, myPort);

            // Create messageHandler
            MessageHandler messageHandler = new MessageHandler(systemTracker, clientTracker, taskTracker, server);

            // Start message queue
            MessageQueue messageQueue = new MessageQueue(server);
            
            // Start Message processor
            CommunicationServer.MessageCommunication.MessageProcessor messageProcessor = new CommunicationServer.MessageCommunication.MessageProcessor(messageQueue, messageHandler);

            // blockade to block untill server is switched to primary mode
            Object backupBlockade = new Object();

            server.Open();
            messageQueue.Start();
            messageProcessor.Start();

            /********************* REGISTER AS NORMAL CLIENT *********************/

            RegisterType type = RegisterType.CommunicationServer;
            NetworkNode node = new NetworkNode(type);
            systemTracker.Node = node;

            NetworkClient client = new NetworkClient(masterAddress, masterPort);

            client.Connect();
            SmartConsole.PrintLine("Sending Register message...", SmartConsole.DebugLevel.Advanced);

            CommunicationServer.MessageCommunication.KeepAliveTimer keepAliveTimer = new 
                                                CommunicationServer.MessageCommunication.KeepAliveTimer(messageHandler,
                                                                                                        client, 
                                                                                                        server,
                                                                                                        systemTracker, 
                                                                                                        node,
                                                                                                        clientTracker,
                                                                                                        backupBlockade);
            keepAliveTimer.Communicate(node.ToRegisterMessage());

            /********************* START COMMUNICATING WITH PRIMARY SERVER *********************/
            SmartConsole.PrintLine("Backup Server starting work", SmartConsole.DebugLevel.Advanced);

            keepAliveTimer.Start();

            // This will hold untill server is switched to primary mode
            lock (backupBlockade)
            {
                Monitor.Wait(backupBlockade);
            }

            /********************* SWITCH TO PRIMARY SERVER *********************/

            SmartConsole.PrintHeader("SWITCHING TO PRIMARY");

            Server.primaryMode = true;

            client.Disconnect();

            clientTracker.RefreshTimeout();
            
            // Start measuring timeout
            clientTracker.StartTimeout();

            // Start console manager
            ConsoleManager consoleManager = new ConsoleManager(server);
            consoleManager.Start();
        }