コード例 #1
0
ファイル: IdpRouter.cs プロジェクト: VitalElement/IdpProtocol
        private IdpResponseCode OnEnumerateAdaptorCommand(UInt16 source, UInt16 address, UInt32 transactionId, OutgoingTransaction outgoing)
        {
            var adaptor = GetNextUnenumeratedAdaptor();

            if (adaptor == null)
            {
                outgoing.Write(false);

                return(IdpResponseCode.OK);
            }
            else
            {
                adaptor.IsRenumerated = true;

                bool adaptorProbed = false;

                if (!adaptor.IsEnumerated)
                {
                    var request = new OutgoingTransaction((UInt16)NodeCommand.RouterDetect, transactionId, IdpCommandFlags.None).Write(address);

                    adaptorProbed = SendRequest(adaptor, source, 0xFFFF, request);

                    if (adaptorProbed)
                    {
                        _currentlyEnumeratingAdaptor = adaptor;
                    }
                }

                outgoing.Write(true).Write(adaptorProbed).WithResponseCode(IdpResponseCode.OK);

                SendRequest(source, outgoing);

                return(IdpResponseCode.Deferred);
            }
        }
コード例 #2
0
ファイル: IdpRouter.cs プロジェクト: VitalElement/IdpProtocol
        public bool AddAdaptor(IdpAdaptor adaptor)
        {
            bool result = false;

            adaptor.LocalPort = this;

            adaptor.Id = _nextAdaptorId;

            _adaptors[_nextAdaptorId] = adaptor;

            _nextAdaptorId++;

            return(result);
        }
コード例 #3
0
ファイル: IdpRouter.cs プロジェクト: VitalElement/IdpProtocol
        public IdpRouter() : base(RouterGuid, "Network.Router")
        {
            _unenumeratedNodes = new List <IdpNode>();
            _enumeratedNodes   = new Dictionary <UInt16, IdpNode>();
            _adaptors          = new Dictionary <UInt16, IdpAdaptor>();
            _routingTable      = new Dictionary <UInt16, UInt16>();

            Manager.RegisterCommand((UInt16)NodeCommand.RouterDetect, (i, o) =>
            {
                var address = i.Read <UInt16>();

                if (Address == UnassignedAddress)
                {
                    Address = address;
                    o.Write(true);
                    o.WithResponseCode(IdpResponseCode.OK);

                    SendRequest(i.Source, o);

                    return(IdpResponseCode.Deferred);
                }
                else
                {
                    o.Write(false);
                    return(IdpResponseCode.OK);
                }
            });

            Manager.RegisterCommand((UInt16)NodeCommand.RouterEnumerateNode, (i, o) =>
            {
                var address = i.Read <UInt16>();

                return(OnEnumerateNodesCommand(i.Source, address, o));
            });

            Manager.RegisterCommand((UInt16)NodeCommand.RouterEnumerateAdaptor, (i, o) =>
            {
                var address       = i.Read <UInt16>();
                var transactionId = i.Read <UInt32>();

                return(OnEnumerateAdaptorCommand(i.Source, address, transactionId, o));
            });

            Manager.RegisterCommand((UInt16)NodeCommand.RouterPrepareToEnumerateAdaptors, (i, o) =>
            {
                foreach (var adaptor in _adaptors.Values)
                {
                    if (!adaptor.IsActive)
                    {
                        adaptor.IsRenumerated = false;
                    }
                }

                return(IdpResponseCode.OK);
            });

            Manager.RegisterCommand((UInt16)NodeCommand.MarkAdaptorConnected, (i, o) =>
            {
                if (_currentlyEnumeratingAdaptor != null)
                {
                    _currentlyEnumeratingAdaptor.IsEnumerated = true;

                    _currentlyEnumeratingAdaptor = null;
                }
                else if (_lastAdaptorId != -1)
                {
                    _adaptors[(ushort)_lastAdaptorId].IsEnumerated = true;

                    _lastAdaptorId = -1;
                }

                return(IdpResponseCode.OK);
            });

            TransmitEndpoint = this;

            _nextAdaptorId = 1;
        }