retouch rgen

pull/375/head
Minh Doan 6 years ago committed by Minh Doan
parent d1f8e8c7cc
commit c80e4b9f3a
  1. 35
      api/service/consensus/consensus.go
  2. 36
      api/service/rgen/service.go

@ -0,0 +1,35 @@
package consensus
import (
"github.com/harmony-one/harmony/consensus"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/internal/utils"
)
// Service is the consensus service.
type Service struct {
blockChannel chan *types.Block // The channel to receive new blocks from Node
consensus *consensus.Consensus
stopChan chan struct{}
stoppedChan chan struct{}
}
// NewService returns consensus service.
func NewService(blockChannel chan *types.Block, consensus *consensus.Consensus) *Service {
return &Service{blockChannel: blockChannel, consensus: consensus}
}
// StartService starts service.
func (s *Service) StartService() {
s.stopChan = make(chan struct{})
s.stoppedChan = make(chan struct{})
s.consensus.WaitForNewBlock(s.blockChannel, s.stopChan, s.stoppedChan)
}
// StopService stops service.
func (s *Service) StopService() {
utils.GetLogInstance().Info("Stopping consensus service.")
s.stopChan <- struct{}{}
<-s.stoppedChan
utils.GetLogInstance().Info("Consensus service stopped.")
}

@ -1,35 +1,35 @@
package service package rgen
import ( import (
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
) )
// RandomGeneration is the consensus service. // Service is the random generation service.
type RandomGeneration struct { type Service struct {
stopChan chan struct{} stopChan chan struct{}
stoppedChan chan struct{} stoppedChan chan struct{}
} }
// NewRandomGeneration returns random generation service. // NewService returns random generation service.
func NewRandomGeneration() *RandomGeneration { func NewService() *Service {
return &RandomGeneration{} return &Service{}
} }
// StartService starts random generation service. // StartService starts random generation service.
func (cs *RandomGeneration) StartService() { func (s *Service) StartService() {
cs.stopChan = make(chan struct{}) s.stopChan = make(chan struct{})
cs.stoppedChan = make(chan struct{}) s.stoppedChan = make(chan struct{})
cs.Init() s.Init()
cs.Run(cs.stopChan, cs.stoppedChan) s.Run(s.stopChan, s.stoppedChan)
} }
// Init initializes random generation. // Init initializes random generation.
func (cs *RandomGeneration) Init() { func (s *Service) Init() {
} }
// Run runs random generation. // Run runs random generation.
func (cs *RandomGeneration) Run(stopChan chan struct{}, stoppedChan chan struct{}) { func (s *Service) Run(stopChan chan struct{}, stoppedChan chan struct{}) {
go func() { go func() {
defer close(stoppedChan) defer close(stoppedChan)
for { for {
@ -37,7 +37,7 @@ func (cs *RandomGeneration) Run(stopChan chan struct{}, stoppedChan chan struct{
default: default:
utils.GetLogInstance().Info("Running random generation") utils.GetLogInstance().Info("Running random generation")
// Write some logic here. // Write some logic here.
cs.DoRandomGeneration() s.DoRandomGeneration()
case <-stopChan: case <-stopChan:
return return
} }
@ -46,14 +46,14 @@ func (cs *RandomGeneration) Run(stopChan chan struct{}, stoppedChan chan struct{
} }
// DoRandomGeneration does random generation. // DoRandomGeneration does random generation.
func (cs *RandomGeneration) DoRandomGeneration() { func (s *Service) DoRandomGeneration() {
} }
// StopService stops random generation service. // StopService stops random generation service.
func (cs *RandomGeneration) StopService() { func (s *Service) StopService() {
utils.GetLogInstance().Info("Stopping random generation service.") utils.GetLogInstance().Info("Stopping random generation service.")
cs.stopChan <- struct{}{} s.stopChan <- struct{}{}
<-cs.stoppedChan <-s.stoppedChan
utils.GetLogInstance().Info("Random generation stopped.") utils.GetLogInstance().Info("Random generation stopped.")
} }
Loading…
Cancel
Save