public void OnNeighborAuthenticated(IPeerNeighbor neighbor)
 {
     if (this.state == State.Created)
     {
         throw Fx.AssertAndThrow("Connector not expected to be in Created state");
     }
     if (!PeerNeighborStateHelper.IsAuthenticatedOrClosed(neighbor.State))
     {
         throw Fx.AssertAndThrow(string.Format(CultureInfo.InvariantCulture, "Neighbor state expected to be Authenticated or Closed, actual state: {0}", new object[] { neighbor.State }));
     }
     if (!neighbor.TrySetState(PeerNeighborState.Connecting))
     {
         if (neighbor.State < PeerNeighborState.Faulted)
         {
             throw Fx.AssertAndThrow(string.Format(CultureInfo.InvariantCulture, "Neighbor state expected to be Faulted or Closed, actual state: {0}", new object[] { neighbor.State }));
         }
     }
     else if (this.AddTimer(neighbor) && neighbor.IsInitiator)
     {
         if (this.neighborManager.ConnectedNeighborCount < this.config.MaxNeighbors)
         {
             this.SendConnect(neighbor);
         }
         else
         {
             this.neighborManager.CloseNeighbor(neighbor, PeerCloseReason.NodeBusy, PeerCloseInitiator.LocalNode);
         }
     }
 }
Ejemplo n.º 2
0
        // Process neighbor authenticated notification
        public void OnNeighborAuthenticated(IPeerNeighbor neighbor)
        {
            if (!(this.state != State.Created))
            {
                throw Fx.AssertAndThrow("Connector not expected to be in Created state");
            }

            if (!(PeerNeighborStateHelper.IsAuthenticatedOrClosed(neighbor.State)))
            {
                throw Fx.AssertAndThrow(string.Format(CultureInfo.InvariantCulture, "Neighbor state expected to be Authenticated or Closed, actual state: {0}", neighbor.State));
            }

            // setting the state fails if neighbor is already closed or closing
            // If so, we have nothing to do.
            if (!neighbor.TrySetState(PeerNeighborState.Connecting))
            {
                if (!(neighbor.State >= PeerNeighborState.Faulted))
                {
                    throw Fx.AssertAndThrow(string.Format(CultureInfo.InvariantCulture, "Neighbor state expected to be Faulted or Closed, actual state: {0}", neighbor.State));
                }
                return;
            }

            // Add a timer to timer table to transition the neighbor to connected state
            // within finite duration. The neighbor is closed if the timer fires and the
            // neighbor has not reached connected state.
            // The timer is not added if neighbor or connector are closed
            if (AddTimer(neighbor))
            {
                // Need to send connect message if the neighbor is the initiator
                if (neighbor.IsInitiator)
                {
                    if (this.neighborManager.ConnectedNeighborCount < this.config.MaxNeighbors)
                    {
                        SendConnect(neighbor);
                    }
                    else
                    {
                        // We have max connected neighbors already. So close this one.
                        this.neighborManager.CloseNeighbor(neighbor, PeerCloseReason.NodeBusy,
                                                           PeerCloseInitiator.LocalNode);
                    }
                }
            }
        }