예제 #1
0
        public void scanAllFilesAttributes()
        {
            // get local path
            LocalDB readLocalDB = new LocalDB();
            readLocalDB = readLocalDB.readfromfile();

            string clientSynFolderPath = readLocalDB.getPath();

            string[] filePaths = Directory.GetFiles(clientSynFolderPath, "*", SearchOption.AllDirectories);

            //return filePaths;
            //foreach (string file in filePaths)
            //{
            //    try
            //    {
            //        // get file attributes
            //        Client.LocalFileSysAccess.getFileAttributes getFileAttr = new Client.LocalFileSysAccess.getFileAttributes(file);

            //        string md5 = getFileAttr.md5Value;
            //        DateTime dt = getFileAttr.lastModified;

            //    }
            //    catch (System.IO.IOException e)
            //    {
            //        Console.WriteLine(e.Message);
            //        MessageBox.Show(e.Message);
            //        return;
            //    }

            foreach (string file in filePaths)
            {
                try
                {
                    // new a FileInfo instance to hold each file's metadata
                    Client.LocalFileSysAccess.FileInfo tmp = null;

                    // get file attributes
                    Client.LocalFileSysAccess.getFileAttributes getFileAttr = new Client.LocalFileSysAccess.getFileAttributes(file);

                    tmp = new Client.LocalFileSysAccess.FileInfo();
                    tmp.time = getFileAttr.lastModified;
                    tmp.md5r = getFileAttr.md5Value;

                    // add to fileList
                    Client.LocalFileSysAccess.FileList.fileInfoDic[file] = tmp;

                }
                catch (System.IO.IOException e)
                {
                    Console.WriteLine(e.Message);
                    return;
                }

            }
        }
예제 #2
0
파일: VCThread.cs 프로젝트: hangmiao/DBLike
        private void threadStartFun()
        {
            try
            {
                Console.WriteLine("in Vc thread");
                // Client create poll msg
                LocalDB readLocalDB = new LocalDB();
                readLocalDB = readLocalDB.readfromfile();
                Client.MessageClasses.MsgPoll msgpoll = new Client.MessageClasses.MsgPoll();
                Client.Message.CreateMsg pollM = new Client.Message.CreateMsg();
                msgpoll.userName = readLocalDB.getUsername();
                msgpoll.password = "******";
                string msg = pollM.pollMsg(msgpoll);

                //send the msg using socket
                ConnectionManager.Connection conn = new ConnectionManager.Connection();
                Socket soc = conn.connect(conf.serverAddr, conf.port);

                SocketCommunication.ReaderWriter rw = new SocketCommunication.ReaderWriter();
                rw.writetoSocket(soc, msg);

                //receive the msg
                string resp = rw.readfromSocket(soc);

                //parse msg and poll
                Client.Message.MessageParser parseResp = new Client.Message.MessageParser();
                msgpoll = parseResp.pollParseMsg(resp);
                if (msgpoll.indicator == "OK")
                {
                    Configuration.userInfo.containerURI = msgpoll.fileContainerUri;
                    // write container somewhere (msgpoll.fileContainerUri);
                }

            }
            catch (Exception e)
            {
                Program.ClientForm.addtoConsole(e.ToString());
                System.Windows.Forms.MessageBox.Show(e.ToString());
                //System.IO.File.WriteAllText("errors.txt", e.ToString());
            }
            finally
            {
                Thread.CurrentThread.Abort();
            }
        }
예제 #3
0
        public void poll()
        {
            Configuration.flag.polling = true;
            Program.ClientForm.addtoConsole("Poll initiated");

            //MessageBox.Show("Polling started", "Client");
            try
            {
                // Client create poll msg
                LocalDB readLocalDB = new LocalDB();
                readLocalDB = readLocalDB.readfromfile();

                Client.MessageClasses.MsgPoll msgpoll = new Client.MessageClasses.MsgPoll();
                Client.Message.CreateMsg pollM = new Client.Message.CreateMsg();
                msgpoll.userName = readLocalDB.getUsername();
                msgpoll.password = readLocalDB.getPassword();
                string msg = pollM.pollMsg(msgpoll);

                //send the msg using socket
                ConnectionManager.Connection conn = new ConnectionManager.Connection();
                Socket soc = conn.connect(conf.serverAddr, conf.port);

                SocketCommunication.ReaderWriter rw = new SocketCommunication.ReaderWriter();
                rw.writetoSocket(soc, msg);
                Program.ClientForm.addtoConsole("Writing Socket");
                //receive the msg
                string resp = rw.readfromSocket(soc);
                Program.ClientForm.addtoConsole("Reading Socket");
                //parse msg and poll
                Client.Message.MessageParser parseResp = new Client.Message.MessageParser();
                msgpoll = parseResp.pollParseMsg(resp);
                if (msgpoll.indicator == "OK")
                {
                    new Client.PollFunction.Poll(msgpoll.fileContainerUri);
                    Configuration.userInfo.containerURI = msgpoll.fileContainerUri;
                }
                Configuration.flag.polling = false;
            }
            catch(Exception ex)
            {
                Program.ClientForm.addtoConsole("Poll thread Exception:" + ex.Message);
                //System.Windows.Forms.MessageBox.Show(ex.ToString());
            }
        }
예제 #4
0
파일: Uploader.cs 프로젝트: hangmiao/DBLike
        private static void threadStartFun(string fullpathOfChnagedFile, string eventType, string addiInfo, DateTime timestamp)
        {
            try
            {

                Program.ClientForm.addtoConsole("Upload thread Started");
                //System.Windows.Forms.MessageBox.Show("Started Uploader for " + fullpathOfChnagedFile, "Client");
                //TBD
                //send msg to server
                //get the information from the localDatabase
                LocalDB readLocalDB = new LocalDB();
                readLocalDB = readLocalDB.readfromfile();

                string clientSyncFolderPath = readLocalDB.getPath();
                string[] pathName = clientSyncFolderPath.Split('\\');

                string[] pathName2 = fullpathOfChnagedFile.Split('\\');
                int i = pathName2.Count();
                string pathInSyncFolderPath = "";
                for (int j = pathName.Count(); j < i; j++)
                {
                    pathInSyncFolderPath += pathName2[j];
                    if ((j + 1) < i)
                    {
                        pathInSyncFolderPath += "\\";
                    }
                }
                string userName = readLocalDB.getUsername();
                string password = readLocalDB.getPassword();

                Client.Message.CreateMsg uploadM = new Client.Message.CreateMsg();
                string additionalInfo = "";

                if (eventType == "create" || eventType == "change" || eventType == "signUpStart")
                {
                    if (eventType == "create")
                    {
                        additionalInfo = "create";
                        // add to the file list
                        Client.LocalFileSysAccess.FileListMaintain addToFileList = new Client.LocalFileSysAccess.FileListMaintain();
                        addToFileList.addSingleFileToFileList(fullpathOfChnagedFile);
                    }
                    if (eventType == "change")
                    {
                        additionalInfo = "change";
                    }
                    if (eventType == "signUpStart")
                    {
                        additionalInfo = "signUpStart";
                    }

                    Program.ClientForm.addtoConsole("Event handling:" + eventType);
                    // get the initial attribute before making this "change"
                    Client.LocalFileSysAccess.FileInfo tmp = new Client.LocalFileSysAccess.FileInfo();
                    Client.LocalFileSysAccess.FileList.fileInfoDic.TryGetValue(fullpathOfChnagedFile, out tmp);
                    DateTime timeBefore = tmp.time;
                    string md5rBefore = tmp.md5r;

                    // belong to these events because for delete event it won't get the attributes anymore
                    Client.LocalFileSysAccess.getFileAttributes att = new Client.LocalFileSysAccess.getFileAttributes(fullpathOfChnagedFile);
                    DateTime time = att.lastModified.ToUniversalTime();
                    string msg;
                    string md5r = att.md5Value;

                    // create the msg

                    msg = uploadM.uploadMsg(userName, password, pathInSyncFolderPath, time, md5r, additionalInfo);

                    //send the msg using socket
                    ConnectionManager.Connection conn = new ConnectionManager.Connection();
                    Socket soc = conn.connect(conf.serverAddr, conf.port);

                    SocketCommunication.ReaderWriter rw = new SocketCommunication.ReaderWriter();
                    rw.writetoSocket(soc, msg);
                    Program.ClientForm.addtoConsole("Wrote to socket");
                    //receive the msg
                    string resp = rw.readfromSocket(soc);
                    Program.ClientForm.addtoConsole("Read from socket");
                    //8 Client parse msg
                    Client.Message.MessageParser par2 = new Client.Message.MessageParser();
                    if (resp != null)
                    {
                        Client.MessageClasses.MsgRespUpload reup = par2.uploadParseMsg(resp);

                        //9 Client upload
                        if (reup.indicator == "OK")
                        {
                            // event type when there's no file conflict
                            string tempEType = reup.addiInfo;

                            string[] str = null;
                            // get current file info on server
                            string[] separators = { "|||" };
                            // when event type is change
                            // otherwise directly upload, don't need to check conflict
                            if (reup.addiInfo.Contains("|||"))
                            {
                                str = reup.addiInfo.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                                string currHashValueOnServer = str[0];
                                string tempTimestampOnServer = str[1];
                                // reassign event type
                                tempEType = str[2];

                                //CultureInfo provider = new CultureInfo("en-US");
                                //CultureInfo provider = CultureInfo.InvariantCulture;
                                //tempTimestampOnServer = tempTimestampOnServer.Replace("-", "/");
                                Program.ClientForm.addtoConsole(tempTimestampOnServer);
                                DateTime currTimestampOnServer = DateTime.Parse(tempTimestampOnServer);
                                //DateTime currTimestampOnServer = DateTime.ParseExact(tempTimestampOnServer, "MM/dd/yyyy HH:mm:ss", null);

                                // file has been changed during open time and save time
                                // aka there's a newer version of this file uploaded by another user during this time
                                if (DateTime.Compare(timeBefore, currTimestampOnServer) < 0 && String.Compare(md5rBefore, currHashValueOnServer) != 0)
                                {
                                    string tMsg = "simultaneous editing confilct";
                                    string cMsg = "\nA newer version has been detected on the server.\nDo you want to save current version?\n\nYes to Save current file in another name\nNo to Download the newest version file from server";
                                    DialogResult dialogResult = MessageBox.Show(string.Format("{0}" + cMsg, fullpathOfChnagedFile), tMsg, MessageBoxButtons.YesNo);
                                    if (dialogResult == DialogResult.Yes)
                                    {

                                        string sourcePath = fullpathOfChnagedFile;
                                        string targetPath = "";

                                        // add time to new name
                                        string dateString = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
                                        //string dateString = DateTime.UtcNow.ToString("MM/dd/yyyy HH:mm:ss");
                                        string[] fileNameArr = sourcePath.Split('.');
                                        string newFileName = fileNameArr[0] + " ( " + Environment.UserName + "'s conflict copy " + dateString + ")." + fileNameArr[1];
                                        targetPath = newFileName;

                                        //// To copy a folder's contents to a new location:
                                        //// Create a new target folder, if necessary.
                                        //if (!System.IO.Directory.Exists(targetPath))
                                        //{
                                        //    System.IO.Directory.CreateDirectory(targetPath);
                                        //}

                                        // To copy a file to another location and
                                        // overwrite the destination file if it already exists.
                                        System.IO.File.Copy(sourcePath, targetPath, true);

                                    }
                                    else if (dialogResult == DialogResult.No)
                                    {

                                        // try delete while it still exists
                                        while (System.IO.File.Exists(fullpathOfChnagedFile))
                                        {
                                            // delete current file
                                            // won't delete blob file on the server
                                            // b/c it's older than that version
                                            System.IO.File.Delete(fullpathOfChnagedFile);

                                        }

                                        // if file is in dic
                                        if (Client.LocalFileSysAccess.FileList.fileInfoDic.ContainsKey(fullpathOfChnagedFile))
                                        {
                                            // after it has been deleted
                                            // del from the file list
                                            Client.LocalFileSysAccess.FileListMaintain delFromFileList = new Client.LocalFileSysAccess.FileListMaintain();
                                            delFromFileList.removeSingleFileFromFileList(fullpathOfChnagedFile, time, md5r);

                                            // TODO
                                            // download newer version from server

                                        }
                                    }
                                }

                            }

                            new Client.UploadFunctions.UploadFile().UploadFileWithContainerUri(reup.fileContainerUri, fullpathOfChnagedFile, reup.filePathInSynFolder, md5r, time, eventType);
                            //System.Windows.Forms.MessageBox.Show(string.Format("Uploaded! \n event type: {0} \n Path: {1}", tempEType, fullpathOfChnagedFile), "DBLike Client");
                            Program.ClientForm.addtoConsole(string.Format("Uploaded! \n event type: {0} \n Path: {1}", tempEType, fullpathOfChnagedFile));
                            // update file list
                            Client.LocalFileSysAccess.FileListMaintain updateFileList = new Client.LocalFileSysAccess.FileListMaintain();
                            updateFileList.updateSingleFileToFileList(fullpathOfChnagedFile, time, md5r);

                        }
                        //// handle simultaneous editing confilct
                        //// currently client will handle this
                        //if (reup.indicator == "simultaneousEditConfilct")
                        //{
                        //}

                    }
                    else
                    {
                        Program.ClientForm.addtoConsole("Error : <<Response from server is null>>");
                    }
                }

                else if (eventType == "delete")
                {
                    Program.ClientForm.addtoConsole("Event handling:" + eventType);
                    additionalInfo = "delete";
                    string msg = " ";
                    //System.Windows.Forms.MessageBox.Show(string.Format("Deletion detected!\n Path: {0}", pathInSyncFolderPath), "DBLike Client");

                    // file list maintainence_1
                    // delete the file from the file list
                    // grab it first by key, then delete
                    Client.LocalFileSysAccess.FileListMaintain reFileList = new Client.LocalFileSysAccess.FileListMaintain();
                    // get value by key
                    Client.LocalFileSysAccess.FileInfo tmpFInfo = new Client.LocalFileSysAccess.FileInfo();
                    Client.LocalFileSysAccess.FileList.fileInfoDic.TryGetValue(fullpathOfChnagedFile, out tmpFInfo);

                    // create the msg
                    // use " " instead of null to avoid parsing issue
                    // use DateTime.MinValue to avoid parsing issue
                    //msg = uploadM.uploadMsg(userName, password, pathInSyncFolderPath, DateTime.MinValue, " ", additionalInfo);
                    msg = uploadM.uploadMsg(userName, password, pathInSyncFolderPath, tmpFInfo.time, " ", additionalInfo);

                    //send the msg using socket
                    ConnectionManager.Connection conn = new ConnectionManager.Connection();
                    Socket soc = conn.connect(conf.serverAddr, conf.port);

                    SocketCommunication.ReaderWriter rw = new SocketCommunication.ReaderWriter();
                    rw.writetoSocket(soc, msg);

                    // file list maintainence_2
                    // remove file from file list
                    reFileList.removeSingleFileFromFileList(fullpathOfChnagedFile, tmpFInfo.time, tmpFInfo.md5r);
                    Program.ClientForm.ballon("Deleted:"+ fullpathOfChnagedFile);

                }
                else if (eventType == "rename")
                {
                    Program.ClientForm.addtoConsole("Event handling:" + eventType);
                    string[] separators = { "<", ">:<", ">" };
                    string[] reNameInfo = addiInfo.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                    //List<string> temp = reNameInfo.ToList();
                    //temp.Insert(0, "rename");

                    //System.Windows.Forms.MessageBox.Show(string.Format("Rename detected!\n + Old Path: {0} \n + New Path: {1}", reNameInfo[0], reNameInfo[1]), "DBLike Client");

                    // get new path in server
                    string[] pathName3 = reNameInfo[1].Split('\\');
                    int m = pathName3.Count();
                    string newPathInSyncFolderPath = "";
                    for (int n = pathName.Count(); n < m; n++)
                    {
                        newPathInSyncFolderPath += pathName3[n];
                        if ((n + 1) < m)
                        {
                            newPathInSyncFolderPath += "\\";
                        }
                    }

                    // send event type and new path to server
                    additionalInfo = "rename" + "|||" + newPathInSyncFolderPath;

                    // only change last modified time to current time
                    // Windows 8 won't change last modified time when only renaming
                    Client.LocalFileSysAccess.getFileAttributes att = new Client.LocalFileSysAccess.getFileAttributes(reNameInfo[1]);
                    DateTime renameTime = DateTime.UtcNow;

                    string msg;
                    string md5r = att.md5Value;

                    // update the renamed file to the file list
                    Client.LocalFileSysAccess.FileListMaintain renameFileList = new Client.LocalFileSysAccess.FileListMaintain();

                    // get value by key
                    Client.LocalFileSysAccess.FileInfo tmpFI = new Client.LocalFileSysAccess.FileInfo();
                    Client.LocalFileSysAccess.FileList.fileInfoDic.TryGetValue(fullpathOfChnagedFile, out tmpFI);

                    // remove older file from file list
                    renameFileList.removeSingleFileFromFileList(fullpathOfChnagedFile, tmpFI.time, tmpFI.md5r);
                    // add new name file to file list
                    renameFileList.addSingleFileToFileList(reNameInfo[1]);

                    // create the msg
                    // pathInSyncFolderPath is the older path in server
                    // renameTime is the newest time
                    msg = uploadM.uploadMsg(userName, password, pathInSyncFolderPath, renameTime, md5r, additionalInfo);

                    //send the msg using socket
                    ConnectionManager.Connection conn = new ConnectionManager.Connection();
                    Socket soc = conn.connect(conf.serverAddr, conf.port);

                    SocketCommunication.ReaderWriter rw = new SocketCommunication.ReaderWriter();
                    rw.writetoSocket(soc, msg);

                    //receive the msg
                    //string resp = rw.readfromSocket(soc);

                    Program.ClientForm.ballon("Rename:" + fullpathOfChnagedFile);

                }

            }
            catch (Exception ex)
            {
                if (eventType != "delete")
                {
                    fileBeingUsed.eventDetails e = new fileBeingUsed.eventDetails();
                    e.eventType = eventType;
                    e.datetime = timestamp;
                    e.filepath = fullpathOfChnagedFile;
                    Client.Program.filesInUse.removefromList(e);

                }
                Program.ClientForm.addtoConsole("Exception Occured:" + ex.ToString());
                //System.Windows.Forms.MessageBox.Show(ex.ToString(), "Client");
                //System.IO.File.WriteAllText("errors.txt", e.ToString());
            }
            finally
            {
                if (eventType != "delete")
                {
                    fileBeingUsed.eventDetails e = new fileBeingUsed.eventDetails();
                    e.eventType = eventType;
                    e.datetime = timestamp;
                    e.filepath = fullpathOfChnagedFile;
                    Client.Program.filesInUse.removefromList(e);
                }
                Program.ClientForm.addtoConsole("Exiting");
                Thread.CurrentThread.Abort();
            }
        }
예제 #5
0
파일: Poll.cs 프로젝트: hangmiao/DBLike
        private string localSyncFolderPath()
        {
            LocalDB readLocalDB = new LocalDB();
            readLocalDB=readLocalDB.readfromfile();

            return readLocalDB.getPath();
        }
예제 #6
0
파일: Form1.cs 프로젝트: hangmiao/DBLike
        private void btnCreateAcctb2_Click(object sender, EventArgs e)
        {
            Appendconsole("Initializing Account creation");
            btnCreateAcctb2.Enabled = false;
            btnCreateAcctb2.Text = "Processing...";
            txtUserNametb1.Enabled = false;
            txtPasstb1.Enabled = false;
            btnSignintb1.Enabled = false;
            btnSignintb1.Text = "Sign In";
            button2.Enabled = false;
            button2.Text = "Sign Off";
            vcbtn.Enabled = false;
            vcbtn.Text = "Version Control";
            txtusernametb2.Enabled = true;
            txtpasstb2.Enabled = true;
            txtfoldertb2.Enabled = true;
            btnBrowsetb2.Enabled = false;
            pictureBox1.Visible = true;
            /*
            if (acountcreated == true)
            {
                //MessageBox.Show("Already Account Created.", "DBLike Client");
                Appendconsole("Error : <<Already Account Created>>");
                Appendconsole("Exiting");
                signUpfailed();
                return;
            }
             */
            if (txtfoldertb2.Text.Equals("") || txtusernametb2.Text.Equals("") || txtpasstb2.Text.Equals(""))
            {
                //MessageBox.Show("All fields are Required.", "Information Missing");
                Appendconsole("Error : <<All fields are Required>>");
                Appendconsole("Exiting");
                signupfail();
                return;
            }
            else
            {
                //MessageBox.Show("Now, DBLike service will start and will automatically upload the content of your selected folder. Creating and Modifying folder content will Upload the file to Server.", "DBLike Client Information");
            }
            if(!Directory.Exists(txtfoldertb2.Text))
            {
                addtoConsole("Error : <<Given Folder does not exists>>");
                addtoConsole("Exiting");
                signupfail();
                return;
            }
            Threads.SignUp signupthread = new Threads.SignUp();
            LocalDbAccess.LocalDB file = new LocalDbAccess.LocalDB();
            String username = txtusernametb2.Text;
            String password = txtpasstb2.Text;
            //System.Windows.Forms.MessageBox.Show(username.Length.ToString());

            bool startWithLetterNum = false;
            bool definedLength = false;
            bool noUpperCase = false;
            bool notZforCoonection = false;
            bool noSpecialChar = false;
            bool hasSpecificFormat = false;
            if (username != "zforconnection")
                notZforCoonection = true;
            if (username.Length > 3 && username.Length < 63)
                definedLength = true;
            if (!username.Any(Char.IsUpper))
                noUpperCase = true;
            if (Char.IsLetter(username[0]) || Char.IsNumber(username[0]))
                startWithLetterNum = true;
            Regex r = new Regex("^[a-zA-Z0-9-]*$");
            if (r.IsMatch(username))
                noSpecialChar = true;

            if (username.Contains("-"))
            {
                for (int i = 0; i < username.Length; i++)
                {
                    if (username[i].ToString() == "-")
                    {
                        if (i != 0 && i != username.Length - 1)
                        {
                            if ((Char.IsLetter(username[i - 1]) || Char.IsNumber(username[i - 1]))
                                && (Char.IsLetter(username[i + 1]) || Char.IsNumber(username[i + 1])))
                            {
                                hasSpecificFormat = true;
                            }
                            else
                            {
                                hasSpecificFormat = false;
                                goto Validation;
                            }
                        }
                        else if (i == username.Length - 1)
                        {
                            if (Char.IsLetter(username[i - 1]) || Char.IsNumber(username[i - 1]))
                            {
                                hasSpecificFormat = true;
                            }
                            else
                            {
                                hasSpecificFormat = false;
                                goto Validation;
                            }
                        }
                    }
                }
            }
            else
                hasSpecificFormat = true;

            Validation:
            {
                if (startWithLetterNum != true)
                {
                    addtoConsole("Error : <<Username must start with a Character or a Number only>>");
                    //label7.Text = "Username must start with a Character or a Number only";
                    //label7.Enabled = true;
                }
                else if (definedLength != true)
                {
                    addtoConsole("Error : <<Username must be atleast of length 3 and atmost oflength 63>>");
                    //label7.Text = "Username must be atleast of length 3 and atmost oflength 63";
                    //label7.Enabled = true;
                }
                else if (noUpperCase != true)
                {
                    addtoConsole("Error : <<Username cannot have any uppercase characters>>");
                    //label7.Text = "Username cannot have any uppercase characters";
                    //label7.Enabled = true;
                }
                else if (notZforCoonection != true)
                {
                    addtoConsole("Error : <<Username provided is invalid, please choose another username>>");
                    //label7.Text = "Username provided is invalid, please choose another username";
                    //label7.Enabled = true;
                }
                else if (noSpecialChar != true)
                {
                    addtoConsole("Error : <<Username cannot contain any special characters except '-'>>");
                    //label7.Text = "Username cannot contain any special characters except '-'";
                    //label7.Enabled = true;
                }
                else if (hasSpecificFormat != true)
                {
                    addtoConsole("Error : <<Cannot have consecutive '-' characters>>");
                    //label7.Text = "Cannot have consecutive '-' characters";
                    //label7.Enabled = true;
                }
                else
                {
                    addtoConsole("Success : Username Validated");
                    //label7.Text = "Username Validated";
                    //label7.ForeColor = System.Drawing.Color.Green;
                    //label7.Enabled = true;
                    signupthread.start(username, password, txtfoldertb2.Text);
                }
                //addtoConsole("Exiting");
                //signupfail();
            }
        }