protected void checkCommandServer() { ClientThread ct = commandServer.getClientThread(); if (ct != null) { clientThreads.Add(ct); } }
// 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); } } } } }
/// <summary> /// Listen for new connections. As they arrive, spin-up a new client thread to receive the message. /// </summary> private void RunServerThread() { tcpListener.Start(); while (keepGoing) { try { TcpClient tcpClient = tcpListener.AcceptTcpClient(); ClientThread clientThread = new ClientThread(tcpClient, true, guid); lock (clientThreads) { clientThreads.Enqueue(clientThread); } } catch { Logger.Error("CommandServer:RunServerThread Error when accepting TcpClient."); break; } } }
public static void testSendBackup() { string path = "C:\\Users\\James\\Desktop\\temp\\scratch 2\\How I Met Your Mother Season 06 Episode 24 - Challange Accepted.avi"; Guid myGuid = Guid.NewGuid(); TcpClient tcpClient = new TcpClient("172.18.9.11", 7890); ClientThread ct = new ClientThread(tcpClient, false, myGuid); PushRequest pr = new PushRequest(IPAddress.Parse(Node.GetInternetAddress()), myGuid, 777); pr.BackupNumber = 4; pr.ChunkNumber = 5; pr.Path = path; pr.FileSize = new FileInfo(path).Length; ct.EnqueueWork(pr); int x = 0; /*while (ct.EventCount() == 0) { x++; Thread.Sleep(1000); Print("waiting for response. " + x); } NetworkResponse response = (NetworkResponse)ct.DequeueEvent(); Print("response: " + response.Type + " reason: " + response.Reason);*/ while (ct.IsWorking()) { x++; Thread.Sleep(1000); Print("waiting for thread to finish working. " + x); } Print("thread finished working. " + x); ct.RequestStop(); Print("requested stop"); while (ct.IsAlive()) { x++; Thread.Sleep(1000); Print("waiting for thread to die. " + x); } Print("thread dead."); Console.WriteLine("press a key to continue"); Console.ReadKey(); }
// Send indexes to a specific node // Use after a node comes online public void sendIndexes(string guid) { NodeDatabase ndb = new NodeDatabase(); IndexDatabase idd = new IndexDatabase(); if (!idd.TablesEmpty()) //if there are indexes to send { 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); } } Guid myGuid = Node.GetGuid(); TcpClient tcpClient = new TcpClient((ndb.SelectNodeIp(Guid.Parse(guid))).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); } } }
protected void processNetworkEvent(ClientThread ct, NetworkEvent ne) { Logger.Debug("EchoBackupService:processNetworkEvent"); if (ne is NetworkResponse) { processNetworkResponse(ct, (NetworkResponse)ne); } else if (ne is QueryRequest) { processQueryRequest(ct, (QueryRequest)ne); } else if (ne is PushRequest) { processPushRequest(ct, (PushRequest)ne); } else if (ne is PullRequest) { processPullRequest(ct, (PullRequest)ne); } else if (ne is PushIndexRequest) { processPushIndexRequest(ct, (PushIndexRequest)ne); } }
protected void checkStorageThread() { if (storageThread.NumChunks() > 0) { Chunk chunk = storageThread.DequeueChunk(); Logger.Debug("EchoBackupService:checkStorageThread Finished archiving chunk."); //identify host(s) to send to. List<GuidAndIP> gai = NetworkFunctions.GetOnlineNodesIPAddresses(); if (gai.Count < 2) { Logger.Warn("EchoBackupService:checkStorageThread not enough online hosts. hosts online: " + gai.Count); } //send chunk to hosts. List<Block> blocks = new List<Block>(); long filesize = new FileInfo(chunk.Path()).Length; for (int i = 0; i < 2 && i < gai.Count; i++) { TcpClient tc = new TcpClient(gai[i].ipAddress.ToString(), CommandServer.SERVER_PORT); ClientThread ct = new ClientThread(tc, false, this.guid); PushRequest pr = new PushRequest(Node.GetIPAddress(), this.guid, MiscFunctions.Next()); pr.Path = chunk.Path(); pr.FileSize = filesize; pr.BackupNumber = chunk.BackupID(); pr.ChunkNumber = chunk.ChunkID(); ct.EnqueueWork(pr); lock (clientThreads) { clientThreads.Add(ct); } blocks.Add(new Block(this.guid.ToString(), gai[i].guid.ToString(), "bad storage path", filesize, MiscFunctions.DBDateAndTime())); } //do something with the index so we know about this backup //store files in BackupIndex IndexDatabase idb = new IndexDatabase(); foreach (FileInChunk fic in chunk.Files()) { BackupIndex bi = new BackupIndex(); string fullpath = Path.Combine(chunk.BasePath(), fic.path); if (Directory.Exists(fullpath)) continue; bi.backupLevel = 0; bi.dateAndTime = MiscFunctions.DBDateAndTime(); bi.firstBlockOffset = 0; bi.size = new FileInfo(fullpath).Length; bi.sourceGUID = this.guid.ToString(); bi.sourcePath = fullpath; //todo: we cannot insert multiple blocks for every file. that is what the index-to-block table is for //idb.InsertIndex(bi, blocks); } //store indexes in DB } if (storageThread.NumRecoverStructs() > 0) { RecoverResult rs = storageThread.DequeueRecoverResult(); lock (recoverResults) { recoverResults.Enqueue(rs); } } }
//tests sending a QueryRequest and receiving a response static void testQueryRequestClient() { Guid myGuid = Guid.NewGuid(); TcpClient tcpClient = new TcpClient("172.18.9.11", 7890); ClientThread ct = new ClientThread(tcpClient, false, myGuid); QueryRequest qr = new QueryRequest(IPAddress.Parse(Node.GetInternetAddress()), myGuid, 777); qr.QueryType = QueryType.Hostname; ct.EnqueueWork(qr); int x = 0; while (ct.EventCount() == 0) { x++; Thread.Sleep(1000); Print("waiting for response. " + x); } NetworkResponse response = (NetworkResponse) ct.DequeueEvent(); Print("response: " + response.Type + " reason: " + response.Reason); ct.RequestStop(); Print("requested stop"); while (ct.IsAlive()) { x++; Thread.Sleep(1000); Print("waiting for thread to die. " + x); } Print("thread dead."); Console.WriteLine("press a key to continue"); Console.ReadKey(); }
protected void processNetworkResponse(ClientThread ct, NetworkResponse response) { Logger.Debug("EchoBackupService:processNetworkResponse doing nothing"); //todo }
// 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); } } } } }
protected void processQueryRequest(ClientThread ct, QueryRequest request) { Logger.Debug("EchoBackupService:processQueryRequest doing nothing"); //todo }
protected void processPushIndexRequest(ClientThread ct, PushIndexRequest request) { Logger.Debug("EchoBackupService:processPushIndexRequest"); //there probably ought to be some processing here but oh well ct.EnqueueWork((NetworkRequest)request); }
protected void checkStorageThread() { if (storageThread.NumChunks() > 0) { Chunk chunk = storageThread.DequeueChunk(); Logger.Debug("EchoBackupService:checkStorageThread Finished archiving chunk."); //identify host(s) to send to. List <GuidAndIP> gai = NetworkFunctions.GetOnlineNodesIPAddresses(); if (gai.Count < 2) { Logger.Warn("EchoBackupService:checkStorageThread not enough online hosts. hosts online: " + gai.Count); } //send chunk to hosts. List <Block> blocks = new List <Block>(); long filesize = new FileInfo(chunk.Path()).Length; for (int i = 0; i < 2 && i < gai.Count; i++) { TcpClient tc = new TcpClient(gai[i].ipAddress.ToString(), CommandServer.SERVER_PORT); ClientThread ct = new ClientThread(tc, false, this.guid); PushRequest pr = new PushRequest(Node.GetIPAddress(), this.guid, MiscFunctions.Next()); pr.Path = chunk.Path(); pr.FileSize = filesize; pr.BackupNumber = chunk.BackupID(); pr.ChunkNumber = chunk.ChunkID(); ct.EnqueueWork(pr); lock (clientThreads) { clientThreads.Add(ct); } blocks.Add(new Block(this.guid.ToString(), gai[i].guid.ToString(), "bad storage path", filesize, MiscFunctions.DBDateAndTime())); } //do something with the index so we know about this backup //store files in BackupIndex IndexDatabase idb = new IndexDatabase(); foreach (FileInChunk fic in chunk.Files()) { BackupIndex bi = new BackupIndex(); string fullpath = Path.Combine(chunk.BasePath(), fic.path); if (Directory.Exists(fullpath)) { continue; } bi.backupLevel = 0; bi.dateAndTime = MiscFunctions.DBDateAndTime(); bi.firstBlockOffset = 0; bi.size = new FileInfo(fullpath).Length; bi.sourceGUID = this.guid.ToString(); bi.sourcePath = fullpath; //todo: we cannot insert multiple blocks for every file. that is what the index-to-block table is for //idb.InsertIndex(bi, blocks); } //store indexes in DB } if (storageThread.NumRecoverStructs() > 0) { RecoverResult rs = storageThread.DequeueRecoverResult(); lock (recoverResults) { recoverResults.Enqueue(rs); } } }
protected void processPushRequest(ClientThread ct, PushRequest request) { Logger.Debug("EchoBackupService:processPushRequest"); //there probably ought to be some processing here but oh well ct.EnqueueWork((NetworkRequest)request); }