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