-
Notifications
You must be signed in to change notification settings - Fork 1
/
paychproto.go
182 lines (160 loc) · 4.35 KB
/
paychproto.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
package paychproto
/*
* Dual-licensed under Apache-2.0 and MIT.
*
* You can get a copy of the Apache License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* You can also get a copy of the MIT License at
*
* http://opensource.org/licenses/MIT
*
* @wcgcyx - https://github.com/wcgcyx
*/
import (
"context"
"sync"
"time"
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-core/host"
"github.com/wcgcyx/fcr/crypto"
"github.com/wcgcyx/fcr/offermgr"
"github.com/wcgcyx/fcr/paychmon"
"github.com/wcgcyx/fcr/paymgr"
"github.com/wcgcyx/fcr/peermgr"
"github.com/wcgcyx/fcr/peernet/protos/addrproto"
"github.com/wcgcyx/fcr/renewmgr"
"github.com/wcgcyx/fcr/settlemgr"
)
const (
QueryAddProtocol = "/fcr-calibr/paynet/paych/query"
AddProtocol = "/fcr-calibr/paynet/paych/add"
RenewProtocol = "/fcr-calibr/paynet/paych/renew"
ProtocolVersion = "/0.0.1"
)
// Logger
var log = logging.Logger("paychproto")
// PaychProtocol manages paych protocol.
// It handles paych creation and renew.
type PaychProtocol struct {
// Host
h host.Host
// Addr proto
addrProto *addrproto.AddrProtocol
// Signer
signer crypto.Signer
// Peer manager
peerMgr peermgr.PeerManager
// Offer manager
offerMgr offermgr.OfferManager
// Settlement manager
settleMgr settlemgr.SettlementManager
// Renew manager
renewMgr renewmgr.RenewManager
// Payment manager
payMgr paymgr.PaymentManager
// Monitor
monitor paychmon.PaychMonitor
// Publish hook
publishHookLock sync.RWMutex
publishHook func(byte, string)
// Process related.
routineCtx context.Context
// Shutdown function.
shutdown func()
// Options
ioTimeout time.Duration
opTimeout time.Duration
offerExpiry time.Duration
renewWindow uint64
}
// NewPaychProtocol creates a new paych protocol.
//
// @input - host, addr protocol, signer, peer manager, offer manager, settlement manager,
// renew manager, payment manager, options.
//
// @output - paych protocol.
func NewPaychProtocol(
h host.Host,
addrProto *addrproto.AddrProtocol,
signer crypto.Signer,
peerMgr peermgr.PeerManager,
offerMgr offermgr.OfferManager,
settleMgr settlemgr.SettlementManager,
renewMgr renewmgr.RenewManager,
payMgr paymgr.PaymentManager,
monitor paychmon.PaychMonitor,
opts Opts,
) *PaychProtocol {
// Parse options
ioTimeout := opts.IOTimeout
if ioTimeout == 0 {
ioTimeout = defaultIOTimeout
}
opTimeout := opts.OpTimeout
if opTimeout == 0 {
opTimeout = defaultOpTimeout
}
offerExpiry := opts.OfferExpiry
if offerExpiry == 0 {
offerExpiry = defaultOfferExpiry
}
renewWindow := opts.RenewWindow
if renewWindow == 0 || renewWindow >= 100 {
renewWindow = defaultRenewWindow
}
log.Infof("Start paych protocol...")
// Create routine
routineCtx, cancel := context.WithCancel(context.Background())
proto := &PaychProtocol{
h: h,
addrProto: addrProto,
signer: signer,
peerMgr: peerMgr,
offerMgr: offerMgr,
settleMgr: settleMgr,
renewMgr: renewMgr,
payMgr: payMgr,
monitor: monitor,
publishHookLock: sync.RWMutex{},
routineCtx: routineCtx,
ioTimeout: ioTimeout,
opTimeout: opTimeout,
offerExpiry: offerExpiry,
renewWindow: renewWindow,
}
h.SetStreamHandler(QueryAddProtocol+ProtocolVersion, proto.handleQueryAdd)
h.SetStreamHandler(AddProtocol+ProtocolVersion, proto.handleAdd)
h.SetStreamHandler(RenewProtocol+ProtocolVersion, proto.handleRenew)
proto.shutdown = func() {
log.Infof("Remove stream handlers...")
// Remove stream handlers
h.RemoveStreamHandler(QueryAddProtocol + ProtocolVersion)
h.RemoveStreamHandler(AddProtocol + ProtocolVersion)
h.RemoveStreamHandler(RenewProtocol + ProtocolVersion)
// Close routines
log.Infof("Stop all routines...")
cancel()
}
return proto
}
// Shutdown safely shuts down the component.
func (p *PaychProtocol) Shutdown() {
log.Infof("Start shutdown...")
p.shutdown()
}
// RegisterPublishHook adds a hook function to call when a new subscriber presents.
//
// @input - hook function.
func (p *PaychProtocol) RegisterPublishFunc(hook func(byte, string)) {
p.publishHookLock.Lock()
defer p.publishHookLock.Unlock()
p.publishHook = hook
}
// DeregisterPublishHook deregister the hook function.
func (p *PaychProtocol) DeregisterPublishHook() {
p.publishHookLock.Lock()
defer p.publishHookLock.Unlock()
p.publishHook = nil
}