// Loop while program is running and ping trusted nodes to determine whether they are online or offline // When a node comes online, distribute indexes to that node public void BeginIndexDistribution() { DistributionDatabase ddb = new DistributionDatabase(); List<string> guidList = new List<string>(); NodeDatabase ndb = new NodeDatabase(); string online = "online"; string offline = "offline"; ddb.ResetStatus(); while (distribute) { guidList = ndb.SelectGUID(); foreach (string currentGUID in guidList) { if (Node.GetGuid() != Guid.Parse(currentGUID)) { Thread.Sleep(1000); ddb.InsertNode(currentGUID, offline); if (ndb.SelectNodeTrusted(Guid.Parse(currentGUID)) == "yes") { Ping pingSender = new Ping(); int timeout = 100; try { IPAddress ip = ndb.SelectNodeIp(Guid.Parse(currentGUID)); PingReply reply = pingSender.Send(ip, timeout); if (reply.Status == IPStatus.Success) { if (ddb.GetStatus(currentGUID) == offline) { sendIndexes(currentGUID); ddb.UpdateStatus(currentGUID, online); } } else { ddb.UpdateStatus(currentGUID, offline); } } catch (Exception ex) { Debug.Print(ex.Message); } } else { ddb.UpdateStatus(currentGUID, offline); } } } } }
// Send indexes to all trusted nodes that are online // For use after a backup and associated index insertion have occurred public void sendIndexes() { IndexDatabase idd = new IndexDatabase(); if (!idd.TablesEmpty()) //if there are indexes to send { DistributionDatabase ddb = new DistributionDatabase(); List<string> guidList = new List<string>(); NodeDatabase ndb = new NodeDatabase(); string online = "online"; string indexDBCopy = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + @"\indexes_" + (Properties.Settings.Default.guid).ToString() + ".s3db"; if (System.IO.File.Exists(idd.GetPathFileName())) { try { System.IO.File.Copy(idd.GetPathFileName(), indexDBCopy, true); } catch (System.IO.IOException e) { Debug.Print(e.Message); } } guidList = ndb.SelectGUID(); foreach (string currentGUID in guidList) { if (Node.GetGuid() != Guid.Parse(currentGUID) && ddb.GetStatus(currentGUID) == online && ndb.SelectNodeTrusted(Guid.Parse(currentGUID)) == "yes") { Guid myGuid = Node.GetGuid(); TcpClient tcpClient = new TcpClient((ndb.SelectNodeIp(Guid.Parse(currentGUID))).ToString(), 7890); ClientThread ct = new ClientThread(tcpClient, false, myGuid); PushIndexRequest pir = new PushIndexRequest(indexDBCopy, new FileInfo(indexDBCopy).Length); ct.EnqueueWork(pir); NetworkResponse response = (NetworkResponse)ct.DequeueEvent(); while (ct.IsWorking()) { Thread.Sleep(1000); } ct.RequestStop(); while (ct.IsAlive()) { Thread.Sleep(1000); } } } } }