-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.go
210 lines (156 loc) · 3.16 KB
/
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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package sputnik
import "time"
type ServerConnection any
type ServerConnector interface {
// Connects to the server and return connection to server
// If connection failed, returns error.
// ' Connect' for already connected
// and still not brocken connection should
// return the same value returned in previous
// successful call(s) and nil error
Connect(cf ConfFactory) (conn ServerConnection, err error)
// Returns false if
// - was not connected at all
// - was connected, but connection is brocken
// True returned if
// - connected and connection is alive
IsConnected() bool
// If connection is alive closes it
Disconnect()
}
func ConnectorDescriptor() BlockDescriptor {
return BlockDescriptor{DefaultConnectorName, DefaultConnectorResponsibility}
}
func init() {
RegisterBlockFactory(DefaultConnectorName, connectorBlockFactory)
}
const DefaultConnectorTimeout = time.Second * 5
func connectorBlockFactory() *Block {
connector := new(connector)
block := NewBlock(
WithInit(connector.init),
WithRun(connector.run),
WithFinish(connector.finish),
WithOnMsg(connector.onSetup))
return block
}
type doIt func()
type connector struct {
cf ConfFactory
mc chan Msg
cnr ServerConnector
to time.Duration
cbc BlockCommunicator
ibc BlockCommunicator
bgfin chan struct{}
endfin chan struct{}
next doIt
connected bool
}
func (c *connector) init(cf ConfFactory) error {
c.cf = cf
c.next = c.connect
c.mc = make(chan Msg, 1)
c.bgfin = make(chan struct{}, 1)
return nil
}
func (c *connector) run(self BlockCommunicator) {
defer close(c.mc)
c.cbc = self
c.ibc, _ = self.Communicator(InitiatorResponsibility)
c.endfin = make(chan struct{}, 1)
defer close(c.endfin)
enableloop := false
select {
case <-c.bgfin:
break
case msg := <-c.mc:
c.setup(msg)
enableloop = true
}
if enableloop {
ticker := time.NewTicker(c.to)
runloop:
for {
select {
case <-c.bgfin:
break runloop
case <-ticker.C:
c.next()
}
}
c.close()
ticker.Stop()
}
return
}
func (c *connector) finish(init bool) {
if init {
return
}
close(c.bgfin)
<-c.endfin
return
}
func (c *connector) onSetup(msg Msg) {
c.mc <- msg
return
}
func (c *connector) setup(msg Msg) {
cntr, exists := msg["__connector"]
if !exists {
return
}
c.cnr = cntr.(ServerConnector)
to, _ := msg["__timeout"]
timeout, _ := to.(time.Duration)
c.to = timeout
return
}
func (c *connector) connect() {
if c.cnr == nil {
return
}
if !c.connected {
conn, err := c.cnr.Connect(c.cf)
if err != nil {
return
}
c.notifyConnected(conn)
return
}
}
func (c *connector) checkConnection() {
if c.cnr == nil {
return
}
if c.cnr.IsConnected() {
return
}
c.notifyDisonnected()
return
}
func (c *connector) close() {
if c.cnr == nil {
return
}
c.cnr.Disconnect()
c.cnr = nil
c.next = c.nop
c.connected = false
}
func (c *connector) nop() {
return
}
func (c *connector) notifyConnected(conn any) {
c.ibc.Send(serverconnectedmsg((conn)))
c.connected = true
c.next = c.checkConnection
return
}
func (c *connector) notifyDisonnected() {
c.ibc.Send(serverdisconnectedmsg())
c.connected = false
c.next = c.connect
return
}