Ejemplo n.º 1
0
        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?
            }
        }