-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnode.go
171 lines (155 loc) · 3.54 KB
/
node.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
169
170
171
package BigBFT
import (
"github.com/salemmohammed/BigBFT/log"
"net/http"
"reflect"
"sync"
)
// Node is the primary access point for every replica
// it includes networking, state machine and RESTful API server
type Node interface {
Socket
Database
ID() ID
Run()
Retry(r Request)
Forward(id ID, r Request)
Register(m interface{}, f interface{})
}
// node implements Node interface
type node struct {
id ID
Socket
Database
MessageChan chan interface{}
handles map[string]reflect.Value
server *http.Server
sync.RWMutex
forwards map[string]*Request
}
// NewNode creates a new Node object from configuration
func NewNode(id ID) Node {
return &node{
id: id,
Socket: NewSocket(id, config.Addrs),
Database: NewDatabase(),
MessageChan: make(chan interface{}, config.ChanBufferSize),
handles: make(map[string]reflect.Value),
forwards: make(map[string]*Request),
}
}
func (n *node) ID() ID {
return n.id
}
func (n *node) Retry(r Request) {
n.MessageChan <- r
}
// Register a handle function for each message type
func (n *node) Register(m interface{}, f interface{}) {
t := reflect.TypeOf(m)
fn := reflect.ValueOf(f)
if fn.Kind() != reflect.Func || fn.Type().NumIn() != 1 || fn.Type().In(0) != t {
panic("register handle function error")
}
n.handles[t.String()] = fn
}
// Run start and run the node
func (n *node) Run() {
log.Infof("node %v start running", n.id)
if len(n.handles) > 0 {
go n.handle()
go n.recv()
}
n.http()
}
// recv receives messages from socket and pass to message channel
func (n *node) recv() {
for {
//log.Debugf("recv receives messages from socket and pass to message channel")
m := n.Recv()
switch m := m.(type) {
case Request:
m.c = make(chan Reply, 1)
go func(r Request) {
n.Send(r.NodeID, <-r.c)
}(m)
n.MessageChan <- m
continue
case Reply:
n.RLock()
r := n.forwards[m.Command.String()]
//log.Debugf("node) %v received reply %v", n.id, m)
n.RUnlock()
r.Reply(m)
continue
}
n.MessageChan <- m
}
}
// handle receives messages from message channel and calls handle function using refection
func (n *node) handle() {
//log.Debugf("handle")
for {
msg := <-n.MessageChan
v := reflect.ValueOf(msg)
name := v.Type().String()
f, exists := n.handles[name]
if !exists {
log.Fatalf("no registered handle function for message type %v", name)
}
f.Call([]reflect.Value{v})
}
}
/*
func (n *node) Forward(id ID, m Request) {
key := m.Command.Key
url := config.HTTPAddrs[id] + "/" + strconv.Itoa(int(key))
log.Debugf("Node %v forwarding %v to %s", n.ID(), m, id)
method := http.MethodGet
var body io.Reader
if !m.Command.IsRead() {
method = http.MethodPut
body = bytes.NewBuffer(m.Command.Value)
}
req, err := http.NewRequest(method, url, body)
if err != nil {
log.Error(err)
return
}
req.Header.Set(HTTPClientID, string(n.id))
req.Header.Set(HTTPCommandID, strconv.Itoa(m.Command.CommandID))
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Error(err)
m.Reply(Reply{
Command: m.Command,
Err: err,
})
return
}
defer res.Body.Close()
if res.StatusCode == http.StatusOK {
b, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Error(err)
}
m.Reply(Reply{
Command: m.Command,
Value: Value(b),
})
} else {
m.Reply(Reply{
Command: m.Command,
Err: errors.New(res.Status),
})
}
}
*/
func (n *node) Forward(id ID, m Request) {
log.Debugf("Node %v forwarding %v to %s", n.ID(), m, id)
m.NodeID = n.id
n.Lock()
n.forwards[m.Command.String()] = &m
n.Unlock()
n.Send(id, m)
}