示例#1
0
        /// <summary>
        /// Interpret a received string from the udp sockets
        /// </summary>
        /// <param name="received_string">the body of the udp packet</param>
        /// <param name="was_broadcasted">TRUE if it was received via broadcast</param>
        private void InterpretReceivedString(string received_string, bool was_broadcasted)
        {
            string message = "Received a";
            IPEndPoint received_from;
            if (was_broadcasted)
            {
                message += " broadcasted unpnp string ";
                message += "from :" + ((IPEndPoint)broadcast_receive_from_endpoint).Address.ToString();
                message += ":" + ((IPEndPoint)broadcast_receive_from_endpoint).Port.ToString();
                received_from = (IPEndPoint)broadcast_receive_from_endpoint;
            }
            else
            {
                message += "n unicasted unpnp string ";
                message += "from :" + ((IPEndPoint)receive_from_endpoint).Address.ToString();
                message += ":" + ((IPEndPoint)receive_from_endpoint).Port.ToString();
                received_from = (IPEndPoint)receive_from_endpoint;
            }
            message += ": ";
            /*
            Console.WriteLine("");
            Console.WriteLine(message);
            Console.WriteLine(received_string);
            Console.WriteLine("");
            //Console.WriteLine("Header Lines:");
             */

            //Console.WriteLine(received_string);
            string[] seps = {"\r\n"};
            string[] lines = received_string.Split(seps, StringSplitOptions.RemoveEmptyEntries);
            string uuid = "";
            string urn = "";
            string server = "";
            string location = "";
            string usn = "";
            string mx = "";
            string st = "";
            string nt = "";
            string man = "";
            bool discovery_request = false;

            foreach (string line in lines)
            {

                //Console.WriteLine("line:" + line);
                if (line.StartsWith("MX:", true, null))
                {
                    mx = line.Substring("MX:".Length).Trim();
                }
                if (line.StartsWith("MAN:", true, null))
                {
                    man = line.Substring("MAN:".Length).Trim();
                }
                if (line.StartsWith("NT:", true, null))
                {
                    nt = line.Substring("NT:".Length).Trim();
                }
                if (line.StartsWith("ST:", true, null))
                {
                    st = line.Substring("ST:".Length).Trim();
                }
                if (line == "M-SEARCH * HTTP/1.1")
                {
                    discovery_request = true;
                }

                if (line.StartsWith("SERVER:", true, null))
                {
                    server = line.Substring(8);
                }
                if (line.StartsWith("LOCATION:", true, null))
                {
                    location = line.Substring(10);
                }
                if (line.StartsWith("USN:",true,null))
                {
                    usn = line.Substring(5);
                    //Console.WriteLine("Found an Unique Service Name: " + usn);
                    //checking if it looks a router
                    int uuid_start = usn.IndexOf("uuid:");
                    int urn_start = usn.IndexOf("urn:");
                    if (uuid_start != -1)
                    {
                        uuid_start += 5;
                        int uuid_end = usn.Substring(uuid_start).IndexOf("::");
                        if (uuid_end != -1)
                        {
                            uuid = usn.Substring(uuid_start, uuid_end);
                            uuid = uuid.Trim();
                        }
                    }
                    if (urn_start != -1)
                    {
                        urn_start += 4;
                        //int urn_end = usn.Substring(urn_start).IndexOf(":");
                        //if (urn_end != -1)
                        //{
                            urn = usn.Substring(urn_start);
                            //urn = usn.Substring(urn_start, urn_end);
                            urn = urn.Trim();
                        //}
                    }
                }
            }

            if (discovery_request)
            {//received a discovery request
                lock (discovery_request_lock)
                {
                    //check header options
                    //and answer if needed
                    Console.WriteLine("checking local devices for service matching the search request: " + st);
                    //Console.WriteLine("searches for: " + st);
                    lock (local_devices_lock)
                    {
                        foreach (LocalDevice device in local_devices)
                        {
                            if (st.Equals("upnp:rootdevice", StringComparison.CurrentCultureIgnoreCase))
                            {
                                ReplyDevice(device, received_from);
                                Thread.Sleep(50);

                            }
                            else if(st.Equals("ssdp:all", StringComparison.CurrentCultureIgnoreCase))
                            {
                                ReplyDevice(device, received_from);
                                Thread.Sleep(50);
                                ReplyDeviceServices(device, received_from);
                                Thread.Sleep(50);
                                ReplyRootDevice(device, received_from);
                                Thread.Sleep(50);
                                ReplyRootDeviceV2(device, received_from);
                                Thread.Sleep(50);
                            }
                            else
                            {
                                foreach (SubDevice.Service service in device.RootDevice.Services)
                                {
                                    //Console.WriteLine("comparing: "+st+" and urn:"+service.ServiceType);
                                    if (st.Equals("urn:" + service.ServiceType, StringComparison.CurrentCultureIgnoreCase))
                                    {
                                        Console.WriteLine("found applicable device + service: " + device.RootDevice.FriendlyName);
                                        //ReplyDevice(device, received_from);
                                        //ReplyDeviceService(device, service, received_from);
                                        //Thread.Sleep(100);
                                        //device.LastAnnouncementTime = DateTime.Now;
                                        device.LastAnnouncementTime = DateTime.MinValue; //force reannouncing if the device missed a reply

                                        //ReplyDeviceServices(device, received_from);
                                        //ReplyRootDevice(device, received_from);
                                        //ReplyRootDeviceV2(device, received_from);
                                    }
                                }
                            }

                        }
                    }
                    //TODO Add response discovery method

                }
            }
            else if (urn != "" && uuid != "" && server != "" && location != "" && usn != "" && !discovery_request)
            {//found a usable packet for a device urn compare
                if (urn == "schemas-upnp-org:service:WANIPConnection:1") //TODO maybe change this to InternetGatewayDevice
                {//we found a router
                    //check if its a new or to be updated entry
                    Device existing = GetDeviceByUUID(uuid);
                    if (existing != null)
                    {//we need to update a router entry
                        Router updated = (Router)existing;
                    }
                    else
                    {//we discovered a new router
                        Router r = new Router(usn, location, server, uuid);
                        devices.Add(r);
                        if (DeviceDiscovered != null)
                            DeviceDiscovered(r);
                    }
                }
                else if (urn == "schemas-upnp-org:device:MediaRenderer:1")
                {//we found a media renderer
                    //check if its a new or to be updated entry
                    Device existing = GetDeviceByUUID(uuid);
                    if (existing != null)
                    {//we need to update a media renderer entry
                        MediaRenderer updated = (MediaRenderer)existing;
                    }
                    else
                    {//we discovered a new media renderer
                        MediaRenderer mr = new MediaRenderer(usn, location, server, uuid);
                        devices.Add(mr);
                        if (DeviceDiscovered != null)
                            DeviceDiscovered(mr);
                    }
                }
                else if (urn == "schemas-upnp-org:device:BinaryLight:1")
                {//we found a binary light
                    //check if its a new or to be updated entry
                    Device existing = GetDeviceByUUID(uuid);
                    if (existing != null)
                    {//we need to update a binary light entry
                        BinaryLight updated = (BinaryLight)existing;
                    }
                    else
                    {//we discovered a binary light
                        BinaryLight bl = new BinaryLight(usn, location, server, uuid);
                        devices.Add(bl);
                        if (DeviceDiscovered != null)
                            DeviceDiscovered(bl);
                    }
                }
            }

            //Console.WriteLine("");
            //Console.WriteLine("-- End of Message --");
            //Console.WriteLine("");
        }