-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
133 lines (119 loc) · 2.98 KB
/
events.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
package gonode
import (
"github.com/itfantasy/gonode/core/errs"
"github.com/itfantasy/gonode/nets"
)
type EventHandler struct {
node *GoNode
}
func newEventHandler(node *GoNode) *EventHandler {
e := new(EventHandler)
e.node = node
errs.BindDigester(e)
return e
}
func (e *EventHandler) OnConn(id string) {
defer errs.AutoRecover()
e.node.onConn(id)
}
func (e *EventHandler) OnMsg(id string, msg []byte) {
defer errs.AutoRecover()
e.node.onMsg(id, msg)
}
func (e *EventHandler) OnClose(id string, reason error) {
defer errs.AutoRecover()
e.node.onClose(id, reason)
}
func (e *EventHandler) OnError(id string, err error) {
e.node.onError(id, err)
}
func (e *EventHandler) OnCheckNode(origin string) (string, bool) {
return e.node.onCheckNode(origin)
}
func (e *EventHandler) OnNewNode(id string) error {
return e.node.onNewNode(id)
}
func (e *EventHandler) OnDCError(err error) {
e.node.onDCError(err)
}
func (e *EventHandler) OnUnregister(id string) {
e.node.onUnregister(id)
}
func (e *EventHandler) OnUpdateNodeStatus() interface{} {
return nets.AllNodes()
}
func (e *EventHandler) OnDigestError(err interface{}) {
e.node.reportError(err)
}
func (g *GoNode) onConn(id string) {
g.logger.Info("conn to " + id + " succeed!")
g.behavior.OnConn(id)
}
func (g *GoNode) onMsg(id string, msg []byte) {
g.behavior.OnMsg(id, msg)
}
func (g *GoNode) onClose(id string, reason error) {
g.behavior.OnClose(id, reason)
}
func (g *GoNode) onError(id string, err error) {
g.logger.Error("the node[" + id + "] occurs errors:" + err.Error())
}
func (g *GoNode) onCheckNode(origin string) (string, bool) {
b := false
id, url, sig, err := nets.ParserOriginInfo(origin)
if err == nil {
b = g.dc.CheckNode(id, sig)
}
if !b {
if !g.info.IsPub {
g.logger.Info("not a inside node! give up the conn:" + origin)
return "", false
} else {
connId := nets.RanPeerId()
return connId, true
}
} else {
exist := nets.NodeConned(id)
if exist {
g.logger.Info("there is a same id in local record:" + url + "#" + id)
return "", false
}
return id, true
}
}
// when a new node is found
func (g *GoNode) onNewNode(id string) error {
g.lock.Lock()
defer g.lock.Unlock()
exist := nets.NodeConned(id)
if !exist {
// check the local node is interested in the new node
if g.checkTargetId(id) {
g.logger.Info("a new node has been found!", id)
// find the node url by the id
info, err := g.dc.GetNodeInfo(id)
if err == nil {
if connErr := g.Connnect(info.NodeId, info.EndPoints[0]); connErr != nil {
g.logger.Error(connErr.Error() + "[" + id + "]")
if g.info.NodeId == SUPERVISOR && g.super != nil {
g.super.OnConnFailed(id)
}
return connErr
}
} else {
g.logger.Error(err.Error() + "[" + id + "]")
}
}
}
return nil
}
func (g *GoNode) onDCError(err error) {
g.logger.Error(err.Error())
}
func (g *GoNode) onUnregister(id string) {
if g.info.NodeId == SUPERVISOR && g.super != nil {
g.super.OnUnregister(id)
}
}
func (g *GoNode) onDispose() {
}