private void handleWork(NetworkRequest request) { Logger.Debug("ClientThread:handleWork"); if (this.guid.Equals(request.SourceGuid)) { //if sourceguid=this.guid: make a network request and go from there if (request is PushRequest) { processMyPushRequest((PushRequest)request); } else if (request is PullRequest) { processMyPullRequest((PullRequest)request); } else if (request is QueryRequest) { processMyQueryRequest((QueryRequest)request); } else if (request is PushIndexRequest) { processMyPushIndexRequest((PushIndexRequest)request); } else { //what kind of request is this? Logger.Warn("ClientThread:handleWork my request is not a known request instance"); } return; } else {//if sourceguid!=this.guid: receive or send the file requested if (request is PushRequest) { PushRequest pr = (PushRequest)request; sendMessage(tcpClient, new NetworkResponse(ResponseType.Yes, "", guid, request.SequenceNumber)); //readFileToDisk(pr.FileSize, Chunk.PathToChunk(pr)); string remoteGuid = request.SourceGuid.ToString(); //string path = Path.Combine(Node.GetBackupDirectory(), remoteGuid, remoteGuid+'_'+pr.ChunkNumber+.tar readFileToDisk(pr.FileSize, Chunk.PathToChunk(request.SourceGuid, 0, pr.ChunkNumber)); } else if (request is PullRequest) { PullRequest pr = (PullRequest)request; sendMessage(tcpClient, new NetworkResponse(ResponseType.Yes, "", guid, request.SequenceNumber)); writeFileToNetwork(Chunk.PathToChunk(pr)); } else if (request is QueryRequest) { QueryRequest qr = (QueryRequest)request; processRemoteQueryRequest(qr); } else if (request is PushIndexRequest) { processRemotePushIndexRequest((PushIndexRequest)request); } else { //what kind of request is this? Logger.Warn("ClientThread:handleWork remote request is not a known request instance"); } } }
private void processMyPushRequest(PushRequest request) { Logger.Debug("ClientThread:processMyPushRequest"); int ret = sendMessage(tcpClient, request); NetworkResponse response = (NetworkResponse)receiveMessage(tcpClient); if (response.Type == Backend.ResponseType.Yes) { ret = writeFileToNetwork(((PushRequest)request).Path); //todo: add result to event queue } else { //other node said willnot, cannot, or notimplemented //add result to event queue lock (_lock) { eventQueue.Enqueue(response); } } }
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(); }
public void AcceptFileTransfer(PushRequest request, string path) { Logger.Debug("ClientThread:AcceptFileTransfer:PushRequest"); acceptFileTransfer(request, path); }
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); }
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 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); } } }