NodeController.go 5.71 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
		err = utils.WriteBenefitFile(nm.HistoryBenefitAcc)
		if err != nil {
			c.ResponseInfo(500, "Write benefit file failed", "")
duanjinfei's avatar
duanjinfei committed
67
			return
duanjinfei's avatar
duanjinfei committed
68 69
		}
	}
duanjinfei's avatar
duanjinfei committed
70
	nm.IsUpdateBenefitAddr = true
duanjinfei's avatar
duanjinfei committed
71 72 73 74 75
	c.ResponseInfo(200, "set benefit address successful", "")
}

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

86
func (c *NodeController) AddNodeManager() {
duanjinfei's avatar
duanjinfei committed
87 88 89 90 91 92 93 94 95 96 97
	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
98
	nodeManager := &nodemanagerV2.NodeManagerInfo{
duanjinfei's avatar
duanjinfei committed
99 100
		Publickey: req.PublicKey,
		Endpoint:  req.EndPoint,
101 102
	}
	nm.AddNodeManager(nodeManager)
duanjinfei's avatar
duanjinfei committed
103
	c.ResponseInfo(200, "add node manager successful", "")
duanjinfei's avatar
duanjinfei committed
104

105 106
}

duanjinfei's avatar
duanjinfei committed
107
func (c *NodeController) GetNodeManagers() {
108
	manager := nm.GetNodeManagers()
duanjinfei's avatar
duanjinfei committed
109 110 111 112 113 114 115 116 117 118
	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
119
}
duanjinfei's avatar
duanjinfei committed
120 121 122 123 124 125 126 127 128 129 130 131 132

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
133 134 135 136
	if nm.IsRecvTask && req.IsRecv {
		c.ResponseInfo(500, "The task current is recv status , don't need setting", "")
		return
	}
duanjinfei's avatar
duanjinfei committed
137
	if req.IsRecv && len(conf.GetConfig().BenefitAddress) >= 1 {
duanjinfei's avatar
duanjinfei committed
138
		go nm.StartMonitor()
duanjinfei's avatar
duanjinfei committed
139
	}
duanjinfei's avatar
duanjinfei committed
140 141 142
	if !nm.IsRecvTask && req.IsRecv {
		nm.RunningState.RunningTime = time.Now().Unix()
		nm.RunningState.CompletedTaskCount = 0
duanjinfei's avatar
duanjinfei committed
143
		circularBuffer = &models.CircularBuffer{}
duanjinfei's avatar
duanjinfei committed
144
	}
duanjinfei's avatar
duanjinfei committed
145 146 147 148 149 150 151 152 153 154 155
	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
156 157 158 159

func (c *NodeController) GetBenefit() {
	c.ResponseInfo(200, "get benefit address successful", conf.GetConfig().BenefitAddress)
}
duanjinfei's avatar
duanjinfei committed
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 185 186

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
187
	conf.GetConfig().RunMode = req.Type
duanjinfei's avatar
duanjinfei committed
188 189 190
	c.ResponseInfo(200, "switch mode successful", "")
}

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

duanjinfei's avatar
duanjinfei committed
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
	}
duanjinfei's avatar
duanjinfei committed
211 212 213 214
	if len(nm.HistoryBenefitAcc) == 1 {
		c.ResponseInfo(500, "Don't del current benefit address", "")
		return
	}
duanjinfei's avatar
duanjinfei committed
215 216
	isExist := false
	for _, s := range nm.HistoryBenefitAcc {
duanjinfei's avatar
duanjinfei committed
217
		if strings.ToLower(s.Address) == strings.ToLower(req.Address) {
duanjinfei's avatar
duanjinfei committed
218 219 220 221 222 223 224 225
			s.IsDel = true
			isExist = true
		}
	}
	if !isExist {
		c.ResponseInfo(500, "The account not exist,don't del", "")
		return
	}
duanjinfei's avatar
duanjinfei committed
226 227 228 229 230
	err = utils.WriteBenefitFile(nm.HistoryBenefitAcc)
	if err != nil {
		c.ResponseInfo(500, "Write benefit acc file failed", "")
		return
	}
duanjinfei's avatar
duanjinfei committed
231 232
	c.ResponseInfo(200, "The account del successful ", "")
}