diff --git a/README.md b/README.md index 80f3d90..aae7e75 100644 --- a/README.md +++ b/README.md @@ -11,27 +11,54 @@ environments. Using the library is very simple, here is an example of publishing a service entry: - // Setup our service export - host, _ := os.Hostname() - info := []string{"My awesome service"}, - service, _ := NewMDNSService(host, "_foobar._tcp", "", "", 8000, nil, info) +``` +package main - // Create the mDNS server, defer shutdown - server, _ := mdns.NewServer(&mdns.Config{Zone: service}) - defer server.Shutdown() +import ( + "github.com/micro/mdns" + "os" +) +func main() { + + // Setup our service export + host, _ := os.Hostname() + info := []string{"My awesome service"} + service, _ := mdns.NewMDNSService(host, "_foobar._tcp", "", "", 8000, nil, info) + + // Create the mDNS server, defer shutdown + server, _ := mdns.NewServer(&mdns.Config{Zone: service}) + + defer server.Shutdown() +} +``` Doing a lookup for service providers is also very simple: - // Make a channel for results and start listening - entriesCh := make(chan *mdns.ServiceEntry, 4) - go func() { - for entry := range entriesCh { - fmt.Printf("Got new entry: %v\n", entry) - } - }() +``` +package main - // Start the lookup - mdns.Lookup("_foobar._tcp", entriesCh) - close(entriesCh) +import ( + "fmt" + "github.com/cryptix/mdns" +) +func main() { + + // Make a channel for results and start listening + entriesCh := make(chan *mdns.ServiceEntry, 8) + go func() { + for entry := range entriesCh { + fmt.Printf("Got new entry: %v\n", entry) + } + }() + + // Start the lookup + err := mdns.Lookup("_foobar._tcp", entriesCh) + if err != nil { + fmt.Println(err) + } + + close(entriesCh) +} +``` diff --git a/examples/client/.gitignore b/examples/client/.gitignore new file mode 100644 index 0000000..b051c6c --- /dev/null +++ b/examples/client/.gitignore @@ -0,0 +1 @@ +client diff --git a/examples/client/README.md b/examples/client/README.md new file mode 100644 index 0000000..cde54ef --- /dev/null +++ b/examples/client/README.md @@ -0,0 +1,17 @@ +# client + +[Run the service first](../service) + +Then, compile + run: + +``` +go build +./client +``` + +Check out other services: + +``` +./client +./client _foobarbaz._tcp +``` diff --git a/examples/client/main.go b/examples/client/main.go new file mode 100644 index 0000000..6408df1 --- /dev/null +++ b/examples/client/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "os" + "os/signal" + + "github.com/micro/mdns" +) + +func main() { + + serviceTag := "_foobar._tcp" + if len(os.Args) > 1 { + serviceTag = os.Args[1] + } + + // Make a channel for results and start listening + entriesCh := make(chan *mdns.ServiceEntry, 8) + defer close(entriesCh) + + go func() { + for entry := range entriesCh { + fmt.Printf("Got new entry: %v\n", entry) + } + }() + + // Start the lookups + err := mdns.Lookup(serviceTag, entriesCh) + if err != nil { + fmt.Println(err) + } + + wait() +} + +func wait() { + ch := make(chan os.Signal) + signal.Notify(ch, os.Interrupt, os.Kill) + <-ch +} diff --git a/examples/service/.gitignore b/examples/service/.gitignore new file mode 100644 index 0000000..24e1098 --- /dev/null +++ b/examples/service/.gitignore @@ -0,0 +1 @@ +service diff --git a/examples/service/README.md b/examples/service/README.md new file mode 100644 index 0000000..5f54edb --- /dev/null +++ b/examples/service/README.md @@ -0,0 +1,18 @@ +# service + +Compile + run: + +``` +go build +./service +``` + +[The, run the client](../service) + + +Use another service tag + +``` +./service +./service _foobarbaz._tcp +``` diff --git a/examples/service/main.go b/examples/service/main.go new file mode 100644 index 0000000..83dd498 --- /dev/null +++ b/examples/service/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "log" + "os" + "os/signal" + + "github.com/micro/mdns" +) + +func main() { + + serviceTag := "_foobar._tcp" + if len(os.Args) > 1 { + serviceTag = os.Args[1] + } + + // Setup our service export + host, err := os.Hostname() + if err != nil { + log.Fatal(err) + } + + info := []string{"My awesome service"} + service, err := mdns.NewMDNSService(host, serviceTag, "", "", 8000, nil, info) + if err != nil { + log.Fatal(err) + } + + // Create the mDNS server, defer shutdown + server, err := mdns.NewServer(&mdns.Config{Zone: service}) + if err != nil { + log.Fatal(err) + } + + defer server.Shutdown() + + wait() +} + +func wait() { + ch := make(chan os.Signal) + signal.Notify(ch, os.Interrupt, os.Kill) + <-ch +}