From 914072fdd8e14201ba6068ccc6d35adc90f4e25b Mon Sep 17 00:00:00 2001 From: John Beisley Date: Sun, 6 Oct 2013 13:23:11 +0100 Subject: [PATCH] Separate lower-level API into separate packages. --- cmd/discoverigd/discoverigd.go | 2 +- device.go | 15 +++++++--- goupnp.go | 7 +++-- httpu.go => httpu/httpu.go | 2 +- scpd.go | 53 ---------------------------------- soap.go => soap/soap.go | 2 +- ssdp.go => ssdp/ssdp.go | 6 ++-- 7 files changed, 23 insertions(+), 64 deletions(-) rename httpu.go => httpu/httpu.go (99%) delete mode 100644 scpd.go rename soap.go => soap/soap.go (99%) rename ssdp.go => ssdp/ssdp.go (92%) diff --git a/cmd/discoverigd/discoverigd.go b/cmd/discoverigd/discoverigd.go index 33c446e..5eb594e 100644 --- a/cmd/discoverigd/discoverigd.go +++ b/cmd/discoverigd/discoverigd.go @@ -89,7 +89,7 @@ func main() { } } - srvClient := goupnp.NewSOAPClient(srv.ControlURL.URL) + srvClient := srv.NewSOAPClient() { inAction := GetExternalIPAddressRequest{XMLName: xml.Name{Space: goupnp.ServiceTypeWANPPPConnection, Local: "GetExternalIPAddress"}} diff --git a/device.go b/device.go index aec13f0..d41ef21 100644 --- a/device.go +++ b/device.go @@ -7,6 +7,9 @@ import ( "errors" "fmt" "net/url" + + "github.com/huin/goupnp/scpd" + "github.com/huin/goupnp/soap" ) const ( @@ -147,15 +150,19 @@ func (srv *Service) String() string { // RequestSCDP requests the SCPD (soap actions and state variables description) // for the service. -func (srv *Service) RequestSCDP() (*SCPD, error) { +func (srv *Service) RequestSCDP() (*scpd.SCPD, error) { if !srv.SCPDURL.Ok { return nil, errors.New("bad/missing SCPD URL, or no URLBase has been set") } - scpd := new(SCPD) - if err := requestXml(srv.SCPDURL.URL.String(), SCPDXMLNamespace, scpd); err != nil { + s := new(scpd.SCPD) + if err := requestXml(srv.SCPDURL.URL.String(), scpd.SCPDXMLNamespace, s); err != nil { return nil, err } - return scpd, nil + return s, nil +} + +func (srv *Service) NewSOAPClient() *soap.SOAPClient { + return soap.NewSOAPClient(srv.ControlURL.URL) } // URLField is a URL that is part of a device description. diff --git a/goupnp.go b/goupnp.go index c1223b1..cc7bb96 100644 --- a/goupnp.go +++ b/goupnp.go @@ -6,6 +6,9 @@ import ( "fmt" "net/http" "net/url" + + "github.com/huin/goupnp/httpu" + "github.com/huin/goupnp/ssdp" ) // Non-exhaustive set of UPnP service types. @@ -46,12 +49,12 @@ type MaybeRootDevice struct { // returned for errors while attempting to send the query. An error or // RootDevice is returned for each discovered service. func DiscoverDevices(searchTarget string) ([]MaybeRootDevice, error) { - httpu, err := NewHTTPUClient() + httpu, err := httpu.NewHTTPUClient() if err != nil { return nil, err } defer httpu.Close() - responses, err := SSDPRawSearch(httpu, string(searchTarget), 2, 3) + responses, err := ssdp.SSDPRawSearch(httpu, string(searchTarget), 2, 3) if err != nil { return nil, err } diff --git a/httpu.go b/httpu/httpu.go similarity index 99% rename from httpu.go rename to httpu/httpu.go index f6ddf74..388d48c 100644 --- a/httpu.go +++ b/httpu/httpu.go @@ -1,4 +1,4 @@ -package goupnp +package httpu import ( "bufio" diff --git a/scpd.go b/scpd.go deleted file mode 100644 index df291f8..0000000 --- a/scpd.go +++ /dev/null @@ -1,53 +0,0 @@ -package goupnp - -import ( - "encoding/xml" -) - -const ( - SCPDXMLNamespace = "urn:schemas-upnp-org:service-1-0" -) - -// SCPD is the service description as described by section 2.5 "Service -// description" in -// http://upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.1.pdf -type SCPD struct { - XMLName xml.Name `xml:"scpd"` - ConfigId string `xml:"configId,attr"` - SpecVersion SpecVersion `xml:"specVersion"` - Actions []Action `xml:"actionList>action"` - StateVariables []StateVariable `xml:"serviceStateTable>stateVariable"` -} - -type Action struct { - Name string `xml:"name"` - Arguments []Argument `xml:"argumentList>argument"` -} - -type Argument struct { - Name string `xml:"name"` - Direction string `xml:"direction"` // in|out - RelatedStateVariable string `xml:"relatedStateVariable"` // ? - Retval string `xml:"retval"` // ? -} - -type StateVariable struct { - Name string `xml:"name"` - SendEvents string `xml:"sendEvents,attr"` // yes|no - Multicast string `xml:"multicast,attr"` // yes|no - DataType DataType `xml:"dataType"` - DefaultValue string `xml:"defaultValue"` - AllowedValueRange AllowedValueRange `xml:"allowedValueRange"` - AllowedValue []string `xml:"allowedValueList>allowedValue"` -} - -type AllowedValueRange struct { - Minimum string `xml:"minimum"` - Maximum string `xml:"maximum"` - Step string `xml:"step"` -} - -type DataType struct { - Name string `xml:",chardata"` - Type string `xml:"type,attr"` -} diff --git a/soap.go b/soap/soap.go similarity index 99% rename from soap.go rename to soap/soap.go index efa87aa..8dd0a87 100644 --- a/soap.go +++ b/soap/soap.go @@ -1,6 +1,6 @@ // Definition for the SOAP structure required for UPnP's SOAP usage. -package goupnp +package soap import ( "bytes" diff --git a/ssdp.go b/ssdp/ssdp.go similarity index 92% rename from ssdp.go rename to ssdp/ssdp.go index 9e0fe05..7b51c1a 100644 --- a/ssdp.go +++ b/ssdp/ssdp.go @@ -1,4 +1,4 @@ -package goupnp +package ssdp import ( "errors" @@ -7,6 +7,8 @@ import ( "net/url" "strconv" "time" + + "github.com/huin/goupnp/httpu" ) const ( @@ -22,7 +24,7 @@ const ( // implementation waits an additional 100ms for responses to arrive), 2 is a // reasonable value for this. numSends is the number of requests to send - 3 is // a reasonable value for this. -func SSDPRawSearch(httpu *HTTPUClient, searchTarget string, maxWaitSeconds int, numSends int) ([]*http.Response, error) { +func SSDPRawSearch(httpu *httpu.HTTPUClient, searchTarget string, maxWaitSeconds int, numSends int) ([]*http.Response, error) { if maxWaitSeconds < 1 { return nil, errors.New("ssdp: maxWaitSeconds must be >= 1") }