/// <summary> /// Use to Download all folders, files from server /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Download_Btn_Click(object sender, RoutedEventArgs e) { try { Ethernet.Ip = IP.Text; Ethernet.Port = Port.Text; Ethernet.Connect(); } catch (Exception ex) { Global.mw.appendReportText("Failed to connect to Server {0}:{1}", IP.Text, Port.Text); } FileObject downloadObj = new FileObject(); downloadObj.filename = "FileTable.json"; downloadObj.mode = "SYNCBACK"; downloadObj.status = "PROCESSING"; download.drive = FolderName.Text.Substring(0, FolderName.Text.IndexOf(":") + 2); string json = JsonConvert.SerializeObject(downloadObj); Ethernet.SendData(json); }
public void UploadThread() { //Console.WriteLine(drive + FileName.filename); int count = 0; using (FileStream fs = new FileStream(FileName.filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var buffer = new byte[2048]; int num_bytes_read = fs.Read(buffer, 0, buffer.Length); while (num_bytes_read > 0) { buffer = buffer.Where(c => c != null).ToArray(); Console.WriteLine(FileName.filename); var name = Encoding.UTF8.GetBytes(FileName.filename); byte[] len = BitConverter.GetBytes(FileName.filename.Length); //byte a = Convert.ToByte(FileName.filename.Length); byte a = Convert.ToByte(name.Length); Array.Reverse(len); //Console.WriteLine(len); Console.WriteLine(Encoding.UTF8.GetString(name)); Console.WriteLine(a); byte[] rv = new byte[num_bytes_read + name.Length + 1]; // 1 + 1 is name length and num_byte reads System.Buffer.BlockCopy(buffer, 0, rv, 0, num_bytes_read); System.Buffer.BlockCopy(name, 0, rv, num_bytes_read, name.Length); rv[num_bytes_read + name.Length] = a; Console.WriteLine(rv[num_bytes_read + FileName.filename.Length]); isReceive = 0; Ethernet.SendData(rv); count++; while (isReceive == 0) { ; } if (isReceive == 2) { return; } Array.Clear(buffer, 0, 1024); num_bytes_read = fs.Read(buffer, 0, buffer.Length); } FileName.status = "DONE"; FileName.mode = "UPLOAD"; string json = JsonConvert.SerializeObject(FileName); Ethernet.SendData(json); Console.WriteLine("Done"); Console.WriteLine("======== COUNT = {0} ==========", count); isDone = 1; } }
private void Sync_Btn_Click(object sender, RoutedEventArgs e) { appendReportText("== Saving Setting =="); SaveSetting(); try { Ethernet.Ip = IP.Text; Ethernet.Port = Port.Text; Ethernet.Connect(); } catch (Exception ex) { Global.mw.appendReportText("Failed to connect to Server {0}:{1}", IP.Text, Port.Text); } /////////// Send Folder To Sync /////////// /// FileObject folderInfo = new FileObject(); string dirName = System.IO.Path.GetFileName(FolderName.Text); folderInfo.filename = dirName; folderInfo.mode = "LOCATION"; folderInfo.status = ""; string json = JsonConvert.SerializeObject(folderInfo); Ethernet.SendData(json); Global.location = dirName; //string current_path = FolderName.Text.Substring(FolderName.Text.IndexOf(":") + 2, // FolderName.Text.Length - 3); // Remove *:/ Console.WriteLine(FolderName.Text); upload.isDone = 1; Global.watcher.location = dirName; Global.watcher.start(); //Watcher.drive = FolderName.Text.Substring(0, FolderName.Text.IndexOf(":") + 2); //Global._upload.FileName.filename = "plate0638718.jpg"; //Global._upload.FileName.mode = "UPLOAD"; //Global._upload.FileName.status = "PROCESSING"; //Console.WriteLine(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)); //Global._upload.Do_Upload(); //Global._download.FileName.filename = "0000_02187_b.jpg"; //Global._download.FileName.mode = "DOWNLOAD"; //Global._download.FileName.status = "PROCESSING"; //Global._download.DoDownload(); }
public void Do_Upload() { Global.mw.Dispatcher.Invoke(new Action(() => Global.mw.appendReportText("== START UPLOADING {0}==", FileName.filename))); //drive = string.Copy(FileName.filename); //drive = drive.Substring(0, drive.IndexOf(":") + 2); //FileName.filename = FileName.filename.Substring(FileName.filename.IndexOf(":") + 2, // FileName.filename.Length - 3); // Remove *:/ FileName.mode = "UPLOAD"; FileName.status = "PROCESSING"; Console.WriteLine(FileName.filename); string json = JsonConvert.SerializeObject(FileName); Ethernet.SendData(json); Thread thr1 = new Thread(new ThreadStart(UploadThread)); thr1.IsBackground = true; thr1.Start(); isDone = 0; }
public void DoDownload() { Console.WriteLine("=== DO DOWNLOAD ==="); // Create file if do not have using (FileStream fs = new FileStream(FileName.filename, FileMode.Create, FileAccess.Write)) { fs.Write(new Byte[1], 0, 0); } FileName.status = "PROCESSING"; FileName.mode = "DOWNLOAD"; string json = JsonConvert.SerializeObject(FileName); Ethernet.SendData(json); // Initialize isJson = 1; Thread thr = new Thread(new ThreadStart(DownloadThread)); thr.IsBackground = true; thr.Start(); }
private void DownloadThread() { isDone = 0; while (isDone == 0) { // Byte and now Get part of File if (isJson == 0) { //count++; int len = Convert.ToInt32(data[bytesRead - 1]); //Console.WriteLine("LENGTH: " + len); Byte[] fileNameArray = new Byte[len]; Array.Copy(data, bytesRead - len - 1, fileNameArray, 0, len); string fileNameStr = Encoding.UTF8.GetString(fileNameArray); //Console.WriteLine(fileNameStr); Byte[] newData = new Byte[bytesRead - len - 1]; Array.Copy(data, 0, newData, 0, bytesRead - len - 1); // Append file using (FileStream fs = new FileStream(fileNameStr, FileMode.Append, FileAccess.Write)) { Console.WriteLine("Wrote: " + newData.Length); fs.Write(newData, 0, newData.Length); } FileName.status = "OK"; GC.Collect(); GC.WaitForPendingFinalizers(); string json = JsonConvert.SerializeObject(FileName); Ethernet.SendData(json); isJson = 1; } } }
private void dispatcherTimer_Tick(object sender, EventArgs e) // use to check file is still edit { try { FileStream fs; List <FileInfoJson> listfileInfo = new List <FileInfoJson>(); // Create file if (!File.Exists(Global.location + "/FileTable.json")) { fs = new FileStream(Global.location + "/FileTable.json", FileMode.Create, FileAccess.Write, FileShare.ReadWrite); fs.Close(); } //// Open File //fs = new FileStream("FileTable.json", FileMode.Open, FileAccess.Write, FileShare.ReadWrite); //fs.Write(new byte[1], 0, 0); //fs.Close(); // Append file //fs = new FileStream("FileTable.json", FileMode.Append, FileAccess.Write, FileShare.ReadWrite); string[] allfiles = Directory.GetFiles(location, "*.*", SearchOption.AllDirectories); //Console.WriteLine("===== WATCHER LOCATION ===== " + Global.location.Substring(Global.location.IndexOf(":") + 2, // Global.location.Length - 3)); //listfileInfo.Add(new FileInfoJson(Global.location.Substring(Global.location.IndexOf(":") + 2, // Global.location.Length - 3))); foreach (var file in allfiles) { if (upload.isDone == 0) { break; } FileInfo info = new FileInfo(file); FileObject fo = new FileObject(); Console.WriteLine("====== TIMER ====== : " + file); Console.WriteLine("====== TIMER ====== : " + info.DirectoryName); fo.filename = file; //// Append to List string temp_file_name = file; if (info.Name != "FileTable.json") { listfileInfo.Add(new FileInfoJson(temp_file_name)); string json = JsonConvert.SerializeObject(listfileInfo.ToArray()); File.WriteAllText(Global.location + "/FileTable.json", json); } fo.mode = "CHECKMD5"; fo.status = EditingFile.getMD5Hash(info.FullName); string json_to_send = JsonConvert.SerializeObject(fo); Console.WriteLine("======== WATCHER JSON TO SEND========= " + json_to_send); Ethernet.SendData(json_to_send); Thread.Sleep(500); upload.isDone = 0; //while (upload.isDone == 0) ; } } catch (Exception ex) { Console.WriteLine("===== Watcher Tick Error {0} =====", ex.ToString()); } }