public void ProcessData(byte[] data, int bytesRead) { try { Console.WriteLine("===== GO TO WATCHER PROCESS DATA ====="); byte[] true_data = new Byte[bytesRead]; Array.Copy(data, 0, true_data, 0, bytesRead); string json = Encoding.UTF8.GetString(true_data); Console.WriteLine("====== WATCHER PROCESSING DATA ====== " + json); FileObject new_file = JsonConvert.DeserializeObject <FileObject>(json); isJson = 1; if (new_file.mode == "CHECKMD5") { if (new_file.status == "DIFF") { Global._upload.FileName.filename = new_file.filename; // Set only file name because status and mode //is set in function Global._upload.Do_Upload(); // Signal done upload.isDone = 0; //while (upload.isDone == 0); Console.WriteLine("============= START UPLOAD =============== " + Global._upload.FileName.filename); } else if (new_file.status == "SAME") { Console.WriteLine("============= SAME MD5 =============== "); upload.isDone = 1; } } } catch (Exception ex) { Console.WriteLine("======== Is Not Json In Watcher {0} =========", ex); isJson = 0; } }
public void process(Byte[] data, int bytesRead) { Console.WriteLine("========== UPLOAD PROCESSING =========="); Global.mw.Dispatcher.Invoke(new Action(() => Global.mw.appendReportText("== UPLOAD PROCESSING =="))); try { byte[] true_data = new Byte[bytesRead]; Array.Copy(data, 0, true_data, 0, bytesRead); string json = Encoding.UTF8.GetString(true_data); Console.WriteLine("========= UPLOAD DATA: " + json + "============"); FileObject new_file = JsonConvert.DeserializeObject <FileObject>(json); isJson = 1; // check if this is the file of own upload if (new_file.mode != "UPLOAD") { return; // Wrong mode } if (new_file.filename.CompareTo(FileName.filename) != 0) { return; // wrong filename pf this process } if (new_file.status.CompareTo("OK") == 0) // Server send ok after receive part of file { isReceive = 1; } else if (new_file.status.CompareTo("FAIL") == 0) { isReceive = 2; } } catch (Exception ex) { Console.WriteLine("========== UPLOAD: NOT JSON =========="); isJson = 0; } }
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()); } }
public download() { FileName = new FileObject(); }
public upload() { FileName = new FileObject(); }