aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/huin/goupnp/service_client.go
blob: c0d16ce2a11df9ce8bb99e5dafbc095269216de0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package goupnp

import (
    "fmt"
    "github.com/huin/goupnp/soap"
)

// ServiceClient is a SOAP client, root device and the service for the SOAP
// client rolled into one value. The root device and service are intended to be
// informational.
type ServiceClient struct {
    SOAPClient *soap.SOAPClient
    RootDevice *RootDevice
    Service    *Service
}

func NewServiceClients(searchTarget string) (clients []ServiceClient, errors []error, err error) {
    var maybeRootDevices []MaybeRootDevice
    if maybeRootDevices, err = DiscoverDevices(searchTarget); err != nil {
        return
    }

    clients = make([]ServiceClient, 0, len(maybeRootDevices))

    for _, maybeRootDevice := range maybeRootDevices {
        if maybeRootDevice.Err != nil {
            errors = append(errors, maybeRootDevice.Err)
            continue
        }

        device := &maybeRootDevice.Root.Device
        srvs := device.FindService(searchTarget)
        if len(srvs) == 0 {
            errors = append(errors, fmt.Errorf("goupnp: service %q not found within device %q (UDN=%q)",
                searchTarget, device.FriendlyName, device.UDN))
            continue
        }

        for _, srv := range srvs {
            clients = append(clients, ServiceClient{
                SOAPClient: srv.NewSOAPClient(),
                RootDevice: maybeRootDevice.Root,
                Service:    srv,
            })
        }
    }

    return
}

// 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
}