package goupnp

import (
	
	
	
	

	
)

// ServiceClient is a SOAP client, root device and the service for the SOAP
// client rolled into one value. The root device, location, and service are
// intended to be informational. Location can be used to later recreate a
// ServiceClient with NewServiceClientByURL if the service is still present;
// bypassing the discovery process.
type ServiceClient struct {
	SOAPClient *soap.SOAPClient
	RootDevice *RootDevice
	Location   *url.URL
	Service    *Service
	localAddr  net.IP
}

// NewServiceClientsCtx discovers services, and returns clients for them. err will
// report any error with the discovery process (blocking any device/service
// discovery), errors reports errors on a per-root-device basis.
func ( context.Context,  string) ( []ServiceClient,  []error,  error) {
	var  []MaybeRootDevice
	if ,  = DiscoverDevicesCtx(, );  != nil {
		return
	}

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

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

		,  := newServiceClientsFromRootDevice(.Root, .Location, , .LocalAddr)
		if  != nil {
			 = append(, )
			continue
		}
		 = append(, ...)
	}

	return
}

// NewServiceClients is the legacy version of NewServiceClientsCtx, but uses
// context.Background() as the context.
func ( string) ( []ServiceClient,  []error,  error) {
	return NewServiceClientsCtx(context.Background(), )
}

// NewServiceClientsByURLCtx creates client(s) for the given service URN, for a
// root device at the given URL.
func ( context.Context,  *url.URL,  string) ([]ServiceClient, error) {
	,  := DeviceByURLCtx(, )
	if  != nil {
		return nil, 
	}
	return NewServiceClientsFromRootDevice(, , )
}

// NewServiceClientsByURL is the legacy version of NewServiceClientsByURLCtx, but uses
// context.Background() as the context.
func ( *url.URL,  string) ([]ServiceClient, error) {
	return NewServiceClientsByURLCtx(context.Background(), , )
}

// NewServiceClientsFromDevice creates client(s) for the given service URN, in
// a given root device. The loc parameter is simply assigned to the
// Location attribute of the returned ServiceClient(s).
func ( *RootDevice,  *url.URL,  string) ([]ServiceClient, error) {
	return newServiceClientsFromRootDevice(, , , nil)
}

func newServiceClientsFromRootDevice(
	 *RootDevice,
	 *url.URL,
	 string,
	 net.IP,
) ([]ServiceClient, error) {
	 := &.Device
	 := .FindService()
	if len() == 0 {
		return nil, fmt.Errorf("goupnp: service %q not found within device %q (UDN=%q)",
			, .FriendlyName, .UDN)
	}

	 := make([]ServiceClient, 0, len())
	for ,  := range  {
		 = append(, ServiceClient{
			SOAPClient: .NewSOAPClient(),
			RootDevice: ,
			Location:   ,
			Service:    ,
			localAddr:  ,
		})
	}
	return , nil
}

// 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 ( *ServiceClient) () *ServiceClient {
	return 
}

// LocalAddr returns the address from which the device was discovered (if known - otherwise empty).
func ( *ServiceClient) () net.IP {
	return .localAddr
}