1
- package wire
1
+ package dht_pb
2
2
3
3
import (
4
4
"bytes"
@@ -16,7 +16,6 @@ import (
16
16
"github.com/multiformats/go-multihash"
17
17
18
18
"github.com/libp2p/go-libp2p-kad-dht/internal"
19
- pb "github.com/libp2p/go-libp2p-kad-dht/pb"
20
19
)
21
20
22
21
var logger = logging .Logger ("dht" )
@@ -60,14 +59,14 @@ func NewProtocolMessenger(msgSender MessageSender, opts ...ProtocolMessengerOpti
60
59
// MessageSender handles sending wire protocol messages to a given peer
61
60
type MessageSender interface {
62
61
// SendRequest sends a peer a message and waits for its response
63
- SendRequest (ctx context.Context , p peer.ID , pmes * pb. Message ) (* pb. Message , error )
62
+ SendRequest (ctx context.Context , p peer.ID , pmes * Message ) (* Message , error )
64
63
// SendMessage sends a peer a message without waiting on a response
65
- SendMessage (ctx context.Context , p peer.ID , pmes * pb. Message ) error
64
+ SendMessage (ctx context.Context , p peer.ID , pmes * Message ) error
66
65
}
67
66
68
67
// PutValue asks a peer to store the given key/value pair.
69
68
func (pm * ProtocolMessenger ) PutValue (ctx context.Context , p peer.ID , rec * recpb.Record ) error {
70
- pmes := pb . NewMessage (pb . Message_PUT_VALUE , rec .Key , 0 )
69
+ pmes := NewMessage (Message_PUT_VALUE , rec .Key , 0 )
71
70
pmes .Record = rec
72
71
rpmes , err := pm .m .SendRequest (ctx , p , pmes )
73
72
if err != nil {
@@ -86,14 +85,14 @@ func (pm *ProtocolMessenger) PutValue(ctx context.Context, p peer.ID, rec *recpb
86
85
// GetValue asks a peer for the value corresponding to the given key. Also returns the K closest peers to the key
87
86
// as described in GetClosestPeers.
88
87
func (pm * ProtocolMessenger ) GetValue (ctx context.Context , p peer.ID , key string ) (* recpb.Record , []* peer.AddrInfo , error ) {
89
- pmes := pb . NewMessage (pb . Message_GET_VALUE , []byte (key ), 0 )
88
+ pmes := NewMessage (Message_GET_VALUE , []byte (key ), 0 )
90
89
respMsg , err := pm .m .SendRequest (ctx , p , pmes )
91
90
if err != nil {
92
91
return nil , nil , err
93
92
}
94
93
95
94
// Perhaps we were given closer peers
96
- peers := pb . PBPeersToPeerInfos (respMsg .GetCloserPeers ())
95
+ peers := PBPeersToPeerInfos (respMsg .GetCloserPeers ())
97
96
98
97
if rec := respMsg .GetRecord (); rec != nil {
99
98
// Success! We were given the value
@@ -120,12 +119,12 @@ func (pm *ProtocolMessenger) GetValue(ctx context.Context, p peer.ID, key string
120
119
// Note: If the peer happens to know another peer whose peerID exactly matches the given id it will return that peer
121
120
// even if that peer is not a DHT server node.
122
121
func (pm * ProtocolMessenger ) GetClosestPeers (ctx context.Context , p peer.ID , id peer.ID ) ([]* peer.AddrInfo , error ) {
123
- pmes := pb . NewMessage (pb . Message_FIND_NODE , []byte (id ), 0 )
122
+ pmes := NewMessage (Message_FIND_NODE , []byte (id ), 0 )
124
123
respMsg , err := pm .m .SendRequest (ctx , p , pmes )
125
124
if err != nil {
126
125
return nil , err
127
126
}
128
- peers := pb . PBPeersToPeerInfos (respMsg .GetCloserPeers ())
127
+ peers := PBPeersToPeerInfos (respMsg .GetCloserPeers ())
129
128
return peers , nil
130
129
}
131
130
@@ -142,33 +141,33 @@ func (pm *ProtocolMessenger) PutProvider(ctx context.Context, p peer.ID, key mul
142
141
return fmt .Errorf ("no known addresses for self, cannot put provider" )
143
142
}
144
143
145
- pmes := pb . NewMessage (pb . Message_ADD_PROVIDER , key , 0 )
146
- pmes .ProviderPeers = pb . RawPeerInfosToPBPeers ([]peer.AddrInfo {pi })
144
+ pmes := NewMessage (Message_ADD_PROVIDER , key , 0 )
145
+ pmes .ProviderPeers = RawPeerInfosToPBPeers ([]peer.AddrInfo {pi })
147
146
148
147
return pm .m .SendMessage (ctx , p , pmes )
149
148
}
150
149
151
150
// GetProviders asks a peer for the providers it knows of for a given key. Also returns the K closest peers to the key
152
151
// as described in GetClosestPeers.
153
152
func (pm * ProtocolMessenger ) GetProviders (ctx context.Context , p peer.ID , key multihash.Multihash ) ([]* peer.AddrInfo , []* peer.AddrInfo , error ) {
154
- pmes := pb . NewMessage (pb . Message_GET_PROVIDERS , key , 0 )
153
+ pmes := NewMessage (Message_GET_PROVIDERS , key , 0 )
155
154
respMsg , err := pm .m .SendRequest (ctx , p , pmes )
156
155
if err != nil {
157
156
return nil , nil , err
158
157
}
159
- provs := pb . PBPeersToPeerInfos (respMsg .GetProviderPeers ())
160
- closerPeers := pb . PBPeersToPeerInfos (respMsg .GetCloserPeers ())
158
+ provs := PBPeersToPeerInfos (respMsg .GetProviderPeers ())
159
+ closerPeers := PBPeersToPeerInfos (respMsg .GetCloserPeers ())
161
160
return provs , closerPeers , nil
162
161
}
163
162
164
163
// Ping sends a ping message to the passed peer and waits for a response.
165
164
func (pm * ProtocolMessenger ) Ping (ctx context.Context , p peer.ID ) error {
166
- req := pb . NewMessage (pb . Message_PING , nil , 0 )
165
+ req := NewMessage (Message_PING , nil , 0 )
167
166
resp , err := pm .m .SendRequest (ctx , p , req )
168
167
if err != nil {
169
168
return fmt .Errorf ("sending request: %w" , err )
170
169
}
171
- if resp .Type != pb . Message_PING {
170
+ if resp .Type != Message_PING {
172
171
return fmt .Errorf ("got unexpected response type: %v" , resp .Type )
173
172
}
174
173
return nil
0 commit comments