override public void Activate() {
if(!_active) {
return;
}
ConnectionList cons = _node.ConnectionTable.GetConnections(Connection.StringToMainType(struc_chota));
// Trim and add OUTSIDE of the lock!
List<Edge> to_trim = new List<Edge>();
List<Address> to_add = new List<Address>();
lock(_sync) {
_node_rank_list.Sort( _cmp );
// Find the guys to trim....
for (int i = _node_rank_list.Count - 1; i >= MAX_CHOTA && i > 0; i--) {
NodeRankInformation node_rank = (NodeRankInformation) _node_rank_list[i];
// Must remove from _dest_to_node_rank to prevent memory leak
_dest_to_node_rank.Remove(node_rank.Addr);
// Now check to see if ChotaCO owns this connections and add to_trim if it does
int idx = cons.IndexOf(node_rank.Addr);
if(idx >= 0 && cons[idx].ConType.Equals(struc_chota)) {
to_trim.Add(cons[idx].Edge);
}
}
// Don't keep around stale state
if(_node_rank_list.Count > MAX_CHOTA) {
_node_rank_list.RemoveRange(MAX_CHOTA, _node_rank_list.Count - MAX_CHOTA);
}
// Find guys to connect to!
for (int i = 0; i < _node_rank_list.Count && i < MAX_CHOTA; i++) {
//we are traversing the list in descending order of
NodeRankInformation node_rank = (NodeRankInformation) _node_rank_list[i];
if (node_rank.Count < MIN_SCORE_THRESHOLD ) {
//too low score to create a connection
continue;
} else if(cons.IndexOf(node_rank.Addr) >= 0) {
// already have a connection to that node!
continue;
}
to_add.Add(node_rank.Addr);
}
}
foreach(Edge e in to_trim) {
_node.GracefullyClose(e, "From Chota, low score trim.");
}
foreach(Address addr in to_add) {
ConnectTo(addr, struc_chota);
}
}