public void receiveAndSendHello(RingInfo ringInfo, Peer peer, Neighbor neighbor, BinaryWriter writer) { if(!peer.node.syncCommunicationPoint.Address.Equals(neighbor.peer.node.syncCommunicationPoint.Address)) //REVISIT: alert security system return; neighbor.peer.node.syncCommunicationPoint = peer.node.syncCommunicationPoint; neighbor.peer.node.asyncCommunicationPoint = peer.node.asyncCommunicationPoint; neighbor.peer.IE = peer.IE; byte[] message = new byte[Constants.WRITEBUFFSIZE]; MemoryStream stream; try { User user = User.getInstance(); BinaryFormatter serializer = new BinaryFormatter(); stream = new MemoryStream(message); NetLib.insertEntropyHeader(serializer, stream); serializer.Serialize(stream, Constants.MessageTypes.MSG_HELLO); serializer.Serialize(stream, ringInfo.ring.ringID); serializer.Serialize(stream, ringInfo.token); serializer.Serialize(stream, new Peer(user.node, user.publicUserInfo, ringInfo.IE)); writer.Write(message); writer.Flush(); } catch (Exception e) { int x = 2; } }
/// <summary> /// Returns the list of best choices for neighbors to send out the query request to. /// The list is not sorted. REVISIT: perhaps the list should be sorted /// </summary> /// <param name="query"></param> /// <returns></returns> private Neighbor[] determineBestChoiceNeighbors(string[] query) { Neighbor[] bestChoiceNeighbors = new Neighbor[neighbors.Count]; uint insertIndex = 0; RelevanceRanking ranker = RelevanceRanking.getInstance(); float score = 0.0f; foreach (Neighbor neighbor in neighbors) { score = ranker.calculateScore(query, neighbor.peer.IE); if (score >= Constants.PEER_SELECTION_THRESHOLD) bestChoiceNeighbors[insertIndex++] = neighbor; } if (insertIndex == 0) //all neighbors had a score of 0.0 { foreach (Neighbor neighbor in neighbors) { if (neighbor == null || insertIndex >= Constants.MAX_PEERS_TO_QUERY) break; bestChoiceNeighbors[insertIndex++] = neighbor; } } return bestChoiceNeighbors; }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="writer"></param> private void processHelloRequest(BinaryReader reader, BinaryWriter writer) { try { BinaryFormatter deserializer = new BinaryFormatter(); uint ringID = (uint)deserializer.Deserialize(reader.BaseStream); ulong token = (ulong)deserializer.Deserialize(reader.BaseStream); Peer peer = (Peer)deserializer.Deserialize(reader.BaseStream); RingInfo ringInfo = RingInfo.findRingInfoByID(User.getInstance().ringsInfo, ringID); Neighbor neighbor = RingInfo.findNeighbor(ringInfo.neighbors, token); if(neighbor == null) { neighbor = new Neighbor(peer, new NeighborProxy()); ringInfo.neighbors.Add(neighbor); } neighbor.neighborProxy.receiveAndSendHello(ringInfo, peer, neighbor, writer); } catch (Exception e) { int x = 2; } }