Debian Codegraph @

goupnp/service_client.go

raw symbols SCIP
1 package goupnp
2
3 import (
4 "context"
5 "fmt"
6 "net/url"
7
8 "github.com/NebulousLabs/go-upnp/goupnp/soap"
9 )
10
11 // ServiceClient is a SOAP client, root device and the service for the SOAP
12 // client rolled into one value. The root device, location, and service are
13 // intended to be informational. Location can be used to later recreate a
14 // ServiceClient with NewServiceClientByURL if the service is still present;
15 // bypassing the discovery process.
16 type ServiceClient struct {
17 SOAPClient *soap.SOAPClient
18 RootDevice *RootDevice
19 Location *url.URL
20 Service *Service
21 }
22
23 // NewServiceClients is deprecated. Use NewServiceClientsCtx instead.
24 func NewServiceClients(searchTarget string) (clients []ServiceClient, errors []error, err error) {
25 return NewServiceClientsCtx(context.Background(), searchTarget)
26 }
27
28 // NewServiceClientsCtx discovers services, and returns clients for them. err will
29 // report any error with the discovery process (blocking any device/service
30 // discovery), errors reports errors on a per-root-device basis.
31 func NewServiceClientsCtx(ctx context.Context, searchTarget string) (clients []ServiceClient, errors []error, err error) {
32 var maybeRootDevices []MaybeRootDevice
33 if maybeRootDevices, err = DiscoverDevicesCtx(ctx, searchTarget); err != nil {
34 return
35 }
36
37 clients = make([]ServiceClient, 0, len(maybeRootDevices))
38
39 for _, maybeRootDevice := range maybeRootDevices {
40 if maybeRootDevice.Err != nil {
41 errors = append(errors, maybeRootDevice.Err)
42 continue
43 }
44
45 deviceClients, err := NewServiceClientsFromRootDevice(maybeRootDevice.Root, maybeRootDevice.Location, searchTarget)
46 if err != nil {
47 errors = append(errors, err)
48 continue
49 }
50 clients = append(clients, deviceClients...)
51 }
52
53 return
54 }
55
56 // NewServiceClientsByURL creates client(s) for the given service URN, for a
57 // root device at the given URL.
58 func NewServiceClientsByURL(loc *url.URL, searchTarget string) ([]ServiceClient, error) {
59 rootDevice, err := DeviceByURL(loc)
60 if err != nil {
61 return nil, err
62 }
63 return NewServiceClientsFromRootDevice(rootDevice, loc, searchTarget)
64 }
65
66 // NewServiceClientsFromDevice creates client(s) for the given service URN, in
67 // a given root device. The loc parameter is simply assigned to the
68 // Location attribute of the returned ServiceClient(s).
69 func NewServiceClientsFromRootDevice(rootDevice *RootDevice, loc *url.URL, searchTarget string) ([]ServiceClient, error) {
70 device := &rootDevice.Device
71 srvs := device.FindService(searchTarget)
72 if len(srvs) == 0 {
73 return nil, fmt.Errorf("goupnp: service %q not found within device %q (UDN=%q)",
74 searchTarget, device.FriendlyName, device.UDN)
75 }
76
77 clients := make([]ServiceClient, 0, len(srvs))
78 for _, srv := range srvs {
79 clients = append(clients, ServiceClient{
80 SOAPClient: srv.NewSOAPClient(),
81 RootDevice: rootDevice,
82 Location: loc,
83 Service: srv,
84 })
85 }
86 return clients, nil
87 }
88
89 // GetServiceClient returns the ServiceClient itself. This is provided so that the
90 // service client attributes can be accessed via an interface method on a
91 // wrapping type.
92 func (client *ServiceClient) GetServiceClient() *ServiceClient {
93 return client
94 }