forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mirrors.go
304 lines (274 loc) · 7.28 KB
/
mirrors.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package mirrors
import (
"encoding/gob"
"errors"
"fmt"
"io"
"log"
"net"
"os"
"regexp"
"sync"
)
var debug bool = false
// Check an error code and print a message if there was an error
func check(err error, format string, message ...interface{}) bool {
if err != nil {
s := err.Error()
_ = s
log.Printf("%v:%s\n", fmt.Sprintf(format, message...), err)
}
return err != nil
}
// Check an error code and print a message if there was an error and exit
func checkOrDie(err error, format string, message ...interface{}) {
if err != nil {
s := err.Error()
_ = s
log.Fatalf("%v:%s\n", fmt.Sprintf(format, message...), err)
}
}
type Mirrors struct {
Name string
mapMu sync.RWMutex
mirrorMap map[[32]byte]int
connMu sync.RWMutex
connections map[int]net.Conn
MaxConn int
l net.Listener
}
func (m *Mirrors) Len() int {
m.mapMu.Lock()
defer m.mapMu.Unlock()
return len(m.mirrorMap)
}
func (m *Mirrors) Done() {
if m.l != nil {
m.l.Close()
}
m.connMu.Lock()
for n, conn := range m.connections {
delete(m.connections, n)
conn.Close()
}
m.connMu.Unlock()
}
func (m *Mirrors) Init(name string) {
m.Name = name
m.mapMu.Lock()
m.mirrorMap = make(map[[32]byte]int)
m.mapMu.Unlock()
m.connMu.Lock()
m.connections = make(map[int]net.Conn)
m.connMu.Unlock()
}
// Send a new mirror to a connection
func (m *Mirrors) SendC(conn net.Conn, h [32]byte) (err error) {
if debug {
fmt.Printf("%s:sendc(%s,%x)\n", m.Name, conn.RemoteAddr(), h)
}
n, err2 := conn.Write(h[:])
if err2 == nil && n != 32 {
err2 = errors.New("Short write to " + conn.RemoteAddr().String())
}
if err == nil && err2 != nil {
err = err2
}
return err
}
// Send a new mirror to all our connections
func (m *Mirrors) Send(h [32]byte) (err error) {
if debug {
fmt.Printf("%s:send(%x)\n", m.Name, h)
}
m.connMu.Lock()
defer m.connMu.Unlock()
for _, conn := range m.connections {
n, err2 := conn.Write(h[:])
if err2 == nil && n != 32 {
err2 = errors.New("Short write to " + conn.RemoteAddr().String())
}
if err == nil && err2 != nil {
err = err2
}
}
return err
}
// Add a mirror to our list of mirrors
func (m *Mirrors) Add(h [32]byte) {
if debug {
fmt.Printf("%s:add(%x)\n", m.Name, h)
}
m.mapMu.Lock()
m.mirrorMap[h] = 1
m.mapMu.Unlock()
m.Send(h)
}
// Check if we have seen a mirror and add it if it is new
func (m *Mirrors) IsMirror(h [32]byte) (rval bool) {
if debug {
// fmt.Printf("%s:ismirror(%x)\n",m.Name, h)
}
m.mapMu.RLock()
x, ok := m.mirrorMap[h]
m.mapMu.RUnlock()
rval = x > 0
if debug {
fmt.Printf("%s:ismirror(%x)=%v\n", m.Name, h, *&rval)
}
if !ok {
m.Add(h)
}
return rval
}
func (m *Mirrors) Save(filename string) error {
if debug {
fmt.Printf("%s:save(%s)\n", m.Name, filename)
}
f, err := os.Create(filename)
check(err, "Mirrors.Save(%s) error:", filename)
if err == nil {
m.mapMu.RLock() // lock the map for read
checkOrDie(gob.NewEncoder(f).Encode(m.mirrorMap), "Mirrors.Save(%s) encode error:", filename)
m.mapMu.RUnlock()
checkOrDie(f.Close(), "Mirrors.Save(%s) close error:", filename)
}
return err
}
func (m *Mirrors) Load(filename string) error {
if debug {
fmt.Printf("%s:load(%s)\n", m.Name, filename)
}
if m.mirrorMap == nil {
m.mirrorMap = make(map[[32]byte]int)
}
f, err := os.Open(filename)
check(err, "Mirrors.Load(%s) error:", filename)
if err == nil {
m.mapMu.Lock() // Lock the map for write
checkOrDie(gob.NewDecoder(f).Decode(&m.mirrorMap), "Mirrors.Load(%s) decode error:", filename)
m.mapMu.Unlock()
checkOrDie(f.Close(), "Mirrors.Load(%s) close error:", filename)
}
return err
}
func (m *Mirrors) Listen(port string) {
if debug {
fmt.Printf("%s:listen(%s)\n", m.Name, port)
}
// Listen for incoming connections.
var err error
m.l, err = net.Listen("tcp", "localhost"+":"+port)
checkOrDie(err, "Mirrors.Listen error:")
// spawn a thread to accept connections
go func() {
// Close the listener when the application closes.
defer m.l.Close()
for {
// Listen for an incoming connection.
conn, err := m.l.Accept()
// Awkward way to detect we are shutting down the listener socket.
if err != nil {
if ok, _ := regexp.MatchString("accept tcp .*:.*: use of closed network connection", err.Error()); ok {
return
}
}
checkOrDie(err, "Mirrors.Listen accept error:")
// simple handshake to insure the connection is alive
{
// When I accept an incoming connection I say hello
_, err := conn.Write([]byte("Hello"))
checkOrDie(err, "%s:Send Hello Failed on %s", m.Name, conn.RemoteAddr())
var hello [5]byte
_, err = conn.Read(hello[:])
checkOrDie(err, "%s:Recv Hello Failed on %s", m.Name, conn.RemoteAddr())
if string(hello[:]) != "Hello" {
log.Fatalf("%s:Recv Hello Failed on %s got <%s>", m.Name, conn.RemoteAddr(), string(hello[:]))
}
}
// send our map when we connect.
m.mapMu.Lock()
if len(m.mirrorMap) > 0 {
for h, _ := range m.mirrorMap {
m.SendC(conn, h)
}
}
m.mapMu.Unlock()
// Handle incoming mirrors in a new goroutine.
go m.handleRequest(conn)
}
}()
}
// Handles connection. Read and add mirrors to the map until the connection closes
func (m *Mirrors) handleRequest(conn net.Conn) {
if debug {
fmt.Printf("%s:handleRequest(%v)\n", m.Name, conn.RemoteAddr())
}
m.connMu.Lock()
var myConn = m.MaxConn
m.MaxConn++
m.connections[myConn] = conn
// Add the new connection to my list
m.connMu.Unlock()
conn.(*net.TCPConn).SetNoDelay(true) // Should be the default.
var err error
for {
// Make a buffer to hold incoming data.
var buf [32]byte
var reqLen int
// Read the incoming connection into the buffer.
reqLen, err = conn.Read(buf[:])
// Normal way to detect the connection has closed
if err == io.EOF {
return
}
// Awkward way to detect we are shutting down the connection socket.
if err != nil {
if ok, _ := regexp.MatchString("read tcp .*: use of closed network connection", err.Error()); ok {
break
}
}
if check(err, "Error on connection to %v read %d bytes, error:", conn.RemoteAddr(), reqLen) || reqLen != 32 {
break
}
m.IsMirror(buf) // Add the mirror to my map if needed
}
if debug {
fmt.Printf("%s:handleRequest(%v) exiting %v\n", m.Name, conn.RemoteAddr(), err)
}
// remove the connection from my list
m.connMu.Lock()
delete(m.connections, myConn)
m.connMu.Unlock()
// Close the connection when you're done with it.
conn.Close()
}
func (m *Mirrors) Connect(addressPort string) error {
if debug {
fmt.Printf("%s:connect(%v)\n", m.Name, addressPort)
}
conn, err := net.Dial("tcp", addressPort)
checkOrDie(err, "Mirrors.Connect(%s) error:", addressPort)
// simple handshake to insure the connection is alive
{
// When I accept an incoming connection I say hello
_, err := conn.Write([]byte("Hello"))
checkOrDie(err, "%s:Send Hello Failed on %s", m.Name, conn.RemoteAddr())
var hello [5]byte
_, err = conn.Read(hello[:])
checkOrDie(err, "%s:Recv Hello Failed on %s", m.Name, conn.RemoteAddr())
if string(hello[:]) != "Hello" {
log.Fatalf("%s:Recv Hello Failed on %s got <%s>", m.Name, conn.RemoteAddr(), string(hello[:]))
}
}
// send our map when we connect.
m.mapMu.Lock()
if len(m.mirrorMap) > 0 {
for h, _ := range m.mirrorMap {
m.SendC(conn, h)
}
}
m.mapMu.Unlock()
go m.handleRequest(conn)
return err
}