Beispiel #1
0
 public Peer SetQuitRpc(QuitRpc quitRpc)
 {
     _quitRpc = quitRpc;
     return(this);
 }
Beispiel #2
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);
        }