You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.2 KiB
60 lines
1.2 KiB
6 years ago
|
package randgen
|
||
6 years ago
|
|
||
|
import (
|
||
|
"github.com/harmony-one/harmony/internal/utils"
|
||
|
)
|
||
|
|
||
6 years ago
|
// Service is the random generation service.
|
||
|
type Service struct {
|
||
6 years ago
|
stopChan chan struct{}
|
||
|
stoppedChan chan struct{}
|
||
|
}
|
||
|
|
||
6 years ago
|
// NewService returns random generation service.
|
||
|
func NewService() *Service {
|
||
|
return &Service{}
|
||
6 years ago
|
}
|
||
|
|
||
|
// StartService starts random generation service.
|
||
6 years ago
|
func (s *Service) StartService() {
|
||
|
s.stopChan = make(chan struct{})
|
||
|
s.stoppedChan = make(chan struct{})
|
||
6 years ago
|
|
||
6 years ago
|
s.Init()
|
||
|
s.Run(s.stopChan, s.stoppedChan)
|
||
6 years ago
|
}
|
||
|
|
||
|
// Init initializes random generation.
|
||
6 years ago
|
func (s *Service) Init() {
|
||
6 years ago
|
}
|
||
|
|
||
|
// Run runs random generation.
|
||
6 years ago
|
func (s *Service) Run(stopChan chan struct{}, stoppedChan chan struct{}) {
|
||
6 years ago
|
go func() {
|
||
|
defer close(stoppedChan)
|
||
|
for {
|
||
|
select {
|
||
|
default:
|
||
|
utils.GetLogInstance().Info("Running random generation")
|
||
6 years ago
|
// Write some logic here.
|
||
6 years ago
|
s.DoRandomGeneration()
|
||
6 years ago
|
case <-stopChan:
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
}
|
||
|
|
||
6 years ago
|
// DoRandomGeneration does random generation.
|
||
6 years ago
|
func (s *Service) DoRandomGeneration() {
|
||
6 years ago
|
|
||
|
}
|
||
|
|
||
6 years ago
|
// StopService stops random generation service.
|
||
6 years ago
|
func (s *Service) StopService() {
|
||
6 years ago
|
utils.GetLogInstance().Info("Stopping random generation service.")
|
||
6 years ago
|
s.stopChan <- struct{}{}
|
||
|
<-s.stoppedChan
|
||
6 years ago
|
utils.GetLogInstance().Info("Random generation stopped.")
|
||
6 years ago
|
}
|