static void Main(string[] args) { /************************************************ * * This block of code is used to instantiate the server * ************************************************/ var tcpServer = new TcpTorrent(); var serverState = new StateObject(); serverState.ClientType = false; tcpServer.StartListener(serverState).Wait(); }
static void Main(string[] args) { using (var sr = new StreamReader(@"../../Intro.txt")) { while (!sr.EndOfStream) Console.WriteLine(sr.ReadLine()); } Tuple<string, string> userCommand; /************************************************ * * This block of code is used to instantiate the server * ************************************************/ //var tcpServer = new TcpTorrent(); //var serverState = new StateObject(); //serverState.ClientType = false; //var serverTask = tcpServer.StartListener(serverState); //tcpServer.StartListener(serverState).Wait(); /************************************************ * * This block of code is used to instantiate the client server * ************************************************/ StateObject clientState = new StateObject(); var clientServer = new TcpTorrent(); clientState.Address = "127.0.0.1" ; //clientState.Address = clientServer.GetLocalIPAddress(); clientState.Port = clientServer.GetOpenPort(); Console.WriteLine("Creating Client's server on Address: {0} , and Port: {1}", clientState.Address, clientState.Port); var task = clientServer.StartListener(clientState); // Creating a temp location to store all the temporary files var random = new Random(); clientState.TempFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"/Temp" + random.Next(1, 10); if (!Directory.Exists(clientState.TempFolderPath)) { Console.WriteLine("Client: Creating directory at {0}", clientState.TempFolderPath); Directory.CreateDirectory(clientState.TempFolderPath); } else { clientState.TempFolderPath += random.Next(1, 10); Console.WriteLine("Client: Creating directory at {0}", clientState.TempFolderPath); Directory.CreateDirectory(clientState.TempFolderPath); } commandPrint(); while (true) { Thread.Sleep(200); userCommand = getCommand(); // Switching through all the user's input switch (userCommand.Item1) { case "LISTUPLOADABLES": // Clearing the previous file paths since we are repopulating it clientState.UploadableFilePath.Clear(); clientState.UploadableFileSize.Clear(); // Displaying all the files that are in the desktop folder string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); int i = 1; Console.WriteLine("\nThe following files are available for download"); foreach (var file in Directory.GetFiles(path)) { // Only present the file if it hasn't already been added if (!clientState.FileDict.ContainsKey(Path.GetFileName(file))) { FileInfo f = new FileInfo(file); Console.WriteLine("{0}) {1}", i, Path.GetFileName(file)); clientState.UploadableFilePath.Add(file); clientState.UploadableFileSize.Add(f.Length); } i++; } commandPrint(); break; case "UPLOAD": Tuple<bool, string> validCheck = IsValidInput(userCommand.Item2, clientState.UploadableFilePath.Count); // If any of the value in the previous loop was invalid, we need to break out of this case. if (validCheck.Item1 == false) { Console.WriteLine(validCheck.Item2); commandPrint(); break; } // Now what we know that the user's inputs are valid, we need to store the path of the files that the user wants to upload // Clearing the previous FilestoREg list because we have a new list. clientState.FilePathsToReg.Clear(); clientState.FilePathsToRegLength.Clear(); foreach (string item in userCommand.Item2.Split(',')) { clientState.FilePathsToReg.Add(clientState.UploadableFilePath[Convert.ToInt32(item) - 1]); clientState.FilePathsToRegLength.Add(clientState.UploadableFileSize[Convert.ToInt32(item) - 1]); } // creating a new tcp client with the command to register the files that the user had selected var uploadcmd = new ClientPassableObject(clientState); uploadcmd.RegisterFiles(); var uploadclient = new TcpTorrent(); var clienttask = uploadclient.ClientStart(uploadcmd); while (uploadcmd.DoneFlag == false) ; // Now that we recieved upload on which file can be uploaded, we need to split the files up into segments and store it in the temp folder for (int j = 0; j < uploadcmd.FilesRegSuccessCount.Count; j++) { if (uploadcmd.FilesRegSuccessCount[j] == true) { // Only do something if we don't already have the file in storage. if (!clientState.FileDict.ContainsKey(Path.GetFileName(clientState.FilePathsToReg[j]))) { var DataParser = new DataSegmentObject(); // splitting the files up and store it in the temp folder DataParser.SplitFile(clientState.FilePathsToReg[j], clientState.MaxChunkSize, clientState.TempFolderPath); var dictObject = new ObjectForFiledict(); dictObject.Hash = DataParser.GetHash(clientState.FilePathsToReg[j]); dictObject.NoOfSegments = DataParser.GetNoOfSegments(clientState.FilePathsToRegLength[j], clientState.MaxChunkSize); Console.WriteLine("Client: splitting file: {0} of length {1} to {2} segments", clientState.FilePathsToReg[j], clientState.FilePathsToRegLength[j], dictObject.NoOfSegments); clientState.FileDict.Add(Path.GetFileName(clientState.FilePathsToReg[j]), dictObject); } else Console.WriteLine("Client: The file already exist in the dictionary. Do not add"); } } commandPrint(); break; case "LISTDOWNLOADABLES": // Creating a new tcp client that will get all the downloadable files var printcmd = new ClientPassableObject(clientState); printcmd.PrintDownloadable(); var printListclient = new TcpTorrent(); var printTask = printListclient.ClientStart(printcmd); while (printcmd.DoneFlag == false) ; clientState.DownloadableFileName = printcmd.DownloadableFiles; clientState.DownloadableFileSize = printcmd.DownloadableFilesLength; commandPrint(); break; case "DOWNLOAD": Tuple<bool, string> downloadValidCheck = IsValidInput(userCommand.Item2, clientState.DownloadableFileName.Count); // If any of the value in the previous loop was invalid, we need to break out of this case. if (downloadValidCheck.Item1 == false) { Console.WriteLine(downloadValidCheck.Item2); commandPrint(); break; } // We are only going to allow one file to be download at a time if (userCommand.Item2.Split(',').Length > 1) { Console.WriteLine("/nYou can only select one download file at a time"); break; } // Clearing the previous FilestoREg list because we have a new list. clientState.FileNameToDownload = string.Empty; clientState.FileNameToDownloadLength = 0; clientState.FileNameToDownload = clientState.DownloadableFileName[Convert.ToInt32(userCommand.Item2) - 1]; clientState.FileNameToDownloadLength = clientState.DownloadableFileSize[Convert.ToInt32(userCommand.Item2) - 1]; // Creating a new TCP client that will download the file from the other client var downloadClient = new TcpTorrent(); var downloadTask = downloadClient.GetDownloadFile(clientState); Thread.Sleep(2000); break; case "LEAVE": // createing a new tcp client that will request to leave var leaveCmd = new ClientPassableObject(clientState); leaveCmd.LeaveRequest(); var leaveClient = new TcpTorrent(); //Console.WriteLine("Creating leave task"); var leaveTask = leaveClient.ClientStart(leaveCmd); while (leaveCmd.DoneFlag == false) ; if (Directory.Exists(clientState.TempFolderPath)) { Console.WriteLine("Client: deleting directory at {0}", clientState.TempFolderPath); Directory.Delete(clientState.TempFolderPath, true); } else Console.WriteLine("Client: Directory to delete doesn't exist"); Console.ReadKey(); Environment.Exit(0); break; case "HELP": // display to the user the available commands if he/she typed help using (var sr = new StreamReader(@"../../Intro.txt")) { while (!sr.EndOfStream) Console.WriteLine(sr.ReadLine()); } break; default: Console.WriteLine("\nSorry. That was an invalid input. Please try again. \nValid Inputs are 'Listuploadables','Upload-#','ListDownloadables','Download-#','Leave','Help'"); commandPrint(); break; } } }
public ClientPassableObject(StateObject state) { port = state.Port; address = state.Address; _state = state; }
static void Main(string[] args) { //using (var sr = new StreamReader(@"../../Intro.txt")) //{ // while(!sr.EndOfStream) // Console.WriteLine(sr.ReadLine()); //} //using (var sr = new StreamReader(@"../../Example.txt")) //{ // while (!sr.EndOfStream) // Console.WriteLine(sr.ReadLine()); //} //commandPrint(); Console.WriteLine("This is the server. Do not close."); //Tuple<string, string> userCommand; var tcpServer = new TcpTorrent(); var serverState = new StateObject(); serverState.ClientType = false; //var serverTask = tcpServer.StartListener(serverState); tcpServer.StartListener(serverState).Wait(); //StateObject clientState = new StateObject(); //clientState.Address = "127.0.0.1"; //var clientServer = new TcpTorrent(); //clientState.Port = clientServer.GetOpenPort(); //var task = clientServer.StartListener(clientState); //while (true) //{ // userCommand = getCommand(); // switch (userCommand.Item1) // { // case "LISTUPLOADABLES": // Clearing the previous file paths since we are repopulating it // clientState.UploadableFilePath.Clear(); // clientState.UploadableFileSize.Clear(); // string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // int i = 1; // Console.WriteLine("\nThe following files are available for download"); // foreach (var file in Directory.GetFiles(path)) // { // Console.WriteLine("{0}) {1}", i, Path.GetFileName(file)); // clientState.UploadableFilePath.Add(file); // clientState.UploadableFileSize.Add(file.Length); // i++; // } // commandPrint(); // break; // case "UPLOAD": // Tuple<bool, string> validCheck = IsValidInput(userCommand.Item2, clientState.UploadableFilePath.Count); // If any of the value in the previous loop was invalid, we need to break out of this case. // if (validCheck.Item1 == false) // { // Console.WriteLine(validCheck.Item2); // commandPrint(); // break; // } // Now what we know that the user's inputs are valid, we need to store the path of the files that the user wants to upload // Clearing the previous FilestoREg list because we have a new list. // clientState.FilePathsToReg.Clear(); // clientState.FilePathsToRegLength.Clear(); // foreach (string item in userCommand.Item2.Split(',')) // { // clientState.FilePathsToReg.Add(clientState.UploadableFilePath[Convert.ToInt32(item) - 1]); // clientState.FilePathsToRegLength.Add(clientState.UploadableFileSize[Convert.ToInt32(item) - 1]); // } // var uploadcmd = new ClientPassableObject(clientState); // uploadcmd.RegisterFiles(); // var uploadclient = new TcpTorrent(); // var clienttask = uploadclient.ClientStart(uploadcmd); // while (uploadcmd.DoneFlag == false) ; // Now that we recieved upload on which file can be uploaded, we need to split the files up into segments and store it in memory // for (int j = 0; j < uploadcmd.FilesRegSuccessCount.Count; j++) // { // if (uploadcmd.FilesRegSuccessCount[j] == true) // { // StringBuilder FileSb = new StringBuilder(); // using (var streamRdr = new StreamReader(clientState.FilePathsToReg[j])) // { // while (!streamRdr.EndOfStream) // { // FileSb.Append(streamRdr.ReadLine()); // } // } // var DataParser = new DataSegmentObject(); // Tuple<string, List<string>> tupleForDict = DataParser.ChunksUpto(FileSb.ToString(), clientState.MaxChunkSize); // var dictObject = new ObjectForFiledict(); // dictObject.Hash = tupleForDict.Item1; // dictObject.Segments = tupleForDict.Item2; // if (!clientState.FileDict.ContainsKey(Path.GetFileName(clientState.FilePathsToReg[j]))) // { // clientState.FileDict.Add(Path.GetFileName(clientState.FilePathsToReg[j]), dictObject); // } // } // } // commandPrint(); // break; // case "LISTDOWNLOADABLES": // var printcmd = new ClientPassableObject(clientState); // printcmd.PrintDownloadable(); // var printListclient = new TcpTorrent(); // var printTask = printListclient.ClientStart(printcmd); // while (printcmd.DoneFlag == false) ; // clientState.DownloadableFileName = printcmd.DownloadableFiles; // clientState.DownloadableFileSize = printcmd.DownloadableFilesLength; // commandPrint(); // break; // case "DOWNLOAD": // Tuple<bool, string> downloadValidCheck = IsValidInput(userCommand.Item2, clientState.DownloadableFileName.Count); // If any of the value in the previous loop was invalid, we need to break out of this case. // if (downloadValidCheck.Item1 == false) // { // Console.WriteLine(downloadValidCheck.Item2); // commandPrint(); // break; // } // We are only going to allow one file to be download at a time // if (userCommand.Item2.Split(',').Length > 1) // { // Console.WriteLine("/nYou can only select one download file at a time"); // break; // } // Clearing the previous FilestoREg list because we have a new list. // clientState.FileNameToDownload = string.Empty; // clientState.FileNameToDownloadLength = 0; // clientState.FileNameToDownload = clientState.DownloadableFileName[Convert.ToInt32(userCommand.Item2) - 1]; // clientState.FileNameToDownloadLength = clientState.DownloadableFileSize[Convert.ToInt32(userCommand.Item2) - 1]; // var downloadClient = new TcpTorrent(); // var downloadTask = downloadClient.GetDownloadFile(clientState); // var uploadCmd = new ClientPassableObject(clientState); // var uploadClient = new TcpTorrent(); // uploadCmd.GetFilesLocation(clientState.FileNameToDownload); // var uploadTask = uploadClient.ClientStart(uploadCmd); // while (uploadCmd.DoneFlag == false) ; // Console.WriteLine("Name of file: {0}", uploadCmd.FileToDownload); // Console.WriteLine("Size of file: {0}", uploadCmd.FileToDownloadLength); // Console.WriteLine("Address: {0}", uploadCmd.AddressAtFile2Download[0]); // Console.WriteLine("Port: {0}", uploadCmd.PortAtFile2Download[0]); // commandPrint(); // break; // case "LEAVE": // var leaveCmd = new ClientPassableObject(clientState); // leaveCmd.LeaveRequest(); // var leaveClient = new TcpTorrent(); // var leaveTask = leaveClient.ClientStart(leaveCmd); // while (leaveCmd.DoneFlag == false) ; // Console.ReadKey(); // Environment.Exit(0); // break; // case "HELP": // using (var sr = new StreamReader(@"../../Intro.txt")) // { // while (!sr.EndOfStream) // Console.WriteLine(sr.ReadLine()); // } // break; // default: // Console.WriteLine("\nSorry. That was an invalid input. Please try again. \nValid Inputs are 'Listuploadables','Upload-#','ListDownloadables','Download-#','Leave','Help'"); // commandPrint(); // break; // } //} }