示例#1
0
        /// <summary>
        /// Creates peers for benchmarking. The first peer will be used as the master.
        /// This means that shutting down the master will shut down all other peers as well.
        /// </summary>
        /// <param name="nrOfPeers">Number of peers to create.</param>
        /// <param name="rnd">The random object used for peer ID creation.</param>
        /// <param name="port">The UDP and TCP port.</param>
        /// <param name="maintenance">Indicates whether maintenance should be enabled.</param>
        /// <param name="timeout">Indicates whether timeout should be enabled.</param>
        /// <returns></returns>
        public static Peer[] CreateNodes(int nrOfPeers, InteropRandom rnd, int port, bool maintenance, bool timeout)
        {
            Console.WriteLine("Creating network with {0} peers...", nrOfPeers);

            var peers = new Peer[nrOfPeers];

            var masterId = CreateRandomId(rnd);
            var masterMap = new PeerMap(new PeerMapConfiguration(masterId));
            var pb = new PeerBuilder(masterId)
                .SetPorts(port)
                .SetEnableMaintenance(maintenance)
                .SetExternalBindings(new Bindings())
                .SetPeerMap(masterMap);
            if (!timeout)
            {
                pb.SetChannelServerConfiguration(CreateInfiniteTimeoutChannelServerConfiguration(port));
            }
            peers[0] = pb.Start();
            //Logger.Info("Created master peer: {0}.", peers[0].PeerId);

            for (int i = 1; i < nrOfPeers; i++)
            {
                peers[i] = CreateSlave(peers[0], rnd, maintenance, timeout);
            }
            return peers;
        }
示例#2
0
        public void Init(Peer peer, ExecutorService timer)
        {
            _peer = peer;
            _timer = timer.ScheduleAtFixedRate(Run, null, IntervalMillis, IntervalMillis);

            // MSDN: The method can be executed simultaneously on two thread pool threads 
            // if the timer interval is less than the time required to execute the method, or
            // if all thread pool threads are in use and the method is queued multiple times.
        }
示例#3
0
 private static Peer CreateSlave(Peer master, InteropRandom rnd, bool maintenance, bool timeout)
 {
     var slaveId = CreateRandomId(rnd);
     var slaveMap = new PeerMap(new PeerMapConfiguration(slaveId).SetPeerNoVerification());
     var pb = new PeerBuilder(slaveId)
         .SetMasterPeer(master)
         .SetEnableMaintenance(maintenance)
         .SetExternalBindings(new Bindings())
         .SetPeerMap(slaveMap);
     if (!timeout)
     {
         pb.SetChannelServerConfiguration(CreateInfiniteTimeoutChannelServerConfiguration(Ports.DefaultPort));
     }
      var slave = pb.Start();
     //Logger.Info("Created slave peer {0}.", slave.PeerId);
     return slave;
 }
示例#4
0
        public async void TestPingUdpPool2()
        {
            var peers = new Peer[50];
            try
            {
                for (int i = 0; i < peers.Length; i++)
                {
                    peers[i] = new PeerBuilder(Number160.CreateHash(i))
                        .SetP2PId(55)
                        .SetPorts(2424 + i)
                        .Start();
                }
                var tasks = new List<Task<Message>>(50);
                for (int i = 0; i < peers.Length; i++)
                {
                    var cc = await peers[0].ConnectionBean.Reservation.CreateAsync(1, 0);

                    var taskResponse = peers[0].PingRpc.PingUdpAsync(peers[i].PeerAddress, cc,
                        new DefaultConnectionConfiguration());
                    Core.Utils.Utils.AddReleaseListener(cc, taskResponse);
                    tasks.Add(taskResponse);
                }
                foreach (var task in tasks)
                {
                    await task;
                    Assert.IsTrue(!task.IsFaulted);
                }
            }
            finally
            {
                for (int i = 0; i < peers.Length; i++)
                {
                    peers[i].ShutdownAsync().Wait();
                }
            }
        }
示例#5
0
 public PingBuilderFactory(Peer peer)
 {
     _peer = peer;
 }
示例#6
0
 public PeerBuilder SetMasterPeer(Peer masterPeer)
 {
     MasterPeer = masterPeer;
     return this;
 }
示例#7
0
        /// <summary>
        /// Creates a peer and starts to listen for incoming connections.
        /// </summary>
        /// <returns>The peer that can operate in the P2P network.</returns>
        public Peer Start()
        {
            if (_behindFirewall == null)
            {
                _behindFirewall = false;
            }

            if (ChannelServerConfiguration == null)
            {
                ChannelServerConfiguration = CreateDefaultChannelServerConfiguration();
                ChannelServerConfiguration.SetPortsForwarding(new Ports(TcpPortForwarding, UdpPortForwarding));
                if (TcpPort == -1)
                {
                    TcpPort = Ports.DefaultPort;
                }
                if (UdpPort == -1)
                {
                    UdpPort = Ports.DefaultPort;
                }
                ChannelServerConfiguration.SetPorts(new Ports(TcpPort, UdpPort));
                ChannelServerConfiguration.SetIsBehindFirewall(_behindFirewall.Value);
            }
            if (ChannelClientConfiguration == null)
            {
                ChannelClientConfiguration = CreateDefaultChannelClientConfiguration();
            }

            if (KeyPair == null)
            {
                KeyPair = EmptyKeyPair;
            }
            if (P2PId == -1)
            {
                P2PId = 1;
            }

            if (InterfaceBindings == null)
            {
                InterfaceBindings = new Bindings();
            }
            ChannelServerConfiguration.SetBindingsIncoming(InterfaceBindings);
            if (ExternalBindings == null)
            {
                ExternalBindings = new Bindings();
            }
            ChannelClientConfiguration.SetBindingsOutgoing(ExternalBindings);

            if (PeerMap == null)
            {
                PeerMap = new PeerMap(new PeerMapConfiguration(PeerId));
            }

            if (MasterPeer == null && Timer == null)
            {
                Timer = new ExecutorService();
            }

            PeerCreator peerCreator;
            if (MasterPeer != null)
            {
                // create slave peer
                peerCreator = new PeerCreator(MasterPeer.PeerCreator, PeerId, KeyPair);
            }
            else
            {
                // create master peer
                peerCreator = new PeerCreator(P2PId, PeerId, KeyPair, ChannelServerConfiguration, ChannelClientConfiguration, Timer);
            }

            var peer = new Peer(P2PId, PeerId, peerCreator);

            var peerBean = peerCreator.PeerBean;
            peerBean.AddPeerStatusListener(PeerMap);

            var connectionBean = peerCreator.ConnectionBean;

            peerBean.SetPeerMap(PeerMap);
            peerBean.SetKeyPair(KeyPair);

            if (BloomfilterFactory == null)
            {
                peerBean.SetBloomfilterFactory(new DefaultBloomFilterFactory());
            }

            if (BroadcastHandler == null)
            {
                BroadcastHandler = new DefaultBroadcastHandler(peer, new Random());
            }

            // set/enable RPC
            if (IsEnabledHandshakeRpc)
            {
                var pingRpc = new PingRpc(peerBean, connectionBean);
                peer.SetPingRpc(pingRpc);
            }
            if (IsEnabledQuitRpc)
            {
                var quitRpc = new QuitRpc(peerBean, connectionBean);
                quitRpc.AddPeerStatusListener(PeerMap);
                peer.SetQuitRpc(quitRpc);
            }
            if (IsEnabledNeighborRpc)
            {
                var neighborRpc = new NeighborRpc(peerBean, connectionBean);
                peer.SetNeighborRpc(neighborRpc);
            }
            if (IsEnabledDirectDataRpc)
            {
                var directDataRpc = new DirectDataRpc(peerBean, connectionBean);
                peer.SetDirectDataRpc(directDataRpc);
            }
            if (IsEnabledBroadcastRpc)
            {
                var broadcastRpc = new BroadcastRpc(peerBean, connectionBean, BroadcastHandler);
                peer.SetBroadcastRpc(broadcastRpc);
            }
            if (IsEnabledRoutingRpc && IsEnabledNeighborRpc)
            {
                var routing = new DistributedRouting(peerBean, peer.NeighborRpc);
                peer.SetDistributedRouting(routing);
            }

            if (MaintenanceTask == null && IsEnabledMaintenance)
            {
                MaintenanceTask = new MaintenanceTask();
            }
            if (MaintenanceTask != null)
            {
                MaintenanceTask.Init(peer, connectionBean.Timer);
                MaintenanceTask.AddMaintainable(PeerMap);
            }
            peerBean.SetMaintenanceTask(MaintenanceTask);

            // set the ping builder for the heart beat
            connectionBean.Sender.SetPingBuilderFactory(new PingBuilderFactory(peer));

            foreach (var peerInit in _toInitialize)
            {
                peerInit.Init(peer);
            }
            return peer;
        }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="peer">The peer that sends the broadcast messages.</param>
 /// <param name="random">Random number, since it is a random walk.</param>
 public DefaultBroadcastHandler(Peer peer, Random random)
 {
     _peer = peer;
     _rnd = random;
 }
示例#9
0
 /// <summary>
 /// Creates peers for testing. The first peer will be used as the master.
 /// This means that shutting down the master will shut down all other peers as well.
 /// </summary>
 /// <param name="nrOfPeers"></param>
 /// <param name="rnd"></param>
 /// <param name="port"></param>
 /// <param name="automaticTask"></param>
 /// <param name="maintenance"></param>
 /// <returns></returns>
 public static Peer[] CreateNodes(int nrOfPeers, Random rnd, int port, IAutomaticTask automaticTask, bool maintenance)
 {
     var bindings = new Bindings();
     var peers = new Peer[nrOfPeers];
     if (automaticTask != null)
     {
         var peerId = new Number160(rnd);
         var peerMap = new PeerMap(new PeerMapConfiguration(peerId));
         peers[0] = new PeerBuilder(peerId)
             .SetPorts(port)
             .SetEnableMaintenance(maintenance)
             .SetExternalBindings(bindings)
             .SetPeerMap(peerMap)
             .Start()
             .AddAutomaticTask(automaticTask);
     }
     else
     {
         var peerId = new Number160(rnd);
         var peerMap = new PeerMap(new PeerMapConfiguration(peerId));
         peers[0] = new PeerBuilder(peerId)
             .SetPorts(port)
             .SetEnableMaintenance(maintenance)
             .SetExternalBindings(bindings)
             .SetPeerMap(peerMap)
             .Start();
     }
     Console.WriteLine("Created master peer: {0}.", peers[0].PeerId);
     for (int i = 1; i < nrOfPeers; i++)
     {
         if (automaticTask != null)
         {
             var peerId = new Number160(rnd);
             var peerMap = new PeerMap(new PeerMapConfiguration(peerId));
             peers[i] = new PeerBuilder(peerId)
                 .SetMasterPeer(peers[0])
                 .SetEnableMaintenance(maintenance)
                 .SetExternalBindings(bindings)
                 .SetPeerMap(peerMap)
                 .Start()
                 .AddAutomaticTask(automaticTask);
         }
         else
         {
             var peerId = new Number160(rnd);
             var peerMap = new PeerMap(new PeerMapConfiguration(peerId).SetPeerNoVerification());
             peers[i] = new PeerBuilder(peerId)
                 .SetMasterPeer(peers[0])
                 .SetEnableMaintenance(maintenance)
                 .SetExternalBindings(bindings)
                 .SetPeerMap(peerMap)
                 .Start();
         }
         Console.WriteLine("Created slave peer {0}: {1}.", i, peers[i].PeerId);
     }
     return peers;
 }