Example #1
0
        private void childChanged(string key, ChangeSet data)
        {
            var obj = innerDictionary[key];

            //todo: massive hack
            if (obj is IList <object> )
            {
                data["type"] = new ChangeSetLeaf("Array");
            }
            else
            {
                data["type"] = new ChangeSetLeaf("Object");
            }

            mapSnapshotToObject(obj, data);
        }
        private void ProcessInput(Responses.Response response)
        {
            //todo: process the input to ensure message re-sending

            //Console.WriteLine(data);
            if (response.Data.Action != null)
            {
                ChangeSet changeset = null;
                if (response.Data.Action == "d")
                {
                    //the data here comes as an object
                    JToken token = response.Data.Payload.Data as JToken;
                    if (token == null)
                    {
                        changeset = new ChangeSetLeaf(response.Data.Payload.Data);
                    }
                    else
                    {
                        changeset = ChangeSet.FromJToken(token);
                    }
                }
                else if (response.Data.Action == "m")
                {
                    //the data here comes as a dictionary of path,value
                    JToken token = response.Data.Payload.Data as JToken;


                    changeset = ChangeSet.FromFlatJToken(token);
                }
                else
                {
                }
                //ensure that the changset is not mine.
                Dictionary <string, object> flattennedChangeset = changeset.Flatten();
                //top of the queue should be the next message if it is mine.
                Dictionary <string, object> current = null;

                if (unconfirmedChangesets.TryPeek(out current))
                {
                    if (flattennedChangeset.All(kvp => current.ContainsKey(kvp.Key) && current[kvp.Key].Equals(kvp.Value)))
                    {
                        //My topmost unconfirmed changeset, did this exact modification, this should indicate that it is my echo
                        //   Console.WriteLine("Echo received");
                    }
                    else
                    {
                        //now, what if this is not an echo, and invalidates any changeset that has not been commited yet?
                        var conflicts = unconfirmedChangesets.Where(unconfirmed => flattennedChangeset.Any(kvp => unconfirmed.ContainsKey(kvp.Key) && unconfirmed[kvp.Key] == kvp.Value));
                        if (conflicts.Count() > 0)
                        {
                            //i have no idea what to do here
                            Console.WriteLine("Collision detected");
                        }
                    }
                }
                else
                {
                    //no unconfirmed messages, i think

                    mergeToLocalCache(response.Data.Payload.Path, changeset);

                    handleCallbacks(response.Data.Payload.Path, changeset);
                }
            }
        }