Ejemplo n.º 1
0
        /// <summary>
        /// Creates the port for the specified port shorthand.
        /// </summary>
        /// <param name="portShorthand">The port shorthand.</param>
        /// <returns>The device port.</returns>
        protected virtual IDevicePort CreatePort(string portShorthand)
        {
            // Try TCP first because it is the most complicated

            if (portShorthand.StartsWith("TCP"))
            {
                try
                {
                    return(DevicePortTcp.FromString(portShorthand));
                }
                catch (Exception)
                {
                    //Do nothing
                }
            }

            int portNum;

            if (Int32.TryParse(portShorthand, out portNum))
            {
                return(new DevicePortNumber(portNum));
            }

            return(new DevicePortName(portShorthand));
        }
Ejemplo n.º 2
0
        public override bool Equals(object obj)
        {
            DevicePortTcp dpt = obj as DevicePortTcp;

            if (object.ReferenceEquals(dpt, null))
            {
                return(false);
            }

            return(this.Address == dpt.Address &&
                   this.Port == dpt.Port &&
                   this.ReplyAddress == dpt.ReplyAddress &&
                   this.ReplyPort == dpt.ReplyPort);
        }
Ejemplo n.º 3
0
        public static DevicePortTcp FromString(string tcpString)
        {
            if (!tcpString.StartsWith("TCP"))
            {
                throw new InvalidCastException(String.Format("{0} is an invalid TCP string.", tcpString));
            }

            string[] tcpParts = tcpString.Split(',');
            tcpParts[0] = tcpParts[0].Substring(4); // removes "TCP="

            string[] deviceIp = tcpParts[0].Split(':');

            if (String.Equals(deviceIp[0], "none", StringComparison.CurrentCultureIgnoreCase))
            {
                return(new DevicePortTcp(null));
            }

            IPAddress     ipAddress = IPAddress.Parse(deviceIp[0]);
            DevicePortTcp dpt       = new DevicePortTcp(ipAddress);

            if (deviceIp.Length == 2)
            {
                dpt.Port = Int32.Parse(deviceIp[1]);
            }

            if (tcpParts.Length == 2)
            {
                tcpParts[1] = tcpParts[1].Substring(6); // removes "Reply="
                string[] replyIp = tcpParts[1].Split(':');

                IPAddress replyAddress = IPAddress.Parse(replyIp[0]);
                dpt.ReplyAddress = replyAddress;

                if (replyIp.Length == 2)
                {
                    dpt.ReplyPort = Int32.Parse(replyIp[1]);
                }
            }

            return(dpt);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a port from the specified settings node tree.
        /// </summary>
        /// <param name="portNode">The port node.</param>
        /// <returns>The device port.</returns>
        /// <exception cref="System.Exception">
        /// Invalid port format.
        /// </exception>
        protected virtual IDevicePort CreatePort(SettingsNode portNode)
        {
            SettingsNode tcpAddrNode = null;

            if (portNode.ContainsName("Address"))
            {
                tcpAddrNode = portNode["Address"];
            }
            else if (portNode.ContainsName("IPAddress"))
            {
                tcpAddrNode = portNode["IPAddress"];
            }
            else if (portNode.ContainsName("IpAddress"))
            {
                tcpAddrNode = portNode["IpAddress"];
            }
            else if (portNode.ContainsName("TCPAddress"))
            {
                tcpAddrNode = portNode["TCPAddress"];
            }
            else if (portNode.ContainsName("TcpAddress"))
            {
                tcpAddrNode = portNode["TcpAddress"];
            }

            if (tcpAddrNode != null)
            {
                string    ipAddrName = tcpAddrNode.GetValueAs <string>();
                IPAddress ipAddress;

                if (!IPAddress.TryParse(ipAddrName, out ipAddress))
                {
                    throw new Exception(String.Format("'{0}' is an invalid IP address.", ipAddrName));
                }

                DevicePortTcp tcpPort = new DevicePortTcp(ipAddress);

                if (portNode.ContainsName("Port"))
                {
                    tcpPort.Port = portNode["Port"].GetValueAs <int>();
                }

                if (portNode.ContainsName("ReplyAddress"))
                {
                    string    replyAddrName = portNode["ReplyAddress"].GetValueAs <string>();
                    IPAddress replyAddress;

                    if (!IPAddress.TryParse(replyAddrName, out replyAddress))
                    {
                        throw new Exception(String.Format("'{0}' is an invalid reply IP address.", replyAddrName));
                    }

                    tcpPort.ReplyAddress = replyAddress;

                    if (portNode.ContainsName("ReplyPort"))
                    {
                        tcpPort.ReplyPort = portNode["ReplyPort"].GetValueAs <int>();
                    }
                }

                return(tcpPort);
            }

            if (portNode.ContainsName("Number"))
            {
                int portNum = portNode["Number"].GetValueAs <int>();
                return(new DevicePortNumber(portNum));
            }

            if (portNode.ContainsName("Name"))
            {
                string portName = portNode["Name"].GetValueAs <string>();
                return(new DevicePortName(portName));
            }

            if (portNode.HasAValue)
            {
                string portShorthand = portNode.GetValueAs <string>();
                return(CreatePort(portShorthand));
            }

            throw new Exception("Invalid port format.");
        }