-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
129 lines (115 loc) · 2.86 KB
/
link.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
package main
import (
"time"
)
// default values
var (
SpeedWireless float64 = 300000000 // m/s
DistanceWireless float64 = 57968000000 // meter 54500000000-401300000000
BandwidthWireless float64 = 2048 // bps, 500~32000, ref: https://mars.nasa.gov/msl/mission/communications/
SpeedWire float64 = 231000000 // .77c
DistanceWire float64 = 30 // meter
BandwidthWire float64 = 1073741824 // 1Gbps
PacketLossRate float64 = 0 // percenttge
)
// Emulate ethernet cables, connect two ports, no direction
type Link struct {
sender1 *Port
sink1 *Port
sender2 *Port
sink2 *Port
PacketLossRate float64 // percentage
Bandwidth float64 // in Mbps
Speed float64 // m/s
Distance float64 // in meter
delay float64
// for mars-earth
HardcodedDelay float64
Failed bool
stopSig chan bool
}
// Connect the ports of two nodes.
// Each node has a used ports counter,
// so no need to specify the port index here
func Connect(n1, n2 Node) {
l := new(Link)
l.PacketLossRate = 0
if n1.Name() == "GCC" || n2.Name() == "GCC" {
l.Bandwidth = BandwidthWireless
l.Speed = SpeedWireless
l.Distance = DistanceWireless
l.HardcodedDelay = 207
} else {
l.Bandwidth = BandwidthWire
l.Speed = SpeedWire
l.Distance = DistanceWire
}
l.sender1 = n1.OutPort()
l.sink1 = n2.InPort()
l.sender1.Neighbor = l.sink1.Owner
l.sink1.Neighbor = l.sender1.Owner
l.sender2 = n2.OutPort()
l.sink2 = n1.InPort()
l.sender2.Neighbor = l.sink2.Owner
l.sink2.Neighbor = l.sender2.Owner
l.stopSig = make(chan bool)
Links = append(Links, l)
go l.forward()
}
// size in bytes
func (l *Link) computeDelay(pktSize int) {
// var jitter float64
// if l.Distance < 1000 { // in-habitat
// jitter = rand.ExpFloat64() * float64(JITTER_BASE) / 1000000 // us
// }
if HARDCODE_DELAY_ENABLED {
l.delay = l.HardcodedDelay
return
}
l.delay = float64(pktSize)*8/l.Bandwidth + l.Distance/l.Speed
// + jitter
// fmt.Println("delay", l.delay)
}
func (l *Link) Stop() {
l.stopSig <- true
}
// forward packet from one port to the other
func (l *Link) forward() {
for {
select {
case <-l.stopSig:
return
case pkt := <-l.sender1.Channel:
go func() {
if l.Failed {
return
}
l.computeDelay(len(pkt.RawBytes))
if ANIMATION_ENABLED {
time.Sleep(1000 * time.Millisecond)
}
if DELAY_ENABLED {
time.Sleep(time.Duration(l.delay) * time.Second)
}
pkt.Delay += l.delay
l.sink1.Channel <- pkt
}()
case pkt := <-l.sender2.Channel:
go func() {
if l.Failed {
return
}
l.computeDelay(len(pkt.RawBytes))
// fmt.Println(l.delay)
pkt.Delay += l.delay
if ANIMATION_ENABLED {
time.Sleep(1000 * time.Millisecond)
}
if DELAY_ENABLED {
time.Sleep(time.Duration(l.delay) * time.Second)
}
l.sink2.Channel <- pkt
}()
}
}
}