Exemplo n.º 1
0
        public PeerService(PeerNodeConfig config,
                           ChannelCallback channelCallback,
                           GetNeighborCallback getNeighborCallback,
                           Dictionary <Type, object> services,
                           IPeerNodeMessageHandling messageHandler)
        {
            this.config             = config;
            this.newChannelCallback = channelCallback;
            Fx.Assert(getNeighborCallback != null, "getNeighborCallback must be passed to PeerService constructor");
            this.getNeighborCallback = getNeighborCallback;
            this.messageHandler      = messageHandler;

            if (services != null)
            {
                object reply = null;
                services.TryGetValue(typeof(IPeerConnectorContract), out reply);
                connector = reply as IPeerConnectorContract;
                Fx.Assert(connector != null, "PeerService must be created with a connector implementation");
                reply = null;
                services.TryGetValue(typeof(IPeerFlooderContract <Message, UtilityInfo>), out reply);
                flooder = reply as IPeerFlooderContract <Message, UtilityInfo>;
                Fx.Assert(flooder != null, "PeerService must be created with a flooder implementation");
            }
            this.serviceHost = new ServiceHost(this);

            // Add throttling
            ServiceThrottlingBehavior throttle = new ServiceThrottlingBehavior();

            throttle.MaxConcurrentCalls    = this.config.MaxPendingIncomingCalls;
            throttle.MaxConcurrentSessions = this.config.MaxConcurrentSessions;
            this.serviceHost.Description.Behaviors.Add(throttle);
        }
 public static PeerFlooder CreateFlooder(PeerNodeConfig config, PeerNeighborManager neighborManager, IPeerNodeMessageHandling messageHandler)
 {
     return(new PeerFlooder(config, neighborManager)
     {
         messageHandler = messageHandler
     });
 }
Exemplo n.º 3
0
 public PeerService(PeerNodeConfig config,
                    ChannelCallback channelCallback,
                    GetNeighborCallback getNeighborCallback,
                    Dictionary <Type, object> services)
     : this(config, channelCallback, getNeighborCallback, services, null)
 {
 }
Exemplo n.º 4
0
        public static PeerFlooder CreateFlooder(PeerNodeConfig config, PeerNeighborManager neighborManager, IPeerNodeMessageHandling messageHandler)
        {
            PeerFlooder flooder = new PeerFlooder(config, neighborManager);

            flooder.messageHandler = messageHandler;
            return(flooder);
        }
 public PeerConnector(PeerNodeConfig config, PeerNeighborManager neighborManager, PeerMaintainer maintainer)
 {
     this.config = config;
     this.neighborManager = neighborManager;
     this.maintainer = maintainer;
     this.timerTable = new Dictionary<IPeerNeighbor, IOThreadTimer>();
     this.state = State.Created;
 }
 public PeerConnector(PeerNodeConfig config, PeerNeighborManager neighborManager, PeerMaintainer maintainer)
 {
     this.config          = config;
     this.neighborManager = neighborManager;
     this.maintainer      = maintainer;
     this.timerTable      = new Dictionary <IPeerNeighbor, IOThreadTimer>();
     this.state           = State.Created;
 }
Exemplo n.º 7
0
 public PeerFlooderBase(PeerNodeConfig config, PeerNeighborManager neighborManager)
 {
     this.neighborManager = neighborManager;
     this.neighbors       = new List <IPeerNeighbor>();
     this.config          = config;
     this.neighbors       = this.neighborManager.GetConnectedNeighbors();
     this.quotaHelper     = new PeerThrottleHelper(this, this.config.MaxPendingOutgoingCalls);
     OnMessageSentHandler = new EventHandler(OnMessageSent);
 }
Exemplo n.º 8
0
 public PeerMaintainerBase(PeerNodeConfig config, PeerNeighborManager neighborManager, PeerFlooder flooder)
 {
     this.neighborManager = neighborManager;
     this.flooder         = flooder;
     this.config          = config;
     this.thisLock        = new object();
     this.referralCache   = new Dictionary <EndpointAddress, Referral>();
     this.maintainerTimer = new IOThreadTimer(new Action <object>(this.OnMaintainerTimer), this, false);
 }
 public void Initialize(IPeerMaintainer maintainer, PeerNodeConfig config, int wantedConnectionCount, Dictionary <EndpointAddress, Referral> referralCache)
 {
     this.maintainer            = maintainer;
     this.config                = config;
     this.wantedConnectionCount = wantedConnectionCount;
     this.UpdateEndpointsCollection(referralCache.Values);
     maintainer.NeighborClosed    += new NeighborClosedHandler(this.OnNeighborClosed);
     maintainer.NeighborConnected += new NeighborConnectedHandler(this.OnNeighborConnected);
     maintainer.MaintainerClosed  += new MaintainerClosedHandler(this.OnMaintainerClosed);
     maintainer.ReferralsAdded    += new ReferralsAddedHandler(this.OnReferralsAdded);
 }
        public void Initialize(IPeerMaintainer maintainer, PeerNodeConfig config, int wantedConnectionCount, Dictionary<EndpointAddress, Referral> referralCache)
        {
            this.maintainer = maintainer;
            this.config = config;
            this.wantedConnectionCount = wantedConnectionCount;
            UpdateEndpointsCollection(referralCache.Values);        // Add to the endpoints connection anything in the referralsCache

            // Hook up the event handlers
            maintainer.NeighborClosed += OnNeighborClosed;
            maintainer.NeighborConnected += OnNeighborConnected;
            maintainer.MaintainerClosed += OnMaintainerClosed;
            maintainer.ReferralsAdded += OnReferralsAdded;
        }
        public void Initialize(IPeerMaintainer maintainer, PeerNodeConfig config, int wantedConnectionCount, Dictionary <EndpointAddress, Referral> referralCache)
        {
            this.maintainer            = maintainer;
            this.config                = config;
            this.wantedConnectionCount = wantedConnectionCount;
            UpdateEndpointsCollection(referralCache.Values);        // Add to the endpoints connection anything in the referralsCache

            // Hook up the event handlers
            maintainer.NeighborClosed    += OnNeighborClosed;
            maintainer.NeighborConnected += OnNeighborConnected;
            maintainer.MaintainerClosed  += OnMaintainerClosed;
            maintainer.ReferralsAdded    += OnReferralsAdded;
        }
        public PeerNeighborManager(PeerIPHelper ipHelper, PeerNodeConfig config, IPeerNodeMessageHandling messageHandler)
        {
            Fx.Assert(ipHelper != null, "Non-null ipHelper is expected");
            Fx.Assert(config != null, "Non-null Config is expected");

            this.neighborList = new List<PeerNeighbor>();
            this.connectedNeighborList = new List<IPeerNeighbor>();
            this.ipHelper = ipHelper;
            this.messageHandler = messageHandler;
            this.config = config;
            this.thisLock = new object();
            this.traceRecord = new PeerNodeTraceRecord(config.NodeId);
            this.state = State.Created;
        }
Exemplo n.º 13
0
        public PeerConnector(PeerNodeConfig config, PeerNeighborManager neighborManager,
                             PeerMaintainer maintainer)
        {
            Fx.Assert(config != null, "Config is expected to non-null");
            Fx.Assert(neighborManager != null, "NeighborManager is expected to be non-null");
            Fx.Assert(maintainer != null, "Maintainer is expected to be non-null");
            Fx.Assert(config.NodeId != PeerTransportConstants.InvalidNodeId, "Invalid NodeId");
            Fx.Assert(config.MaxNeighbors > 0, "MaxNeighbors is expected to be non-zero positive value");
            Fx.Assert(config.ConnectTimeout > 0, "ConnectTimeout is expected to be non-zero positive value");

            this.thisLock        = new object();
            this.config          = config;
            this.neighborManager = neighborManager;
            this.maintainer      = maintainer;
            this.timerTable      = new Dictionary <IPeerNeighbor, IOThreadTimer>();
            this.state           = State.Created;
        }
Exemplo n.º 14
0
        public PeerConnector(PeerNodeConfig config, PeerNeighborManager neighborManager,
            PeerMaintainer maintainer)
        {
            Fx.Assert(config != null, "Config is expected to non-null");
            Fx.Assert(neighborManager != null, "NeighborManager is expected to be non-null");
            Fx.Assert(maintainer != null, "Maintainer is expected to be non-null");
            Fx.Assert(config.NodeId != PeerTransportConstants.InvalidNodeId, "Invalid NodeId");
            Fx.Assert(config.MaxNeighbors > 0, "MaxNeighbors is expected to be non-zero positive value");
            Fx.Assert(config.ConnectTimeout > 0, "ConnectTimeout is expected to be non-zero positive value");

            this.thisLock = new object();
            this.config = config;
            this.neighborManager = neighborManager;
            this.maintainer = maintainer;
            this.timerTable = new Dictionary<IPeerNeighbor, IOThreadTimer>();
            this.state = State.Created;
        }
 public PeerService(PeerNodeConfig config, ChannelCallback channelCallback, GetNeighborCallback getNeighborCallback, Dictionary<System.Type, object> services, IPeerNodeMessageHandling messageHandler)
 {
     this.config = config;
     this.newChannelCallback = channelCallback;
     this.getNeighborCallback = getNeighborCallback;
     this.messageHandler = messageHandler;
     if (services != null)
     {
         object obj2 = null;
         services.TryGetValue(typeof(IPeerConnectorContract), out obj2);
         this.connector = obj2 as IPeerConnectorContract;
         obj2 = null;
         services.TryGetValue(typeof(IPeerFlooderContract<Message, UtilityInfo>), out obj2);
         this.flooder = obj2 as IPeerFlooderContract<Message, UtilityInfo>;
     }
     this.serviceHost = new ServiceHost(this, new Uri[0]);
     ServiceThrottlingBehavior item = new ServiceThrottlingBehavior {
         MaxConcurrentCalls = this.config.MaxPendingIncomingCalls,
         MaxConcurrentSessions = this.config.MaxConcurrentSessions
     };
     this.serviceHost.Description.Behaviors.Add(item);
 }
        public PeerService(PeerNodeConfig config, ChannelCallback channelCallback, GetNeighborCallback getNeighborCallback, Dictionary <System.Type, object> services, IPeerNodeMessageHandling messageHandler)
        {
            this.config              = config;
            this.newChannelCallback  = channelCallback;
            this.getNeighborCallback = getNeighborCallback;
            this.messageHandler      = messageHandler;
            if (services != null)
            {
                object obj2 = null;
                services.TryGetValue(typeof(IPeerConnectorContract), out obj2);
                this.connector = obj2 as IPeerConnectorContract;
                obj2           = null;
                services.TryGetValue(typeof(IPeerFlooderContract <Message, UtilityInfo>), out obj2);
                this.flooder = obj2 as IPeerFlooderContract <Message, UtilityInfo>;
            }
            this.serviceHost = new ServiceHost(this, new Uri[0]);
            ServiceThrottlingBehavior item = new ServiceThrottlingBehavior {
                MaxConcurrentCalls    = this.config.MaxPendingIncomingCalls,
                MaxConcurrentSessions = this.config.MaxConcurrentSessions
            };

            this.serviceHost.Description.Behaviors.Add(item);
        }
 private void CloseCore(TimeSpan timeout, bool graceful)
 {
     PeerService service;
     PeerMaintainer maintainer;
     PeerNeighborManager neighborManager;
     PeerConnector connector;
     PeerIPHelper ipHelper;
     PeerNodeConfig config;
     PeerFlooder flooder;
     Exception exception = null;
     TimeoutHelper helper2 = new TimeoutHelper(timeout);
     if (System.ServiceModel.DiagnosticUtility.ShouldTraceInformation)
     {
         TraceUtility.TraceEvent(TraceEventType.Information, 0x40043, System.ServiceModel.SR.GetString("TraceCodePeerNodeClosing"), this.traceRecord, this, null);
     }
     lock (this.ThisLock)
     {
         this.isOpen = false;
         maintainer = this.maintainer;
         neighborManager = this.neighborManager;
         connector = this.connector;
         ipHelper = this.ipHelper;
         service = this.service;
         config = this.config;
         flooder = this.flooder;
     }
     try
     {
         if (graceful)
         {
             this.UnregisterAddress(timeout);
         }
         else if (config != null)
         {
             ActionItem.Schedule(new Action<object>(this.UnregisterAddress), config.UnregisterTimeout);
         }
     }
     catch (Exception exception2)
     {
         if (Fx.IsFatal(exception2))
         {
             throw;
         }
         System.ServiceModel.DiagnosticUtility.ExceptionUtility.TraceHandledException(exception2, TraceEventType.Information);
         if (exception == null)
         {
             exception = exception2;
         }
     }
     try
     {
         if (connector != null)
         {
             connector.Closing();
         }
         if (service != null)
         {
             try
             {
                 service.Abort();
             }
             catch (Exception exception3)
             {
                 if (Fx.IsFatal(exception3))
                 {
                     throw;
                 }
                 System.ServiceModel.DiagnosticUtility.ExceptionUtility.TraceHandledException(exception3, TraceEventType.Information);
                 if (exception == null)
                 {
                     exception = exception3;
                 }
             }
         }
         if (maintainer != null)
         {
             try
             {
                 maintainer.Close();
             }
             catch (Exception exception4)
             {
                 if (Fx.IsFatal(exception4))
                 {
                     throw;
                 }
                 System.ServiceModel.DiagnosticUtility.ExceptionUtility.TraceHandledException(exception4, TraceEventType.Information);
                 if (exception == null)
                 {
                     exception = exception4;
                 }
             }
         }
         if (ipHelper != null)
         {
             try
             {
                 ipHelper.Close();
                 ipHelper.AddressChanged -= new EventHandler(this.stateManager.OnIPAddressesChanged);
             }
             catch (Exception exception5)
             {
                 if (Fx.IsFatal(exception5))
                 {
                     throw;
                 }
                 System.ServiceModel.DiagnosticUtility.ExceptionUtility.TraceHandledException(exception5, TraceEventType.Information);
                 if (exception == null)
                 {
                     exception = exception5;
                 }
             }
         }
         if (neighborManager != null)
         {
             neighborManager.NeighborConnected -= new EventHandler(this.OnNeighborConnected);
             neighborManager.NeighborOpened -= new EventHandler(this.securityManager.OnNeighborOpened);
             this.securityManager.OnNeighborAuthenticated = (EventHandler) Delegate.Remove(this.securityManager.OnNeighborAuthenticated, new EventHandler(this.OnNeighborAuthenticated));
             neighborManager.Online -= new EventHandler(this.FireOnline);
             neighborManager.Offline -= new EventHandler(this.FireOffline);
             try
             {
                 neighborManager.Shutdown(graceful, helper2.RemainingTime());
             }
             catch (Exception exception6)
             {
                 if (Fx.IsFatal(exception6))
                 {
                     throw;
                 }
                 System.ServiceModel.DiagnosticUtility.ExceptionUtility.TraceHandledException(exception6, TraceEventType.Information);
                 if (exception == null)
                 {
                     exception = exception6;
                 }
             }
             neighborManager.NeighborClosed -= new EventHandler<PeerNeighborCloseEventArgs>(this.OnNeighborClosed);
             neighborManager.NeighborClosing -= new EventHandler<PeerNeighborCloseEventArgs>(this.OnNeighborClosing);
             neighborManager.Close();
         }
         if (connector != null)
         {
             try
             {
                 connector.Close();
             }
             catch (Exception exception7)
             {
                 if (Fx.IsFatal(exception7))
                 {
                     throw;
                 }
                 System.ServiceModel.DiagnosticUtility.ExceptionUtility.TraceHandledException(exception7, TraceEventType.Information);
                 if (exception == null)
                 {
                     exception = exception7;
                 }
             }
         }
         if (flooder != null)
         {
             try
             {
                 flooder.Close();
             }
             catch (Exception exception8)
             {
                 if (Fx.IsFatal(exception8))
                 {
                     throw;
                 }
                 System.ServiceModel.DiagnosticUtility.ExceptionUtility.TraceHandledException(exception8, TraceEventType.Information);
                 if (exception == null)
                 {
                     exception = exception8;
                 }
             }
         }
     }
     catch (Exception exception9)
     {
         if (Fx.IsFatal(exception9))
         {
             throw;
         }
         if (exception == null)
         {
             exception = exception9;
         }
     }
     EventHandler aborted = null;
     lock (this.ThisLock)
     {
         this.neighborManager = null;
         this.connector = null;
         this.maintainer = null;
         this.flooder = null;
         this.ipHelper = null;
         this.service = null;
         this.config = null;
         this.meshId = null;
         aborted = this.Aborted;
     }
     if (!graceful && (aborted != null))
     {
         try
         {
             aborted(this, EventArgs.Empty);
         }
         catch (Exception exception10)
         {
             if (Fx.IsFatal(exception10))
             {
                 throw;
             }
             System.ServiceModel.DiagnosticUtility.ExceptionUtility.TraceHandledException(exception10, TraceEventType.Information);
             if (exception == null)
             {
                 exception = exception10;
             }
         }
     }
     if (System.ServiceModel.DiagnosticUtility.ShouldTraceInformation)
     {
         TraceUtility.TraceEvent(TraceEventType.Information, 0x40044, System.ServiceModel.SR.GetString("TraceCodePeerNodeClosed"), this.traceRecord, this, null);
     }
     if ((exception != null) && graceful)
     {
         throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
     }
 }
Exemplo n.º 18
0
 internal PeerFlooderSimple(PeerNodeConfig config, PeerNeighborManager neighborManager)
     : base(config, neighborManager)
 {
     //we want a message id cache that holds message ids for atmost 5 mins.
     this.messageIds = new ListManager(MaxBuckets);
 }
Exemplo n.º 19
0
 PeerFlooder(PeerNodeConfig config, PeerNeighborManager neighborManager) : base(config, neighborManager)
 {
 }
 public static PeerFlooder CreateFlooder(PeerNodeConfig config, PeerNeighborManager neighborManager, IPeerNodeMessageHandling messageHandler)
 {
     return new PeerFlooder(config, neighborManager) { messageHandler = messageHandler };
 }
 private PeerFlooder(PeerNodeConfig config, PeerNeighborManager neighborManager) : base(config, neighborManager)
 {
 }
 private void OpenCore(TimeSpan timeout)
 {
     PeerMaintainer maintainer;
     TimeoutHelper helper = new TimeoutHelper(timeout);
     lock (this.ThisLock)
     {
         if (this.ListenUri == null)
         {
             throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("ListenUriNotSet", new object[] { base.GetType() })));
         }
         this.meshId = this.ListenUri.Host;
         byte[] buffer = new byte[8];
         ulong id = 0L;
         do
         {
             CryptoHelper.FillRandomBytes(buffer);
             for (int i = 0; i < 8; i++)
             {
                 id |= buffer[i] << (i * 8);
             }
         }
         while (id == 0L);
         this.traceRecord = new PeerNodeTraceRecord(id, this.meshId);
         if (System.ServiceModel.DiagnosticUtility.ShouldTraceInformation)
         {
             TraceUtility.TraceEvent(TraceEventType.Information, 0x40040, System.ServiceModel.SR.GetString("TraceCodePeerNodeOpening"), this.traceRecord, this, null);
         }
         this.config = new PeerNodeConfig(this.meshId, id, this.resolver, this.messagePropagationFilter, this.encoder, this.ListenUri, this.listenIPAddress, this.port, this.maxReceivedMessageSize, this.minNeighbors, this.idealNeighbors, this.maxNeighbors, this.maxReferrals, this.connectTimeout, this.maintainerInterval, this.securityManager, this.readerQuotas, this.maxBufferPoolSize, this.MaxSendQueue, this.MaxReceiveQueue);
         if (this.listenIPAddress != null)
         {
             this.ipHelper = new PeerIPHelper(this.listenIPAddress);
         }
         else
         {
             this.ipHelper = new PeerIPHelper();
         }
         this.bufferManager = BufferManager.CreateBufferManager(0x40L * this.config.MaxReceivedMessageSize, (int) this.config.MaxReceivedMessageSize);
         this.neighborManager = new PeerNeighborManager(this.ipHelper, this.config, this);
         this.flooder = PeerFlooder.CreateFlooder(this.config, this.neighborManager, this);
         this.maintainer = new PeerMaintainer(this.config, this.neighborManager, this.flooder);
         this.connector = new PeerConnector(this.config, this.neighborManager, this.maintainer);
         Dictionary<System.Type, object> serviceHandlers = this.serviceHandlers;
         if (serviceHandlers == null)
         {
             serviceHandlers = new Dictionary<System.Type, object>();
             serviceHandlers.Add(typeof(IPeerConnectorContract), this.connector);
             serviceHandlers.Add(typeof(IPeerFlooderContract<Message, UtilityInfo>), this.flooder);
         }
         this.service = new PeerService(this.config, new PeerService.ChannelCallback(this.neighborManager.ProcessIncomingChannel), new PeerService.GetNeighborCallback(this.neighborManager.GetNeighborFromProxy), serviceHandlers, this);
         this.securityManager.MeshId = this.meshId;
         this.service.Open(helper.RemainingTime());
         this.neighborManager.NeighborClosed += new EventHandler<PeerNeighborCloseEventArgs>(this.OnNeighborClosed);
         this.neighborManager.NeighborClosing += new EventHandler<PeerNeighborCloseEventArgs>(this.OnNeighborClosing);
         this.neighborManager.NeighborConnected += new EventHandler(this.OnNeighborConnected);
         this.neighborManager.NeighborOpened += new EventHandler(this.SecurityManager.OnNeighborOpened);
         this.securityManager.OnNeighborAuthenticated = (EventHandler) Delegate.Combine(this.securityManager.OnNeighborAuthenticated, new EventHandler(this.OnNeighborAuthenticated));
         this.neighborManager.Online += new EventHandler(this.FireOnline);
         this.neighborManager.Offline += new EventHandler(this.FireOffline);
         this.ipHelper.AddressChanged += new EventHandler(this.stateManager.OnIPAddressesChanged);
         this.ipHelper.Open();
         PeerNodeAddress address = new PeerNodeAddress(this.service.GetListenAddress(), this.ipHelper.GetLocalAddresses());
         this.config.SetListenAddress(address);
         this.neighborManager.Open(this.service.Binding, this.service);
         this.connector.Open();
         this.maintainer.Open();
         this.flooder.Open();
         this.isOpen = true;
         this.completeTraceRecord = new PeerNodeTraceRecord(id, this.meshId, address);
         maintainer = this.maintainer;
         this.openException = null;
     }
     if (this.isOpen)
     {
         maintainer.ScheduleConnect(new PeerMaintainerBase<ConnectAlgorithms>.ConnectCallback(this.OnConnectionAttemptCompleted));
     }
 }
Exemplo n.º 23
0
 internal PeerFlooderSimple(PeerNodeConfig config, PeerNeighborManager neighborManager) : base(config, neighborManager)
 {
     this.messageIds = new ListManager(5);
 }
        // the core functionality of open (all but waiting for a connection)
        void OpenCore(TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
            PeerMaintainer lclMaintainer;
            PeerNodeConfig lclConfig;
            string lclMeshId;

            lock (ThisLock)
            {
                if (ListenUri == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ListenUriNotSet, this.GetType())));
                }

                // extract mesh id from listen uri
                meshId = ListenUri.Host;

                // generate the node id
                byte[] bytes = new byte[sizeof(ulong)];
                ulong nodeId = 0;
                do
                {
                    System.ServiceModel.Security.CryptoHelper.FillRandomBytes(bytes);
                    for (int i = 0; i < sizeof(ulong); i++)
                        nodeId |= ((ulong)bytes[i]) << i * 8;
                }
                while (nodeId == PeerTransportConstants.InvalidNodeId);

                // now that the node id has been generated, create the trace record that describes this
                traceRecord = new PeerNodeTraceRecord(nodeId, meshId);
                if (DiagnosticUtility.ShouldTraceInformation)
                {
                    TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.PeerNodeOpening, SR.GetString(SR.TraceCodePeerNodeOpening), this.traceRecord, this, null);
                }

                // create the node configuration
                config = new PeerNodeConfig(meshId,
                                                nodeId,
                                                resolver,
                                                messagePropagationFilter,
                                                encoder,
                                                ListenUri, listenIPAddress, port,
                                                maxReceivedMessageSize, minNeighbors, idealNeighbors, maxNeighbors, maxReferrals,
                                                connectTimeout, maintainerInterval,
                                                securityManager,
                                                this.readerQuotas,
                                                this.maxBufferPoolSize,
                                                this.MaxSendQueue,
                                                this.MaxReceiveQueue);

                // create components
                if (listenIPAddress != null)
                    ipHelper = new PeerIPHelper(listenIPAddress);
                else
                    ipHelper = new PeerIPHelper();
                bufferManager = BufferManager.CreateBufferManager(64 * config.MaxReceivedMessageSize, (int)config.MaxReceivedMessageSize);
                neighborManager = new PeerNeighborManager(ipHelper,
                                                            config,
                                                            this);
                flooder = PeerFlooder.CreateFlooder(config, neighborManager, this);
                maintainer = new PeerMaintainer(config, neighborManager, flooder);
                connector = new PeerConnector(config, neighborManager, maintainer);

                Dictionary<Type, object> services = serviceHandlers;
                if (services == null)
                {
                    services = new Dictionary<Type, object>();
                    services.Add(typeof(IPeerConnectorContract), connector);
                    services.Add(typeof(IPeerFlooderContract<Message, UtilityInfo>), flooder);
                }
                service = new PeerService(this.config,
                                        neighborManager.ProcessIncomingChannel,
                                        neighborManager.GetNeighborFromProxy,
                                        services,
                                        this);
                this.securityManager.MeshId = this.meshId;
                service.Open(timeoutHelper.RemainingTime());

                // register for events
                neighborManager.NeighborClosed += new EventHandler<PeerNeighborCloseEventArgs>(OnNeighborClosed);
                neighborManager.NeighborClosing += new EventHandler<PeerNeighborCloseEventArgs>(OnNeighborClosing);
                neighborManager.NeighborConnected += new EventHandler(OnNeighborConnected);
                neighborManager.NeighborOpened += new EventHandler(this.SecurityManager.OnNeighborOpened);
                this.securityManager.OnNeighborAuthenticated += new EventHandler(this.OnNeighborAuthenticated);
                neighborManager.Online += new EventHandler(FireOnline);
                neighborManager.Offline += new EventHandler(FireOffline);
                ipHelper.AddressChanged += new EventHandler(stateManager.OnIPAddressesChanged);

                // open components
                ipHelper.Open();

                // Set the listen address before opening any more components
                PeerNodeAddress nodeAddress = new PeerNodeAddress(service.GetListenAddress(), ipHelper.GetLocalAddresses());
                config.SetListenAddress(nodeAddress);

                neighborManager.Open(service.Binding, service);
                connector.Open();
                maintainer.Open();
                flooder.Open();

                isOpen = true;
                completeTraceRecord = new PeerNodeTraceRecord(nodeId, meshId, nodeAddress);

                // Set these locals inside the lock (Abort may occur whilst Opening)
                lclMaintainer = maintainer;

                lclMeshId = meshId;
                lclConfig = config;
                openException = null;

            }

            // retrieve listen addresses and register with the resolver
            if (isOpen)
            {
                // attempt to connect to the mesh
                lclMaintainer.ScheduleConnect(new PeerMaintainer.ConnectCallback(OnConnectionAttemptCompleted));
            }
        }
Exemplo n.º 25
0
 public PeerMaintainer(PeerNodeConfig config, PeerNeighborManager neighborManager, PeerFlooder flooder) : base(config, neighborManager, flooder)
 {
 }
 public PeerNeighborManager(PeerIPHelper ipHelper, PeerNodeConfig config)
     :
     this(ipHelper, config, null) { }
 private void OnOpen(TimeSpan timeout, bool waitForOnline)
 {
     bool aborted = false;
     EventHandler handler = (source, args) => this.connectCompletedEvent.Set();
     EventHandler handler2 = delegate (object source, EventArgs args) {
         aborted = true;
         this.connectCompletedEvent.Set();
     };
     this.openException = null;
     TimeoutHelper helper = new TimeoutHelper(timeout);
     try
     {
         this.NeighborConnected += handler;
         this.Aborted += handler2;
         this.OpenCore(timeout);
         if (waitForOnline && !TimeoutHelper.WaitOne(this.connectCompletedEvent, helper.RemainingTime()))
         {
             throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException());
         }
         if (aborted)
         {
             throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationObjectAbortedException(System.ServiceModel.SR.GetString("PeerNodeAborted")));
         }
         if (this.isOpen)
         {
             if (this.openException != null)
             {
                 throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.openException);
             }
             string lclMeshId = null;
             PeerNodeConfig config = null;
             lock (this.ThisLock)
             {
                 lclMeshId = this.meshId;
                 config = this.config;
             }
             this.RegisterAddress(lclMeshId, config.GetListenAddress(false), helper.RemainingTime());
         }
     }
     catch (Exception exception)
     {
         if (Fx.IsFatal(exception))
         {
             throw;
         }
         this.CloseCore(TimeSpan.FromTicks(0L), false);
         throw;
     }
     finally
     {
         this.NeighborConnected -= handler;
         this.Aborted -= handler2;
     }
 }
 public PeerNeighbor(PeerNodeConfig config,
                     IPeerNodeMessageHandling messageHandler)
 {
     this.closeReason = PeerCloseReason.None;
     this.closeInitiator = PeerCloseInitiator.LocalNode;
     this.config = config;
     this.state = PeerNeighborState.Created;
     this.extensions = new ExtensionCollection<IPeerNeighbor>(this, thisLock);
     this.messageHandler = messageHandler;
 }
 public void Initialize(IPeerMaintainer maintainer, PeerNodeConfig config, int wantedConnectionCount, Dictionary<EndpointAddress, Referral> referralCache)
 {
     this.maintainer = maintainer;
     this.config = config;
     this.wantedConnectionCount = wantedConnectionCount;
     this.UpdateEndpointsCollection(referralCache.Values);
     maintainer.NeighborClosed += new NeighborClosedHandler(this.OnNeighborClosed);
     maintainer.NeighborConnected += new NeighborConnectedHandler(this.OnNeighborConnected);
     maintainer.MaintainerClosed += new MaintainerClosedHandler(this.OnMaintainerClosed);
     maintainer.ReferralsAdded += new ReferralsAddedHandler(this.OnReferralsAdded);
 }
        void CloseCore(TimeSpan timeout, bool graceful)
        {
            PeerService lclService;
            PeerMaintainer lclMaintainer;
            PeerNeighborManager lclNeighborManager;
            PeerConnector lclConnector;
            PeerIPHelper lclIPHelper;
            PeerNodeConfig lclConfig;
            PeerFlooder lclFlooder;
            Exception exception = null;

            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
            if (DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.PeerNodeClosing, SR.GetString(SR.TraceCodePeerNodeClosing), this.traceRecord, this, null);
            }

            lock (ThisLock)
            {
                isOpen = false;
                lclMaintainer = maintainer;
                lclNeighborManager = neighborManager;
                lclConnector = connector;
                lclIPHelper = ipHelper;
                lclService = service;
                lclConfig = config;
                lclFlooder = flooder;
            }

            // only unregister if we are doing a g----ful shutdown
            try
            {
                if (graceful)
                {
                    UnregisterAddress(timeout);
                }
                else
                {
                    if (lclConfig != null)
                    {
                        ActionItem.Schedule(new Action<object>(UnregisterAddress), lclConfig.UnregisterTimeout);
                    }
                }
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e)) throw;
                DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                if (exception == null) exception = e;
            }

            try
            {
                if (lclConnector != null)
                    lclConnector.Closing();

                if (lclService != null)
                {
                    try
                    {
                        lclService.Abort();
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e)) throw;
                        DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                        if (exception == null) exception = e;
                    }
                }

                if (lclMaintainer != null)
                {
                    try
                    {
                        lclMaintainer.Close();
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e)) throw;
                        DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                        if (exception == null) exception = e;
                    }
                }

                if (lclIPHelper != null)
                {
                    try
                    {
                        lclIPHelper.Close();
                        lclIPHelper.AddressChanged -= new EventHandler(stateManager.OnIPAddressesChanged);
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e)) throw;
                        DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                        if (exception == null) exception = e;
                    }
                }
                if (lclNeighborManager != null)
                {
                    lclNeighborManager.NeighborConnected -= new EventHandler(OnNeighborConnected);
                    lclNeighborManager.NeighborOpened -= new EventHandler(this.securityManager.OnNeighborOpened);
                    this.securityManager.OnNeighborAuthenticated -= new EventHandler(this.OnNeighborAuthenticated);
                    lclNeighborManager.Online -= new EventHandler(FireOnline);
                    lclNeighborManager.Offline -= new EventHandler(FireOffline);
                    try
                    {
                        lclNeighborManager.Shutdown(graceful, timeoutHelper.RemainingTime());
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e)) throw;
                        DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                        if (exception == null) exception = e;
                    }

                    // unregister for neighbor close events once shutdown has completed
                    lclNeighborManager.NeighborClosed -= new EventHandler<PeerNeighborCloseEventArgs>(OnNeighborClosed);
                    lclNeighborManager.NeighborClosing -= new EventHandler<PeerNeighborCloseEventArgs>(OnNeighborClosing);
                    lclNeighborManager.Close();
                }

                if (lclConnector != null)
                {
                    try
                    {
                        lclConnector.Close();
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e)) throw;
                        DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                        if (exception == null) exception = e;
                    }
                }

                if (lclFlooder != null)
                {
                    try
                    {
                        lclFlooder.Close();
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e)) throw;
                        DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                        if (exception == null) exception = e;
                    }
                }

            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e)) throw;
                if (exception == null) exception = e;
            }

            // reset object for next call to open
            EventHandler abortedHandler = null;
            lock (ThisLock)
            {
                // clear out old components (so they can be garbage collected)
                neighborManager = null;
                connector = null;
                maintainer = null;
                flooder = null;
                ipHelper = null;
                service = null;

                // reset generated config
                config = null;
                meshId = null;
                abortedHandler = Aborted;
            }

            // Notify anyone who is interested that abort has occured 
            if (!graceful && abortedHandler != null)
            {
                try
                {
                    abortedHandler(this, EventArgs.Empty);
                }
                catch (Exception e)
                {
                    if (Fx.IsFatal(e)) throw;
                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                    if (exception == null) exception = e;
                }
            }

            if (DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.PeerNodeClosed, SR.GetString(SR.TraceCodePeerNodeClosed), this.traceRecord, this, null);
            }
            if (exception != null && graceful == true)                          // Swallows all non fatal exceptions during Abort
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
            }
        }
 public PeerService(PeerNodeConfig config, ChannelCallback channelCallback, GetNeighborCallback getNeighborCallback, Dictionary<System.Type, object> services) : this(config, channelCallback, getNeighborCallback, services, null)
 {
 }