Exemplo n.º 1
0
        /// <summary>
        /// Function that saves a TCPSettings object to the location new setup file path and connect it to a HashDictionary file.
        /// </summary>
        /// <param name="newSetupFile">Path to a new setup file TCPSetting will be save to.</param>
        /// <param name="hashDictionaryName">Path to a HashDictionary file.</param>
        /// <param name="tcp">TCPSetting that has to be saved.</param>
        /// <returns></returns>
        public static void WriteTCPSettingToFile(string newSetupFile, string hashDictionaryName, TCPSettings tcp)
        {
            IniData id = GetSectionTemplate();

            // Field data of the class that has to be filled up.
            var fields = tcp.GetType().GetFields();

            foreach (var item in fields)
            {
                // Getting if the attribute does exist on the field.
                var attr = (Reading[])item.GetCustomAttributes(typeof(Reading), false);

                // Going while several Reading sections is going.
                foreach (var attrVal in attr)
                {
                    // Getting field type to make right cast.
                    var ft = item.FieldType;

                    // Casting to get the right value.
                    // The data in file has to be made right for this to work!
                    switch (ft.Name)
                    {
                    case "String":
                        if (item.Name != "hashDictionaryName")
                        {
                            id.Sections[attrVal.Section].AddKey(item.Name, (string)item.GetValue(tcp));
                        }
                        else
                        {
                            id.Sections[attrVal.Section].AddKey(item.Name, hashDictionaryName);

                            // Initialize HashDictionaryFile or reinitialize it if this existed.
                            FilerHashesIO.InitializeHashDictionaryFile(hashDictionaryName);
                        }
                        break;

                    case "Int32":
                        id.Sections[attrVal.Section].AddKey(item.Name, ((int)item.GetValue(tcp)).ToString());
                        break;

                    case "Boolean":
                        id.Sections[attrVal.Section].AddKey(item.Name, ((bool)item.GetValue(tcp) ? "Yes" : "No"));
                        break;

                    default:
                        break;
                    }
                }
            }

            FileIniDataParser fidp = new FileIniDataParser();

            fidp.SaveFile(newSetupFile, id);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Event handler for SyncRequest being received by Host.
        /// </summary>
        /// <param name="arg">The SyncRequest that recieved.</param>
        /// <returns></returns>
        //======Joined requests======
        //!qq = Exit
        //!getFile *relative path to file* = upload file to Joined
        //!catchFile *relative path to file* = get file from Joined
        //!exists *relative path to file* = check if file exists on servers side and then answer for Joined
        //!getFileDataList = get all relative pathes and hashes to it and send it to Joined
        //!sessiondone = updates servers Filer and Hasher
        //!rm *relative path to file* = remove file on Host side
        //!getFileInfo *relative path to file* = get file info from Host

        //======Host Respones======
        //!dd = ready for next operation
        //!Yes = answer for file existance if it does exist
        //!No = answer for file existance if it does not exist
        private SyncResponse SyncSolver(SyncRequest arg)
        {
            servH.Events.StreamReceived -= StreamReceived;
            string       cmd = GetStringFromBytes(arg.Data);
            SyncResponse sr  = new SyncResponse(arg, GetBytesFromString("NotRecognized"));

            if (cmd.Contains("!qq"))
            {
                servH.DisconnectClients();
                servH.Stop();
                servH.Dispose();
            }
            else if (cmd.Contains("!getFile "))
            {
                cmd = cmd.Replace("!getFile ", "");
                UploadFile(arg.IpPort, cmd);
                sr = new SyncResponse(arg, GetBytesFromString("!dd"));
            }
            else if (cmd.Contains("!catchFile "))
            {
                while (gettingFile)
                {
                }

                DownloadFileTo = Filed.RootPath + cmd.Replace("!catchFile ", "");
                sr             = new SyncResponse(arg, GetBytesFromString("!dd"));
                servH.Events.StreamReceived += StreamReceived;
            }
            else if (cmd.Contains("!exists "))
            {
                cmd = cmd.Replace("!exists ", "");
                if (Filed.CheckFileExistanceFromRelative(cmd))
                {
                    sr = new SyncResponse(arg, GetBytesFromString("!Yes"));
                }
                else
                {
                    sr = new SyncResponse(arg, GetBytesFromString("!No"));
                }
            }
            else if (cmd.Contains("!getFileDataList"))
            {
                cmd = cmd.Replace("!getFileDataList", "");
                sr  = new SyncResponse(arg, GetBytesFromString(GetFileList()));
            }
            else if (cmd.Contains("!sessiondone"))
            {
                cmd = cmd.Replace("!sessiondone", "");
                Filed.RecomputeHashesBasedOnModifiedStatus();
                FilerHashesIO.WriteHashesToFile(ts.hashDictionaryName, Filed);
                sr = new SyncResponse(arg, GetBytesFromString("!dd"));
                UIHandler.WriteLog("Session done!", Color.Green);
            }
            else if (cmd.Contains("!rm "))
            {
                cmd = cmd.Replace("!rm ", "");
                if (Filed.GetLocalFromRelative(cmd) != "?FileNotFound?")
                {
                    File.Delete(Filed.GetLocalFromRelative(cmd));
                    Filed.ChangeFileModifiedStatusByRelativePath(cmd, FileModifiedStatus.Deleted);
                }
                sr = new SyncResponse(arg, GetBytesFromString("!dd"));
            }
            else if (cmd.Contains("!getFileInfo "))
            {
                cmd = cmd.Replace("!getFileInfo ", "");
                FileInfo fi = Filed.GetLocalFileInfoFromRelative(cmd);
                sr = new SyncResponse(arg, GetBytesFromString($"{fi.Length}\n{fi.LastAccessTime.ToString()}"));
            }
            return(sr);
        }
Exemplo n.º 3
0
        /// <summary>
        /// The main syncronization procedure.
        /// </summary>
        public void Syncronize()
        {
            UIHandler.PlayColorfulBarAnimation(true);
            GetHostFileDataList();
            UIHandler.WriteLog($"Recieved Host FileData list!", Color.Green);
            Syncronizer sync = new Syncronizer(Filed.FilesData, FDGotFromServer);

            var fddList = sync.fdd.ToList();

            for (int i = 0; i < sync.saList.Count; i++)
            {
                // Switch that makes stuff specified on what was chosen.
                switch (sync.saList[i])
                {
                case SyncAction.GetFromHost:
                    if (File.Exists(Filed.MakeLocalPathFromRelative(fddList[i].FileRelativePath)))
                    {
                        File.Delete(Filed.MakeLocalPathFromRelative(fddList[i].FileRelativePath));
                    }
                    DownloadFile(fddList[i].FileRelativePath);
                    Filed.ChangeFileModifiedStatusByRelativePath(fddList[i].FileRelativePath, FileModifiedStatus.Changed);
                    break;

                case SyncAction.GetFromJoined:
                    DeleteOnHost(fddList[i].FileRelativePath);
                    UploadFile(fddList[i].FileRelativePath);
                    break;

                case SyncAction.Skip:
                    break;

                case SyncAction.GetNewClone:
                    string tempFileName             = Filed.RootPath + fddList[i].FileRelativePath;
                    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(tempFileName);
                    string extenstion = Path.GetExtension(tempFileName);
                    int    cntr       = 1;
                    while (File.Exists(Filed.RootPath + fileNameWithoutExtension + $"(Cloned {cntr})" + extenstion))
                    {
                        cntr++;
                    }
                    DownloadFile(fddList[i].FileRelativePath, fileNameWithoutExtension + $"(Cloned {cntr})" + extenstion);
                    Filed.FilesData.Add(new FileData(Filed.RootPath, Filed.MakeLocalPathFromRelative(fileNameWithoutExtension + $"(Cloned {cntr})" + extenstion)));
                    Filed.ChangeFileModifiedStatusByRelativePath(fileNameWithoutExtension + $"(Cloned {cntr})" + extenstion, FileModifiedStatus.Changed);
                    break;

                case SyncAction.Delete:
                    if (File.Exists(Filed.MakeLocalPathFromRelative(fddList[i].FileRelativePath)))
                    {
                        File.Delete(Filed.MakeLocalPathFromRelative(fddList[i].FileRelativePath));
                    }
                    DeleteOnHost(fddList[i].FileRelativePath);
                    Filed.ChangeFileModifiedStatusByRelativePath(fddList[i].FileRelativePath, FileModifiedStatus.Deleted);
                    break;

                case SyncAction.NotChosen:
                    break;

                default:
                    break;
                }
            }

            SyncResponse sr = SendMessage("!sessiondone");

            Filed.RecomputeHashesBasedOnModifiedStatus();
            FilerHashesIO.WriteHashesToFile(ts.hashDictionaryName, Filed);
            UIHandler.WriteLog("Session done!");
            UIHandler.ToggleProgressBarVisibility(false);
            UIHandler.StopColorfulBarAnimation();
        }