-
Notifications
You must be signed in to change notification settings - Fork 0
/
actor.go
216 lines (198 loc) · 5.84 KB
/
actor.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Copyright (C) 2018 The DNA Authors
* This file is part of The DNA library.
*
* The DNA is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The DNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The DNA. If not, see <http://www.gnu.org/licenses/>.
*/
package server
import (
"reflect"
"github.com/dnaproject2/DNA/common/log"
"github.com/dnaproject2/DNA/p2pserver"
"github.com/dnaproject2/DNA/p2pserver/common"
"github.com/ontio/ontology-eventbus/actor"
)
type P2PActor struct {
props *actor.Props
server *p2pserver.P2PServer
}
// NewP2PActor creates an actor to handle the messages from
// txnpool and consensus
func NewP2PActor(p2pServer *p2pserver.P2PServer) *P2PActor {
return &P2PActor{
server: p2pServer,
}
}
//start a actor called net_server
func (this *P2PActor) Start() (*actor.PID, error) {
this.props = actor.FromProducer(func() actor.Actor { return this })
p2pPid, err := actor.SpawnNamed(this.props, "net_server")
return p2pPid, err
}
//message handler
func (this *P2PActor) Receive(ctx actor.Context) {
switch msg := ctx.Message().(type) {
case *actor.Restarting:
log.Warn("[p2p]actor restarting")
case *actor.Stopping:
log.Warn("[p2p]actor stopping")
case *actor.Stopped:
log.Warn("[p2p]actor stopped")
case *actor.Started:
log.Debug("[p2p]actor started")
case *actor.Restart:
log.Warn("[p2p]actor restart")
case *StopServerReq:
this.handleStopServerReq(ctx, msg)
case *GetPortReq:
this.handleGetPortReq(ctx, msg)
case *GetVersionReq:
this.handleGetVersionReq(ctx, msg)
case *GetConnectionCntReq:
this.handleGetConnectionCntReq(ctx, msg)
case *GetIdReq:
this.handleGetIDReq(ctx, msg)
case *GetConnectionStateReq:
this.handleGetConnectionStateReq(ctx, msg)
case *GetTimeReq:
this.handleGetTimeReq(ctx, msg)
case *GetNeighborAddrsReq:
this.handleGetNeighborAddrsReq(ctx, msg)
case *GetRelayStateReq:
this.handleGetRelayStateReq(ctx, msg)
case *GetNodeTypeReq:
this.handleGetNodeTypeReq(ctx, msg)
case *TransmitConsensusMsgReq:
this.handleTransmitConsensusMsgReq(ctx, msg)
case *common.AppendPeerID:
this.server.OnAddNode(msg.ID)
case *common.RemovePeerID:
this.server.OnDelNode(msg.ID)
case *common.AppendHeaders:
this.server.OnHeaderReceive(msg.FromID, msg.Headers)
case *common.AppendBlock:
this.server.OnBlockReceive(msg.FromID, msg.BlockSize, msg.Block, msg.MerkleRoot)
default:
err := this.server.Xmit(ctx.Message())
if nil != err {
log.Warn("[p2p]error xmit message ", err.Error(), reflect.TypeOf(ctx.Message()))
}
}
}
//stop handler
func (this *P2PActor) handleStopServerReq(ctx actor.Context, req *StopServerReq) {
this.server.Stop()
if ctx.Sender() != nil {
resp := &StopServerRsp{}
ctx.Sender().Request(resp, ctx.Self())
}
}
//get port handler
func (this *P2PActor) handleGetPortReq(ctx actor.Context, req *GetPortReq) {
syncPort := this.server.GetPort()
if ctx.Sender() != nil {
resp := &GetPortRsp{
SyncPort: syncPort,
}
ctx.Sender().Request(resp, ctx.Self())
}
}
//version handler
func (this *P2PActor) handleGetVersionReq(ctx actor.Context, req *GetVersionReq) {
version := this.server.GetVersion()
if ctx.Sender() != nil {
resp := &GetVersionRsp{
Version: version,
}
ctx.Sender().Request(resp, ctx.Self())
}
}
//connection count handler
func (this *P2PActor) handleGetConnectionCntReq(ctx actor.Context, req *GetConnectionCntReq) {
cnt := this.server.GetConnectionCnt()
if ctx.Sender() != nil {
resp := &GetConnectionCntRsp{
Cnt: cnt,
}
ctx.Sender().Request(resp, ctx.Self())
}
}
//get id handler
func (this *P2PActor) handleGetIDReq(ctx actor.Context, req *GetIdReq) {
id := this.server.GetID()
if ctx.Sender() != nil {
resp := &GetIdRsp{
Id: id,
}
ctx.Sender().Request(resp, ctx.Self())
}
}
//connection state handler
func (this *P2PActor) handleGetConnectionStateReq(ctx actor.Context, req *GetConnectionStateReq) {
state := this.server.GetConnectionState()
if ctx.Sender() != nil {
resp := &GetConnectionStateRsp{
State: state,
}
ctx.Sender().Request(resp, ctx.Self())
}
}
//timestamp handler
func (this *P2PActor) handleGetTimeReq(ctx actor.Context, req *GetTimeReq) {
time := this.server.GetTime()
if ctx.Sender() != nil {
resp := &GetTimeRsp{
Time: time,
}
ctx.Sender().Request(resp, ctx.Self())
}
}
//nbr peer`s address handler
func (this *P2PActor) handleGetNeighborAddrsReq(ctx actor.Context, req *GetNeighborAddrsReq) {
addrs := this.server.GetNeighborAddrs()
if ctx.Sender() != nil {
resp := &GetNeighborAddrsRsp{
Addrs: addrs,
}
ctx.Sender().Request(resp, ctx.Self())
}
}
//peer`s relay state handler
func (this *P2PActor) handleGetRelayStateReq(ctx actor.Context, req *GetRelayStateReq) {
ret := this.server.GetNetWork().GetRelay()
if ctx.Sender() != nil {
resp := &GetRelayStateRsp{
Relay: ret,
}
ctx.Sender().Request(resp, ctx.Self())
}
}
//peer`s service type handler
func (this *P2PActor) handleGetNodeTypeReq(ctx actor.Context, req *GetNodeTypeReq) {
ret := this.server.GetNetWork().GetServices()
if ctx.Sender() != nil {
resp := &GetNodeTypeRsp{
NodeType: ret,
}
ctx.Sender().Request(resp, ctx.Self())
}
}
func (this *P2PActor) handleTransmitConsensusMsgReq(ctx actor.Context, req *TransmitConsensusMsgReq) {
peer := this.server.GetNetWork().GetPeer(req.Target)
if peer != nil {
this.server.Send(peer, req.Msg, true)
} else {
log.Warnf("[p2p]can`t transmit consensus msg:no valid neighbor peer: %d\n", req.Target)
}
}