Пример #1
0
 private void Scheduler()
 {
     //var Pipeline = PipelineManager.Pipeline;
     lock (Pipeline)
     {
         var NetworkSyncTimeSpan = MappingNetwork.NetworkSyncTimeSpan(_networkConnection.NodeList.Count);
         var toRemove            = new List <ElementPipeline>();
         var thisTime            = _networkConnection.Now;
         foreach (var item in Pipeline)
         {
             //For objects of level 0 the timestamp is added by the spooler at the time of forwarding in order to reduce the time difference between the timestamp and the reception of the object on the other node
             if (item.Element.Timestamp == 0)
             {
                 continue;
             }
             if ((thisTime - new DateTime(item.Element.Timestamp)) > NetworkSyncTimeSpan)
             {
                 toRemove.Add(item);
                 var objectName = Utility.GetObjectName(item.Element.XmlObject);
                 OnReceiveObjectFromPipeline(objectName, item.Element.XmlObject, item.Element.Timestamp);
             }
             else
             {
                 break;                        //because the pipeline is sorted by Timespan
             }
         }
         Pipeline.Remove(toRemove);
     }
 }
Пример #2
0
            private void PlanNextSchedulerRun()
            {
                _pipelineTimer.Enabled = false;
                ElementPipeline finded;

                lock (this)
                    finded = Find(x => x.Element.Timestamp != 0);
                if (finded != null)
                {
                    var NetworkSyncTimeSpan  = MappingNetwork.NetworkSyncTimeSpan(_networkConnection.NodeList.Count);
                    var exitFromPipelineTime = new DateTime(finded.Element.Timestamp).Add(NetworkSyncTimeSpan);
                    var remainingTime        = (exitFromPipelineTime - _networkConnection.Now).TotalMilliseconds;
                    if (remainingTime < 0)
                    {
                        remainingTime = 0;
                    }
                    _pipelineTimer.Interval = remainingTime + 1;
                    _pipelineTimer.Start();
                }
            }
Пример #3
0
        /// <summary>
        /// Insert the objects in the local pipeline to be synchronized
        /// The objectsFromNode come from other nodes
        /// </summary>
        /// <param name="objectsFromNode">Elements come from other nodes</param>
        /// <param name="fromNode">From which node comes the element</param>
        internal ObjToNode.TimestampVector AddLocalFromNode(IEnumerable <ObjToNode> objectsFromNode, Node fromNode)
        {
            ObjToNode.TimestampVector result = null;
            lock (Pipeline)
            {
                var networkSyncTimeSpan = MappingNetwork.NetworkSyncTimeSpan(_networkConnection.NodeList.Count);
                var addList             = new List <ElementPipeline>();
                var thisTime            = _networkConnection.Now;
                // Remove any objects in standby that have not received the timestamp signed by everyone
                lock (_standByList) _standByList.FindAll(o => (thisTime - new DateTime(o.Timestamp)).TotalSeconds >= 5).ForEach(o => _standByList.Remove(o));
                foreach (var objFromNode in objectsFromNode)
                {
                    var timePassedFromInsertion = thisTime - new DateTime(objFromNode.Timestamp);
                    UpdateStats(timePassedFromInsertion);
                    if (timePassedFromInsertion <= networkSyncTimeSpan)
                    {
                        if (objFromNode.Level == 1)
                        {
                            UpdateStats(timePassedFromInsertion, true);
                            lock (_standByList)
                            {
                                // You received an object from level 0
                                // This is an object that is just inserted, so you must certify the timestamp and send the certificate to the node that took delivery of the object.
                                // The object must then be put on standby until the node sends all the certificates for the timestamp.
                                if (objFromNode.CheckNodeThatStartedDistributingTheObject(fromNode))
                                {
                                    var signature = objFromNode.CreateTheSignatureForTheTimestamp(_networkConnection.MyNode, _networkConnection.Now);
#if DEBUG
                                    if (signature == null)
                                    {
                                        Debugger.Break();
                                    }
#endif
                                    _standByList.Add(objFromNode);
                                    if (result == null)
                                    {
                                        result = new ObjToNode.TimestampVector();
                                    }
                                    result.Add(objFromNode.ShortHash(), signature);
                                }
                                else
                                {
                                    Utility.Log("security", "check failure fromNode " + fromNode.Ip);
                                    Debugger.Break();
                                }
                            }
                        }
                        else
                        {
                            var elementPipeline = Pipeline.Find(x => x.Element.Timestamp == objFromNode.Timestamp && x.Element.XmlObject == objFromNode.XmlObject);
                            if (elementPipeline != null)
                            {
                                var level = objFromNode.Level + 1;
                                lock (elementPipeline.Levels)
                                    if (!elementPipeline.Levels.Contains(level))
                                    {
                                        elementPipeline.Levels.Add(level);
                                    }
                            }
                            else
                            {
                                UpdateStats(timePassedFromInsertion, true);
                                var CheckSignature = objFromNode.CheckSignedTimestamp(_networkConnection, fromNode.Ip);
                                if (CheckSignature != ObjToNode.CheckSignedTimestampResult.Ok)
                                {
                                    Debugger.Break();
                                }
                                else
                                {
#if DEBUG
                                    var duplicate = Pipeline.Find(x => x.Element.XmlObject == objFromNode.XmlObject);
                                    if (duplicate != null)
                                    {
                                        Debugger.Break();
                                    }
#endif
                                    elementPipeline = new ElementPipeline(objFromNode);
                                    addList.Add(elementPipeline);
                                }
                            }
                            if (elementPipeline != null)
                            {
                                lock (elementPipeline.ExcludeNodes)
                                    elementPipeline.ExcludeNodes.Add(fromNode);
                                elementPipeline.Received++;
                            }
                        }
                    }
                    else
                    {
                        //A dishonest node has started a fork through a fake timestamp?
                        Stats24H.ElementsArrivedOutOfTime++;
                        _stats12H.ElementsArrivedOutOfTime++;
                    }
                }
                Pipeline.Add(addList);
            }
            return(result);
        }