示例#1
0
        public bool AddNode(IdpNode node)
        {
            bool result = false;

            if (node.Address == UnassignedAddress)
            {
                _unenumeratedNodes.Add(node);
                result = true;
            }
            else
            {
                if (FindNode(node.Address) == null)
                {
                    _enumeratedNodes[node.Address] = node;
                    result = true;
                }
            }

            if (result)
            {
                node.TransmitEndpoint = this;
                node.Enabled          = true;
            }

            return(result);
        }
示例#2
0
        public void MarkUnenumerated(IdpNode node)
        {
            if (!_unenumeratedNodes.Contains(node))
            {
                _unenumeratedNodes.Add(node);
            }

            if (_enumeratedNodes.ContainsKey(node.Address))
            {
                _enumeratedNodes.Remove(node.Address);
            }
        }
示例#3
0
        public void RemoveNode(IdpNode node)
        {
            if (node.Address == UnassignedAddress)
            {
                _unenumeratedNodes.Remove(node);
            }
            else if (FindNode(node.Address) != null)
            {
                _enumeratedNodes.Remove(node.Address);
            }

            node.Address = UnassignedAddress;
        }
示例#4
0
        public bool MarkEnumerated(IdpNode node)
        {
            bool result = false;

            if (FindNode(node.Address) == null)
            {
                _unenumeratedNodes.Remove(node);

                _enumeratedNodes[node.Address] = node;

                result = true;
            }

            return(result);
        }
示例#5
0
        public bool Route(IdpPacket packet)
        {
            var destination = packet.Destination;
            var source      = packet.Source;

            packet.ResetReadToPayload();
            var command = packet.Read <UInt16>();

            packet.ResetRead();

            //Debug.WriteLine($"Router: 0x{Address.ToString("X4")} from: 0x{source.ToString("X4")}, to: 0x{ destination.ToString("X4")} Command: 0x{ command.ToString("X4")} ({(NodeCommand)command})");

            if (destination == 0 && Address != UnassignedAddress)
            {
                foreach (var adaptor in _adaptors.Values)
                {
                    if (!_routingTable.ContainsKey(source) || adaptor != _adaptors[_routingTable[source]])
                    {
                        packet.ResetRead();

                        adaptor.Transmit(packet);
                    }
                }

                foreach (var node in _enumeratedNodes.Values)
                {
                    packet.ResetRead();

                    var packetResponse = node.ProcessPacket(packet);

                    if (packetResponse != null)
                    {
                        Route(packetResponse);
                    }
                }


                packet.ResetRead();

                var response = ProcessPacket(packet);

                if (response != null)
                {
                    Route(response);
                }

                return(true);
            }
            else if (destination == RouterPollAddress && Address != UnassignedAddress)
            {
                var response = ProcessPacket(packet);

                if (response != null)
                {
                    return(Route(response));
                }

                return(false);
            }
            else
            {
                IdpNode node = null;

                if (destination == Address)
                {
                    node = this;
                }
                else
                {
                    node = FindNode(destination);
                }

                if (node != null)
                {
                    var responsePacket = node.ProcessPacket(packet);

                    if (responsePacket != null)
                    {
                        return(Route(responsePacket));
                    }

                    return(true);
                }
                else
                {
                    if (_routingTable.ContainsKey(destination))
                    {
                        return(_adaptors[_routingTable[destination]].Transmit(packet));
                    }
                    else
                    {
                        if (destination == 0xFFFF && Address != 0xFFFF)
                        {
                            return(false);
                        }

                        return(_adaptors.FirstOrDefault().Value?.Transmit(packet) ?? false);
                    }
                }
            }
        }