From 350efc0bf9d341b01a9e10612ffaa3d2c3453484 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Fri, 25 Jan 2019 11:57:36 -0800 Subject: [PATCH] add service type, action -- modify test for TakeAction --- node/node.go | 1 + node/service.go | 77 +++++++++++++++++++++++++++++++++----------- node/service_test.go | 4 +-- 3 files changed, 61 insertions(+), 21 deletions(-) diff --git a/node/node.go b/node/node.go index 1f54d7b48..3301610f9 100644 --- a/node/node.go +++ b/node/node.go @@ -140,6 +140,7 @@ type Node struct { // Action Channel actionChannel chan *Action + serviceStore *ServiceStore // For test only TestBankKeys []*ecdsa.PrivateKey diff --git a/node/service.go b/node/service.go index 686004336..44ecaa2a5 100644 --- a/node/service.go +++ b/node/service.go @@ -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): diff --git a/node/service_test.go b/node/service_test.go index fea7d81b3..2e4c6932c 100644 --- a/node/service_test.go +++ b/node/service_test.go @@ -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}) }