-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
57 lines (43 loc) · 1.06 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
package main
import (
"log"
"net"
"time"
coap "github.com/lobaro/coap-go/coap-old"
)
func main() {
log.Println("Init liblobarocoap")
log.Println("Creating UDP CoAP socket")
addr := &net.UDPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 5683,
}
conn, err := net.ListenUDP("udp4", addr)
if err != nil {
log.Fatal("Failed to create UDP listener", err)
}
defer conn.Close()
coap.CreateSocket(1)
go handleUdpOutgoing(conn)
handleUdpIncoming(conn)
}
func handleUdpOutgoing(conn *net.UDPConn) {
for {
response := <-coap.PendingResponses
log.Println("Sending UDP Data to ", response.Address, " => ", response.Message)
conn.WriteTo(response.Message, response.Address)
}
}
func handleUdpIncoming(conn *net.UDPConn) {
for {
buff := make([]byte, 1024)
n, addr, err := conn.ReadFromUDP(buff)
if err != nil {
log.Println("Failed to read message from UDP socket", err)
}
msg := buff[:n]
log.Println("Received UDP Data@", addr.IP, ":", addr.Port, "=>", msg)
coap.HandleReceivedUdp4Message(1, addr, msg)
time.Sleep(time.Millisecond * 100)
}
}