/
getvalue.go
42 lines (36 loc) · 1.13 KB
/
getvalue.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
package dht
import (
"errors"
"github.com/curltech/go-colla-core/logger"
"github.com/curltech/go-colla-node/libp2p/dht"
"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 getValueAction struct {
action.BaseAction
}
var GetValueAction getValueAction
/**
接收消息进行处理,返回为空则没有返回消息,否则,有返回消息
*/
func (this *getValueAction) Receive(chainMessage *msg.ChainMessage) (*msg.ChainMessage, error) {
logger.Sugar.Infof("Receive %v message", this.MsgType)
v := chainMessage.Payload
key, ok := v.(string)
if !ok {
return nil, errors.New("ErrorKey")
}
buf, err := dht.PeerEndpointDHT.GetValue(key)
if err != nil {
return nil, err
}
response := handler.Response(chainMessage.MessageType, string(buf))
return response, nil
}
func init() {
GetValueAction = getValueAction{}
GetValueAction.MsgType = msgtype.GETVALUE
handler.RegistChainMessageHandler(msgtype.GETVALUE, GetValueAction.Send, GetValueAction.Receive, GetValueAction.Response)
}