| 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 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
type ServiceClient struct { |
| 17 |
SOAPClient *soap.SOAPClient |
| 18 |
RootDevice *RootDevice |
| 19 |
Location *url.URL |
| 20 |
Service *Service |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
func NewServiceClients(searchTarget string) (clients []ServiceClient, errors []error, err error) { |
| 25 |
return NewServiceClientsCtx(context.Background(), searchTarget) |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 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 |
|
| 57 |
|
| 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 |
|
| 67 |
|
| 68 |
|
| 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 |
|
| 90 |
|
| 91 |
|
| 92 |
func (client *ServiceClient) GetServiceClient() *ServiceClient { |
| 93 |
return client |
| 94 |
} |