/// <summary>
        ///	This factory method initializes the ConnectionMonitor single instance adding networks acording to the
        ///	native Connection Manager API and connections from the supplied configuration section
        ///	name.
        /// </summary>
        /// <param name="sectionName">
        ///	Name of the section in the configuration file (App.config), wich contains the different
        ///	types of connection
        /// </param>
        /// <returns>The <see cref="ConnectionMonitor"/> instance.</returns>
        public static ConnectionMonitor CreateFromConfiguration(string sectionName)
        {
            lock (_syncObject)
            {
                if (_instance == null)
                {
                    ConnectionSettingsSection configuration = ConfigurationManager.GetSection(sectionName) as ConnectionSettingsSection;

                    NetworkCollection networks = null;

                    if (configuration != null)
                    {
                        // Create the strategy for network detection
                        if (!String.IsNullOrEmpty(configuration.Networks.StrategyType))
                        {
                            Type strategyType = Type.GetType(configuration.Networks.StrategyType);
                            INetworkStatusStrategy statusService = (INetworkStatusStrategy)Activator.CreateInstance(strategyType);
                            networks = new NetworkCollection(statusService);
                        }
                        else
                        {
                            networks = new NetworkCollection();
                        }


                        foreach (NetworkElement networkConfig in configuration.Networks)
                        {
                            networks.Add(new Network(networkConfig.Name, networkConfig.Address));
                        }

                        ConnectionCollection connections = new ConnectionCollection();

                        foreach (ConnectionItemElement itemConnection in configuration.Connections)
                        {
                            connections.Add(ConnectionFactory.CreateConnection(itemConnection.Type, itemConnection.Price));
                        }

                        _instance = new ConnectionMonitor(connections, networks);
                    }
                    else
                    {
                        _instance = new ConnectionMonitor();
                    }

                    if (_instance.Connections.Count == 0)
                    {
                        _instance.Connections.Add(new NicConnection("NicConnection", 0));
                    }
                }
            }
            return(_instance);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance using the specified <see cref="ConnectionCollection"/> and
        /// <see cref="NetworkCollection"/>.
        /// </summary>
        /// <param name="connections"></param>
        /// <param name="networks"></param>
        public ConnectionMonitor(ConnectionCollection connections, NetworkCollection networks)
        {
            this.connections = connections;
            this.networks    = networks;

            if (this.connections.Count > 0)
            {
                foreach (Connection conn in this.connections)
                {
                    conn.StateChanged += OnStateChanged;
                }
            }

            this.connections.ConnectionAdded   += OnConnectionAdded;
            this.connections.ConnectionRemoved += OnConnectionRemoved;
        }