-
Notifications
You must be signed in to change notification settings - Fork 13
/
setup.go
146 lines (109 loc) · 3.21 KB
/
setup.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
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package announce
import (
"path"
"sync"
"github.com/bitmark-inc/bitmarkd/announce/broadcast"
"github.com/bitmark-inc/bitmarkd/announce/domain"
"github.com/bitmark-inc/bitmarkd/announce/parameter"
"github.com/bitmark-inc/bitmarkd/announce/receptor"
"github.com/bitmark-inc/bitmarkd/announce/rpc"
"github.com/bitmark-inc/bitmarkd/background"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/messagebus"
"github.com/bitmark-inc/logger"
)
const (
logCategory = "announce"
)
// file for storing saves peers
const backupFile = "peers.json"
// globals for background process
type announcerData struct {
sync.RWMutex // to allow locking
log *logger.L
// RPC interface
rpcs rpc.RPC
// Receptor interface
receptors receptor.Receptor
backupFile string
// data for thread
brdc background.Process
domain background.Process
// for background
background *background.T
// set once during initialise
initialised bool
}
// global data
var globalData announcerData
// Initialise - set up the announcement system
// pass a fully qualified domain for root node list
// or empty string for no root nodes
func Initialise(domainName, cacheDirectory string, f func(string) ([]string, error)) error {
globalData.Lock()
defer globalData.Unlock()
var err error
// no need to start if already started
if globalData.initialised {
return fault.AlreadyInitialised
}
globalData.log = logger.New(logCategory)
globalData.log.Info("starting…")
globalData.receptors = receptor.New(globalData.log)
globalData.backupFile = path.Join(cacheDirectory, backupFile)
globalData.log.Info("start restoring backup data…")
if err := receptor.Restore(globalData.backupFile, globalData.receptors); err != nil {
globalData.log.Errorf("fail to restore backup data: %s", err.Error())
}
globalData.rpcs = rpc.New()
globalData.domain, err = domain.New(
globalData.log,
domainName,
globalData.receptors,
f,
)
if nil != err {
return err
}
globalData.brdc = broadcast.New(
globalData.log,
globalData.receptors,
globalData.rpcs,
parameter.InitialiseInterval,
parameter.PollingInterval,
)
// all data initialised
globalData.initialised = true
// start background processes
globalData.log.Info("start background…")
processes := background.Processes{
globalData.domain, globalData.brdc,
}
globalData.background = background.Start(processes, messagebus.Bus.Announce.Chan())
return nil
}
// Finalise - stop all background tasks
func Finalise() error {
if !globalData.initialised {
return fault.NotInitialised
}
globalData.log.Info("shutting down…")
globalData.log.Flush()
// stop background
globalData.background.Stop()
// release message bus
messagebus.Bus.Announce.Release()
globalData.log.Info("start backing up peer data…")
if err := receptor.Backup(globalData.backupFile, globalData.receptors.Connectable()); err != nil {
globalData.log.Errorf("fail to backup peer data: %s", err.Error())
}
// finally...
globalData.initialised = false
globalData.log.Info("finished")
globalData.log.Flush()
return nil
}