Beispiel #1
0
        public static void AddPort(this Computer comp, PortState port)
        {
            if (port == null)
            {
                throw new ArgumentNullException(nameof(port));
            }

            if (port.Computer != null && port.Computer != comp)
            {
                throw new InvalidOperationException($"{nameof(port)} already has a Computer assigned to it");
            }

            if (port.Record == null)
            {
                throw new InvalidOperationException($"{nameof(port)}.{nameof(port.Record)} can not be null");
            }

            if (!PortManager.IsPortRecordRegistered(port.Record))
            {
                Logger.Log(LogLevel.Warning, $"Protocol '{port.Record.Protocol}' not registered, please correct, this will crash in 6.0.0");
                PortManager.RegisterPort(port.Record);
            }

            var ports = PortTable.GetOrCreateValue(comp);

            if (ports.ContainsKey(port.Record.Protocol))
            {
                return;
            }

            ports.Add(port.Record.Protocol, port);
            port.Computer = comp;
        }
Beispiel #2
0
        public static void AddPort(this Computer comp, PortRecord record)
        {
            record.ThrowNull(nameof(record));
            if (!PortManager.IsPortRecordRegistered(record))
            {
                Logger.Log(LogLevel.Warning, $"Protocol '{record.Protocol}' not registered, please correct, this will crash in 6.0.0");
                PortManager.RegisterPort(record);
            }

            comp.AddPort(record.CreateState(comp));
        }
Beispiel #3
0
        public static void LoadPortsFromChildren(Computer comp, IEnumerable <ElementInfo> children, bool clearAll)
        {
            if (clearAll)
            {
                comp.ClearPorts();
            }
            foreach (var element in children)
            {
                var protocol = element.Name;
                if (element.Attributes.GetString("Remove")?.ToLower() == "true")
                {
                    comp.RemovePort(protocol);
                    continue;
                }
                var portNumString = element.Attributes.GetString("Number", null);
                var displayName   = element.Attributes.GetString("Display", null);
                if (element.Content != null)
                {
                    displayName = element.Content;
                }
                var numElement     = element.Children.GetElement("Number");
                var displayElement = element.Children.GetElement("Display");
                if (numElement != null)
                {
                    portNumString = numElement.Content;
                }
                if (displayElement != null)
                {
                    displayName = displayElement.Content;
                }
                var record  = GetPortRecordFromProtocol(protocol);
                int portNum = -1;
                if (record == null)
                {
                    if (portNumString == null || displayName == null)
                    {
                        throw new ArgumentException($"Protocol '{protocol}' does not exist");
                    }

                    if (!int.TryParse(portNumString, out portNum))
                    {
                        throw new FormatException($"Unable to parse port number for protocol '{protocol}'");
                    }

                    PortManager.RegisterPort(protocol, displayName, portNum);
                }
                else if (portNumString != null && !int.TryParse(portNumString, out portNum))
                {
                    throw new FormatException($"Unable to parse port number for protocol '{protocol}'");
                }

                var state = comp.GetPortState(protocol);
                if (state != null)
                {
                    if (displayName != null)
                    {
                        state.DisplayName = displayName;
                    }
                    if (portNumString != null)
                    {
                        state.PortNumber = portNum;
                    }
                    continue;
                }

                comp.AddPort(protocol, portNum, displayName);
            }
        }