示例#1
0
        public void GetNetwork()
        {
            var neighbours = File.ReadAllLines("NB.txt");

            foreach (string n in neighbours)
            {
                NeighbourNodes.Add(int.Parse(n));
            }
        }
示例#2
0
        private void Receive()
        {
            while (_active)
            {
                // Make sure that we call the blocking Receive function
                // only if there is data waiting
                if (_udpReceive.Available == 0)
                {
                    continue;
                }
                byte[] bytes          = _udpReceive.Receive(ref _receiveEndPoint);
                Packet receivedPacket = new Packet(bytes);

                // Do not Process Packets if they are not coming from
                // Physical Neighbours
                if (!NeighbourNodes.Contains(receivedPacket.NodeSource))
                {
                    continue;
                }

                // Do not Process packets when they already exists
                // In previous seen packets
                if (_previousSeenPackets[receivedPacket.NodeOriginalSource] >=
                    receivedPacket.NodeOriginalSourceCount)
                {
                    continue;
                }

                _previousSeenPackets[receivedPacket.NodeOriginalSource] =
                    receivedPacket.NodeOriginalSourceCount;

                // Check if Current Message is Broadcast Message
                // Send Packet To All Neighbours
                if (receivedPacket.NodeDestination == -1)
                {
                    // Prepare Packet
                    Packet newPacket = PreparePacket(receivedPacket);
                    OnBroadcastMessageReceived(new MessageArgs(newPacket.NodeOriginalSource, newPacket.NodeDestination, newPacket.Info, newPacket.IsAcknoledgment == 1));

                    // Enqueue Packet for Send
                    _packetQueueToSend.Enqueue(newPacket);

                    newPacket.PrintBroadcastInfo();
                }
                else if (Id == receivedPacket.NodeDestination)
                {
                    // My Message

                    // This Package is not Acknowledgment
                    if (receivedPacket.IsAcknoledgment != 1)
                    {
                        OnMessageReceived(new MessageArgs(receivedPacket.NodeOriginalSource, receivedPacket.NodeDestination, receivedPacket.Info, receivedPacket.IsAcknoledgment == 1));

                        #region Prepare Acknowledge Packet

                        Int64  timeCount = Utility.GetTimeCount();
                        Packet newPacket = new Packet()
                        {
                            NodeOriginalSource      = Id,
                            NodeSource              = Id,
                            NodeDestination         = receivedPacket.NodeOriginalSource,
                            IsAcknoledgment         = 1,
                            NodeOriginalSourceCount = timeCount,
                            AcknoledgmentCount      = receivedPacket.NodeOriginalSourceCount,
                            InfoSize = 4
                        };
                        newPacket.Info = new byte[newPacket.InfoSize];

                        // Enqueue Packet for Send
                        _packetQueueToSend.Enqueue(newPacket);

                        newPacket.PrintAcknoledgmentInfo();

                        #endregion
                    }
                    else // Acknowledge
                    {
                        OnMessageAcknowledged(new MessageArgs(receivedPacket.NodeOriginalSource, receivedPacket.NodeDestination, receivedPacket.Info, receivedPacket.IsAcknoledgment == 1));


                        // Remove from List
                        lock (mutex)
                        {
                            Packet founded = null;
                            foreach (Packet p in _packetListForAcknoledge)
                            {
                                if (p.NodeOriginalSourceCount ==
                                    receivedPacket.AcknoledgmentCount)
                                {
                                    founded = p;
                                    break;
                                }
                            }
                            if (founded != null)
                            {
                                _packetListForAcknoledge.Remove(founded);
                            }
                        }
                    }
                }
                else // Send to Next Node Hop
                {
                    OnMessageRelayed(new MessageArgs(receivedPacket.NodeOriginalSource, receivedPacket.NodeDestination, receivedPacket.Info, receivedPacket.IsAcknoledgment == 1));


                    // Prepare Packet
                    Packet newPacket = PreparePacket(receivedPacket);

                    // Enqueue Packet for Send
                    _packetQueueToSend.Enqueue(newPacket);

                    newPacket.PrintRelayInfo();
                }
            }
        }