Пример #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();
                }
            }
        /// <summary>
        ///   This method initializes the network.
        ///   You can join the network as a node, and contribute to decentralization, or hook yourself to the network as an
        ///   external user.
        ///   To create a node, set the MyAddress parameter with your web address.If MyAddress is not set then you are an external
        ///   user.
        /// </summary>
        /// <param name="entryPoints">The list of permanent access points nodes, to access the network</param>
        /// <param name="networkName">The name of the infrastructure. For tests we recommend using "testnet"</param>
        /// <param name="myNode">Data related to your node. If you do not want to create the node, omit this parameter</param>
        public NetworkConnection(IEnumerable <Node> entryPoints, string networkName = "testnet", NodeInitializer myNode = null) : base(myNode?.VirtualDevice)
        {
            //if (VirtualDevice != null)
            //{
            //  //base = new Device() { VirtualDevice = VirtualDevice };
            //}
            Networks.Add(this);
            Communication   = new Communication(this);
            Protocol        = new Protocol(this);
            PipelineManager = new PipelineManager(this);
            MappingNetwork  = new MappingNetwork(this);
            var entry = new List <Node>(entryPoints as Node[] ?? entryPoints.ToArray());

            NodeList = new NodeList(this);
            if (myNode != null)
            {
                MyNode    = new Node(myNode);
                MyNode.Ip = VirtualDevice?.Ip ?? MyNode.DetectIp();
                var count = entry.Count;
                entry.RemoveAll(x => x.Address == MyNode.Address || x.Ip == MyNode.Ip);
                _imEntryPoint = count != entry.Count;
                ThisNode      = new InfoNode(MyNode);
                NodeList.Add(MyNode);
            }
            entry.ForEach(x => x.DetectIp());
            NodeList.AddRange(entry);
            NetworkName = networkName;

            //Setup.NetworkConnection.MyAddress = MyAddress;
            //Setup.NetworkConnection.NetworkName = NetworkName;
            //if (EntryPoints != null)
            //  Setup.NetworkConnection.EntryPoints = EntryPoints;
#pragma warning disable CS0618                                  // 'NetworkConnection.OnReceivesHttpRequest' è obsoleto: 'We recommend using this method from the Device class because each device could handle multiple networks'
            OnReceivesHttpRequest = base.OnReceivesHttpRequest; //Is ok! Don't worry
#pragma warning restore CS0618                                  // 'NetworkConnection.OnReceivesHttpRequest' è obsoleto: 'We recommend using this method from the Device class because each device could handle multiple networks'
            OnlineDetection.WaitForInternetConnection();
        }
Пример #4
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);
        }