private void Copy_Click(object sender, EventArgs e) { // Create a list of node IDs List <String> IDs = new List <String>(); foreach (var node in Nodes) { IDs.Add(node.Key); } // Open prompt for selecting the node nodeQuery = new ChooseNode(IDs); nodeQuery.ShowDialog(this); // Don't continue if no input was given if ((CopyNode = nodeQuery.CopyNode) == null) { return; } File_Difference(CopyNode); if (diffCount == 0) { Log.Items.Add("There is nothing to copy."); } progressBar1.Step = 1; progressBar1.Maximum = diffCount; progressBar1.Value = 0; diffCount = 0; // Copy all files on a separate thread both ways Task.Factory.StartNew(() => { for (int i = 0; i < Nodes[CopyNode].Count; i++) { // Path to first node NodeClass node1 = Nodes[CopyNode].ElementAt(i); for (int j = i + 1; j < Nodes[CopyNode].Count; j++) { // Path to second node NodeClass node2 = Nodes[CopyNode].ElementAt(j); if (node1.CanSend && node2.CanReceive) { Copy_All(node1.Path, node2.Path); } if (node2.CanSend && node1.CanReceive) { Copy_All(node1.Path, node2.Path); } } } }); nodeQuery = null; }
private void File_Difference(String nodeID) { int count = Nodes[nodeID].Count(); for (int i = 0; i < count; i++) { NodeClass node1 = Nodes[nodeID].ElementAt(i); for (int j = i + 1; j < count; j++) { NodeClass node2 = Nodes[nodeID].ElementAt(j); if (node1.CanSend && node2.CanReceive) { Count(node1.Path, node2.Path); } if (node1.CanReceive && node2.CanSend) { Count(node2.Path, node1.Path); } void Count(String from, String to) { String[] files = Directory.GetFiles(from); String[] directories = Directory.GetDirectories(from); foreach (var file in files) { String fileName = Path.GetFileName(file); String destFile = Path.Combine(to, fileName); if (String.Compare(fileName, nodeID + ".node") == 0) { continue; } if (!File.Exists(destFile)) { diffCount++; } } foreach (var dir in directories) { String dirName = Path.GetFileName(dir); String destDir = Path.Combine(to, dirName); Count(dir, destDir); } } } } }
private void LoadData() { try { // If the file doesn't exist create it if (!File.Exists("Node_data.data")) { Log.Items.Add("Creating data file"); File.Create("Node_data.data").Close(); return; } // Open file and read it StreamReader sr = new StreamReader("Node_data.data"); while (!sr.EndOfStream) { // Read the node name String str = sr.ReadLine(); // Skip potential empty lines on the start if (String.IsNullOrEmpty(str)) { continue; } String[] info = str.Split(' '); String nodeName = info[0]; String net = info[1]; bool network; if (net.Equals("Network=1")) { network = true; } else { network = false; } List <NodeClass> NodePaths = new List <NodeClass>(); String path; // Read node paths and store them in the list while (!String.IsNullOrEmpty((path = sr.ReadLine()))) { NodeClass node = new NodeClass(); node.Path = path; node.NodeID = nodeName; node.IsNetwork = network; StreamReader srNode = new StreamReader(path + "\\" + nodeName + ".node"); String line = srNode.ReadLine(); if (String.IsNullOrEmpty(line)) { Log.Items.Add("Node at: " + path + " could not be read and it will be skipped"); srNode.Close(); continue; } if (line.Equals(GetHash("canReceive=1"))) { node.CanReceive = true; } else if (line.Equals(GetHash("canReceive=0"))) { node.CanReceive = false; } else { Log.Items.Add("Node at: " + path + " could not be read and it will be skipped"); srNode.Close(); continue; } line = srNode.ReadLine(); if (String.IsNullOrEmpty(line)) { Log.Items.Add("Node at: " + path + " could not be read and it will be skipped"); srNode.Close(); continue; } if (line.Equals(GetHash("canSend=1"))) { node.CanSend = true; } else if (line.Equals(GetHash("canReceive=0"))) { node.CanSend = false; } else { Log.Items.Add("Node at: " + path + " could not be read and it will be skipped"); srNode.Close(); continue; } NodePaths.Add(node); } // Add node with paths to dictionary Nodes.Add(nodeName, NodePaths); } sr.Close(); } // Catch any exceptions catch (Exception e) { Log.Items.Add(e); } }