<commit_before>/*   
Copyright 2006 - 2010 Intel Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Collections;

namespace OpenSource.UPnP
{
    /// <summary>
    /// The object by which you can leverage UPnPDevices on your network.
    /// </summary>
    public sealed class UPnPControlPoint
    {
        private static int _mx = 5;
        public static int MX
        {
            get { return (_mx); }
            set { if (value > 0) _mx = value; }
        }
        public delegate void SearchHandler(IPEndPoint ResponseFromEndPoint, IPEndPoint ResponseReceivedOnEndPoint, Uri DescriptionLocation, String USN, String SearchTarget, int MaxAge);
        /// <summary>
        /// Triggered when an AsyncSearch result returns
        /// </summary>
        public event SearchHandler OnSearch;

        public delegate void CreateDeviceHandler(UPnPDevice Device, Uri DescriptionURL);
        /// <summary>
        /// Triggered when an Async CreateDevice returns...
        /// <para>
        /// Depracated. Use UPnPDeviceFactory</para>
        /// </summary>
        public event CreateDeviceHandler OnCreateDevice;

        private NetworkInfo NetInfo;
        private ArrayList SyncData;
        private DeviceNode SyncDevice;
        private Hashtable CreateTable;
        private SSDP SSDPServer;
        private Hashtable SSDPSessions;
        private LifeTimeMonitor Lifetime;

        private struct DeviceNode
        {
            public UPnPDevice TheDevice;
            public Uri URL;
        }

        /// <summary>
        /// This event is triggered when other UPnPDevices on your network are alive.
        /// </summary>
        public event SSDP.NotifyHandler OnNotify;

        /// <summary>
        /// Constructs a new Control Point, and waits for your commands and receives events
        /// </summary>
        public UPnPControlPoint()
        {
            CreateTable = Hashtable.Synchronized(new Hashtable());
            NetInfo = new NetworkInfo(new NetworkInfo.InterfaceHandler(NewInterface));
            SyncData = ArrayList.Synchronized(new ArrayList());
            SSDPSessions = Hashtable.Synchronized(new Hashtable());
            Lifetime = new LifeTimeMonitor();
            Lifetime.OnExpired += new LifeTimeMonitor.LifeTimeHandler(HandleExpired);

            SSDPServer = new SSDP(65535);
            SSDPServer.OnNotify += new SSDP.NotifyHandler(HandleNotify);
        }

        public UPnPControlPoint(NetworkInfo ni)
        {
            CreateTable = Hashtable.Synchronized(new Hashtable());
            NetInfo = ni;
            SyncData = ArrayList.Synchronized(new ArrayList());
            SSDPSessions = Hashtable.Synchronized(new Hashtable());
            Lifetime = new LifeTimeMonitor();
            Lifetime.OnExpired += new LifeTimeMonitor.LifeTimeHandler(HandleExpired);

            SSDPServer = new SSDP(65535);
            SSDPServer.OnNotify += new SSDP.NotifyHandler(HandleNotify);
        }

        ~UPnPControlPoint()
        {
        }

        private void PreProcessNotify(IPEndPoint source, String LocationURL, bool IsAlive, String USN, String ST, int MaxAge)
        {

        }

        private void HandleAlert(Object sender, Object RObj)
        {

        }

        private void NewInterface(NetworkInfo sender, IPAddress Intfce)
        {
        }

        private void HandleNotify(IPEndPoint source, IPEndPoint local, Uri LocationURL, bool IsAlive, String USN, String ST, int MaxAge, HTTPMessage Packet)
        {
            if (IsAlive && LocationURL != null) OpenSource.Utilities.EventLogger.Log(this, System.Diagnostics.EventLogEntryType.SuccessAudit, LocationURL.ToString());
            if (OnNotify != null) OnNotify(source, local, LocationURL, IsAlive, USN, ST, MaxAge, Packet);
        }

        /// <summary>
        /// Creates a device from a URL. [Depracated] use UPnPDeviceFactory
        /// </summary>
        /// <param name="DescriptionURL"></param>
        /// <param name="LifeTime"></param>
        public void CreateDeviceAsync(Uri DescriptionURL, int LifeTime)
        {
            //ToDo: Replace the failed callback
            UPnPDeviceFactory fac = new UPnPDeviceFactory(DescriptionURL, LifeTime, new UPnPDeviceFactory.UPnPDeviceHandler(HandleDeviceCreation), null, null, null);
            CreateTable[fac] = fac;
            Lifetime.Add(fac, 30);
        }

        private void HandleDeviceCreation(UPnPDeviceFactory Factory, UPnPDevice device, Uri URL)
        {
            Factory.Shutdown();
            if (OnCreateDevice != null)
            {
                OnCreateDevice(device, URL);
            }
        }

        private void CreateSyncCallback(UPnPDevice Device, Uri URL)
        {
            SyncDevice.TheDevice = Device;
            SyncDevice.URL = URL;<commit_msg>Expose the Sniffing API to the UPnP control point so higher levels can gain access and it will include more info<commit_after>/*   
Copyright 2006 - 2010 Intel Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Collections;

namespace OpenSource.UPnP
{
    /// <summary>
    /// The object by which you can leverage UPnPDevices on your network.
    /// </summary>
    public sealed class UPnPControlPoint
    {
        private static int _mx = 5;
        public static int MX
        {
            get { return (_mx); }
            set { if (value > 0) _mx = value; }
        }
        public delegate void SearchHandler(IPEndPoint ResponseFromEndPoint, IPEndPoint ResponseReceivedOnEndPoint, Uri DescriptionLocation, String USN, String SearchTarget, int MaxAge);
        /// <summary>
        /// Triggered when an AsyncSearch result returns
        /// </summary>
        public event SearchHandler OnSearch;

        public delegate void CreateDeviceHandler(UPnPDevice Device, Uri DescriptionURL);
        /// <summary>
        /// Triggered when an Async CreateDevice returns...
        /// <para>
        /// Depracated. Use UPnPDeviceFactory</para>
        /// </summary>
        public event CreateDeviceHandler OnCreateDevice;

        private NetworkInfo NetInfo;
        private ArrayList SyncData;
        private DeviceNode SyncDevice;
        private Hashtable CreateTable;
        private SSDP SSDPServer;
        private Hashtable SSDPSessions;
        private LifeTimeMonitor Lifetime;

        private struct DeviceNode
        {
            public UPnPDevice TheDevice;
            public Uri URL;
        }

        /// <summary>
        /// This event is triggered when other UPnPDevices on your network are alive.
        /// </summary>
        public event SSDP.NotifyHandler OnNotify;

        /// <summary>
        /// Packet Sniffing Event
        /// </summary>
        public event SSDP.SnifferHandler OnSniffPacket;

        /// <summary>
        /// Constructs a new Control Point, and waits for your commands and receives events
        /// </summary>
        public UPnPControlPoint()
          : this(null)
        {
        }

        public UPnPControlPoint(NetworkInfo ni)
        {
            CreateTable = Hashtable.Synchronized(new Hashtable());
            NetInfo = (null != ni) ? ni : new NetworkInfo(new NetworkInfo.InterfaceHandler(NewInterface));
            SyncData = ArrayList.Synchronized(new ArrayList());
            SSDPSessions = Hashtable.Synchronized(new Hashtable());
            Lifetime = new LifeTimeMonitor();
            Lifetime.OnExpired += new LifeTimeMonitor.LifeTimeHandler(HandleExpired);

            SSDPServer = new SSDP(65535);
            SSDPServer.OnNotify += new SSDP.NotifyHandler(HandleNotify);
            SSDPServer.OnSniffPacket += new SSDP.SnifferHandler(HandleSniffer);
        }

        ~UPnPControlPoint()
        {
        }

        private void PreProcessNotify(IPEndPoint source, String LocationURL, bool IsAlive, String USN, String ST, int MaxAge)
        {

        }

        private void HandleAlert(Object sender, Object RObj)
        {

        }

        private void NewInterface(NetworkInfo sender, IPAddress Intfce)
        {
        }

        private void HandleNotify(IPEndPoint source, IPEndPoint local, Uri LocationURL, bool IsAlive, String USN, String ST, int MaxAge, HTTPMessage Packet)
        {
            if (IsAlive && LocationURL != null) OpenSource.Utilities.EventLogger.Log(this, System.Diagnostics.EventLogEntryType.SuccessAudit, LocationURL.ToString());
            if (OnNotify != null) OnNotify(source, local, LocationURL, IsAlive, USN, ST, MaxAge, Packet);
        }

        private void HandleSniffer(IPEndPoint source, IPEndPoint dest, HTTPMessage Packet)
        {
          if (OnSniffPacket != null) OnSniffPacket(source, dest, Packet);
        }

        /// <summary>
        /// Creates a device from a URL. [Depracated] use UPnPDeviceFactory
        /// </summary>
        /// <param name="DescriptionURL"></param>
        /// <param name="LifeTime"></param>
        public void CreateDeviceAsync(Uri DescriptionURL, int LifeTime)
        {
            //ToDo: Replace the failed callback
            UPnPDeviceFactory fac = new UPnPDeviceFactory(DescriptionURL, LifeTime, new UPnPDeviceFactory.UPnPDeviceHandler(HandleDeviceCreation), null, null, null);
            CreateTable[fac] = fac;
            Lifetime.Add(fac, 30);
        }

        private void HandleDeviceCreation(UPnPDeviceFactory Factory, UPnPDevice device, Uri URL)
        {
            Factory.Shutdown();
            if (OnCreateDevice != null)
            {
                OnCreateDevice(device, URL);
            }
        }

        private void CreateSyncCallback(UPnPDevice Device, Uri URL)
        {
            SyncDevice.TheDevice = Device;
            SyncDevice.URL = URL;
{'fraction_non_alphanumeric': 0.08248742569730225, 'fraction_numerical': 0.0042981252857796064, 'mean_word_length': 2.3260340632603405, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1661408', 'n_tokens_mistral': 2935, 'n_tokens_neox': 2804, 'n_words': 882}