-
Notifications
You must be signed in to change notification settings - Fork 1
/
hub.go
162 lines (139 loc) · 3.08 KB
/
hub.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
package collect
import (
"sync"
"github.com/libp2p/go-libp2p-core/peer"
)
type TopicHub struct {
tw TopicWires
rw sync.RWMutex
outbounds map[string]*outbound
}
func NewTopicHub(tw TopicWires) *TopicHub {
out := &TopicHub{
tw: tw,
rw: sync.RWMutex{},
outbounds: make(map[string]*outbound),
}
tw.SetListener(out)
tw.SetTopicMsgHandler(out.handleMsg)
return out
}
func (th *TopicHub) Wires(topic string) Wires {
th.rw.RLock()
defer th.rw.RUnlock()
return th.outbounds[topic]
}
func (th *TopicHub) Join(topic string) error {
return th.JoinWithNotifiee(topic, &defaultWireNotifiee{})
}
func (th *TopicHub) JoinWithNotifiee(topic string, wn WireListener) error {
th.rw.Lock()
defer th.rw.Unlock()
if _, ok := th.outbounds[topic]; ok {
return ErrTopicJoined
}
o := th.newOutbound(topic)
o.SetListener(wn)
th.outbounds[topic] = o
return th.tw.Join(topic)
}
func (th *TopicHub) Leave(topic string) error {
th.rw.Lock()
defer th.rw.Unlock()
return th.leave(topic)
}
func (th *TopicHub) Close() error {
th.rw.Lock()
defer th.rw.Unlock()
for topic := range th.outbounds {
if err := th.leave(topic); err != nil {
// TODO: log
continue
}
}
return nil
}
func (th *TopicHub) leave(topic string) error {
if _, ok := th.outbounds[topic]; !ok {
return ErrTopicNotJoined
}
delete(th.outbounds, topic)
return th.tw.Leave(topic)
}
func (th *TopicHub) HandlePeerUp(p peer.ID, topic string) {
th.rw.RLock()
defer th.rw.RUnlock()
o, ok := th.outbounds[topic]
if !ok {
// TODO
return
}
o.handleUp(p)
}
func (th *TopicHub) HandlePeerDown(p peer.ID, topic string) {
th.rw.RLock()
defer th.rw.RUnlock()
o, ok := th.outbounds[topic]
if !ok {
// TODO
return
}
o.handleDown(p)
}
func (th *TopicHub) handleMsg(topic string, from peer.ID, data []byte) {
th.rw.RLock()
defer th.rw.RUnlock()
o, ok := th.outbounds[topic]
if !ok {
//TODO: receive unsubscribed topic
return
}
o.handler(from, data)
}
func (th *TopicHub) newOutbound(topic string) *outbound {
return &outbound{
hub: th,
topic: topic,
listener: &defaultWireNotifiee{},
handler: func(from peer.ID, data []byte) {},
}
}
type outbound struct {
rw sync.RWMutex
hub *TopicHub
topic string
listener WireListener
handler MsgHandler
}
func (o *outbound) ID() peer.ID {
return o.hub.tw.ID()
}
func (o *outbound) Neighbors() []peer.ID {
return o.hub.tw.Neighbors(o.topic)
}
func (o *outbound) SetListener(wl WireListener) {
o.rw.Lock()
defer o.rw.Unlock()
o.listener = wl
}
func (o *outbound) SendMsg(to peer.ID, data []byte) error {
return o.hub.tw.SendMsg(o.topic, to, data)
}
func (o *outbound) SetMsgHandler(h MsgHandler) {
o.rw.Lock()
defer o.rw.Unlock()
o.handler = h
}
func (o *outbound) handleUp(p peer.ID) {
o.rw.RLock()
defer o.rw.RUnlock()
go o.listener.HandlePeerUp(p)
}
func (o *outbound) handleDown(p peer.ID) {
o.rw.RLock()
defer o.rw.RUnlock()
go o.listener.HandlePeerDown(p)
}
type defaultWireNotifiee struct{}
func (d *defaultWireNotifiee) HandlePeerUp(p peer.ID) {}
func (d *defaultWireNotifiee) HandlePeerDown(p peer.ID) {}