public ConnectionState GetReplacement(ConnectionTableState cts, Connection c, ConnectionState c1, ConnectionState c2) { if( (c1.Edge is RelayEdge) && (c2.Edge is RelayEdge) ) { return GetIdx(c1) <= GetIdx(c2) ? c1 : c2; } return _fallback.GetReplacement(cts,c,c1,c2); }
public ConnectionEventArgs(Connection c, ConnectionState cs, int idx, ConnectionTableState old, ConnectionTableState news) { Connection = c; ConnectionState = cs; Index = idx; OldState = old; NewState = news; }
/** * Prefered constructor for a Connection */ public Connection(Edge e, Address a, string connectiontype, StatusMessage sm, LinkMessage peerlm) { Address = a; ConType = String.Intern(connectiontype); CreationTime = DateTime.UtcNow; MainType = StringToMainType(ConType); //Mutable state: var cs = new ConnectionState(e, sm, peerlm, false); _state = new Mutable<ConnectionState>(cs); }
/** return the old state, and new state */ public Pair<ConnectionState,ConnectionState> SetStatus(StatusMessage sm) { var res = _state.Update(delegate(ConnectionState old_state) { if( old_state.Disconnected ) { throw new Exception(String.Format("Connection: {0} is disconnected",this)); } var new_state = new ConnectionState(old_state.Edge, sm, old_state.PeerLinkMessage, false); return new_state; }); var ev = StateChangeEvent; if( null != ev ) { ev(this, res); } return res; }
public Pair<ConnectionState,ConnectionState> SetState(ConnectionState cs) { var res = _state.Update(delegate(ConnectionState old_state) { if( old_state.Disconnected ) { throw new Exception(String.Format("Connection: {0} is disconnected",this)); } return cs; }); if( res.First != res.Second ) { //Only send the event if there is an actual change var ev = StateChangeEvent; if( null != ev ) { ev(this, res); } } return res; }
/** * This sets the ConState to new_cs and returns the old * ConState. * * This method knows about the allowable state transitions. * @param success is set to false if we can't do the state transition. * @return the value of ConState prior to the method being called */ protected ConnectionState SetConState(ConnectionState new_cs, out bool success) { ConnectionState old_state; success = false; lock( _sync ) { old_state = _con_state; if( old_state == new_cs ) { //This is not a state change return old_state; } if( new_cs == Node.ConnectionState.Joining ) { success = (old_state == Node.ConnectionState.Offline); } else if( new_cs == Node.ConnectionState.Connected ) { success = (old_state == Node.ConnectionState.Joining) || (old_state == Node.ConnectionState.SeekingConnections); } else if( new_cs == Node.ConnectionState.SeekingConnections ) { success = (old_state == Node.ConnectionState.Connected); } else if( new_cs == Node.ConnectionState.Leaving ) { success = (old_state != Node.ConnectionState.Disconnected); } else if( new_cs == Node.ConnectionState.Disconnected ) { success = (old_state == Node.ConnectionState.Leaving ); } else if( new_cs == Node.ConnectionState.Offline ) { // We can never move into the Offline state. success = false; } /* * Now let's update _con_state */ if( success ) { _con_state = new_cs; } } return old_state; }
/** * Send the StateChange event */ protected void SendStateChange(ConnectionState new_state) { if( new_state == Node.ConnectionState.Joining && ArrivalEvent != null) { ArrivalEvent(this, null); } if( new_state == Node.ConnectionState.Leaving && DepartureEvent != null) { DepartureEvent(this, null); } StateChangeEvent(this, new_state); }
/// <summary>If the node is connecting, we need TAs, if its in any other /// state, we'll deal with what we have.</summary> protected void HandleTADiscoveryState(Node n, ConnectionState newstate) { ImmutableList<Discovery> discs = _ta_discovery; if(newstate == ConnectionState.Joining || newstate == ConnectionState.SeekingConnections) { foreach(Discovery disc in discs) { disc.BeginFindingTAs(); } } else if(newstate == ConnectionState.Leaving || newstate == ConnectionState.Disconnected) { foreach(Discovery disc in discs) { disc.Stop(); } } else { foreach(Discovery disc in discs) { disc.EndFindingTAs(); } } }
protected int GetIdx(ConnectionState cs) { var ue = (RelayEdge)cs.Edge; if( ue.IsInbound ) { return ue.LocalID; } else { return ue.RemoteID; } }
/** * Prefered constructor for a Connection */ public Connection(Edge e, Address a, string connectiontype, StatusMessage sm, LinkMessage peerlm) { if( null == a ) { throw new System.ArgumentNullException( String.Format("Address cannot be null in Connection: Edge: {0} contype: {1}", e, connectiontype)); } Address = a; ConType = String.Intern(connectiontype); CreationTime = DateTime.UtcNow; MainType = StringToMainType(ConType); //Mutable state: var cs = new ConnectionState(e, sm, peerlm, false); _state = new Mutable<ConnectionState>(cs); }
/** return the old state, and new state */ public Pair<ConnectionState,ConnectionState> SetStatus(StatusMessage sm) { var res = _state.Update(delegate(ConnectionState old_state) { if( old_state.Disconnected ) { throw new Exception(String.Format("Connection: {0} is disconnected",this)); } var new_state = new ConnectionState(old_state.Edge, sm, old_state.PeerLinkMessage, false); return new_state; }); ProtocolLog.WriteIf(ProtocolLog.Connections, String.Format( "SetStatus called on {0}, old status: {1}, new status: {2}", this, res.First.StatusMessage, sm)); var ev = StateChangeEvent; if( null != ev ) { ev(this, res); } return res; }