Example #1
0
        public void UpdateRequestServer_ExistingIsXA1UpdateRequestXB1_PatchXA1B1()
        {
            const string memberIdA        = "1";
            const string memberIdB        = "2";
            const string documentId       = "MyDoc";
            const string initialContent   = "test";
            const string contentA1        = "tests";
            const string contentB1        = "testi";
            const string resultingContent = "testsi";
            const string owner            = "max";
            const string host             = "http://localhost:9000";
            SHA1         sha1             = new SHA1CryptoServiceProvider();

            byte[] hash          = sha1.ComputeHash(Encoding.UTF8.GetBytes(initialContent));
            var    diffMatchPath = new diff_match_patch();
            var    editor        = MockRepository.GenerateStub <SharedTextEditor>();

            editor.Stub(x => x.GetText(documentId)).Return(initialContent).Repeat.Once();
            editor.Stub(x => x.GetText(documentId)).Return(contentA1).Repeat.Once();
            var communication = MockRepository.GenerateStub <IClientServerCommunication>();

            //act
            var logic = new SharedTextEditorPatchingLogic(owner, host, editor, communication);

            editor.Raise(x => x.FindDocumentRequest += null, editor, documentId);
            logic.OpenDocument(new DocumentDto
            {
                DocumentId = documentId,
                Content    = initialContent,
                Owner      = owner,
                OwnerHost  = host,
                RevisionId = 1
            });

            logic.UpdateRequest(new UpdateDto
            {
                DocumentId         = documentId,
                MemberName         = memberIdA,
                PreviousRevisionId = 1,
                PreviousHash       = hash,
                Patch = diffMatchPath.patch_make(initialContent, contentA1)
            });

            logic.UpdateRequest(new UpdateDto
            {
                DocumentId         = documentId,
                MemberName         = memberIdB,
                PreviousRevisionId = 1,
                PreviousHash       = hash,
                Patch = diffMatchPath.patch_make(initialContent, contentB1)
            });


            //assert
            var args = editor.GetArgumentsForCallsMadeOn(x => x.UpdateText(null, null), x => x.IgnoreArguments());

            Assert.That(args[0][1], Is.EqualTo(initialContent));
            Assert.That(args[1][1], Is.EqualTo(contentA1));
            Assert.That(args[2][1], Is.EqualTo(resultingContent));
        }
Example #2
0
        private static bool StartServerHost(int port,  string memberName, SharedTextEditor editor, SharedTextEditorPatchingLogic patchingLogic)
        {
            var serviceHost = ServiceHostAddress(port);
            var serviceUrl = new Uri(serviceHost);

            var serviceAddress = ServiceHostEndpoint(port);

            var host = new ServiceHost(patchingLogic, serviceUrl);

                host.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;
                host.AddServiceEndpoint(typeof(ISharedTextEditorC2S), new BasicHttpBinding(), serviceAddress);
                host.OpenTimeout = new TimeSpan(10000);

                host.Closed += (sender, args) =>
                {
                    Console.WriteLine("host closed");
                };

                host.Faulted += (sender, args) =>
                {
                    Console.WriteLine("host faulted");
                };

            try
            {
                host.Open();

                Console.WriteLine("Service up and running at:");
                foreach (var ea in host.Description.Endpoints)
                {
                    Console.WriteLine(ea.Address);
                }

                return true;
            }
            catch (AddressAlreadyInUseException)
            {
                return false;
            }
            catch (AddressAccessDeniedException)
            {
                MessageBox.Show(
                "Unable to use port " + port + ", please allow access to ports between 9000 and 9010.",
                "No port available",
                  MessageBoxButtons.RetryCancel,
                  MessageBoxIcon.Warning);

                return false;
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            var memberName = GetMemberName(args);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var editor = new SharedTextEditor(memberName);


            bool hostOpen;
            var  portRetries = 0;

            do
            {
                var serverPort          = GetRandomPortForServer();
                var patchingClientLogic = new SharedTextEditorPatchingLogic(memberName, ServiceHostEndpoint(serverPort), editor, new ClientServerCommunication());

                hostOpen = StartServerHost(serverPort, memberName, editor, patchingClientLogic);

                if (hostOpen)
                {
                    new SharedTextEditorP2PLogic(memberName, editor, patchingClientLogic, ServiceHostEndpoint(serverPort));
                }

                portRetries++;
            } while (!hostOpen && portRetries < 10);


            if (!hostOpen)
            {
                MessageBox.Show(
                    "Unable to find open port to start service host",
                    "No port available",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);

                Application.Exit();
            }

            Application.Run(editor);
        }
Example #4
0
        static void Main(string[] args)
        {
            var memberName = GetMemberName(args);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var editor = new SharedTextEditor(memberName);

            bool hostOpen;
            var portRetries = 0;
            do
            {
                var serverPort = GetRandomPortForServer();
                var patchingClientLogic = new SharedTextEditorPatchingLogic(memberName, ServiceHostEndpoint(serverPort), editor, new ClientServerCommunication());

                hostOpen = StartServerHost(serverPort, memberName, editor, patchingClientLogic);

                if (hostOpen)
                {
                    new SharedTextEditorP2PLogic(memberName, editor, patchingClientLogic, ServiceHostEndpoint(serverPort));
                }

                portRetries++;
            } while (!hostOpen && portRetries < 10);

            if (!hostOpen)
            {
                MessageBox.Show(
                  "Unable to find open port to start service host",
                  "No port available",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);

                Application.Exit();
            }

            Application.Run(editor);
        }
        public void UpdateRequestServer_ExistingIsXA1B1A2UpdateRequestXC1_PatchXA1B1A2()
        {
            const string memberIdA = "1";
            const string memberIdB = "2";
            const string memberIdC = "3";
            const string documentId = "MyDoc";
            const string initialContent = "test";
            const string contentA1 = "tests";
            const string contentA2 = "testst";
            const string contentB1 = "testi";
            const string contentC1 = "test ";
            const string contentA1B1 = "testsi";
            const string contentA1B1A2 = "teststi";
            const string resultingContent = "teststi ";
            const string owner = "max";
            const string host = "http://localhost:9000";
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] initialHash = sha1.ComputeHash(Encoding.UTF8.GetBytes(initialContent));
            byte[] hashA1 = sha1.ComputeHash(Encoding.UTF8.GetBytes(contentA1));
            var diffMatchPath = new diff_match_patch();
            var editor = MockRepository.GenerateStub<SharedTextEditor>();
            editor.Stub(x => x.GetText(documentId)).Return(initialContent).Repeat.Once();
            editor.Stub(x => x.GetText(documentId)).Return(contentA1).Repeat.Once();
            editor.Stub(x => x.GetText(documentId)).Return(contentA1B1).Repeat.Once();
            editor.Stub(x => x.GetText(documentId)).Return(contentA1B1A2).Repeat.Once();
            var communication = MockRepository.GenerateStub<IClientServerCommunication>();

            //act
            var logic = new SharedTextEditorPatchingLogic(owner, host, editor, communication);

            editor.Raise(x => x.FindDocumentRequest += null, editor, documentId);
            logic.OpenDocument(new DocumentDto
            {
                DocumentId = documentId,
                Content = initialContent,
                Owner = owner,
                OwnerHost = host,
                RevisionId = 1
            });

            logic.UpdateRequest(new UpdateDto
            {
                DocumentId = documentId,
                MemberName = memberIdA,
                PreviousHash = initialHash,
                PreviousRevisionId = 1,
                Patch = diffMatchPath.patch_make(initialContent, contentA1)
            });

            logic.UpdateRequest(new UpdateDto
            {
                DocumentId = documentId,
                MemberName = memberIdB,
                PreviousHash = initialHash,
                PreviousRevisionId = 1,
                Patch = diffMatchPath.patch_make(initialContent, contentB1)
            });

            logic.UpdateRequest(new UpdateDto
            {
                DocumentId = documentId,
                MemberName = memberIdA,
                PreviousHash = hashA1,
                PreviousRevisionId = 2,
                Patch = diffMatchPath.patch_make(contentA1, contentA2)
            });

            logic.UpdateRequest(new UpdateDto
            {
                DocumentId = documentId,
                MemberName = memberIdC,
                PreviousHash = initialHash,
                PreviousRevisionId = 1,
                Patch = diffMatchPath.patch_make(initialContent, contentC1)
            });

            //assert
            var args = editor.GetArgumentsForCallsMadeOn(x => x.UpdateText(null, null), x => x.IgnoreArguments());
            Assert.That(args[0][1], Is.EqualTo(initialContent));
            Assert.That(args[1][1], Is.EqualTo(contentA1));
            Assert.That(args[2][1], Is.EqualTo(contentA1B1));
            Assert.That(args[3][1], Is.EqualTo(contentA1B1A2));
            Assert.That(args[4][1], Is.EqualTo(resultingContent));
        }
Example #6
0
        private static bool StartServerHost(int port, string memberName, SharedTextEditor editor, SharedTextEditorPatchingLogic patchingLogic)
        {
            var serviceHost = ServiceHostAddress(port);
            var serviceUrl  = new Uri(serviceHost);

            var serviceAddress = ServiceHostEndpoint(port);

            var host = new ServiceHost(patchingLogic, serviceUrl);

            host.Description.Behaviors.Find <ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;
            host.AddServiceEndpoint(typeof(ISharedTextEditorC2S), new BasicHttpBinding(), serviceAddress);
            host.OpenTimeout = new TimeSpan(10000);

            host.Closed += (sender, args) =>
            {
                Console.WriteLine("host closed");
            };

            host.Faulted += (sender, args) =>
            {
                Console.WriteLine("host faulted");
            };


            try
            {
                host.Open();

                Console.WriteLine("Service up and running at:");
                foreach (var ea in host.Description.Endpoints)
                {
                    Console.WriteLine(ea.Address);
                }

                return(true);
            }
            catch (AddressAlreadyInUseException)
            {
                return(false);
            }
            catch (AddressAccessDeniedException)
            {
                MessageBox.Show(
                    "Unable to use port " + port + ", please allow access to ports between 9000 and 9010.",
                    "No port available",
                    MessageBoxButtons.RetryCancel,
                    MessageBoxIcon.Warning);

                return(false);
            }
        }