public List<AHAddress> FindMissing(bool log)
{
if(log) {
Console.WriteLine("Checking ring...");
}
Dictionary<AHAddress, bool> found = new Dictionary<AHAddress, bool>();
if(Nodes.Count == 0) {
return new List<AHAddress>(0);
}
Address start_addr = Nodes.Keys[0];
Address curr_addr = start_addr;
while(found.Count < Nodes.Count) {
found[curr_addr as AHAddress] = true;
Node node = Nodes[curr_addr].Node;
ConnectionTable con_table = node.ConnectionTable;
Connection con = null;
try {
con = con_table.GetLeftStructuredNeighborOf((AHAddress) curr_addr);
} catch {
if(log) {
Console.WriteLine("Found no connection.");
}
break;
}
if(log) {
Console.WriteLine("Hop {2}\t Address {0}\n\t Connection to left {1}\n", curr_addr, con, found.Count);
}
Address next_addr = con.Address;
Connection lc = null;
try {
Node tnode = Nodes[next_addr].Node;
lc = tnode.ConnectionTable.GetRightStructuredNeighborOf((AHAddress) next_addr);
} catch {}
if( (lc == null) || !curr_addr.Equals(lc.Address)) {
if(log) {
if(lc != null) {
Console.WriteLine(curr_addr + " != " + lc.Address);
}
Console.WriteLine("Right had edge, but left has no record of it!\n{0} != {1}", con, lc);
}
break;
}
curr_addr = next_addr;
if(curr_addr.Equals(start_addr)) {
break;
}
}
List<AHAddress> missing = new List<AHAddress>();
if(found.Count == Nodes.Count) {
if(log) {
Console.WriteLine("Ring properly formed!");
}
} else {
foreach(AHAddress addr in Nodes.Keys) {
if(!found.ContainsKey(addr)) {
missing.Add(addr);
}
}
}
if(found.Count < CurrentNetworkSize) {
// A node must be registered, but uncreated
missing.Add(default(AHAddress));
}
return missing;
}