-
Notifications
You must be signed in to change notification settings - Fork 3
/
latency_connector.go
109 lines (88 loc) · 2.87 KB
/
latency_connector.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
package overlay
import (
. "github.com/danalex97/Speer/events"
"github.com/danalex97/Speer/interfaces"
"github.com/danalex97/Speer/underlay"
)
// A LatencyConnector is an interface which allows sending and
// receiving packets. Moreover, the interface allows access to an
// ActiveObserver which can be used to react to packet receival.
type LatencyConnector interface {
Observer() ActiveObserver
interfaces.ControlTransport
}
// The UnderlayChan implements a LatencyConnector by using a proxy to strip
// the payload of the underlay packets at receiving a packet and sending a
// packet is done by using the network map to decorate the overlay packet
// inside an underlay packet.
//
// The mechanism used for delivering packets is a PassiveObserver attached to
// the router corresponing to the overlay id.
type UnderlayChan struct {
id string
eventReceiver Receiver
simulation *underlay.NetworkSimulation
networkMap LatencyMap
passiveObserver PassiveObserver
activeObserver ActiveObserver
}
func NewUnderlayChan(
id string,
simulation *underlay.NetworkSimulation,
networkMap LatencyMap,
) LatencyConnector {
u := new(UnderlayChan)
u.id = id
u.simulation = simulation
u.networkMap = networkMap
// Establish listener
u.eventReceiver = u.networkMap.Router(u.id)
u.passiveObserver = NewPassiveEventObserver(u.eventReceiver)
u.passiveObserver.SetProxy(u.ReceiveEvent)
// Create active observer
u.activeObserver = NewActiveEventObserver(u.eventReceiver)
// Register observers; Note that the active observer should be attached
// AFTER the passive observer so that the event delivered by the passive
// observer in enqueued when the active observer while allow its Proxy to
// be called. !Note this relies on details of the events package.!
u.simulation.RegisterObserver(u.passiveObserver)
u.simulation.RegisterObserver(u.activeObserver)
return u
}
func (u *UnderlayChan) Observer() ActiveObserver {
return u.activeObserver
}
// Proxy function used to strip the contents of an underlay packet. The
// UnderlayChan chan is a Decorator, so we call the Proxy function before
// delivering the packet.
func (u *UnderlayChan) ReceiveEvent(m interface{}) interface{} {
event := (m).(*Event)
packet := event.Payload().(underlay.Packet)
if packet.Src() == nil {
return nil
}
// We need to look only at our own packets.
if packet.Dest() != u.eventReceiver {
return nil
}
return packet.Payload()
}
func (u *UnderlayChan) ControlSend(dst string, msg interface{}) {
packet := underlay.NewPacket(
u.networkMap.Router(u.id),
u.networkMap.Router(dst),
msg,
)
if u.id == dst {
// Packet sent to self.
u.passiveObserver.Receive(NewEvent(0, packet, packet.Dest()))
return
}
u.simulation.SendPacket(packet)
}
func (u *UnderlayChan) ControlRecv() <-chan interface{} {
return u.passiveObserver.Recv()
}
func (u *UnderlayChan) ControlPing(id string) bool {
return true
}