Beispiel #1
0
        /// <summary>
        /// Creates a master peer and starts UDP and TCP channels.
        /// </summary>
        /// <param name="p2PId">The ID of the network.</param>
        /// <param name="peerId">The ID of this peer.</param>
        /// <param name="keyPair">The key pair or null.</param>
        /// <param name="channelServerConfiguration">The server configuration to create the
        /// channel server that is used for listening for incoming connections.</param>
        /// <param name="channelClientConfiguration">The client-side configuration.</param>
        /// <param name="timer"></param>
        public PeerCreator(int p2PId, Number160 peerId, KeyPair keyPair,
                           ChannelServerConfiguration channelServerConfiguration,
                           ChannelClientConfiguration channelClientConfiguration,
                           ExecutorService timer)
        {
            // peer bean
            PeerBean = new PeerBean(keyPair);
            PeerAddress self = FindPeerAddress(peerId, channelClientConfiguration, channelServerConfiguration);

            PeerBean.SetServerPeerAddress(self);
            Logger.Info("Visible address to other peers: {0}.", self);

            // start server
            var dispatcher    = new Dispatcher(p2PId, PeerBean, channelServerConfiguration.HearBeatMillis);
            var channelServer = new ChannelServer(channelServerConfiguration, dispatcher, PeerBean.PeerStatusListeners);

            if (!channelServer.Startup())
            {
                ShutdownNetty();
                throw new IOException("Cannot bind to TCP or UDP port.");
            }

            // connection bean
            var sender      = new Sender(peerId, PeerBean.PeerStatusListeners, channelClientConfiguration, dispatcher);
            var reservation = new Reservation(channelClientConfiguration);

            ConnectionBean = new ConnectionBean(p2PId, dispatcher, sender, channelServer, reservation, channelClientConfiguration, timer);
            _master        = true;
        }
Beispiel #2
0
        /// <summary>
        /// Creates the peer address based on the network discovery that was done./>
        /// </summary>
        /// <param name="peerId">The ID of this peer.</param>
        /// <param name="channelClientConfiguration"></param>
        /// <param name="channelServerConfiguration"></param>
        /// <returns>The peer address of this peer.</returns>
        private static PeerAddress FindPeerAddress(Number160 peerId,
                                                   ChannelClientConfiguration channelClientConfiguration, ChannelServerConfiguration channelServerConfiguration)
        {
            string status = DiscoverNetworks.DiscoverInterfaces(channelClientConfiguration.BindingsOutgoing);

            Logger.Info("Status of external address search: " + status);

            IPAddress outsideAddress = channelClientConfiguration.BindingsOutgoing.FoundAddress;

            if (outsideAddress == null)
            {
                throw new IOException("Not listening to anything. Maybe the binding information is wrong.");
            }

            var peerSocketAddress = new PeerSocketAddress(outsideAddress,
                                                          channelServerConfiguration.Ports.TcpPort,
                                                          channelServerConfiguration.Ports.UdpPort);

            var self = new PeerAddress(peerId, peerSocketAddress,
                                       channelServerConfiguration.IsBehindFirewall,
                                       channelServerConfiguration.IsBehindFirewall,
                                       false, PeerAddress.EmptyPeerSocketAddresses);

            return(self);
        }
Beispiel #3
0
 public Sender(Number160 peerId, IList <IPeerStatusListener> peerStatusListeners,
               ChannelClientConfiguration channelClientConfiguration, Dispatcher dispatcher)
 {
     _peerStatusListeners       = peerStatusListeners;
     ChannelClientConfiguration = channelClientConfiguration;
     _dispatcher = dispatcher;
     _random     = new InteropRandom((ulong)peerId.GetHashCode());
 }
Beispiel #4
0
 public Sender(Number160 peerId, IList<IPeerStatusListener> peerStatusListeners,
     ChannelClientConfiguration channelClientConfiguration, Dispatcher dispatcher)
 {
     _peerStatusListeners = peerStatusListeners;
     ChannelClientConfiguration = channelClientConfiguration;
     _dispatcher = dispatcher;
     _random = new InteropRandom((ulong)peerId.GetHashCode());
 }
Beispiel #5
0
 /// <summary>
 /// The connection bean with unmodifiable objects. Once it is set, it cannot be changed.
 /// If it is required to change, then the peer must be shut down and a new one created.
 /// </summary>
 /// <param name="p2PId">The P2P ID.</param>
 /// <param name="dispatcher">The dispatcher object that receives all messages.</param>
 /// <param name="sender">The sender object that sends out messages.</param>
 /// <param name="channelServer">The channel server that listens on incoming connections.</param>
 /// <param name="reservation">The connection reservation that is responsible for resource management.</param>
 /// <param name="resourceConfiguration">The configuration that is responsible for the resource numbers.</param>
 /// <param name="timer">The timer for the discovery process.</param>
 public ConnectionBean(int p2PId, Dispatcher dispatcher, Sender sender, ChannelServer channelServer,
                       Reservation reservation, ChannelClientConfiguration resourceConfiguration, ExecutorService timer)
 {
     P2PId                 = p2PId;
     Dispatcher            = dispatcher;
     Sender                = sender;
     ChannelServer         = channelServer;
     Reservation           = reservation;
     ResourceConfiguration = resourceConfiguration;
     Timer                 = timer;
 }
Beispiel #6
0
 /// <summary>
 /// The connection bean with unmodifiable objects. Once it is set, it cannot be changed.
 /// If it is required to change, then the peer must be shut down and a new one created.
 /// </summary>
 /// <param name="p2PId">The P2P ID.</param>
 /// <param name="dispatcher">The dispatcher object that receives all messages.</param>
 /// <param name="sender">The sender object that sends out messages.</param>
 /// <param name="channelServer">The channel server that listens on incoming connections.</param>
 /// <param name="reservation">The connection reservation that is responsible for resource management.</param>
 /// <param name="resourceConfiguration">The configuration that is responsible for the resource numbers.</param>
 /// <param name="timer">The timer for the discovery process.</param>
 public ConnectionBean(int p2PId, Dispatcher dispatcher, Sender sender, ChannelServer channelServer,
     Reservation reservation, ChannelClientConfiguration resourceConfiguration, ExecutorService timer)
 {
     P2PId = p2PId;
     Dispatcher = dispatcher;
     Sender = sender;
     ChannelServer = channelServer;
     Reservation = reservation;
     ResourceConfiguration = resourceConfiguration;
     Timer = timer;
 }
Beispiel #7
0
        /// <summary>
        /// Creates a new reservation class with the 3 permits contained in the provided configuration.
        /// </summary>
        /// <param name="channelClientConfiguration">Contains the 3 permits:
        /// - MaxPermitsUdp: The number of maximum short-lived UDP connections.
        /// - MaxPermitsTcp: The number of maximum short-lived TCP connections.
        /// - MaxPermitsPermanentTcp: The number of maximum permanent TCP connections.</param>
        public Reservation(ChannelClientConfiguration channelClientConfiguration)
        {
            _maxPermitsUdp = channelClientConfiguration.MaxPermitsUdp;
            _maxPermitsTcp = channelClientConfiguration.MaxPermitsTcp;
            _maxPermitsPermanentTcp = channelClientConfiguration.MaxPermitsPermanentTcp;
            _semaphoreUdp = new Semaphore(_maxPermitsUdp, _maxPermitsUdp);
            _semaphoreTcp = new Semaphore(_maxPermitsTcp, _maxPermitsTcp);
            _semaphorePermanentTcp = new Semaphore(_maxPermitsPermanentTcp, _maxPermitsPermanentTcp);
            _channelClientConfiguration = channelClientConfiguration;

            // .NET-specific:
            var limitedScheduler = new LimitedConcurrenctyTaskScheduler(1);
            _singleThreadTaskFactory = new TaskFactory(limitedScheduler);
        }
Beispiel #8
0
        /// <summary>
        /// Creates a new reservation class with the 3 permits contained in the provided configuration.
        /// </summary>
        /// <param name="channelClientConfiguration">Contains the 3 permits:
        /// - MaxPermitsUdp: The number of maximum short-lived UDP connections.
        /// - MaxPermitsTcp: The number of maximum short-lived TCP connections.
        /// - MaxPermitsPermanentTcp: The number of maximum permanent TCP connections.</param>
        public Reservation(ChannelClientConfiguration channelClientConfiguration)
        {
            _maxPermitsUdp              = channelClientConfiguration.MaxPermitsUdp;
            _maxPermitsTcp              = channelClientConfiguration.MaxPermitsTcp;
            _maxPermitsPermanentTcp     = channelClientConfiguration.MaxPermitsPermanentTcp;
            _semaphoreUdp               = new Semaphore(_maxPermitsUdp, _maxPermitsUdp);
            _semaphoreTcp               = new Semaphore(_maxPermitsTcp, _maxPermitsTcp);
            _semaphorePermanentTcp      = new Semaphore(_maxPermitsPermanentTcp, _maxPermitsPermanentTcp);
            _channelClientConfiguration = channelClientConfiguration;

            // .NET-specific:
            var limitedScheduler = new LimitedConcurrenctyTaskScheduler(1);

            _singleThreadTaskFactory = new TaskFactory(limitedScheduler);
        }
Beispiel #9
0
 /// <summary>
 /// Internal constructor since this is created by <see cref="Reservation"/> and should never be called directly.
 /// </summary>
 /// <param name="tcsChannelShutdownDone"></param>
 /// <param name="maxPermitsUdp"></param>
 /// <param name="maxPermitsTcp"></param>
 /// <param name="channelClientConfiguration"></param>
 internal ChannelCreator(TaskCompletionSource<object> tcsChannelShutdownDone,
     int maxPermitsUdp, int maxPermitsTcp,
     ChannelClientConfiguration channelClientConfiguration)
 {
     _tcsChannelShutdownDone = tcsChannelShutdownDone;
     _maxPermitsUdp = maxPermitsUdp;
     _maxPermitsTcp = maxPermitsTcp;
     _channelClientConfiguration = channelClientConfiguration;
     _externalBindings = channelClientConfiguration.BindingsOutgoing;
     if (maxPermitsUdp > 0)
     {
         _semaphoreUdp = new Semaphore(maxPermitsUdp, maxPermitsUdp);
     }
     if (maxPermitsTcp > 0)
     {
         _semaphoreTcp = new Semaphore(maxPermitsTcp, maxPermitsTcp);
     }
 }
Beispiel #10
0
 /// <summary>
 /// Internal constructor since this is created by <see cref="Reservation"/> and should never be called directly.
 /// </summary>
 /// <param name="tcsChannelShutdownDone"></param>
 /// <param name="maxPermitsUdp"></param>
 /// <param name="maxPermitsTcp"></param>
 /// <param name="channelClientConfiguration"></param>
 internal ChannelCreator(TaskCompletionSource <object> tcsChannelShutdownDone,
                         int maxPermitsUdp, int maxPermitsTcp,
                         ChannelClientConfiguration channelClientConfiguration)
 {
     _tcsChannelShutdownDone     = tcsChannelShutdownDone;
     _maxPermitsUdp              = maxPermitsUdp;
     _maxPermitsTcp              = maxPermitsTcp;
     _channelClientConfiguration = channelClientConfiguration;
     _externalBindings           = channelClientConfiguration.BindingsOutgoing;
     if (maxPermitsUdp > 0)
     {
         _semaphoreUdp = new Semaphore(maxPermitsUdp, maxPermitsUdp);
     }
     if (maxPermitsTcp > 0)
     {
         _semaphoreTcp = new Semaphore(maxPermitsTcp, maxPermitsTcp);
     }
 }
Beispiel #11
0
 public PeerBuilder SetChannelClientConfiguration(ChannelClientConfiguration channelClientConfiguration)
 {
     ChannelClientConfiguration = channelClientConfiguration;
     return this;
 }