/
findpeer.go
50 lines (44 loc) · 1.47 KB
/
findpeer.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
package dht
import (
"errors"
"github.com/curltech/go-colla-core/logger"
"github.com/curltech/go-colla-node/p2p/dht/service"
"github.com/curltech/go-colla-node/p2p/chain/action"
"github.com/curltech/go-colla-node/p2p/chain/handler"
"github.com/curltech/go-colla-node/p2p/msg"
"github.com/curltech/go-colla-node/p2p/msgtype"
)
type findPeerAction struct {
action.BaseAction
}
var FindPeerAction findPeerAction
/**
接收消息进行处理,返回为空则没有返回消息,否则,有返回消息
*/
func (this *findPeerAction) Receive(chainMessage *msg.ChainMessage) (*msg.ChainMessage, error) {
logger.Sugar.Infof("Receive %v message", this.MsgType)
var response *msg.ChainMessage = nil
conditionBean, ok := chainMessage.Payload.(map[string]interface{})
if !ok {
response = handler.Error(chainMessage.MessageType, errors.New("ErrorCondition"))
return response, nil
}
var peerId string = ""
if conditionBean["peerId"] != nil {
peerId = conditionBean["peerId"].(string)
}
if len(peerId) > 0 {
addrInfo, err := service.GetPeerEndpointService().FindPeer(peerId)
if err != nil {
response = handler.Error(chainMessage.MessageType, err)
return response, nil
}
response = handler.Response(chainMessage.MessageType, addrInfo)
}
return response, nil
}
func init() {
FindPeerAction = findPeerAction{}
FindPeerAction.MsgType = msgtype.FINDPEER
handler.RegistChainMessageHandler(msgtype.FINDPEER, FindPeerAction.Send, FindPeerAction.Receive, FindPeerAction.Response)
}