/******************************************************************/ /************************** CONSTRUCTORS **************************/ /******************************************************************/ public MessageHandler(SystemTracker systemTracker, ClientTracker clientTracker , TaskTracker taskTracker, NetworkServer server) { this.systemTracker = systemTracker; this.clientTracker = clientTracker; this.taskTracker = taskTracker; this.server = server; }
/******************************************************************/ /************************** 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); }
/*******************************************************************/ /************************ PRIVATE METHODS **************************/ /*******************************************************************/ /*******************************************************************/ /************************* PUBLIC METHODS **************************/ /*******************************************************************/ public void InitiatePrimary(IPAddress address, int port) { SmartConsole.PrintHeader("Starting primary server"); SmartConsole.PrintLine("Address: " + address.ToString() + ":" + port, SmartConsole.DebugLevel.Advanced); // Create overall system tracker SystemTracker systemTracker = new SystemTracker(); // Create list of all clients ClientTracker clientTracker = new ClientTracker(); // Start measuring timeout clientTracker.StartTimeout(); // Task Tracker TaskTracker taskTracker = new TaskTracker(); // Start network connection NetworkServer server = new NetworkServer(address, port); server.Open(); // Create messageHandler MessageHandler messageHandler = new MessageHandler(systemTracker, clientTracker, taskTracker, server); // Start message queue MessageQueue messageQueue = new MessageQueue(server); messageQueue.Start(); // Start Message processor CommunicationServer.MessageCommunication.MessageProcessor messageProcessor = new CommunicationServer.MessageCommunication.MessageProcessor(messageQueue, messageHandler); messageProcessor.Start(); Thread.Sleep(100); // Start console manager ConsoleManager consoleManager = new ConsoleManager(server); consoleManager.Start(); }
/******************************************************************/ /************************** CONSTRUCTORS **************************/ /******************************************************************/ public ConsoleManager(NetworkServer server) { this.server = server; }
/********************************************************************/ /*************************** CONSTRUCTORS ***************************/ /********************************************************************/ /// <summary> /// /// </summary> /// <param name="server"> /// The server which will listen to the network /// </param> public MessageQueue(NetworkServer server) { this.server = server; Active = false; }
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(); }