add service type, action -- modify test for TakeAction

pull/334/head
Minh Doan 6 years ago committed by Minh Doan
parent f1245c117b
commit 350efc0bf9
  1. 1
      node/node.go
  2. 77
      node/service.go
  3. 4
      node/service_test.go

@ -140,6 +140,7 @@ type Node struct {
// Action Channel
actionChannel chan *Action
serviceStore *ServiceStore
// For test only
TestBankKeys []*ecdsa.PrivateKey

@ -10,16 +10,42 @@ import (
// ActionType ...
type ActionType byte
// Constants ...
// Constants for Action Type.
const (
SyncingAction ActionType = iota
KillSyncingAction
Start ActionType = iota
Stop
)
// Type is service type.
type Type byte
// Constants for Type.
const (
SyncingSupport Type = iota
SupportClient
SupportExplorer
SyncingActionTest
Test
Done
)
func (t Type) String() string {
switch t {
case SupportClient:
return "SupportClient"
case SyncingSupport:
return "SyncingSupport"
case SupportExplorer:
return "SupportExplorer"
case Test:
return "Test"
case Done:
return "Done"
default:
return "Unknown"
}
}
// Constants for timing.
const (
// WaitForStatusUpdate is the delay time to update new status. Currently set 1 second for development. Should be 30 minutes for production.
WaitForStatusUpdate = time.Second * 1
@ -27,8 +53,20 @@ const (
// Action is type of service action.
type Action struct {
t ActionType
params map[string]interface{}
action ActionType
serviceType Type
params map[string]interface{}
}
// ServiceInterface ...
type ServiceInterface interface {
Start()
Stop()
}
// ServiceStore stores all services for service manager.
type ServiceStore struct {
services map[Type]ServiceInterface
}
// Start node.
@ -43,18 +81,19 @@ func (node *Node) SendAction(action *Action) {
// TakeAction ...
func (node *Node) TakeAction(action *Action) {
switch action.t {
case SyncingActionTest:
fmt.Println("Running syncing support")
case SyncingAction:
utils.GetLogInstance().Info("Running syncing support")
go node.SupportSyncing()
case KillSyncingAction:
utils.GetLogInstance().Info("Killing syncing")
case SupportClient:
go node.SupportClient()
case SupportExplorer:
go node.SupportExplorer()
if node.serviceStore == nil {
utils.GetLogInstance().Error("Service store is not initialized.")
return
}
if service, ok := node.serviceStore.services[action.serviceType]; ok {
switch action.action {
case Start:
fmt.Printf("Start %s\n", action.serviceType)
service.Start()
case Stop:
fmt.Printf("Stop %s\n", action.serviceType)
service.Stop()
}
}
}
@ -66,7 +105,7 @@ func (node *Node) StartServiceManager() chan *Action {
select {
case action := <-ch:
node.TakeAction(action)
if action.t == Done {
if action.serviceType == Done {
return
}
case <-time.After(WaitForStatusUpdate):

@ -12,9 +12,9 @@ func TestTakeAction(t *testing.T) {
for i := 0; i < 2; i++ {
select {
case <-time.After(WaitForStatusUpdate):
node.SendAction(&Action{t: SyncingActionTest})
node.SendAction(&Action{action: Start, serviceType: SyncingSupport})
}
}
node.SendAction(&Action{t: Done})
node.SendAction(&Action{serviceType: Done})
}

Loading…
Cancel
Save