| 1 |
package goupnp |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/url" |
| 6 |
|
| 7 |
"github.com/huin/goupnp/soap" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
type ServiceClient struct { |
| 16 |
SOAPClient *soap.SOAPClient |
| 17 |
RootDevice *RootDevice |
| 18 |
Location *url.URL |
| 19 |
Service *Service |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 51 |
|
| 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 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 84 |
|
| 85 |
|
| 86 |
func (client *ServiceClient) GetServiceClient() *ServiceClient { |
| 87 |
return client |
| 88 |
} |