コード例 #1
0
        private void PreProcessInput(string data)
        {
            int numeric = -1;

            if (int.TryParse(data, out numeric))
            {
                //this means that the next N packages are to be used together
                packages          = numeric;
                currentCount      = 0;
                bigMessageBuilder = new StringBuilder();
            }
            else
            {
                if (packages > 1)
                {
                    bigMessageBuilder.Append(data);
                    currentCount += data.Length;
                    if (currentCount == 16384)
                    {
                        packages--;
                        currentCount = 0;
                    }
                }
                else if (packages == 1)
                {
                    bigMessageBuilder.Append(data);
                    if (data.EndsWith("}"))
                    {
                        packages--;
                    }
                    if (packages == 0)
                    {
                        data = bigMessageBuilder.ToString();
                        bigMessageBuilder = new StringBuilder();
                    }
                }
                if (packages == 0)
                {
                    Responses.Response response = null;
                    response = Newtonsoft.Json.JsonConvert.DeserializeObject <Responses.Response>(data);

                    if (response.Data.Action == null)
                    {
                        Dictionary <string, object> current;
                        unconfirmedChangesets.TryDequeue(out current);
                        //  Console.WriteLine(string.Format("Request: {0} -> {1}", response.Data.ResquestId, response.Data.Payload.Status));
                    }
                    else
                    {
                        ProcessInput(response);
                    }
                }
            }
        }
コード例 #2
0
        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);
                }
            }
        }