Exemplo n.º 1
0
        public void UpdateTextEditor(PatchMessage message)
        {
            Edit edit = message.Edits.Pop();

            Console.WriteLine($"Server version in the edit = {edit.ServerVersion}");

            Dispatcher.Invoke(() =>
            {
                try
                {
                    string currentText = TextEditor.Text;
                    Console.WriteLine($"Current text = {currentText}");
                    // Update Server Shadow
                    List <Patch> patches = _client.DMP.patch_make(currentText, edit.Diffs);
                    Console.WriteLine("Created patches");
                    string updatedText = _client.DMP.patch_apply(patches, currentText)[0].ToString();
                    Console.WriteLine($"Updated text = {updatedText}");
                    TextEditor.Text        = updatedText;
                    _previousEditorContent = TextEditor.Text;
                }
                catch (ArgumentOutOfRangeException e)
                {
                    Console.WriteLine(e);
                    _previousEditorContent = TextEditor.Text;
                    _client.SendOutOfSyncMessage();
                }
            });


            //_client.Document.ShadowCopy.ServerVersion++;
        }
Exemplo n.º 2
0
        public HttpResult Patch(PatchMessage request)
        {
            if (request.Id <= 0) return new HttpResult(HttpStatusCode.NotFound, "Message was not found.");

            // Only resend e-mail supported by patch at the moment
            if (!request.ResendEmail) return new HttpResult(HttpStatusCode.NotImplemented, "Message PATCH only supports resending e-mail.");

            var ds = new DataStore();
            var message = ds.GetMessage(request.Id);

            var client = new EmailClient();
            var emailResult = client.Send(message);

            if (emailResult.Success)
            {
                ds.InsertMessageStatus(request.Id, 2);
            }
            else
            {
                ds.InsertMessageStatus(request.Id, 3, emailResult.Message, emailResult.SmtpException);
                return new HttpResult(HttpStatusCode.OK, "Message failed to send.");
            }

            return new HttpResult(HttpStatusCode.OK, "Message was updated.");
        }
Exemplo n.º 3
0
        public HttpResult Patch(PatchMessage request)
        {
            if (request.Id <= 0)
            {
                return(new HttpResult(HttpStatusCode.NotFound, "Message was not found."));
            }

            // Only resend e-mail supported by patch at the moment
            if (!request.ResendEmail)
            {
                return(new HttpResult(HttpStatusCode.NotImplemented, "Message PATCH only supports resending e-mail."));
            }

            var ds      = new DataStore();
            var message = ds.GetMessage(request.Id);

            var client      = new EmailClient();
            var emailResult = client.Send(message);

            if (emailResult.Success)
            {
                ds.InsertMessageStatus(request.Id, 2);
            }
            else
            {
                ds.InsertMessageStatus(request.Id, 3, emailResult.Message, emailResult.SmtpException);
                return(new HttpResult(HttpStatusCode.OK, "Message failed to send."));
            }

            return(new HttpResult(HttpStatusCode.OK, "Message was updated."));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a patch from the PatchMessage sent by the client.
        /// Then applies the patch to the ShadowCopy followed by applying the patch to the live server copy.
        /// Then creates new diffs from the live server text and the shadow (which contains the exact copy of latest client message).
        /// These diffs will then be send to the client so that the client now has the latest updates.
        /// </summary>
        /// <param name="message"></param>
        private void HandlePatchMessage(PatchMessage message)
        {
            _edits.Clear();
            Edit edit = message.Edits.Pop();

            Task.Factory.StartNew(() =>
            {
                if (edit.ClientVersion > User.Document.ShadowCopy.ClientVersion || edit.ClientVersion == 0)
                {
                    // Update Server Shadow
                    bool success;
                    List <Patch> patches;
                    try
                    {
                        patches = _dmp.patch_make(User.Document.ShadowCopy.ShadowText, edit.Diffs);
                        User.Document.ShadowCopy.ShadowText =
                            _dmp.patch_apply(patches, User.Document.ShadowCopy.ShadowText)[0].ToString();
                        //User.Document.ShadowCopy.ClientVersion++;
                        User.Document.BackupShadowCopy.BackupText = User.Document.ShadowCopy.ShadowText;
                        success = true;
                    }
                    catch (ArgumentOutOfRangeException e)
                    {
                        Console.WriteLine(e);
                        success = false;
                        Session.BroadCastOutOfSyncResponse(Session.Document.CurrentText);
                    }

                    if (success)
                    {
                        // Update Server Current
                        patches = _dmp.patch_make(Session.Document.CurrentText, edit.Diffs);
                        Session.Document.CurrentText =
                            _dmp.patch_apply(patches, Session.Document.CurrentText)[0].ToString();
                        Console.WriteLine(
                            $"Updated client: {User.Username}, With test: {Session.Document.CurrentText}");
                    }
                }
            }).ContinueWith((result) =>
            {
                List <Diff> diffs = _dmp.diff_main(User.Document.ShadowCopy.ShadowText, Session.Document.CurrentText,
                                                   false);
                _dmp.diff_cleanupEfficiency(diffs);
                _edits.Push(new Edit(diffs, User.Document.ShadowCopy.ClientVersion,
                                     User.Document.ShadowCopy.ServerVersion));
                SendMessage(new PatchMessage("Server", _edits));
                User.Document.ShadowCopy.ShadowText = Session.Document.CurrentText;
                _edits.Clear();
            });
        }
Exemplo n.º 5
0
        private void ProcessOSCMessage(OSCMessage message)
        {
            if (message.Address == "/k/init")
            {
                FSaveTargets.Clear();

                FAutoIP     = IPAddress.Parse((string)message.Values[0]);
                FAutoPort   = (int)message.Values[1];
                FResolution = new Vector2D((int)message.Values[2], (int)message.Values[3]);

                InitNetwork();

                //get initial list of exposed ioboxes
                ReExposeIOBoxes();

                return;
            }
            else if (message.Address == "/k/save")
            {
                //save all savetargets to patch by sending a patchmessage
                //savetargets can be scattered over many patches so potentially this needs to send multiple patchmessages

                FPatchMessages.Clear();
                foreach (var target in FSaveTargets)
                {
                    var          patchClass = target.Key.Split('/');
                    PatchMessage pm;
                    if (FPatchMessages.ContainsKey(patchClass[0]))
                    {
                        pm = FPatchMessages[patchClass[0]];
                    }
                    else
                    {
                        pm = new PatchMessage(patchClass[0]);
                        pm.AddSaveMe();
                        FPatchMessages.Add(patchClass[0], pm);
                    }

                    var node = pm.AddNode(int.Parse(patchClass[1]));
                    var pin  = node.AddPin("Y Input Value");
                    pin.Spread = target.Value;
                }

                foreach (var pm in FPatchMessages)
                {
                    FHDEHost.SendXMLSnippet(pm.Key, pm.Value.ToString(), true);
                }
            }
            else if (message.Address == "/acceleration")
            {
                FAcceleration[0] = new Vector3D((float)message.Values[0], (float)message.Values[1], (float)message.Values[2]);
            }
            else if (message.Address == "/magnetism")
            {
                FMagnetism[0] = new Vector3D((float)message.Values[0], (float)message.Values[1], (float)message.Values[2]);
            }
            else if (message.Address == "/orientation")
            {
                FOrientation[0] = new Vector3D((float)message.Values[0], (float)message.Values[1], (float)message.Values[2]);
            }
            else if (message.Address == "/touches")
            {
                int touchCount = message.Values.Count / 4;
                int t          = 0;
                FTouchID.SliceCount       = touchCount;
                FTouchXY.SliceCount       = touchCount;
                FTouchPressure.SliceCount = touchCount;

                for (int i = 0; i < touchCount; i++)
                {
                    FTouchID[i]       = (int)message.Values[t];
                    FTouchXY[i]       = new Vector2D((float)message.Values[t + 1] / FResolution.x * 2 - 1, (float)message.Values[t + 2] / FResolution.y * -2 + 1);
                    FTouchPressure[i] = (float)message.Values[t + 3];
                    t += 4;
                }
            }
            else if (message.Address.StartsWith("/k/"))
            {
                FAllowUpdates = false;
                FTimer.Start();

                var address = "/" + message.Address.Trim('/', 'k');
                if (FTargets.ContainsKey(address))
                {
                    var pin = FTargets[address].Pin;

                    //var values = string.Join(",", message.Values.ToArray(typeof(string)));
                    var values = "";
                    foreach (var v in message.Values)
                    {
                        values += v.ToString().Replace(',', '.') + ",";
                    }
                    values = values.TrimEnd(',');

                    if (values == "bang")
                    {
                        values = "1";
                        //save pin for sending 0 next frame
                        FBangs.Add(pin);
                    }

                    //save last value sent per path (for optional later saving to patch)
                    if (FSaveTargets.ContainsKey(FTargets[address].SourceNodePath))
                    {
                        FSaveTargets[FTargets[address].SourceNodePath] = values;
                    }
                    else
                    if (!(FTargets[address].Type == "Bang"))
                    {
                        FSaveTargets.Add(FTargets[address].SourceNodePath, values);
                    }

                    pin.Spread = values;
                }
            }
        }
Exemplo n.º 6
0
 private void HandlePatchMessage(PatchMessage message)
 {
     _edits.Clear();
     _editorUpdateCallback(message);
 }
Exemplo n.º 7
0
        void SyncNodeInfos(Document oldDocument, Document newDocument)
        {
            var oldFilePath = oldDocument.FilePath;
            var filePath    = newDocument.FilePath;
            var oldMap      = GetNodeDefinitions(oldDocument).ToDictionary(d => d.Identity);

            foreach (var newDef in GetNodeDefinitions(newDocument))
            {
                var oldDef = oldMap.ValueOrDefault(newDef.Identity);
                if (oldDef == newDef)
                {
                    continue;
                }

                var(oldName, oldCategory, oldVersion) = GetNodeInfoKeys(oldDef ?? newDef);
                var(name, category, version)          = GetNodeInfoKeys(newDef);

                var nodeInfo    = FNodeInfoFactory.CreateNodeInfo(oldName, oldCategory, oldVersion, oldFilePath, beginUpdate: true);
                var oldNodeInfo = nodeInfo;

                // Check if document was moved or the definition renamed.
                if (name != oldName || category != oldCategory || version != oldVersion || filePath != oldFilePath)
                {
                    if (FNodeInfoFactory.ContainsKey(name, category, version, filePath))
                    {
                        // A node info already exists (document already existed). Switch to the existing one (otherwise we'd see double entries).
                        nodeInfo = FNodeInfoFactory.CreateNodeInfo(name, category, version, filePath, beginUpdate: false);
                    }
                    else
                    {
                        // Update the existing node info.
                        FNodeInfoFactory.UpdateNodeInfo(nodeInfo, name, category, version, filePath);
                    }

                    // Point all existing nodes to the updated node info
                    foreach (var node in FHDEHost.RootNode.AsDepthFirstEnumerable())
                    {
                        if (node.NodeInfo == oldNodeInfo)
                        {
                            var patchMessage = new PatchMessage(node.Parent.NodeInfo.Filename);
                            var nodeMessage  = patchMessage.AddNode(node.ID);
                            nodeMessage.CreateMe   = true;
                            nodeMessage.SystemName = nodeInfo.Systemname;
                            nodeMessage.Filename   = nodeInfo.Filename;
                            FHDEHost.SendXMLSnippet(patchMessage.Filename, patchMessage.ToString(), true);
                        }
                    }
                }

                if (nodeInfo == oldNodeInfo)
                {
                    UpdateNodeInfo(newDef, nodeInfo);
                }
                else
                {
                    FNodeInfoFactory.DestroyNodeInfo(oldNodeInfo);
                }
            }

            var newMap = GetNodeDefinitions(newDocument).ToDictionary(d => d.Identity);

            foreach (var oldDef in GetNodeDefinitions(oldDocument))
            {
                if (newMap.ContainsKey(oldDef.Identity))
                {
                    continue;
                }

                var(name, category, version) = GetNodeInfoKeys(oldDef);
                var nodeInfo = FNodeInfoFactory.CreateNodeInfo(name, category, version, filePath, beginUpdate: false);
                if (nodeInfo.Factory == this)
                {
                    FNodeInfoFactory.DestroyNodeInfo(nodeInfo);
                }
            }

            if (filePath != oldFilePath && File.Exists(oldFilePath))
            {
                // The document was saved under a new name but old document still exists on disk.
                // Scan the old document again so we have double entries in the node browser as we'd have after a restart.
                foreach (var info in LoadNodeInfos(oldFilePath))
                {
                    Touch(info);
                }
            }
        }