-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
main.go
67 lines (57 loc) · 1.37 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"bytes"
"log"
gonet "net"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/go-coap/v3/message/codes"
"github.com/plgd-dev/go-coap/v3/mux"
"github.com/plgd-dev/go-coap/v3/net"
"github.com/plgd-dev/go-coap/v3/options"
"github.com/plgd-dev/go-coap/v3/udp"
)
func handleMcast(w mux.ResponseWriter, r *mux.Message) {
path, err := r.Options().Path()
if err != nil {
log.Printf("cannot get path: %v", err)
return
}
log.Printf("Got mcast message: path=%q: from %v", path, w.Conn().RemoteAddr())
w.SetResponse(codes.Content, message.TextPlain, bytes.NewReader([]byte("mcast response")))
}
func main() {
m := mux.NewRouter()
m.Handle("/oic/res", mux.HandlerFunc(handleMcast))
multicastAddr := "224.0.1.187:5683"
l, err := net.NewListenUDP("udp4", multicastAddr)
if err != nil {
log.Println(err)
return
}
ifaces, err := gonet.Interfaces()
if err != nil {
log.Println(err)
return
}
a, err := gonet.ResolveUDPAddr("udp", multicastAddr)
if err != nil {
log.Println(err)
return
}
for i := range ifaces {
iface := ifaces[i]
err := l.JoinGroup(&iface, a)
if err != nil {
log.Printf("cannot JoinGroup(%v, %v): %v", iface, a, err)
}
}
err = l.SetMulticastLoopback(true)
if err != nil {
log.Println(err)
return
}
defer l.Close()
s := udp.NewServer(options.WithMux(m))
defer s.Stop()
log.Fatal(s.Serve(l))
}