/
server.go
168 lines (136 loc) · 3.13 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package state
import (
"fmt"
"os"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/factomd/common/primitives/random"
)
type Server struct {
ChainID interfaces.IHash
Name string
Online bool
Replace interfaces.IHash
}
var _ interfaces.IServer = (*Server)(nil)
var _ interfaces.BinaryMarshallable = (*Server)(nil)
func (s *Server) Init() {
if s.ChainID == nil {
s.ChainID = primitives.NewZeroHash()
}
if s.Replace == nil {
s.Replace = primitives.NewZeroHash()
}
}
func RandomServer() interfaces.IServer {
s := new(Server)
s.Init()
s.ChainID = primitives.RandomHash()
s.Name = random.RandomString()
s.Online = (random.RandInt()%2 == 0)
s.Replace = primitives.RandomHash()
return s
}
func (s *Server) IsSameAs(b interfaces.IServer) bool {
serv := b.(*Server)
if s.ChainID.IsSameAs(serv.ChainID) == false {
return false
}
if s.Name != serv.Name {
return false
}
if s.Online != serv.Online {
return false
}
if s.Replace.IsSameAs(serv.Replace) == false {
return false
}
return true
}
func (s *Server) MarshalBinary() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "Server.MarshalBinary err:%v", *pe)
}
}(&err)
s.Init()
buf := new(primitives.Buffer)
err = buf.PushBinaryMarshallable(s.ChainID)
if err != nil {
return nil, err
}
err = buf.PushString(s.Name)
if err != nil {
return nil, err
}
err = buf.PushBool(s.Online)
if err != nil {
return nil, err
}
err = buf.PushBinaryMarshallable(s.Replace)
if err != nil {
return nil, err
}
return buf.DeepCopyBytes(), nil
}
func (s *Server) UnmarshalBinaryData(p []byte) (newData []byte, err error) {
s.Init()
buf := primitives.NewBuffer(p)
newData = p
err = buf.PopBinaryMarshallable(s.ChainID)
if err != nil {
return
}
s.Name, err = buf.PopString()
if err != nil {
return
}
s.Online, err = buf.PopBool()
if err != nil {
return
}
err = buf.PopBinaryMarshallable(s.Replace)
if err != nil {
return
}
newData = buf.DeepCopyBytes()
return
}
func (s *Server) UnmarshalBinary(p []byte) error {
_, err := s.UnmarshalBinaryData(p)
return err
}
func (s *Server) GetName() string {
return s.Name
}
func (s *Server) GetChainID() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "Server.GetChainID") }()
return s.ChainID
}
func (s *Server) String() string {
return fmt.Sprintf("Server[:4]: %x", s.GetChainID().Bytes()[:10])
}
func (s *Server) IsOnline() bool {
return s.Online
}
func (s *Server) SetOnline(o bool) {
s.Online = o
}
func (s *Server) LeaderToReplace() (rval interfaces.IHash) {
if s.Replace != nil {
defer func() { rval = primitives.CheckNil(rval, "Server.LeaderToReplace") }()
}
return s.Replace
}
func (s *Server) SetReplace(h interfaces.IHash) {
s.Replace = h
}
func (e *Server) JSONByte() ([]byte, error) {
return primitives.EncodeJSON(e)
}
func (e *Server) JSONString() (string, error) {
return primitives.EncodeJSONString(e)
}