コード例 #1
0
        public void Test()
        {
            InvitationRequest request = new InvitationRequest(1111);
            this.TestFactory(request);

            InvitationAck ack = new InvitationAck(2222);
            this.TestFactory(ack);

            byte[] data = { 12, 34, 56 };
            FileDataResponse dataResponse = new FileDataResponse(data, 3333);
            this.TestFactory(dataResponse);

            FileEvent e1 = new FileEvent();
            e1.Name = "1234";
            List<FileEvent> lf = new List<FileEvent>();
            lf.Add(e1);
            PatchRequest patchRequest = new PatchRequest(lf, 4444);
            this.TestFactory(patchRequest);

            PeerList peerList = PeerList.GetPeerList("abc");
            peerList.AddPeer(new Peer("127.0.0.1", 5555));
            PeerListMessage pm = new PeerListMessage(peerList, 6666);
            this.TestFactory(pm);

            SyncAck syncAck = new SyncAck(7777);
            this.TestFactory(syncAck);

            SyncRequest syncRequest = new SyncRequest(8888);
            this.TestFactory(syncRequest);

            File.WriteAllText(".Distribox/VersionList.txt", "[]");
            VersionList vl = new VersionList();
            VersionListMessage vm = new VersionListMessage(vl, 9999);
            this.TestFactory(vm);
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: hoangvv1409/codebase
        private string GetTempFile(FileEvent e)
        {
            AbsolutePath tmpPath = new AbsolutePath(Path.GetTempPath() + CommonHelper.GetRandomHash());
            Directory.CreateDirectory(tmpPath);

            string name = e.Name.Substring(e.Name.IndexOf("/") + 1, e.Name.Length - e.Name.IndexOf("/") - 1);
            if (e.SHA1 == null)
            {
                File.WriteAllText(tmpPath.File(name), "");
            }
            else
            {
                File.Copy(Config.MetaFolderData.File(e.SHA1), tmpPath.File(name));
            }

            return tmpPath.File(name);
        }
コード例 #3
0
        public void BasicFlow()
        {
            RequestManager manager = new RequestManager();

            FileEvent e1 = new FileEvent();
            e1.EventId = "1";
            e1.Size = 5 * 1024;
            FileEvent e2 = new FileEvent();
            e2.EventId = "2";
            Peer p1 = new Peer();
            p1.Port = 1111;
            Peer p2 = new Peer();
            p2.Port = 2222;

            // No request yet
            Assert.IsNull(manager.GetRequests());

            // Add a request
            List<FileEvent> l1 = new List<FileEvent>();
            l1.Add(e1);
            manager.AddRequests(l1, p1);

            // Check
            var tlp = manager.GetRequests();
            Assert.IsNotNull(tlp);
            Assert.IsTrue(Enumerable.SequenceEqual(tlp.Item1, l1));
            Assert.AreEqual(tlp.Item2, p1);

            // No request
            Assert.IsNull(manager.GetRequests());

            // The request fails
            Thread.Sleep(3000);

            // Should be available again
            tlp = manager.GetRequests();

            // The request finished
            manager.FinishRequests(l1);
        }
コード例 #4
0
        public void SpeedHeuristics()
        {
            RequestManager manager = new RequestManager();

            FileEvent e1 = new FileEvent();
            e1.EventId = "1";
            e1.Size = 4 * 1024 * 1024;
            FileEvent e2 = new FileEvent();
            e2.EventId = "2";
            e2.Size = 4 * 1024;
            Peer p1 = new Peer();
            p1.Port = 1111;
            Peer p2 = new Peer();
            p2.Port = 2222;
            List<FileEvent> l1 = new List<FileEvent>();
            l1.Add(e1);
            List<FileEvent> l2 = new List<FileEvent>();
            l2.Add(e2);

            // Fast peer
            manager.AddRequests(l1, p1);
            manager.GetRequests();
            manager.FinishRequests(l1);

            // Slow peer
            manager.AddRequests(l2, p2);
            manager.GetRequests();
            Thread.Sleep(3);
            manager.FinishRequests(l2);

            // Add two files
            manager.AddRequests(l1, p2);
            manager.AddRequests(l2, p1);

            // Should be fast peer
            var tlp = manager.GetRequests();
            Assert.AreEqual(p1, tlp.Item2);
        }
コード例 #5
0
        public void UniquenessHeuristics()
        {
            RequestManager manager = new RequestManager();

            FileEvent e1 = new FileEvent();
            e1.EventId = "1";
            e1.Size = 8 * 1024;
            FileEvent e2 = new FileEvent();
            e2.EventId = "2";
            e2.Size = 4 * 1024;
            Peer p1 = new Peer();
            p1.Port = 1111;
            Peer p2 = new Peer();
            p2.Port = 2222;
            Peer p3 = new Peer();
            p3.Port = 3333;
            List<FileEvent> l1 = new List<FileEvent>();
            l1.Add(e1);
            List<FileEvent> l2 = new List<FileEvent>();
            l2.Add(e2);

            // Many user have l1
            manager.AddRequests(l1, p1);
            manager.AddRequests(l1, p2);

            // Few user have l2
            manager.AddRequests(l2, p3);

            // Should be l2
            var tlp = manager.GetRequests();
        }
コード例 #6
0
ファイル: Form1.cs プロジェクト: hoangvv1409/codebase
        void visualTree1_NodeDoubleClick(FileEvent e)
        {
            string file = GetTempFile(e);

            this.Command("start", file);
        }
コード例 #7
0
ファイル: Form1.cs プロジェクト: hoangvv1409/codebase
 void visualTree1_NodeClick(FileEvent e)
 {
     string text = "";
     if (e.SHA1 != null)
     {
         text = File.ReadAllText(Config.MetaFolderData.File(e.SHA1));
     }
     this.textBox1.Text = text;
 }
コード例 #8
0
ファイル: Tree.cs プロジェクト: hoangvv1409/codebase
 public Tree(FileEvent e)
 {
     this.Event = e;
     this.Children = new List<Tree>();
 }
コード例 #9
0
ファイル: FileItemTest.cs プロジェクト: hoangvv1409/codebase
        public void MergeTest()
        {
            // File Test
            FileItem file = new FileItem();

            {
                FileEvent e = new FileEvent();
                e.Type = FileEventType.Created;
                e.Name = "haha";
                e.SHA1 = "11";
                e.When = DateTime.FromFileTimeUtc(1);
                file.Merge(e);
            }

            {
                FileEvent e = new FileEvent();
                e.Type = FileEventType.Created;
                e.Name = "haha";
                e.SHA1 = "11";
                e.When = DateTime.FromFileTimeUtc(2);
                file.Merge(e);
            }

            {
                FileEvent e = new FileEvent();
                e.Type = FileEventType.Created;
                e.Name = "haha";
                e.SHA1 = null;
                e.When = DateTime.FromFileTimeUtc(3);
                file.Merge(e);
            }

            {
                FileEvent e = new FileEvent();
                e.Type = FileEventType.Renamed;
                e.Name = "haha";
                e.SHA1 = null;
                e.When = DateTime.FromFileTimeUtc(4);
                file.Merge(e);
            }

            {
                FileEvent e = new FileEvent();
                e.Type = FileEventType.Changed;
                e.Name = "haha";
                e.SHA1 = "11";
                e.When = DateTime.FromFileTimeUtc(5);
                file.Merge(e);
            }

            {
                FileEvent e = new FileEvent();
                e.Type = FileEventType.Deleted;
                e.Name = "haha";
                e.SHA1 = "11";
                e.When = DateTime.FromFileTimeUtc(6);
                file.Merge(e);
            }

            FileItem dir = new FileItem();

            {
                FileEvent e = new FileEvent();
                e.Type = FileEventType.Created;
                e.IsDirectory = true;
                e.Name = "dir";
                e.When = DateTime.FromFileTimeUtc(1);
                dir.Merge(e);
            }

            {
                FileEvent e = new FileEvent();
                e.Type = FileEventType.Created;
                e.IsDirectory = true;
                e.Name = "dir";
                e.When = DateTime.FromFileTimeUtc(2);
                dir.Merge(e);
            }

            {
                FileEvent e = new FileEvent();
                e.Type = FileEventType.Created;
                e.IsDirectory = true;
                e.Name = "dir";
                e.When = DateTime.FromFileTimeUtc(3);
                dir.Merge(e);
            }

            {
                FileEvent e = new FileEvent();
                e.Type = FileEventType.Changed;
                e.IsDirectory = true;
                e.Name = "dir";
                e.When = DateTime.FromFileTimeUtc(4);
                dir.Merge(e);
            }

            {
                FileEvent e = new FileEvent();
                e.Type = FileEventType.Renamed;
                e.IsDirectory = true;
                e.Name = "dir2";
                e.When = DateTime.FromFileTimeUtc(5);
                dir.Merge(e);
            }

            {
                FileEvent e = new FileEvent();
                e.Type = FileEventType.Deleted;
                e.IsDirectory = true;
                e.Name = "dir2";
                e.When = DateTime.FromFileTimeUtc(6);
                dir.Merge(e);
            }
        }
コード例 #10
0
        /// <summary>
        /// Merge the specified event.
        /// </summary>
        /// <param name="vs">The event.</param>
        public void Merge(FileEvent vs)
        {
            if (this.History.Count() == 0)
            {
                this.Id          = vs.FileId;
                this.IsDirectory = vs.IsDirectory;

                this.PushHistory(vs);
                if (this.IsDirectory)
                {
                    switch (vs.Type)
                    {
                    case FileEventType.Created:
                        GlobalFlag.AcceptFileEvent = false;
                        Directory.CreateDirectory(Config.RootFolder.Enter(vs.Name));
                        GlobalFlag.AcceptFileEvent = true;
                        break;

                    default:
                        Debug.Assert(false, "Not implement!");
                        break;
                    }
                }
                else
                {
                    switch (vs.Type)
                    {
                    case FileEventType.Created:
                        if (vs.SHA1 == null)
                        {
                            GlobalFlag.AcceptFileEvent = false;
                            File.WriteAllText(Config.RootFolder.File(vs.Name), string.Empty);
                            GlobalFlag.AcceptFileEvent = true;
                        }
                        else
                        {
                            GlobalFlag.AcceptFileEvent = false;
                            File.Copy(Config.MetaFolderData.File(vs.SHA1), Config.RootFolder.File(vs.Name), true);
                            GlobalFlag.AcceptFileEvent = true;
                        }

                        break;

                    default:
                        Debug.Assert(false, "Not implement!");
                        break;
                    }
                }

                return;
            }

            FileEvent last = this.History.Last();

            this.PushHistory(vs);

            // Display changes
            if (vs.When.Ticks > last.When.Ticks)
            {
                if (this.IsDirectory)
                {
                    switch (vs.Type)
                    {
                    case FileEventType.Created:
                        GlobalFlag.AcceptFileEvent = false;
                        Directory.CreateDirectory(Config.RootFolder.Enter(vs.Name));
                        GlobalFlag.AcceptFileEvent = true;
                        break;

                    case FileEventType.Changed:
                        break;

                    case FileEventType.Renamed:
                        GlobalFlag.AcceptFileEvent = false;
                        Directory.Move(Config.RootFolder.Enter(last.Name), Config.RootFolder.Enter(vs.Name));
                        GlobalFlag.AcceptFileEvent = true;
                        break;

                    case FileEventType.Deleted:
                        GlobalFlag.AcceptFileEvent = false;
                        Directory.Delete(Config.RootFolder.Enter(vs.Name));
                        GlobalFlag.AcceptFileEvent = true;
                        break;
                    }
                }
                else
                {
                    switch (vs.Type)
                    {
                    case FileEventType.Created:
                        if (vs.SHA1 == null)
                        {
                            GlobalFlag.AcceptFileEvent = false;
                            File.WriteAllText(Config.RootFolder.File(vs.Name), string.Empty);
                            GlobalFlag.AcceptFileEvent = true;
                        }
                        else
                        {
                            GlobalFlag.AcceptFileEvent = false;
                            File.Copy(Config.MetaFolderData.File(vs.SHA1), Config.RootFolder.File(vs.Name), true);
                            GlobalFlag.AcceptFileEvent = true;
                        }

                        break;

                    case FileEventType.Changed:
                        if (vs.SHA1 == null)
                        {
                            GlobalFlag.AcceptFileEvent = false;
                            File.WriteAllText(Config.RootFolder.File(vs.Name), string.Empty);
                            GlobalFlag.AcceptFileEvent = true;
                        }
                        else
                        {
                            GlobalFlag.AcceptFileEvent = false;
                            File.Copy(Config.MetaFolderData.File(vs.SHA1), Config.RootFolder.File(vs.Name), true);
                            GlobalFlag.AcceptFileEvent = true;
                        }

                        break;

                    case FileEventType.Renamed:
                        GlobalFlag.AcceptFileEvent = false;
                        File.Move(Config.RootFolder.File(last.Name), Config.RootFolder.File(vs.Name));
                        GlobalFlag.AcceptFileEvent = true;
                        break;

                    case FileEventType.Deleted:
                        GlobalFlag.AcceptFileEvent = false;
                        File.Delete(Config.RootFolder.File(vs.Name));
                        GlobalFlag.AcceptFileEvent = true;
                        break;
                    }
                }
            }
        }
コード例 #11
0
ファイル: FileItem.cs プロジェクト: hoangvv1409/codebase
 /// <summary>
 /// Push the history.
 /// </summary>
 /// <param name="e">The event.</param>
 private void PushHistory(FileEvent e)
 {
     this.History.Add(e);
     this.History.Sort((x, y) => x.When.Ticks.CompareTo(y.When.Ticks));
     this.CurrentEventId = e.EventId;
 }
コード例 #12
0
ファイル: FileItem.cs プロジェクト: hoangvv1409/codebase
        /// <summary>
        /// Create a version of renaming
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="when">The when.</param>
        public void Rename(string name, DateTime when)
        {
            FileEvent vs = new FileEvent();
            vs.FileId = this.Id;
            vs.EventId = CommonHelper.GetRandomHash();
            vs.ParentId = this.CurrentParentId;
            vs.IsDirectory = this.IsDirectory;
            vs.Name = name;
            vs.When = when;
            vs.SHA1 = this.CurrentSHA1;
            vs.Size = this.CurrentSize;
            vs.Type = FileEventType.Renamed;

            this.PushHistory(vs);
        }
コード例 #13
0
ファイル: FileItem.cs プロジェクト: hoangvv1409/codebase
        /// <summary>
        /// Merge the specified event.
        /// </summary>
        /// <param name="vs">The event.</param>
        public void Merge(FileEvent vs)
        {
            if (this.History.Count() == 0)
            {
                this.Id = vs.FileId;
                this.IsDirectory = vs.IsDirectory;

                this.PushHistory(vs);
                if (this.IsDirectory)
                {
                    switch (vs.Type)
                    {
                        case FileEventType.Created:
                            GlobalFlag.AcceptFileEvent = false;
                            Directory.CreateDirectory(Config.RootFolder.Enter(vs.Name));
                            GlobalFlag.AcceptFileEvent = true;
                            break;
                        default:
                            Debug.Assert(false, "Not implement!");
                            break;
                    }
                }
                else
                {
                    switch (vs.Type)
                    {
                        case FileEventType.Created:
                            if (vs.SHA1 == null)
                            {
                                GlobalFlag.AcceptFileEvent = false;
                                File.WriteAllText(Config.RootFolder.File(vs.Name), string.Empty);
                                GlobalFlag.AcceptFileEvent = true;
                            }
                            else
                            {
                                GlobalFlag.AcceptFileEvent = false;
                                File.Copy(Config.MetaFolderData.File(vs.SHA1), Config.RootFolder.File(vs.Name), true);
                                GlobalFlag.AcceptFileEvent = true;
                            }

                            break;
                        default:
                            Debug.Assert(false, "Not implement!");
                            break;
                    }
                }

                return;
            }

            FileEvent last = this.History.Last();
            this.PushHistory(vs);

            // Display changes
            if (vs.When.Ticks > last.When.Ticks)
            {
                if (this.IsDirectory)
                {
                    switch (vs.Type)
                    {
                        case FileEventType.Created:
                            GlobalFlag.AcceptFileEvent = false;
                            Directory.CreateDirectory(Config.RootFolder.Enter(vs.Name));
                                GlobalFlag.AcceptFileEvent = true;
                            break;
                        case FileEventType.Changed:
                            break;
                        case FileEventType.Renamed:
                            GlobalFlag.AcceptFileEvent = false;
                            Directory.Move(Config.RootFolder.Enter(last.Name), Config.RootFolder.Enter(vs.Name));
                            GlobalFlag.AcceptFileEvent = true;
                            break;
                        case FileEventType.Deleted:
                            GlobalFlag.AcceptFileEvent = false;
                            Directory.Delete(Config.RootFolder.Enter(vs.Name));
                            GlobalFlag.AcceptFileEvent = true;
                            break;
                    }
                }
                else
                {
                    switch (vs.Type)
                    {
                        case FileEventType.Created:
                            if (vs.SHA1 == null)
                            {
                                GlobalFlag.AcceptFileEvent = false;
                                File.WriteAllText(Config.RootFolder.File(vs.Name), string.Empty);
                                GlobalFlag.AcceptFileEvent = true;
                            }
                            else
                            {
                                GlobalFlag.AcceptFileEvent = false;
                                File.Copy(Config.MetaFolderData.File(vs.SHA1), Config.RootFolder.File(vs.Name), true);
                                GlobalFlag.AcceptFileEvent = true;
                            }

                            break;
                        case FileEventType.Changed:
                            if (vs.SHA1 == null)
                            {
                                GlobalFlag.AcceptFileEvent = false;
                                File.WriteAllText(Config.RootFolder.File(vs.Name), string.Empty);
                                GlobalFlag.AcceptFileEvent = true;
                            }
                            else
                            {
                                GlobalFlag.AcceptFileEvent = false;
                                File.Copy(Config.MetaFolderData.File(vs.SHA1), Config.RootFolder.File(vs.Name), true);
                                GlobalFlag.AcceptFileEvent = true;
                            }

                            break;
                        case FileEventType.Renamed:
                            GlobalFlag.AcceptFileEvent = false;
                            File.Move(Config.RootFolder.File(last.Name), Config.RootFolder.File(vs.Name));
                            GlobalFlag.AcceptFileEvent = true;
                            break;
                        case FileEventType.Deleted:
                            GlobalFlag.AcceptFileEvent = false;
                            File.Delete(Config.RootFolder.File(vs.Name));
                            GlobalFlag.AcceptFileEvent = true;
                            break;
                    }
                }
            }
        }
コード例 #14
0
ファイル: FileItem.cs プロジェクト: hoangvv1409/codebase
        /// <summary>
        /// Create a version of changing.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="sha1">The SHA1.</param>
        /// <param name="when">The when.</param>
        public void Change(string name, string sha1, DateTime when)
        {
            if (this.CurrentSHA1 == sha1)
            {
                return;
            }

            FileEvent vs = new FileEvent();
            vs.FileId = this.Id;
            vs.EventId = CommonHelper.GetRandomHash();
            vs.ParentId = this.CurrentParentId;
            vs.IsDirectory = this.IsDirectory;
            vs.Name = this.CurrentName;
            vs.When = when;
            vs.SHA1 = sha1;
            vs.Size = this.IsDirectory || sha1 == null ? 0 : (new FileInfo(Config.MetaFolderData.File(vs.SHA1))).Length;
            vs.Type = FileEventType.Changed;

            this.PushHistory(vs);
        }
コード例 #15
0
ファイル: FileEvent.cs プロジェクト: hoangvv1409/codebase
 /// <summary>
 /// Creates the event.
 /// </summary>
 /// <returns>The event.</returns>
 public static FileEvent CreateEvent()
 {
     FileEvent e = new FileEvent();
     e.EventId = CommonHelper.GetRandomHash();
     return e;
 }
コード例 #16
0
 /// <summary>
 /// Push the history.
 /// </summary>
 /// <param name="e">The event.</param>
 private void PushHistory(FileEvent e)
 {
     this.History.Add(e);
     this.History.Sort((x, y) => x.When.Ticks.CompareTo(y.When.Ticks));
     this.CurrentEventId = e.EventId;
 }
コード例 #17
0
        /// <summary>
        /// Initializes a new instance of the <see cref="VersionControlTest" /> class.
        /// </summary>
        public VersionControlTest()
        {
            // Initialize
            if (!Directory.Exists(".Distribox"))
            {
                Directory.CreateDirectory(".Distribox");
            }

            if (!Directory.Exists(".Distribox/data"))
            {
                Directory.CreateDirectory(".Distribox/data");

            }
            if (Directory.Exists("Dir"))
            {
                Directory.Delete("Dir");
            }

            FileEvent e1 = new FileEvent();
            e1.Type = FileEventType.Created;
            e1.EventId = "1";
            e1.FileId = "1";
            e1.Name = "haha";

            FileEvent e2 = new FileEvent();
            e2.Type = FileEventType.Changed;
            e2.EventId = "2";
            e2.FileId = "1";
            e2.SHA1 = "11";
            e2.Name = "haha";
            File.WriteAllText(".Distribox/data/11", "haha");

            FileEvent e3 = new FileEvent();
            e3.Type = FileEventType.Renamed;
            e3.EventId = "3";
            e3.FileId = "1";
            e3.SHA1 = "11";
            e3.Name = "XX";

            FileEvent e4 = new FileEvent();
            e4.Type = FileEventType.Deleted;
            e4.EventId = "4";
            e4.FileId = "1";
            e4.Name = "XX";

            FileEvent e5 = new FileEvent();
            e5.Type = FileEventType.Created;
            e5.EventId = "5";
            e5.FileId = "2";
            e5.Name = "TT";

            FileEvent e6 = new FileEvent();
            e6.Type = FileEventType.Changed;
            e6.EventId = "6";
            e6.FileId = "2";
            e6.SHA1 = "11";
            e6.Name = "TT";

            FileEvent e7 = new FileEvent();
            e7.Type = FileEventType.Created;
            e7.EventId = "7";
            e7.FileId = "3";
            e7.Name = "Dir";
            e7.IsDirectory = true;
            e7.When = DateTime.FromFileTimeUtc(7);

            FileItem item1 = new FileItem();
            item1.Merge(e1);
            item1.Merge(e2);
            item1.Merge(e3);
            item1.Merge(e4);

            FileItem item2 = new FileItem();
            item2.Merge(e5);
            item2.Merge(e6);

            FileItem item3 = new FileItem();
            item3.Merge(e7);

            this.list.Add(item1);
            this.list.Add(item2);
            this.list.Add(item3);
        }