代码拉取完成,页面将自动刷新
package zk
import (
"encoding/json"
"fmt"
"gitee.com/wujianhai/protoactor-go/cluster"
)
const (
metaKeyID = "id"
metaKeySeq = "seq"
)
type Node struct {
ID string `json:"id"`
Name string `json:"name"`
Host string `json:"host"`
Address string `json:"address"`
Port int `json:"port"`
Kinds []string `json:"kinds"`
Meta map[string]string `json:"-"`
Alive bool `json:"alive"`
}
func NewNode(name, host string, port int, kinds []string) *Node {
return &Node{
ID: name,
Name: name,
Address: host,
Host: host,
Port: port,
Kinds: kinds,
Meta: map[string]string{},
Alive: true,
}
}
func (n *Node) GetAddress() (host string, port int) {
host = n.Host
port = n.Port
if host == "" {
host = n.Address
}
return
}
func (n *Node) GetAddressString() string {
h, p := n.GetAddress()
return fmt.Sprintf("%v:%v", h, p)
}
func (n *Node) Equal(other *Node) bool {
if n == nil || other == nil {
return false
}
if n == other {
return true
}
return n.ID == other.ID
}
func (n *Node) GetMeta(name string) (string, bool) {
if n.Meta == nil {
return "", false
}
val, ok := n.Meta[name]
return val, ok
}
func (n *Node) GetSeq() int {
if seqStr, ok := n.GetMeta(metaKeySeq); ok {
return strToInt(seqStr)
}
return 0
}
func (n *Node) MemberStatus() *cluster.Member {
host, port := n.GetAddress()
kinds := n.Kinds
if kinds == nil {
kinds = []string{}
}
return &cluster.Member{
Id: n.ID,
Host: host,
Port: int32(port),
Kinds: kinds,
}
}
func (n *Node) SetMeta(name string, val string) {
if n.Meta == nil {
n.Meta = map[string]string{}
}
n.Meta[name] = val
}
func (n *Node) Serialize() ([]byte, error) {
data, err := json.Marshal(n)
if err != nil {
return nil, err
}
return data, nil
}
func (n *Node) Deserialize(data []byte) error {
return json.Unmarshal(data, n)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。