From fdb21f966f84d7d395381232e96eaa2193e899db Mon Sep 17 00:00:00 2001 From: John Beisley Date: Sat, 28 Sep 2013 17:58:28 +0100 Subject: [PATCH] Add example of using the library to discover an InternetGatewayDevice. --- cmd/discoverigd/discoverigd.go | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 cmd/discoverigd/discoverigd.go diff --git a/cmd/discoverigd/discoverigd.go b/cmd/discoverigd/discoverigd.go new file mode 100644 index 0000000..da44bc2 --- /dev/null +++ b/cmd/discoverigd/discoverigd.go @@ -0,0 +1,42 @@ +// Serves as a simple example/test of discovering UPnP devices on the local +// network. +package main + +import ( + "fmt" + + "github.com/huin/goupnp" +) + +var spaces = " " + +type indentLevel int + +func (i indentLevel) String() string { + return spaces[:i] +} + +func displayDevice(indent indentLevel, device *goupnp.Device) { + fmt.Println(indent.String(), device) + for _, srv := range device.Services { + fmt.Println((indent + 1).String(), srv) + } + for _, subdev := range device.Devices { + displayDevice(indent+1, subdev) + } +} + +func main() { + if results, err := goupnp.DiscoverDevices(goupnp.DeviceTypeInternetGatewayDevice); err != nil { + fmt.Println("Error discovering InternetGatewayDevice with UPnP: ", err) + } else { + fmt.Printf("Discovered %d InternetGatewayDevices:\n", len(results)) + for _, maybeRootDevice := range results { + if maybeRootDevice.Err != nil { + fmt.Println(maybeRootDevice.Err) + } else { + displayDevice(0, maybeRootDevice.Root.Device) + } + } + } +}