/*---------------------------------------------------------------------- | PLT_SsdpDeviceAnnounceUnicastTask::DoRun +---------------------------------------------------------------------*/ void PLT_SsdpDeviceAnnounceTask::DoRun() { NPT_Result res = NPT_SUCCESS; NPT_List<NPT_NetworkInterface*> if_list; while (1) { NPT_CHECK_LABEL_FATAL(PLT_UPnPMessageHelper::GetNetworkInterfaces(if_list), cleanup); // if we're announcing our arrival, sends a byebye first (NMPR compliance) if (m_IsByeByeFirst == true) { m_IsByeByeFirst = false; res = if_list.Apply(PLT_SsdpAnnounceInterfaceIterator(m_Device, true, m_IsBroadcast)); if (NPT_FAILED(res)) goto cleanup; // schedule to announce alive in 300 ms if (NPT_FAILED(res) || IsAborting((NPT_Timeout)300)) break; } res = if_list.Apply(PLT_SsdpAnnounceInterfaceIterator(m_Device, false, m_IsBroadcast)); cleanup: if_list.Apply(NPT_ObjectDeleter<NPT_NetworkInterface>()); if_list.Clear(); if (NPT_FAILED(res) || IsAborting(m_Repeat.m_Seconds*1000)) break; }; }
/*---------------------------------------------------------------------- | PLT_LightSampleDevice::SetupServices +---------------------------------------------------------------------*/ NPT_Result PLT_LightSampleDevice::SetupServices() { NPT_Result res; PLT_Service* service = new PLT_Service( this, "urn:schemas-upnp-org:service:SwitchPower:1", "urn:upnp-org:serviceId:SwitchPower.001", "SwitchPower"); NPT_CHECK_LABEL_FATAL(res = service->SetSCPDXML((const char*)SCPDXML), failure); NPT_CHECK_LABEL_FATAL(res = AddService(service), failure); service->SetStateVariable("Status", "True"); return NPT_SUCCESS; failure: delete service; return res; }
/*---------------------------------------------------------------------- | PLT_Simple::SetupServices +---------------------------------------------------------------------*/ NPT_Result PLT_Simple::SetupServices() { NPT_Result res; PLT_Service* service = new PLT_Service( this, "urn:schemas-upnp-org:service:Test:1", "urn:upnp-org:serviceId:Test.001", "Test"); NPT_CHECK_LABEL_FATAL(res = service->SetSCPDXML((const char*)SCPDXML_SIMPLE), failure); NPT_CHECK_LABEL_FATAL(res = AddService(service), failure); service->SetStateVariable("Status", "True"); return NPT_SUCCESS; failure: delete service; return res; }
/*---------------------------------------------------------------------- | PLT_SsdpListenTask::DoInit +---------------------------------------------------------------------*/ void PLT_SsdpListenTask::DoInit() { if (m_IsMulticast) { NPT_List<NPT_NetworkInterface*> if_list; NPT_CHECK_LABEL_FATAL(PLT_UPnPMessageHelper::GetNetworkInterfaces(if_list), done); /* Join multicast group for every interface we found */ if_list.ApplyUntil( PLT_SsdpInitMulticastIterator((NPT_UdpMulticastSocket*)m_Socket), NPT_UntilResultNotEquals(NPT_SUCCESS)); if_list.Apply(NPT_ObjectDeleter<NPT_NetworkInterface>()); } done: return; }
/*---------------------------------------------------------------------- | PLT_SsdpDeviceAnnounceUnicastTask::DoRun +---------------------------------------------------------------------*/ void PLT_SsdpDeviceAnnounceTask::DoRun() { NPT_List<NPT_NetworkInterface*> if_list; while (1) { NPT_CHECK_LABEL_FATAL(PLT_UPnPMessageHelper::GetNetworkInterfaces(if_list, false), cleanup); // if we're announcing our arrival, sends a byebye first (NMPR compliance) if (m_IsByeByeFirst == true) { m_IsByeByeFirst = false; if (m_ExtraBroadcast) { if_list.Apply(PLT_SsdpAnnounceInterfaceIterator(m_Device, true, m_ExtraBroadcast)); } // multicast now if_list.Apply(PLT_SsdpAnnounceInterfaceIterator(m_Device, true, false)); // schedule to announce alive in 200 ms if (IsAborting(200)) break; } if (m_ExtraBroadcast) { if_list.Apply(PLT_SsdpAnnounceInterfaceIterator(m_Device, false, m_ExtraBroadcast)); } // multicast now if_list.Apply(PLT_SsdpAnnounceInterfaceIterator(m_Device, false, false)); cleanup: if_list.Apply(NPT_ObjectDeleter<NPT_NetworkInterface>()); if_list.Clear(); if (IsAborting((NPT_Timeout)m_Repeat.ToMillis())) break; }; }
/*---------------------------------------------------------------------- | PLT_FileMediaServer::BuildFromFilePath +---------------------------------------------------------------------*/ PLT_MediaObject* PLT_FileMediaServer::BuildFromFilePath(const NPT_String& filepath, bool with_count /* = true */, const NPT_SocketAddress* req_local_address /* = NULL */, bool keep_extension_in_title /* = false */) { NPT_String root = m_Path; PLT_MediaItemResource resource; PLT_MediaObject* object = NULL; /* retrieve the entry type (directory or file) */ NPT_FileInfo info; NPT_CHECK_LABEL_FATAL(NPT_File::GetInfo(filepath, &info), failure); if (info.m_Type == NPT_FileInfo::FILE_TYPE_REGULAR) { object = new PLT_MediaItem(); /* Set the title using the filename for now */ object->m_Title = NPT_FilePath::BaseName(filepath, keep_extension_in_title); if (object->m_Title.GetLength() == 0) goto failure; /* Set the protocol Info from the extension */ resource.m_ProtocolInfo = PLT_MediaItem::GetProtInfoFromExt(NPT_FilePath::FileExtension(filepath)); if (resource.m_ProtocolInfo.GetLength() == 0) goto failure; /* Set the resource file size */ resource.m_Size = info.m_Size; /* format the resource URI */ NPT_String url = filepath.SubString(root.GetLength()+1); // get list of ip addresses NPT_List<NPT_String> ips; NPT_CHECK_LABEL_SEVERE(PLT_UPnPMessageHelper::GetIPAddresses(ips), failure); // if we're passed an interface where we received the request from // move the ip to the top if (req_local_address && req_local_address->GetIpAddress().ToString() != "0.0.0.0") { ips.Remove(req_local_address->GetIpAddress().ToString()); ips.Insert(ips.GetFirstItem(), req_local_address->GetIpAddress().ToString()); } // iterate through list and build list of resources NPT_List<NPT_String>::Iterator ip = ips.GetFirstItem(); while (ip) { /* prepend the base URI and url encode it */ //resource.m_Uri = NPT_Uri::Encode(uri.ToString(), NPT_Uri::UnsafeCharsToEncode); resource.m_Uri = BuildResourceUri(m_FileBaseUri, *ip, url); /* Look to see if a metadatahandler exists for this extension */ PLT_MetadataHandler* handler = NULL; NPT_Result res = NPT_ContainerFind( m_MetadataHandlers, PLT_MetadataHandlerFinder(NPT_FilePath::FileExtension(filepath)), handler); if (NPT_SUCCEEDED(res) && handler) { /* if it failed loading data, reset the metadatahandler so we don't use it */ if (NPT_SUCCEEDED(handler->LoadFile(filepath))) { /* replace the title with the one from the Metadata */ NPT_String newTitle; if (handler->GetTitle(newTitle) != NULL) { object->m_Title = newTitle; } /* assign description */ handler->GetDescription(object->m_Description.long_description); /* assign album art uri if we haven't yet */ /* prepend the album art base URI and url encode it */ if (object->m_ExtraInfo.album_art_uri.GetLength() == 0) { object->m_ExtraInfo.album_art_uri = NPT_Uri::PercentEncode(BuildResourceUri(m_AlbumArtBaseUri, *ip, url), NPT_Uri::UnsafeCharsToEncode); } /* duration */ handler->GetDuration(resource.m_Duration); /* protection */ handler->GetProtection(resource.m_Protection); } } object->m_ObjectClass.type = PLT_MediaItem::GetUPnPClassFromExt(NPT_FilePath::FileExtension(filepath)); object->m_Resources.Add(resource); ++ip; } } else { object = new PLT_MediaContainer; /* Assign a title for this container */ if (filepath.Compare(root, true) == 0) { object->m_Title = "Root"; } else { object->m_Title = NPT_FilePath::BaseName(filepath, keep_extension_in_title); if (object->m_Title.GetLength() == 0) goto failure; } /* Get the number of children for this container */ NPT_Cardinal count = 0; if (with_count && NPT_SUCCEEDED(NPT_File::GetCount(filepath, count))) { ((PLT_MediaContainer*)object)->m_ChildrenCount = count; } object->m_ObjectClass.type = "object.container"; } /* is it the root? */ if (filepath.Compare(root, true) == 0) { object->m_ParentID = "-1"; object->m_ObjectID = "0"; } else { NPT_String directory = NPT_FilePath::DirectoryName(filepath); /* is the parent path the root? */ if (directory.GetLength() == root.GetLength()) { object->m_ParentID = "0"; } else { object->m_ParentID = "0" + filepath.SubString(root.GetLength(), directory.GetLength() - root.GetLength()); } object->m_ObjectID = "0" + filepath.SubString(root.GetLength()); } return object; failure: delete object; return NULL; }