Add another GetExternalIPAddress example, using a common function.

This commit is contained in:
John Beisley 2014-01-06 20:17:14 +00:00
parent 90e83f960e
commit 79a1c07c05
2 changed files with 36 additions and 15 deletions

View File

@ -4,19 +4,38 @@ import (
"fmt"
"os"
"github.com/huin/goupnp"
"github.com/huin/goupnp/dcps/internetgateway1"
)
// Discovering a internet gateway devices on the local network, and asking each
// of them for their external IP address.
func Example_getExternalIPAddress() {
// import (
// "fmt"
// "os"
// "github.com/huin/goupnp/dcps/internetgateway1"
// )
fmt.Println("Running")
// Use discovered WANPPPConnection1 services to find external IP addresses.
func Example_WANPPPConnection1_GetExternalIPAddress() {
clients, errors, err := internetgateway1.NewWANPPPConnection1Clients()
extIPClients := make([]GetExternalIPAddresser, len(clients))
for i, client := range clients {
extIPClients[i] = client
}
DisplayExternalIPResults(extIPClients, errors, err)
// Output:
}
// Use discovered WANIPConnection services to find external IP addresses.
func Example_WANIPConnection_GetExternalIPAddress() {
clients, errors, err := internetgateway1.NewWANIPConnection1Clients()
extIPClients := make([]GetExternalIPAddresser, len(clients))
for i, client := range clients {
extIPClients[i] = client
}
DisplayExternalIPResults(extIPClients, errors, err)
// Output:
}
type GetExternalIPAddresser interface {
GetExternalIPAddress() (NewExternalIPAddress string, err error)
GetServiceClient() *goupnp.ServiceClient
}
func DisplayExternalIPResults(clients []GetExternalIPAddresser, errors []error, err error) {
if err != nil {
fmt.Fprintln(os.Stderr, "Error discovering service with UPnP: ", err)
return
@ -31,7 +50,7 @@ func Example_getExternalIPAddress() {
fmt.Fprintf(os.Stderr, "Successfully discovered %d services:\n", len(clients))
for _, client := range clients {
device := &client.RootDevice.Device
device := &client.GetServiceClient().RootDevice.Device
fmt.Fprintln(os.Stderr, " Device:", device.FriendlyName)
if addr, err := client.GetExternalIPAddress(); err != nil {
@ -40,9 +59,4 @@ func Example_getExternalIPAddress() {
fmt.Fprintf(os.Stderr, " External IP address: %v\n", addr)
}
}
fmt.Println("Complete")
// Output:
// Running
// Complete
}

View File

@ -47,3 +47,10 @@ func NewServiceClients(searchTarget string) (clients []ServiceClient, errors []e
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
}