internal void SetLocalRuntimePeer(NodePeerList list) { foreach (var peer in list.Peers) { if (peer.Code.ToString() == Engine.PEER_CODE) { localRuntimePeer = peer; break; } } }
private void Receive(string message) { //TextWriter tw = new StreamWriter("receive_" + this.ID.ToString() + ".txt", true); //tw.WriteLine(message); //tw.Close(); if (ValidateXmlToSchema(message)) { FieldGuid toPeerID = NodeBase.ToPeerFromXML(message); FieldGuid fromPeerID = NodeBase.FromPeerFromXML(message); if (toPeerID != null && fromPeerID != null) { //it's a peer-to-peer message //find the ToPeer in the local peer list NodePeer ToPeer = CommunicationManager.LocalPeerList.FindPeerByID(toPeerID); //find the FromPeer in the remote peer list NodePeer FromPeer = RemotePeerList.FindPeerByID(fromPeerID); if (ToPeer != null && FromPeer != null) { //see if this message is based on another node FieldGuid BasedOnNodeID = NodeBase.BasedOnNodeIDFromXML(message); NodeBase basedOnNode = null; if (BasedOnNodeID != null) { basedOnNode = ToPeer.ReceiveDeltaFromPeer(FromPeer, BasedOnNodeID); NodeBase msg = NodeBase.NodeFromXML(message, basedOnNode.GetChildrenRecursive()); CommunicationManager.SendDeltaToPeer(ToPeer, FromPeer, msg, basedOnNode); } else { NodeBase msg = NodeBase.NodeFromXML(message, null); CommunicationManager.SendToPeer(ToPeer, FromPeer, msg); } } else { //TODO - log this - undeliverable message (peer lists not up to date?) } } else { //it's a system message (not peer-to-peer) FieldNodeType nodeType = NodeBase.NodeTypeFromXML(message); if (nodeType.ToString() == typeof(NodePeerList).FullName) { lock (m_remotePeerList_Lock) //lock so we read/write in one operation { //When we receive a remote peer list, it is generally just a diff //from the last peer list. RemotePeerList = (NodePeerList)NodeBase.NodeFromXML( message, RemotePeerList.GetChildrenRecursive()); } } else { //TODO - log this? Unknown root message type? } } } else { //TODO - log this? Unknown garbage message? } }
/// <summary> /// When the local peer list changes, we have to send updates to /// the other end of this connection. /// </summary> /// <param name="newPeerList"></param> /// <param name="oldPeerList"></param> private void LocalPeerListChangedCallback(NodePeerList newPeerList, NodePeerList oldPeerList) { string s = newPeerList.ToXml(oldPeerList); Send(s); }
/// <summary> /// Unregisters a peer that was created by calling the /// RegisterLocalPeer function. Throws an exception if /// the peer is not a local peer. /// </summary> /// <param name="Peer"></param> /// <returns>True if successful, False otherwise</returns> public static bool UnregisterLocalPeer(NodePeer Peer) { if (Peer == null) { throw new ArgumentNullException("Peer"); } if (!Peer.IsLocal) { throw new ArgumentOutOfRangeException("Peer"); } lock (Instance.m_localPeerList_Lock) { if (Instance.m_localPeerList.Peers.Contains(Peer)) { Peer.Unregister(); //clears all event handlers LocalPeerList = LocalPeerList.Peers.Remove(Peer); return true; } else { return false; } } }
/// <summary> /// Registers a new local peer. The calling function has to pass /// in a Code that is a unique name for the peer. No other peer /// by this name can exist locally. Returns null if it already /// exists, or a new NodePeer object if successful. /// </summary> /// <param name="Code">Unique name for the new peer</param> /// <returns></returns> public static NodePeer RegisterLocalPeer(FieldIdentifier Code) { if (Code == null) { throw new ArgumentNullException("Code"); } lock (Instance.m_localPeerList_Lock) { //make sure "Local.{thisCode}" doesn't exist yet //(no duplicate peer names) bool found = false; foreach (NodePeer p in Instance.m_localPeerList.Peers) { if (p.Code == Code) { found = true; break; } } if (found) { return null; } else { NodePeer newPeer = NodePeer.BuildWith(Code); LocalPeerList = LocalPeerList.Peers.Append(newPeer); return newPeer; } } }