2013-12-31 20:01:17 +00:00
|
|
|
package goupnp
|
|
|
|
|
|
|
|
import (
|
2022-10-01 11:49:02 +00:00
|
|
|
"context"
|
2013-12-31 20:01:17 +00:00
|
|
|
"fmt"
|
2022-03-21 13:54:05 +00:00
|
|
|
"net"
|
2015-06-06 10:35:31 +00:00
|
|
|
"net/url"
|
2015-06-06 10:01:33 +00:00
|
|
|
|
2022-11-01 19:02:13 +00:00
|
|
|
"git.cyrilix.bzh/cyrilix/goupnp/soap"
|
2013-12-31 20:01:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServiceClient is a SOAP client, root device and the service for the SOAP
|
2015-06-06 10:35:31 +00:00
|
|
|
// client rolled into one value. The root device, location, and service are
|
|
|
|
// intended to be informational. Location can be used to later recreate a
|
|
|
|
// ServiceClient with NewServiceClientByURL if the service is still present;
|
|
|
|
// bypassing the discovery process.
|
2013-12-31 20:01:17 +00:00
|
|
|
type ServiceClient struct {
|
|
|
|
SOAPClient *soap.SOAPClient
|
|
|
|
RootDevice *RootDevice
|
2015-06-06 10:35:31 +00:00
|
|
|
Location *url.URL
|
2013-12-31 20:01:17 +00:00
|
|
|
Service *Service
|
2022-03-21 13:54:05 +00:00
|
|
|
localAddr net.IP
|
2013-12-31 20:01:17 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 11:49:02 +00:00
|
|
|
// NewServiceClientsCtx discovers services, and returns clients for them. err will
|
2015-06-06 10:35:31 +00:00
|
|
|
// report any error with the discovery process (blocking any device/service
|
|
|
|
// discovery), errors reports errors on a per-root-device basis.
|
2022-10-01 11:49:02 +00:00
|
|
|
func NewServiceClientsCtx(ctx context.Context, searchTarget string) (clients []ServiceClient, errors []error, err error) {
|
2013-12-31 20:01:17 +00:00
|
|
|
var maybeRootDevices []MaybeRootDevice
|
2022-10-01 11:49:02 +00:00
|
|
|
if maybeRootDevices, err = DiscoverDevicesCtx(ctx, searchTarget); err != nil {
|
2013-12-31 20:01:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
clients = make([]ServiceClient, 0, len(maybeRootDevices))
|
|
|
|
|
|
|
|
for _, maybeRootDevice := range maybeRootDevices {
|
|
|
|
if maybeRootDevice.Err != nil {
|
|
|
|
errors = append(errors, maybeRootDevice.Err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-03-21 13:54:05 +00:00
|
|
|
deviceClients, err := newServiceClientsFromRootDevice(maybeRootDevice.Root, maybeRootDevice.Location, searchTarget, maybeRootDevice.LocalAddr)
|
2015-06-06 10:35:31 +00:00
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
2013-12-31 20:01:17 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-06-06 10:35:31 +00:00
|
|
|
clients = append(clients, deviceClients...)
|
2013-12-31 20:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2014-01-06 20:17:14 +00:00
|
|
|
|
2022-10-01 11:49:02 +00:00
|
|
|
// NewServiceClients is the legacy version of NewServiceClientsCtx, but uses
|
|
|
|
// context.Background() as the context.
|
|
|
|
func NewServiceClients(searchTarget string) (clients []ServiceClient, errors []error, err error) {
|
|
|
|
return NewServiceClientsCtx(context.Background(), searchTarget)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServiceClientsByURLCtx creates client(s) for the given service URN, for a
|
2015-06-06 10:35:31 +00:00
|
|
|
// root device at the given URL.
|
2022-10-01 11:49:02 +00:00
|
|
|
func NewServiceClientsByURLCtx(ctx context.Context, loc *url.URL, searchTarget string) ([]ServiceClient, error) {
|
|
|
|
rootDevice, err := DeviceByURLCtx(ctx, loc)
|
2015-06-06 10:35:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewServiceClientsFromRootDevice(rootDevice, loc, searchTarget)
|
|
|
|
}
|
|
|
|
|
2022-10-01 11:49:02 +00:00
|
|
|
// NewServiceClientsByURL is the legacy version of NewServiceClientsByURLCtx, but uses
|
|
|
|
// context.Background() as the context.
|
|
|
|
func NewServiceClientsByURL(loc *url.URL, searchTarget string) ([]ServiceClient, error) {
|
|
|
|
return NewServiceClientsByURLCtx(context.Background(), loc, searchTarget)
|
|
|
|
}
|
|
|
|
|
2015-06-06 10:53:50 +00:00
|
|
|
// NewServiceClientsFromDevice creates client(s) for the given service URN, in
|
|
|
|
// a given root device. The loc parameter is simply assigned to the
|
|
|
|
// Location attribute of the returned ServiceClient(s).
|
|
|
|
func NewServiceClientsFromRootDevice(rootDevice *RootDevice, loc *url.URL, searchTarget string) ([]ServiceClient, error) {
|
2022-03-21 13:54:05 +00:00
|
|
|
return newServiceClientsFromRootDevice(rootDevice, loc, searchTarget, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newServiceClientsFromRootDevice(
|
|
|
|
rootDevice *RootDevice,
|
|
|
|
loc *url.URL,
|
|
|
|
searchTarget string,
|
|
|
|
lAddr net.IP,
|
|
|
|
) ([]ServiceClient, error) {
|
2015-06-06 10:35:31 +00:00
|
|
|
device := &rootDevice.Device
|
|
|
|
srvs := device.FindService(searchTarget)
|
|
|
|
if len(srvs) == 0 {
|
|
|
|
return nil, fmt.Errorf("goupnp: service %q not found within device %q (UDN=%q)",
|
|
|
|
searchTarget, device.FriendlyName, device.UDN)
|
|
|
|
}
|
|
|
|
|
|
|
|
clients := make([]ServiceClient, 0, len(srvs))
|
|
|
|
for _, srv := range srvs {
|
|
|
|
clients = append(clients, ServiceClient{
|
|
|
|
SOAPClient: srv.NewSOAPClient(),
|
|
|
|
RootDevice: rootDevice,
|
2015-06-06 10:53:50 +00:00
|
|
|
Location: loc,
|
2015-06-06 10:35:31 +00:00
|
|
|
Service: srv,
|
2022-03-21 13:54:05 +00:00
|
|
|
localAddr: lAddr,
|
2015-06-06 10:35:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return clients, nil
|
|
|
|
}
|
|
|
|
|
2014-01-06 20:17:14 +00:00
|
|
|
// GetServiceClient returns the ServiceClient itself. This is provided so that the
|
|
|
|
// service client attributes can be accessed via an interface method on a
|
|
|
|
// wrapping type.
|
|
|
|
func (client *ServiceClient) GetServiceClient() *ServiceClient {
|
|
|
|
return client
|
|
|
|
}
|
2022-03-21 13:54:05 +00:00
|
|
|
|
|
|
|
// LocalAddr returns the address from which the device was discovered (if known - otherwise empty).
|
|
|
|
func (client *ServiceClient) LocalAddr() net.IP {
|
|
|
|
return client.localAddr
|
|
|
|
}
|