Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Distribox.Network.AntiEntropyProtocol"/> class.
        /// </summary>
        /// <param name="listeningPort">Listening port.</param>
        /// <param name="peerFileName">Peer file name.</param>
        /// <param name="versionControl">Version control.</param>
        public AntiEntropyProtocol(int listeningPort, string peerFileName, VersionControl versionControl)
        {
            // Initialize version control
            this.versionControl = versionControl;

            // Initialize peer list
            this.peers = PeerList.GetPeerList(peerFileName);
            this.listeningPort = listeningPort;

            // Initialize listener
            this.listener = new AtomicMessageListener(listeningPort);
            this.listener.OnReceive += this.OnReceiveMessage;

            // Initialize timer to connect other peers periodically
            System.Timers.Timer timer = new System.Timers.Timer(Config.ConnectPeriodMs);
            timer.Elapsed += this.OnTimerEvent;
            timer.AutoReset = true;
            timer.Enabled = true;

            // Initialize request manager
            this.requestManager = new RequestManager();

            // Initialize Protocol message factory
            this.messageFactory = new ProtocolMessageFactory();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Distribox.Network.AntiEntropyProtocol"/> class.
        /// </summary>
        /// <param name="listeningPort">Listening port.</param>
        /// <param name="peerFileName">Peer file name.</param>
        /// <param name="versionControl">Version control.</param>
        public AntiEntropyProtocol(int listeningPort, string peerFileName, VersionControl versionControl)
        {
            // Initialize version control
            this.versionControl = versionControl;

            // Initialize peer list
            this.peers         = PeerList.GetPeerList(peerFileName);
            this.listeningPort = listeningPort;

            // Initialize listener
            this.listener            = new AtomicMessageListener(listeningPort);
            this.listener.OnReceive += this.OnReceiveMessage;

            // Initialize timer to connect other peers periodically
            System.Timers.Timer timer = new System.Timers.Timer(Config.ConnectPeriodMs);
            timer.Elapsed  += this.OnTimerEvent;
            timer.AutoReset = true;
            timer.Enabled   = true;

            // Initialize request manager
            this.requestManager = new RequestManager();

            // Initialize Protocol message factory
            this.messageFactory = new ProtocolMessageFactory();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Merges the with another peer list.
        /// </summary>
        /// <param name="list">The list.</param>
        public void MergeWith(PeerList list)
        {
            foreach (Peer peer in list.Peers)
            {
                if (!this.Peers.Contains(peer))
                {
                    this.AddPeerVolatile(peer);
                }
            }

            this.FlushToDisk();
        }
Exemplo n.º 4
0
        public void TestOperation()
        {
            string fileName = "peerList.test.tmp";
            string fileName2 = "peerList.test.tmp2";
            PeerList peerList = new PeerList(fileName);

            Assert.AreEqual(fileName, peerList.PeerFileName);
            Assert.AreEqual(0, peerList.Peers.Count);

            Peer peer1 = new Peer("127.0.0.1", 8888);
            Peer peer2 = new Peer("127.0.0.2", 8888);
            Peer peer3 = new Peer("127.0.0.2", 9999);

            peerList.AddPeer(peer1);
            Assert.AreEqual(1, peerList.Peers.Count);

            peerList.AddPeer(peer1);
            Assert.AreEqual(1, peerList.Peers.Count);
            Assert.IsTrue(peerList.Peers.Contains(peer1));
            Assert.IsFalse(peerList.Peers.Contains(peer2));

            // Test FileOperation
            PeerList peerList2 = PeerList.GetPeerList(fileName);
            Assert.AreEqual(1, peerList.Peers.Count);
            Assert.IsTrue(peerList.Peers.Contains(peer1));
            Assert.IsFalse(peerList.Peers.Contains(peer2));

            peerList2.AddPeer(peer2);

            Assert.IsFalse(peerList.Peers.Contains(peer2));
            peerList = PeerList.GetPeerList(fileName);
            Assert.IsTrue(peerList.Peers.Contains(peer2));

            // Test Merge
            PeerList peerList3 = PeerList.GetPeerList(fileName2);
            Assert.AreEqual(0, peerList3.Peers.Count);
            peerList3.AddPeer(peer3);
            Assert.AreEqual(1, peerList3.Peers.Count);

            peerList3.MergeWith(peerList2);
            Assert.IsTrue(peerList3.Peers.Contains(peer1));
            Assert.IsTrue(peerList3.Peers.Contains(peer2));
            Assert.IsTrue(peerList3.Peers.Contains(peer3));

            int peer1Cnt = 0, peer2Cnt = 0;
            for (int i = 0; i < 10000; ++i)
            {
                Peer peer = peerList2.SelectRandomPeer();

                Assert.IsTrue(peerList2.Peers.Contains(peer));
                Assert.IsTrue(peer.Equals(peer1) || peer.Equals(peer2));

                if (peer.Equals(peer1))
                {
                    ++peer1Cnt;
                }
                else
                {
                    ++peer2Cnt;
                }
            }

            Assert.Less((double)(Math.Abs(peer1Cnt - peer2Cnt) / 10000), 0.05);

            // Clean up
            File.Delete(fileName);
            File.Delete(fileName2);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Merges the with another peer list.
        /// </summary>
        /// <param name="list">The list.</param>
        public void MergeWith(PeerList list)
        {
            foreach (Peer peer in list.Peers)
            {
                if (!this.Peers.Contains(peer))
                {
                    this.AddPeerVolatile(peer);
                }
            }

            this.FlushToDisk();
        }