Merge pull request #433 from harmony-one/rj_branch
Make drand as a service and make message passing workpull/439/head
commit
aded2838ff
@ -1,59 +0,0 @@ |
||||
package randgen |
||||
|
||||
import ( |
||||
"github.com/harmony-one/harmony/internal/utils" |
||||
) |
||||
|
||||
// Service is the random generation service.
|
||||
type Service struct { |
||||
stopChan chan struct{} |
||||
stoppedChan chan struct{} |
||||
} |
||||
|
||||
// New returns random generation service.
|
||||
func New() *Service { |
||||
return &Service{} |
||||
} |
||||
|
||||
// StartService starts random generation service.
|
||||
func (s *Service) StartService() { |
||||
s.stopChan = make(chan struct{}) |
||||
s.stoppedChan = make(chan struct{}) |
||||
|
||||
s.Init() |
||||
s.Run(s.stopChan, s.stoppedChan) |
||||
} |
||||
|
||||
// Init initializes random generation.
|
||||
func (s *Service) Init() { |
||||
} |
||||
|
||||
// Run runs random generation.
|
||||
func (s *Service) Run(stopChan chan struct{}, stoppedChan chan struct{}) { |
||||
go func() { |
||||
defer close(stoppedChan) |
||||
for { |
||||
select { |
||||
default: |
||||
utils.GetLogInstance().Info("Running random generation") |
||||
// Write some logic here.
|
||||
s.DoRandomGeneration() |
||||
case <-stopChan: |
||||
return |
||||
} |
||||
} |
||||
}() |
||||
} |
||||
|
||||
// DoRandomGeneration does random generation.
|
||||
func (s *Service) DoRandomGeneration() { |
||||
|
||||
} |
||||
|
||||
// StopService stops random generation service.
|
||||
func (s *Service) StopService() { |
||||
utils.GetLogInstance().Info("Stopping random generation service.") |
||||
s.stopChan <- struct{}{} |
||||
<-s.stoppedChan |
||||
utils.GetLogInstance().Info("Random generation stopped.") |
||||
} |
@ -0,0 +1,33 @@ |
||||
package randomness |
||||
|
||||
import ( |
||||
"github.com/harmony-one/harmony/drand" |
||||
"github.com/harmony-one/harmony/internal/utils" |
||||
) |
||||
|
||||
// Service is the randomness generation service.
|
||||
type Service struct { |
||||
stopChan chan struct{} |
||||
stoppedChan chan struct{} |
||||
DRand *drand.DRand |
||||
} |
||||
|
||||
// New returns randomness generation service.
|
||||
func New(dRand *drand.DRand) *Service { |
||||
return &Service{DRand: dRand} |
||||
} |
||||
|
||||
// StartService starts randomness generation service.
|
||||
func (s *Service) StartService() { |
||||
s.stopChan = make(chan struct{}) |
||||
s.stoppedChan = make(chan struct{}) |
||||
s.DRand.WaitForEpochBlock(s.DRand.ConfirmedBlockChannel, s.stopChan, s.stoppedChan) |
||||
} |
||||
|
||||
// StopService stops randomness generation service.
|
||||
func (s *Service) StopService() { |
||||
utils.GetLogInstance().Info("Stopping random generation service.") |
||||
s.stopChan <- struct{}{} |
||||
<-s.stoppedChan |
||||
utils.GetLogInstance().Info("Random generation stopped.") |
||||
} |
Loading…
Reference in new issue