Simplify Service/Reactor pattern

This commit is contained in:
Jae Kwon
2015-07-22 06:21:21 -07:00
parent 7441e322d0
commit e7c1febb65
14 changed files with 67 additions and 56 deletions
+4 -2
View File
@@ -121,13 +121,15 @@ func (a *AddrBook) init() {
}
}
func (a *AddrBook) AfterStart() {
func (a *AddrBook) OnStart() {
a.QuitService.OnStart()
a.loadFromFile(a.filePath)
a.wg.Add(1)
go a.saveRoutine()
}
func (a *AddrBook) AfterStop() {
func (a *AddrBook) OnStop() {
a.QuitService.OnStop()
a.wg.Wait()
}
+4 -2
View File
@@ -127,7 +127,8 @@ func NewMConnection(conn net.Conn, chDescs []*ChannelDescriptor, onReceive recei
return mconn
}
func (c *MConnection) AfterStart() {
func (c *MConnection) OnStart() {
c.BaseService.OnStart()
c.quit = make(chan struct{})
c.flushTimer = NewThrottleTimer("flush", flushThrottleMS*time.Millisecond)
c.pingTimer = NewRepeatTimer("ping", pingTimeoutSeconds*time.Second)
@@ -136,7 +137,8 @@ func (c *MConnection) AfterStart() {
go c.recvRoutine()
}
func (c *MConnection) AfterStop() {
func (c *MConnection) OnStop() {
c.BaseService.OnStop()
c.flushTimer.Stop()
c.pingTimer.Stop()
c.chStatsTimer.Stop()
+4 -2
View File
@@ -97,11 +97,13 @@ SKIP_UPNP:
return dl
}
func (l *DefaultListener) AfterStart() {
func (l *DefaultListener) OnStart() {
l.BaseService.OnStart()
go l.listenRoutine()
}
func (l *DefaultListener) AfterStop() {
func (l *DefaultListener) OnStop() {
l.BaseService.OnStop()
l.listener.Close()
}
+4 -2
View File
@@ -72,11 +72,13 @@ func newPeer(conn net.Conn, peerNodeInfo *types.NodeInfo, outbound bool, reactor
return p
}
func (p *Peer) AfterStart() {
func (p *Peer) OnStart() {
p.BaseService.OnStart()
p.mconn.Start()
}
func (p *Peer) AfterStop() {
func (p *Peer) OnStop() {
p.BaseService.OnStop()
p.mconn.Stop()
}
+4 -2
View File
@@ -41,11 +41,13 @@ func NewPEXReactor(book *AddrBook) *PEXReactor {
return pexR
}
func (pexR *PEXReactor) AfterStart() {
func (pexR *PEXReactor) OnStart() {
pexR.BaseReactor.OnStart()
go pexR.ensurePeersRoutine()
}
func (pexR *PEXReactor) AfterStop() {
func (pexR *PEXReactor) OnStop() {
pexR.BaseReactor.OnStop()
}
// Implements Reactor
+4 -2
View File
@@ -153,7 +153,8 @@ func (sw *Switch) SetNodePrivKey(nodePrivKey acm.PrivKeyEd25519) {
}
// Switch.Start() starts all the reactors, peers, and listeners.
func (sw *Switch) AfterStart() {
func (sw *Switch) OnStart() {
sw.BaseService.OnStart()
// Start reactors
for _, reactor := range sw.reactors {
reactor.Start()
@@ -168,7 +169,8 @@ func (sw *Switch) AfterStart() {
}
}
func (sw *Switch) AfterStop() {
func (sw *Switch) OnStop() {
sw.BaseService.OnStop()
// Stop listeners
for _, listener := range sw.listeners {
listener.Stop()