-
Notifications
You must be signed in to change notification settings - Fork 0
/
neighbour.go
189 lines (162 loc) · 4.71 KB
/
neighbour.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
// "fmt"
"encoding/hex"
"net"
"strings"
"time"
"github.com/go-ndn/log"
// "github.com/go-ndn/mux"
"github.com/go-ndn/ndn"
// "github.com/go-ndn/tlv"
"github.com/go-ndn/packet"
)
const maxDatagramSize = 8192
var (
linkedNeighbours map[string]neighbour
)
// Neighbour struct to store captured neighbour
type neighbour struct {
Address string
Network string
Cost uint64
LocalFace *face
RemoteFace *face
}
type remoteNode struct {
Network, Address string
Cost uint64
}
func multicastMsgHandler(dataSource *net.UDPAddr, numOfBytes int, dataBytes []byte) (node remoteNode) {
// temporary logic. For neighbourhood discovery, we will replace it to other logic
if !selfIPAddress(dataSource.IP.String()) {
if _, linked := linkedNeighbours[dataSource.IP.String()]; !linked {
address := dataSource.IP.String() + ":6363"
node = remoteNode{"udp", address, 0}
log.Println(numOfBytes, "bytes read from", dataSource)
log.Println(hex.Dump(dataBytes[:numOfBytes]))
} else {
log.Println("linked!")
}
}
return
}
func selfIPAddress(recvIPAddress string) (selfIP bool) {
addrs, err := net.InterfaceAddrs()
if err != nil {
panic(err)
}
for _, addr := range addrs {
ipAddr := strings.Split(addr.String(), "/")
if recvIPAddress == ipAddr[0] {
selfIP = true
}
}
return
}
func serveMulticastUDP(tempNode chan remoteNode) {
addr, err := net.ResolveUDPAddr(config.Multicast.Network, config.Multicast.Address)
if err != nil {
log.Fatalln("ResolveUDPAddr error:", err)
}
listen, err := net.ListenMulticastUDP(config.Multicast.Network, nil, addr)
if err != nil {
log.Fatalln("ListenMulticastUDP error:", err)
}
err = listen.SetReadBuffer(maxDatagramSize)
if err != nil {
log.Fatalln("SetReadBuffer error:", err)
}
for {
dataBytes := make([]byte, maxDatagramSize)
numOfBytes, dataSource, err := listen.ReadFromUDP(dataBytes)
if err != nil {
log.Fatalln("ReadFromUDP failed:", err)
}
node := multicastMsgHandler(dataSource, numOfBytes, dataBytes)
log.Println("Detected neighbour node address = ", node.Address)
tempNode <- node
time.Sleep(time.Duration(config.HelloInterval) * time.Second)
}
}
func sendUDPHelloPacket() {
srvAddr := config.Multicast.Address
addr, err := net.ResolveUDPAddr(config.Multicast.Network, srvAddr)
if err != nil {
log.Fatalln(err)
}
connect, err := net.DialUDP(config.Multicast.Network, nil, addr)
for {
connect.Write([]byte("hello\n"))
time.Sleep(time.Duration(config.HelloInterval) * time.Second)
}
}
func neighbourhoodDiscovery(neighbourChan chan<- remoteNode) {
log.Println("Neighbourhood discovery...")
tempNode := make(chan remoteNode)
go serveMulticastUDP(tempNode)
go sendUDPHelloPacket()
for {
// node := <-tempNode
// neighbourChan <- node
neighbourChan <- <-tempNode
time.Sleep(time.Duration(config.HelloInterval) * time.Second)
}
}
func checkLinkedNeighbour() {
// Check if linked neighbour is still available.
for address, linkedNode := range linkedNeighbours {
// cuba cari cara lain utk connection checking
if checkLink, err := packet.Dial(linkedNode.Network, address); err != nil {
if linkedNode.RemoteFace.Handler == nil {
log.Println("Face not exist")
}
delete(linkedNeighbours, address)
log.Println(err)
} else {
checkLink.Close()
}
}
log.Println("Linked neighbours :", len(linkedNeighbours))
}
// function to create link between local and available neighbour
func createLink(neighbourChan <-chan remoteNode) {
neighbourNode := <-neighbourChan
// only add unlinked neighbour as remote face
if _, linked := linkedNeighbours[neighbourNode.Address]; !linked {
log.Println("Create link between remote and local face")
log.Println("Neighbour Network :", neighbourNode.Network)
log.Println("Neighbour Address :", neighbourNode.Address)
// create interest channel
interestChan := make(chan *ndn.Interest)
// remote face
remote, err := newFace(neighbourNode.Network, neighbourNode.Address, neighbourNode.Cost, interestChan)
if err != nil {
delete(linkedNeighbours, neighbourNode.Address)
log.Println(err)
} else {
// local face
local, err := newFace(config.Local.Network, config.Local.Address, 0, nil)
if err != nil {
log.Fatalln(err)
}
defer local.Close()
defer remote.Close()
// Register remote face as linked neighbour
linkedNeighbours[neighbourNode.Address] =
neighbour{
Network: neighbourNode.Network,
Address: neighbourNode.Address,
Cost: neighbourNode.Cost,
LocalFace: local,
RemoteFace: remote,
}
// advertise name prefix
go local.advertise(remote)
// create remote tunnel
for interest := range interestChan {
local.ServeNDN(remote, interest)
}
}
}
}