libupnpp
0.16.0
A C++ wrapper for the Portable UPnP reference library
|
Base Device class. More...
#include <device.hxx>
Classes | |
class | Internal |
class | InternalStatic |
Public Member Functions | |
UpnpDevice (const std::string &deviceId) | |
Construct a device object. More... | |
UpnpDevice (UpnpDevice *rootdev, const std::string &deviceId) | |
Construct an embedded device. More... | |
void | setProductVersion (const char *product, const char *version) |
Set the product name and version to be used in SERVER headers. More... | |
virtual bool | readLibFile (const std::string &name, std::string &contents)=0 |
Librarian utility. More... | |
bool | addVFile (const std::string &name, const std::string &contents, const std::string &mime, std::string &path) |
Add virtual file to virtual directory. More... | |
void | addActionMapping (const UpnpService *, const std::string &actName, soapfun) |
Add mapping from service+action-name to handler function. More... | |
const std::string & | getDeviceId () const |
Retrieve Device ID (UDN) | |
bool | ok () |
Check status. | |
void | eventloop () |
Event-generating loop. More... | |
void | startloop () |
Start a thread to run the event loop and return immediately. More... | |
void | loopWakeup () |
Trigger an early event. More... | |
void | shouldExit () |
To be called to get the event loop to return. | |
bool | start () |
Register and activate this device. More... | |
void | notifyEvent (const UpnpService *service, const std::vector< std::string > &names, const std::vector< std::string > &values) |
Generate an event for the service. More... | |
bool | addService (UpnpService *) |
Add service to our list. More... | |
void | forgetService (const std::string &serviceId) |
Static Public Member Functions | |
static bool | ipv4 (std::string *host, unsigned short *port) |
Retrieve the network endpoint the server is listening on. | |
Base Device class.
This class dispatches the UPnP action calls to the appropriate UpnpService derived class methods, and receives, then forwards, the UPnP events.
The UpnpService objects attach themselves to the UpnpDevice object during their construction and do most of the application work.
The derived UPnPDevice classes mostly need to implement the readLibFile() method for retrieving misc XML description fragments and files.
UPnPProvider::UpnpDevice::UpnpDevice | ( | const std::string & | deviceId | ) |
Construct a device object.
The device is not started. This will be done by the startloop() or eventloop() call when everything is set up.
deviceId | uuid for device: "uuid:UUIDvalue" |
UPnPProvider::UpnpDevice::UpnpDevice | ( | UpnpDevice * | rootdev, |
const std::string & | deviceId | ||
) |
Construct an embedded device.
The device is not started. This will be done by the startloop() or eventloop() call when everything is set up.
rootdev | if not null, the device description will be stored in the <devices> section of the root device (this device will be embedded). Else behave as the other constructor. !! The root device must not be already started. !! |
void UPnPProvider::UpnpDevice::addActionMapping | ( | const UpnpService * | serv, |
const std::string & | actName, | ||
soapfun | fun | ||
) |
Add mapping from service+action-name to handler function.
This is called by the services implementations during their initialization.
bool UPnPProvider::UpnpDevice::addService | ( | UpnpService * | serv | ) |
Add service to our list.
We only ever keep one instance of a serviceId. Multiple calls will only keep the last one. This is called from the generic UpnpService constructor.
bool UPnPProvider::UpnpDevice::addVFile | ( | const std::string & | name, |
const std::string & | contents, | ||
const std::string & | mime, | ||
std::string & | path | ||
) |
Add virtual file to virtual directory.
This is mostly used internally by the base Service class to populate the virtual directory with data retrieved through the readLibFile() method, but it can also be used by client code for serving other files (e.g. the presentation page).
name | Base file name. Somewhat arbitrary, but simple and descriptive may help debugging (e.g. "presentation.html"). |
contents | File contents. |
mime | MIME type (e.g: "text/html"). |
void UPnPProvider::UpnpDevice::eventloop | ( | ) |
Event-generating loop.
This only returns if shouldExit() is called from another thread. eventloop() can be called from an application thread (maybe the main one when program initialisation is done). Alternatively, it can be entered through startloop(), which will create a thread to run it.
The loop runs every second to call the getEventData() methods from the attached UpnpService objects and generates UPnP events if they return changed variables.
The UPnP action calls happen in other (npupnp) threads with which we synchronize, currently using a global lock.
Alternatively to running this method, either directly or through startloop(), it is possible to initially call start() (which returns after initializing the device with the lower level library), and then call notifyEvent() when needed, from the application own event loop. No polling occurs in this case, events are pushed from the application code.
void UPnPProvider::UpnpDevice::loopWakeup | ( | ) |
Trigger an early event.
This is called from a service action callback to wake up the event loop early if something needs to be broadcast without waiting for the normal delay.
Will only do something if the previous event is not too recent.
void UPnPProvider::UpnpDevice::notifyEvent | ( | const UpnpService * | service, |
const std::vector< std::string > & | names, | ||
const std::vector< std::string > & | values | ||
) |
Generate an event for the service.
This is mostly useful if eventloop() is not used.
service | the service generating the event. |
names | the names of changed variables. |
values | the parallel values of the changed variables. |
|
pure virtual |
Librarian utility.
This must be implemented by the derived class for the services to retrieve their definition XML files (by a call from the base Service class constructor).
This is also used with an empty name parameter to retrieve an XML text fragment to be added to the <device> node in the device description XML. E.G. things like <serialNumber>42</serialNumber>. Mandatory: deviceType and friendlyName must be in there, UDN must not (it is generated by the base Device class). This empty name call will happen when the first service is added, so the data should be prepared before this (e.g. if there are paths, like the one for presentation.html which need to be computed by addVFile(), this needs to happen before adding the first service).
name | the designator set in the service constructor (e.g. AVTransport.xml). Empty for retrieving the description properties (see above). |
void UPnPProvider::UpnpDevice::setProductVersion | ( | const char * | product, |
const char * | version | ||
) |
Set the product name and version to be used in SERVER headers.
If not set, the library default will be used
bool UPnPProvider::UpnpDevice::start | ( | ) |
Register and activate this device.
This should only be called if eventloop() is not used, and all events will be generated by calls to notifyEvent().
void UPnPProvider::UpnpDevice::startloop | ( | ) |
Start a thread to run the event loop and return immediately.
This is an alternative to running eventloop() from an application thread. The destructor will take care of the internal thread.