public void Parse_XMLString_NoOperationMessage() { /*********** Actual message ***********/ string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"xml_samples\NoOperation.xml"); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); string xmlStr = xmlDoc.OuterXml; string name = Message.GetMessageName(xmlStr); NoOperationMessage actualMessage = null; if (name == NoOperationMessage.ELEMENT_NAME) { actualMessage = NoOperationMessage.Construct(xmlStr); } /*********** Expected message ***********/ BackupCommunicationServer backupServer1 = new BackupCommunicationServer("192.168.1.0", 80); BackupCommunicationServer backupServer2 = new BackupCommunicationServer("192.168.1.0", 80); BackupCommunicationServer[] backupServers = { backupServer1, backupServer2 }; NoOperationMessage expectedMessage = new NoOperationMessage(backupServers); Assert.AreEqual(expectedMessage, actualMessage); }
public void Parse_NoOperationMessage_XMLString() { string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"xml_samples\NoOperation.xml"); BackupCommunicationServer backupServer1 = new BackupCommunicationServer("192.168.1.0", 80); BackupCommunicationServer backupServer2 = new BackupCommunicationServer("192.168.1.0", 80); BackupCommunicationServer[] backupServers = { backupServer1, backupServer2 }; NoOperationMessage noOperation = new NoOperationMessage(backupServers); string actualXmlStr = noOperation.ToXmlString(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); string expectedXmlStr = xmlDoc.OuterXml; Assert.AreEqual(expectedXmlStr, actualXmlStr); }
/// <summary> /// /// </summary> /// <param name="id"> /// The ID assigned by the Communication Server /// </param> /// <param name="timeout"> /// The communication timeout configured on Communication Server /// </param> /// <param name="backupServer"> /// Backup server /// </param> public RegisterResponseMessage(ulong id, uint timeout, BackupCommunicationServer backupServer) { Id = id; Timeout = timeout; BackupCommunicationServer[] backupServers = { backupServer }; BackupCommunicationServers = backupServers; }
public NoOperationMessage(BackupCommunicationServer backupServer) { BackupCommunicationServer[] backupServers = { backupServer }; BackupCommunicationServers = backupServers; }
/*******************************************************************/ /************************* PUBLIC METHODS **************************/ /*******************************************************************/ public void AddBackupServer(BackupCommunicationServer backupServer) { int newSize = backupServersArray.Length + 1; BackupCommunicationServer[] newBackupServers = new BackupCommunicationServer[newSize]; for (int i = 0; i < newSize - 1; i++) { newBackupServers[i] = backupServersArray[i]; } newBackupServers[newSize - 1] = backupServer; backupServersArray = newBackupServers; }
public BackupCommunicationServer[] ToBackupServersArray() { int backupSize = backupServersList.Count; BackupCommunicationServer[] backupServersArr = new BackupCommunicationServer[backupSize]; for (int i = 0; i < backupSize; i++) { NetworkNode server = backupServersList[i]; BackupCommunicationServer backupServer = new BackupCommunicationServer(server.Address.ToString(), server.Port); backupServersArr[i] = backupServer; } return(backupServersArr); }
public static NoOperationMessage CreateNoOperationMessage() { BackupCommunicationServer backupServer1 = new BackupCommunicationServer("192.168.1.0", 80); BackupCommunicationServer backupServer2 = new BackupCommunicationServer("192.168.1.0", 80); BackupCommunicationServer[] backupServers = { backupServer1, backupServer2 }; NoOperationMessage expectedMessage = new NoOperationMessage(backupServers); return(expectedMessage); }
private void InformBackup(Message message) { s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); int length = clientTracker.BackupServers.Length; // If this is primary server and it has atleast one backup if (Server.primaryMode && length > 0) { BackupCommunicationServer bserver = clientTracker.BackupServers[0]; IPAddress address = IPAddress.Parse(bserver.address); int port = bserver.port; s.Connect(address, port); server.Send(s, message); SmartConsole.PrintLine("Informed Backup Server: " + address.ToString() + ":" + port, SmartConsole.DebugLevel.Advanced); } // If this is one of the backups else { // Find yourself, and inform next backup for (int i = 0; i < length; i++) { BackupCommunicationServer bserver = clientTracker.BackupServers[i]; if (server.Address.ToString().Equals(bserver.address) && server.Port == bserver.port) { if (i + 1 < length) { BackupCommunicationServer bserverToSend = clientTracker.BackupServers[i + 1]; IPAddress address = IPAddress.Parse(bserverToSend.address); int port = bserverToSend.port; s.Connect(address, port); server.Send(s, message); SmartConsole.PrintLine("Informed Backup Server: " + address.ToString() + ":" + port, SmartConsole.DebugLevel.Advanced); } } } } }
/// <summary> /// Adds a network node to the list of clients /// </summary> /// <param name="node"></param> public void AddNode(NetworkNode node) { switch (node.Type) { case RegisterType.CommunicationServer: backupServersList.Add(node); BackupCommunicationServer bserver = new BackupCommunicationServer(node.Address.ToString(), node.Port); AddBackupServer(bserver); break; case RegisterType.ComputationalNode: compNodes.Add(node); break; case RegisterType.TaskManager: taskManagers.Add(node); break; } }
public void RemoveBackupServer(int index) { int newSize = backupServersArray.Length - 1; BackupCommunicationServer[] newBackupServers = new BackupCommunicationServer[newSize]; int j = 0; for (int i = 0; i < newSize + 1; i++) { if (i != index) { newBackupServers[j] = backupServersArray[i]; j++; } } backupServersArray = newBackupServers; }
/*******************************************************************/ /************************ PRIVATE METHODS **************************/ /*******************************************************************/ private void keepAlive(Object source, ElapsedEventArgs e) { SmartConsole.PrintLine("Sending Status message", SmartConsole.DebugLevel.Basic); try { Communicate(node.ToStatusMessage()); } catch (SocketException excep) { if (clientTracker.BackupServers.Length == 0) { SmartConsole.PrintLine("No other backup server avaiable", SmartConsole.DebugLevel.Advanced); return; } BackupCommunicationServer bserver = clientTracker.BackupServers[0]; // If it is next backup server, switch to primary and remove it from the list if (server.Address.ToString().Equals(bserver.address) && server.Port == bserver.port) { // TODO Switch only if it is next back up in the list, and decreament the list it self SmartConsole.PrintLine("Switching to primary...", SmartConsole.DebugLevel.Advanced); // stop the timer this.Stop(); clientTracker.RemoveBackupServer(0); lock (backupBlockade) { Monitor.Pulse(backupBlockade); } return; } // connect to next backup server. else { client.Address = IPAddress.Parse(bserver.address); client.Port = bserver.port; } } }
/*******************************************************************/ /************************ PRIVATE METHODS **************************/ /*******************************************************************/ private void keepAliveSolution(Object source, ElapsedEventArgs e) { SmartConsole.PrintLine("Sending SolutionRequestMessage", SmartConsole.DebugLevel.Basic); try { messageProcessor.Communicate(solutionRequestMessage); } catch (SocketException excep) { SmartConsole.PrintLine("Lost connection with primary server, reconnecting to next backup...", SmartConsole.DebugLevel.Advanced); if (systemTracker.Node.BackupServers.Length == 0) { SmartConsole.PrintLine("No other backup server avaiable", SmartConsole.DebugLevel.Advanced); return; } BackupCommunicationServer bserver = systemTracker.Node.BackupServers[0]; // connect to next backup server. messageProcessor.client.Address = IPAddress.Parse(bserver.address); messageProcessor.client.Port = bserver.port; } }