public ServerClientMsg ServerCreateRly(ServerClientMsg ReceivedMsgObject) { var MsgObjectToReturn = new ServerClientMsg(); switch (ReceivedMsgObject.Command) { // If we received a Register Files message from the client case (int)ServerClientMsg.Commands.RegisterRq: List <bool> SuccessList = new List <bool>(); // Looping through all the messages and add it to the dictionary if possible for (int i = 0; i < ReceivedMsgObject.Files.Count; i++) { if (!ServerDict.ContainsKey(ReceivedMsgObject.Files[i])) { ServerDataObject Sdo = new ServerDataObject(); // Store each success and failure in a list for a message back to the client SuccessList.Add(Sdo.AddEndPoint(ReceivedMsgObject.ClientIP, ReceivedMsgObject.ClientPort)); Sdo.Length = ReceivedMsgObject.FilesLength[i]; ServerDict.Add(ReceivedMsgObject.Files[i], Sdo); Console.WriteLine("Server: Added file: {0} to the server dict for client: {1} , {2}", ReceivedMsgObject.Files[i], ReceivedMsgObject.ClientIP, ReceivedMsgObject.ClientPort); } else { bool successflag = true; foreach (var pair in ServerDict) { if (pair.Key == ReceivedMsgObject.Files[i]) { ServerDataObject ServObj = pair.Value; for (int k = 0; k < ServObj.Addresses.Count; k++) { if (ServObj.Ports[k] == ReceivedMsgObject.ClientPort) { if (ServObj.Addresses[k] == ReceivedMsgObject.ClientIP) { Console.WriteLine("Server: Reject file {0} reg for client: {1} , {2}", ReceivedMsgObject.Files[i], ReceivedMsgObject.ClientIP, ReceivedMsgObject.ClientPort); successflag = false; } } } if (successflag == true) { Console.WriteLine("Server: Added client {0} , {1} to server dict", ReceivedMsgObject.ClientIP, ReceivedMsgObject.ClientPort, ReceivedMsgObject.Files[i]); ServObj.AddEndPoint(ReceivedMsgObject.ClientIP, ReceivedMsgObject.ClientPort); } break; } } SuccessList.Add(successflag); } } // Compile the return message for the client MsgObjectToReturn.RegisterRly(SuccessList); break; // If we received a file list request from the client case (int)ServerClientMsg.Commands.FileListRq: // For a fileName list and a file size list List <string> fileName = new List <string>(); List <long> fileSize = new List <long>(); foreach (var pair in ServerDict) { bool HasFileAlready = false; ServerDataObject ServObj = pair.Value; for (int k = 0; k < ServObj.Addresses.Count; k++) { if (ServObj.Ports[k] == ReceivedMsgObject.ClientPort) { if (ServObj.Addresses[k] == ReceivedMsgObject.ClientIP) { Console.WriteLine("Server: client {0} , {1} already has the file {2}", ReceivedMsgObject.ClientIP, ReceivedMsgObject.ClientPort, pair.Key); HasFileAlready = true; break; } } } if (HasFileAlready == false) { fileName.Add(pair.Key); ServerDataObject obj = pair.Value; fileSize.Add(obj.Length); } } MsgObjectToReturn.FileListRly(fileName, fileSize); break; // If we received a file location request from the client case (int)ServerClientMsg.Commands.FileLocRq: // For a fileName list and a file size list List <string> addresses = new List <string>(); List <int> ports = new List <int>(); long length = 0; foreach (var pair in ServerDict) { if (pair.Key == ReceivedMsgObject.NameOfFile) { ServerDataObject SObj = pair.Value; addresses = SObj.Addresses; ports = SObj.Ports; length = SObj.Length; } } MsgObjectToReturn.FileLocRly(length, addresses, ports); break; // On leave request, the server need to remove all files that has the client's ip address. // if the client is the last address that is associated with that file, then the server needs to remove the file case (int)ServerClientMsg.Commands.LeaveRq: List <string> FilestoRemove = new List <string>(); foreach (var pair in ServerDict) { List <int> indexToRemove = new List <int>(); ServerDataObject objOfFile = pair.Value; for (int i = 0; i < objOfFile.Addresses.Count; i++) { if (objOfFile.Addresses[i] == ReceivedMsgObject.ClientIP) { if (objOfFile.Ports[i] == ReceivedMsgObject.ClientPort) { indexToRemove.Add(i); } } } // This allows the removal process to be skipped if the client doesn't have this file if (indexToRemove.Any()) { foreach (var index in indexToRemove) { objOfFile.Addresses.RemoveAt(index); objOfFile.Ports.RemoveAt(index); } if (!objOfFile.Addresses.Any()) { FilestoRemove.Add(pair.Key); } } } // Now the server has to remove all the files that doesn't have any clients on it if (FilestoRemove.Any()) { foreach (var key in FilestoRemove) { ServerDict.Remove(key); } } MsgObjectToReturn.LeaveRly(); break; case (int)ServerClientMsg.Commands.DataRly: Console.WriteLine("Requesting Data from server instead of client"); break; default: break; } return(MsgObjectToReturn); }
public ServerClientMsg ServerCreateRly(ServerClientMsg ReceivedMsgObject) { var MsgObjectToReturn = new ServerClientMsg(); switch (ReceivedMsgObject.Command) { // If we received a Register Files message from the client case (int)ServerClientMsg.Commands.RegisterRq: List<bool> SuccessList = new List<bool>(); // Looping through all the messages and add it to the dictionary if possible for (int i = 0; i < ReceivedMsgObject.Files.Count; i++) { if (!ServerDict.ContainsKey(ReceivedMsgObject.Files[i])) { ServerDataObject Sdo = new ServerDataObject(); // Store each success and failure in a list for a message back to the client SuccessList.Add(Sdo.AddEndPoint(ReceivedMsgObject.ClientIP, ReceivedMsgObject.ClientPort)); Sdo.Length = ReceivedMsgObject.FilesLength[i]; ServerDict.Add(ReceivedMsgObject.Files[i], Sdo); } else { bool successflag = false; foreach (var pair in ServerDict) { if (pair.Key == ReceivedMsgObject.Files[i]) { ServerDataObject ServObj = pair.Value; for (int k =0; k < ServObj.Addresses.Count; k++) { if (ServObj.Ports[k] == ReceivedMsgObject.ClientPort) { if (ServObj.Addresses[k] == ReceivedMsgObject.ClientIP) successflag = false; } else { ServObj.AddEndPoint(ReceivedMsgObject.ClientIP, ReceivedMsgObject.ClientPort); successflag = true; } } } } SuccessList.Add(successflag); } } // Compile the return message for the client MsgObjectToReturn.RegisterRly(SuccessList); break; // If we received a file list request from the client case (int)ServerClientMsg.Commands.FileListRq: // For a fileName list and a file size list List<string> fileName = new List<string>(); List<int> fileSize = new List<int>(); foreach (var pair in ServerDict) { fileName.Add(pair.Key); ServerDataObject obj = pair.Value; fileSize.Add(obj.Length); } MsgObjectToReturn.FileListRly(fileName, fileSize); break; // If we received a file location request from the client case (int)ServerClientMsg.Commands.FileLocRq: // For a fileName list and a file size list List<string> addresses = new List<string>(); List<int> ports = new List<int>(); int length = 0; foreach (var pair in ServerDict) { if (pair.Key == ReceivedMsgObject.NameOfFile) { ServerDataObject SObj = pair.Value; addresses = SObj.Addresses; ports = SObj.Ports; length = SObj.Length; } } MsgObjectToReturn.FileLocRly(length, addresses, ports); break; // On leave request, the server need to remove all files that has the client's ip address. // if the client is the last address that is associated with that file, then the server needs to remove the file case (int)ServerClientMsg.Commands.LeaveRq: List<string> FilestoRemove = new List<string>(); foreach (var pair in ServerDict) { List<int> indexToRemove = new List<int>(); ServerDataObject objOfFile = pair.Value; for (int i = 0; i < objOfFile.Addresses.Count; i++) { if (objOfFile.Addresses[i] == ReceivedMsgObject.ClientIP) { if (objOfFile.Ports[i] == ReceivedMsgObject.ClientPort) indexToRemove.Add(i); } } // This allows the removal process to be skipped if the client doesn't have this file if (indexToRemove.Any()) { foreach (var index in indexToRemove) { objOfFile.Addresses.RemoveAt(index); objOfFile.Ports.RemoveAt(index); } if (!objOfFile.Addresses.Any()) { FilestoRemove.Add(pair.Key); } } } // Now the server has to remove all the files that doesn't have any clients on it if (FilestoRemove.Any()) { foreach (var key in FilestoRemove) { ServerDict.Remove(key); } } MsgObjectToReturn.LeaveRly(); break; case (int)ServerClientMsg.Commands.DataRly: Console.WriteLine("Requesting Data from server instead of client"); break; default: break; } return MsgObjectToReturn; }