Debian Codegraph @

service_client.go

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