NodeController.go 5.54 KB
Newer Older
duanjinfei's avatar
duanjinfei committed
1 2
package controllers

3
import (
duanjinfei's avatar
duanjinfei committed
4 5 6
	"encoding/json"
	"example.com/m/conf"
	"example.com/m/models"
7
	"example.com/m/nm"
duanjinfei's avatar
duanjinfei committed
8
	"example.com/m/operate"
duanjinfei's avatar
duanjinfei committed
9
	"example.com/m/utils"
duanjinfei's avatar
duanjinfei committed
10
	nodemanagerV2 "github.com/odysseus/odysseus-protocol/gen/proto/go/nodemanager/v2"
duanjinfei's avatar
duanjinfei committed
11
	"io"
duanjinfei's avatar
duanjinfei committed
12
	"strings"
duanjinfei's avatar
duanjinfei committed
13
	"time"
14
)
duanjinfei's avatar
duanjinfei committed
15 16 17 18 19 20

type NodeController struct {
	BaseController
}

func (c *NodeController) SetNmSeed() {
duanjinfei's avatar
duanjinfei committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
	bodyReq, err := io.ReadAll(c.Ctx.Request.Body)
	if err != nil || bodyReq == nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
	req := &models.SeedUrl{}
	err = json.Unmarshal(bodyReq, req)
	if err != nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
	serviceClient := operate.ConnNmGrpc(req.Seed)
	if serviceClient == nil {
		c.ResponseInfo(500, "seed is not connected", "")
	}
	conf.GetConfig().SetNmSeed(req.Seed)
	c.ResponseInfo(200, "set seed successful", "")
}

duanjinfei's avatar
duanjinfei committed
40
func (c *NodeController) SetBenefitAddress() {
duanjinfei's avatar
duanjinfei committed
41 42 43 44 45
	bodyReq, err := io.ReadAll(c.Ctx.Request.Body)
	if err != nil || bodyReq == nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
duanjinfei's avatar
duanjinfei committed
46
	req := &models.BenefitAddressReq{}
duanjinfei's avatar
duanjinfei committed
47 48 49 50 51
	err = json.Unmarshal(bodyReq, req)
	if err != nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
duanjinfei's avatar
duanjinfei committed
52
	if !conf.GetConfig().SetBenefitAddress(req.Address) {
duanjinfei's avatar
duanjinfei committed
53
		c.ResponseInfo(500, "param is not address", "")
duanjinfei's avatar
duanjinfei committed
54 55 56 57
		return
	}
	isExist := false
	for _, s := range nm.HistoryBenefitAcc {
duanjinfei's avatar
duanjinfei committed
58
		if strings.ToLower(s.Address) == strings.ToLower(req.Address) && !s.IsDel {
duanjinfei's avatar
duanjinfei committed
59 60
			isExist = true
		}
duanjinfei's avatar
duanjinfei committed
61
	}
duanjinfei's avatar
duanjinfei committed
62
	if !isExist {
duanjinfei's avatar
duanjinfei committed
63
		nm.HistoryBenefitAcc = append(nm.HistoryBenefitAcc, &models.BenefitAddressStruct{Address: req.Address, IsDel: false})
duanjinfei's avatar
duanjinfei committed
64 65 66 67 68 69 70 71 72 73
		err = utils.WriteBenefitFile(nm.HistoryBenefitAcc)
		if err != nil {
			c.ResponseInfo(500, "Write benefit file failed", "")
		}
	}
	c.ResponseInfo(200, "set benefit address successful", "")
}

func (c *NodeController) ListHistoryBenefitAddress() {
	fileBenefitAcc, _ := utils.ReadBenefitFile()
duanjinfei's avatar
duanjinfei committed
74
	res := make([]string, 0)
duanjinfei's avatar
duanjinfei committed
75 76 77 78
	for _, addressStruct := range fileBenefitAcc {
		if addressStruct.IsDel {
			continue
		}
duanjinfei's avatar
duanjinfei committed
79
		res = append(res, addressStruct.Address)
duanjinfei's avatar
duanjinfei committed
80 81
	}
	c.ResponseInfo(200, "list history benefit address successful", res)
duanjinfei's avatar
duanjinfei committed
82 83
}

84
func (c *NodeController) AddNodeManager() {
duanjinfei's avatar
duanjinfei committed
85 86 87 88 89 90 91 92 93 94 95
	bodyReq, err := io.ReadAll(c.Ctx.Request.Body)
	if err != nil || bodyReq == nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
	req := &models.NodeManagerReq{}
	err = json.Unmarshal(bodyReq, req)
	if err != nil || req.PublicKey == "" || req.EndPoint == "" {
		c.ResponseInfo(500, "param error", "")
		return
	}
duanjinfei's avatar
duanjinfei committed
96
	nodeManager := &nodemanagerV2.NodeManagerInfo{
duanjinfei's avatar
duanjinfei committed
97 98
		Publickey: req.PublicKey,
		Endpoint:  req.EndPoint,
99 100
	}
	nm.AddNodeManager(nodeManager)
duanjinfei's avatar
duanjinfei committed
101
	c.ResponseInfo(200, "add node manager successful", "")
duanjinfei's avatar
duanjinfei committed
102

103 104
}

duanjinfei's avatar
duanjinfei committed
105
func (c *NodeController) GetNodeManagers() {
106
	manager := nm.GetNodeManagers()
duanjinfei's avatar
duanjinfei committed
107 108 109 110 111 112 113 114 115 116
	res := make([]*nm.NodeManager, 0)
	for _, nodeManager := range manager {
		if !nodeManager.IsExist {
			continue
		}
		if nodeManager.IsUsed {
			res = append(res, nodeManager)
		}
	}
	c.ResponseInfo(200, "Get used node manager successful", res)
duanjinfei's avatar
duanjinfei committed
117
}
duanjinfei's avatar
duanjinfei committed
118 119 120 121 122 123 124 125 126 127 128 129 130

func (c *NodeController) UpdateRecvStatus() {
	bodyReq, err := io.ReadAll(c.Ctx.Request.Body)
	if err != nil || bodyReq == nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
	req := &models.RecvTask{}
	err = json.Unmarshal(bodyReq, req)
	if err != nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
duanjinfei's avatar
duanjinfei committed
131 132 133 134 135
	if nm.IsRecvTask && req.IsRecv {
		c.ResponseInfo(500, "The task current is recv status , don't need setting", "")
		return
	}
	if req.IsRecv && !nm.IsRunning {
duanjinfei's avatar
duanjinfei committed
136
		go nm.StartMonitor()
duanjinfei's avatar
duanjinfei committed
137
	}
duanjinfei's avatar
duanjinfei committed
138 139 140
	if !nm.IsRecvTask && req.IsRecv {
		nm.RunningState.RunningTime = time.Now().Unix()
		nm.RunningState.CompletedTaskCount = 0
duanjinfei's avatar
duanjinfei committed
141
		circularBuffer = &models.CircularBuffer{}
duanjinfei's avatar
duanjinfei committed
142
	}
duanjinfei's avatar
duanjinfei committed
143 144 145 146 147 148 149 150 151 152 153
	nm.IsRecvTask = req.IsRecv
	c.ResponseInfo(200, "update recv status successful", "")
}

func (c *NodeController) GetRecvStatus() {
	c.ResponseInfo(200, "get recv status  successful", nm.IsRecvTask)
}

func (c *NodeController) GetConfigInfo() {
	c.ResponseInfo(200, "get config successful", conf.GetConfig())
}
duanjinfei's avatar
duanjinfei committed
154 155 156 157

func (c *NodeController) GetBenefit() {
	c.ResponseInfo(200, "get benefit address successful", conf.GetConfig().BenefitAddress)
}
duanjinfei's avatar
duanjinfei committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

func (c *NodeController) SwitchMode() {
	bodyReq, err := io.ReadAll(c.Ctx.Request.Body)
	if err != nil || bodyReq == nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
	req := &models.RunMode{}
	err = json.Unmarshal(bodyReq, req)
	if err != nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
	switch req.Type {
	case models.BasicMode:
		{

		}
	case models.HealthMode:
		{

		}
	case models.SaveMode:
		{

		}
	}
duanjinfei's avatar
duanjinfei committed
185
	conf.GetConfig().RunMode = req.Type
duanjinfei's avatar
duanjinfei committed
186 187 188
	c.ResponseInfo(200, "switch mode successful", "")
}

duanjinfei's avatar
duanjinfei committed
189 190 191 192
func (c *NodeController) GetRunMode() {
	c.ResponseInfo(200, "switch mode successful", conf.GetConfig().RunMode)
}

duanjinfei's avatar
duanjinfei committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
func (c *NodeController) DelBenefitAddress() {
	bodyReq, err := io.ReadAll(c.Ctx.Request.Body)
	if err != nil || bodyReq == nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
	req := &models.BenefitAddressReq{}
	err = json.Unmarshal(bodyReq, req)
	if err != nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
	if req.Address == conf.GetConfig().BenefitAddress {
		c.ResponseInfo(500, "Don't del current benefit address", "")
		return
	}
	isExist := false
	for _, s := range nm.HistoryBenefitAcc {
duanjinfei's avatar
duanjinfei committed
211
		if strings.ToLower(s.Address) == strings.ToLower(req.Address) {
duanjinfei's avatar
duanjinfei committed
212 213 214 215 216 217 218 219
			s.IsDel = true
			isExist = true
		}
	}
	if !isExist {
		c.ResponseInfo(500, "The account not exist,don't del", "")
		return
	}
duanjinfei's avatar
duanjinfei committed
220 221 222 223 224
	err = utils.WriteBenefitFile(nm.HistoryBenefitAcc)
	if err != nil {
		c.ResponseInfo(500, "Write benefit acc file failed", "")
		return
	}
duanjinfei's avatar
duanjinfei committed
225 226
	c.ResponseInfo(200, "The account del successful ", "")
}