private object QueryFind(NodeContact[] initialContacts, BinaryNumber nodeID, DhtRpcType queryType) { if (initialContacts.Length < 1) { return(null); } List <NodeContact> seenContacts = new List <NodeContact>(initialContacts); List <NodeContact> learnedNotQueriedContacts = new List <NodeContact>(initialContacts); List <NodeContact> respondedContacts = new List <NodeContact>(); List <PeerEndPoint> receivedPeers = null; int alpha = KADEMLIA_ALPHA; bool finalRound = false; bool checkTerminationCondition = false; if (queryType == DhtRpcType.FIND_PEERS) { receivedPeers = new List <PeerEndPoint>(); } NodeContact previousClosestSeenContact = KBucket.SelectClosestContacts(seenContacts, nodeID, 1)[0]; while (true) { NodeContact[] alphaContacts; //pick alpha contacts to query from learned contacts lock (learnedNotQueriedContacts) { alphaContacts = KBucket.SelectClosestContacts(learnedNotQueriedContacts, nodeID, alpha); //remove selected alpha contacts from learned not queries contacts list foreach (NodeContact alphaContact in alphaContacts) { learnedNotQueriedContacts.Remove(alphaContact); } } if (alphaContacts.Length < 1) { checkTerminationCondition = true; } else { object lockObj = new object(); lock (lockObj) { //query each alpha contact async foreach (NodeContact alphaContact in alphaContacts) { Thread t = new Thread(delegate(object state) { DhtRpcPacket response; if (queryType == DhtRpcType.FIND_NODE) { response = Query(DhtRpcPacket.CreateFindNodePacketQuery(_currentNode, nodeID), alphaContact); } else { response = Query(DhtRpcPacket.CreateFindPeersPacketQuery(_currentNode, nodeID), alphaContact); } if ((response == null) || (response.Type != queryType)) { //time out or error //ignore contact by removing from seen contacts list lock (seenContacts) { seenContacts.Remove(alphaContact); } return; } //got reply! if ((queryType == DhtRpcType.FIND_PEERS) && (response.Peers.Length > 0)) { lock (receivedPeers) { foreach (PeerEndPoint peer in response.Peers) { if (!receivedPeers.Contains(peer)) { receivedPeers.Add(peer); } } } } //add alpha contact to responded contacts list lock (respondedContacts) { if (!respondedContacts.Contains(alphaContact)) { respondedContacts.Add(alphaContact); } } //add received contacts to learned contacts list lock (seenContacts) { lock (learnedNotQueriedContacts) { foreach (NodeContact contact in response.Contacts) { if (!seenContacts.Contains(contact)) { seenContacts.Add(contact); learnedNotQueriedContacts.Add(contact); } } } } //no pulse for final round to wait for all k contacts to respond. this allows any failed node contact to be removed from seen contacts list during the wait. lock (lockObj) { Monitor.Pulse(lockObj); } }); t.IsBackground = true; t.Start(); } //wait for any of the node contact to return new contacts if (Monitor.Wait(lockObj, QUERY_TIMEOUT)) { //got reply or final round! NodeContact currentClosestSeenContact; lock (seenContacts) { currentClosestSeenContact = KBucket.SelectClosestContacts(seenContacts, nodeID, 1)[0]; } BinaryNumber previousDistance = nodeID ^ previousClosestSeenContact.NodeID; BinaryNumber currentDistance = nodeID ^ currentClosestSeenContact.NodeID; if (previousDistance <= currentDistance) { //current round failed to return a node contact any closer than the closest already seen if (finalRound) { //final round over, check for termination condition checkTerminationCondition = true; } else { //resend query to k closest node not already queried finalRound = true; alpha = KADEMLIA_K; } } else { //current closest seen contact is closer than previous closest seen contact previousClosestSeenContact = currentClosestSeenContact; finalRound = false; alpha = KADEMLIA_ALPHA; } } } } if (checkTerminationCondition) { checkTerminationCondition = false; //reset if (queryType == DhtRpcType.FIND_PEERS) { //check only in final round to get most peers lock (receivedPeers) { if (receivedPeers.Count > 0) { return(receivedPeers.ToArray()); } return(null); } } //lookup terminates when k closest seen contacts have responded NodeContact[] kClosestSeenContacts; lock (seenContacts) { kClosestSeenContacts = KBucket.SelectClosestContacts(seenContacts, nodeID, KADEMLIA_K); } lock (respondedContacts) { bool success = true; foreach (NodeContact contact in kClosestSeenContacts) { if (!respondedContacts.Contains(contact)) { success = false; break; } } if (success) { return(kClosestSeenContacts); } if (alphaContacts.Length < 1) { return(KBucket.SelectClosestContacts(respondedContacts, nodeID, KADEMLIA_K)); } } } } }
private void QueryFindAsync(object state) { try { object[] parameters = state as object[]; object lockObj = parameters[0] as object; RpcQueryType queryType = (RpcQueryType)parameters[1]; NodeContact contact = parameters[2] as NodeContact; BinaryID nodeID = parameters[3] as BinaryID; List <NodeContact> availableContacts = parameters[4] as List <NodeContact>; List <NodeContact> respondedContacts = parameters[5] as List <NodeContact>; List <NodeContact> failedContacts = parameters[6] as List <NodeContact>; List <NodeContact> receivedContacts = parameters[7] as List <NodeContact>; DhtRpcPacket responsePacket; if (queryType == RpcQueryType.FIND_NODE) { responsePacket = Query(DhtRpcPacket.CreateFindNodePacketQuery(_currentNode, nodeID), contact); } else { responsePacket = Query(DhtRpcPacket.CreateFindPeersPacketQuery(_currentNode, nodeID), contact); } if ((responsePacket == null) || (responsePacket.QueryType != queryType)) { //time out //add contact to failed contacts lock (failedContacts) { if (!failedContacts.Contains(contact)) { failedContacts.Add(contact); } } return; } //got reply! switch (queryType) { case RpcQueryType.FIND_NODE: lock (receivedContacts) { lock (respondedContacts) { //add contact to responded contacts list if (!respondedContacts.Contains(contact)) { respondedContacts.Add(contact); } lock (failedContacts) { //add received contacts to received contacts list foreach (NodeContact receivedContact in responsePacket.Contacts) { if (!respondedContacts.Contains(receivedContact) && !failedContacts.Contains(receivedContact)) { receivedContacts.Add(receivedContact); } } } } //add received contacts to available contacts list lock (availableContacts) { foreach (NodeContact receivedContact in receivedContacts) { if (!availableContacts.Contains(receivedContact)) { availableContacts.Add(receivedContact); } } } } if (responsePacket.Contacts.Length > 0) { //pulse only if the contact has sent next level contacts list lock (lockObj) { Monitor.Pulse(lockObj); } } break; case RpcQueryType.FIND_PEERS: if (responsePacket.Peers.Length > 0) { List <PeerEndPoint> receivedPeers = parameters[8] as List <PeerEndPoint>; lock (receivedPeers) { foreach (PeerEndPoint peer in responsePacket.Peers) { if (!receivedPeers.Contains(peer)) { receivedPeers.Add(peer); } } } lock (lockObj) { Monitor.Pulse(lockObj); } } break; } } catch { } }