/******************************************************************/
        /************************** CONSTRUCTORS **************************/
        /******************************************************************/

        public MessageProcessor(ClientMessageHandler messageHandler, NetworkClient client, NetworkNode node)
        {
            this.client = client;
            this.messageHandler = messageHandler;

            this.node = node;
        }
Example #2
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);
            }
            
        }
        /******************************************************************/
        /************************** CONSTRUCTORS **************************/
        /******************************************************************/

        /// <summary>
        ///     Creates KeepAliveTimer
        /// </summary>
        /// <param name="messageProcessor"></param>
        /// /// <param name="systemTracker"></param>
        public KeepAliveTimer(MessageHandler messageHandler, NetworkClient networkClient, NetworkServer server,
                                SystemTracker systemTracker, NetworkNode node,
                                ClientTracker clientTracker, Object backupBlockade)
        {
            this.messageHandler = messageHandler;
            this.client = networkClient;
            this.server = server;
            this.systemTracker = systemTracker;
            this.node = node;
            this.clientTracker = clientTracker;
            this.backupBlockade = backupBlockade;

            // TODO Magic numbers
            this.timer = new System.Timers.Timer((systemTracker.Timeout * 1000) / 2);
            this.timer.Elapsed += keepAlive;

            Active = false;
        }
        public void SocketCommunication_ServerSends_ClientReceives()
        {
            IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
            string localhost = "";
            foreach(IPAddress ip in host.AddressList)
                if(ip.AddressFamily.ToString() == "InterNetwork")
                    localhost = ip.ToString();

            // Create message to send
            RegisterMessage expectedMessage = TestHelper.CreateRegisterMessage();
            string messageStr = expectedMessage.ToXmlString();

            // Create server
            IPAddress address = IPAddress.Parse(localhost);
            int port = 5555;

            NetworkServer server = new NetworkServer(address, port);
            server.Open();
            server.StartListeningForClients();
            
            // connect client
            NetworkClient client = new NetworkClient(address, port);
            client.Connect();

            client.Send(expectedMessage);

            ArrayList sockets = new ArrayList();

            while ((sockets = server.SelectForRead()).Count == 0) ;

            Socket socket = (Socket)sockets[0];
            Message actualMessage = server.Receive(socket);

            server.Close();
            client.Disconnect();
            
            Assert.AreEqual(expectedMessage, actualMessage);

        }
Example #5
0
        static void Main(string[] args)
        {
            RegisterType type = RegisterType.ComputationalClient;
            byte parallelThreads = 5;
            string[] problems = { "DVRP" };

            SolveRequestMessage solveRequestMessage = new SolveRequestMessage();

            string inputLine = "";
            foreach (string arg in args)
                inputLine += arg + " ";

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

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

            NetworkNode node = new NetworkNode(type, parallelThreads, problems) { Timeout = CLIENT_REQUEST_FREQUENCY };



            SmartConsole.PrintLine("ComputationalClient starting work", SmartConsole.DebugLevel.Advanced);

            NetworkClient client = new NetworkClient(address, port);

            for (; ; )
            {
                /*************** Register *****************/

                doWork = true;

                SmartConsole.PrintLine("Type in a file path", SmartConsole.DebugLevel.Advanced);
                String filePath = Console.ReadLine();
                solveRequestMessage = loadDataFromDisc(filePath);

                /******  setup logic modules *****************/
                SystemTracker systemTracker = new SystemTracker(node);
                MessageHandler messageHandler = new MessageHandler(systemTracker, client);
                MessageProcessor messageProcessor = new MessageProcessor(messageHandler, client, node);
                KeepAliveTimer keepAliveTimer = new KeepAliveTimer(messageProcessor, systemTracker);

                messageHandler.keepAliveTimer = keepAliveTimer;

                node.MessageProcessor = messageProcessor;

                /************ send solve request *****************/
                client.Connect();

                messageProcessor.Communicate(solveRequestMessage);
                COMP_TIME = DateTime.Now;

                while (doWork)
                {
                    Thread.Sleep(1000);
                }

                /*Object mutex = new Object();

                lock (mutex)
                {
                    Monitor.Wait(mutex);
                }*/
            }
        }
Example #6
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");
        }
 /******************************************************************/
 /************************** CONSTRUCTORS **************************/
 /******************************************************************/
 public ClientMessageHandler(ClientSystemTracker systemTracker, NetworkClient client)
 {
     this.systemTracker = systemTracker;
     this.client = client;
 }
        /******************************************************************/
        /******************* PROPERTIES, PRIVATE FIELDS *******************/
        /******************************************************************/


        /******************************************************************/
        /************************** CONSTRUCTORS **************************/
        /******************************************************************/
        public MessageHandler(SystemTracker systemTracker, NetworkClient client) : base(systemTracker, client)
        {
        }
 public ConsoleManager(NetworkClient client, NetworkNode node)
 { 
     this.client = client;
     this.node = node;
 }
 public ConsoleManager(NetworkClient client)
 {
     this.client = client;
 }
        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();
        }