Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-unixfs
Commits
043c09e1
Commit
043c09e1
authored
10 years ago
by
Juan Batiz-Benet
Committed by
Brian Tiger Chow
10 years ago
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed get/put
parent
9dd39de4
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
28 additions
and
18 deletions
+28
-18
routing/dht/dht.go
routing/dht/dht.go
+10
-4
routing/dht/dht_test.go
routing/dht/dht_test.go
+1
-1
routing/dht/handlers.go
routing/dht/handlers.go
+7
-6
routing/dht/query.go
routing/dht/query.go
+8
-7
routing/dht/routing.go
routing/dht/routing.go
+2
-0
No files found.
routing/dht/dht.go
View file @
043c09e1
package
dht
import
(
"bytes"
"crypto/rand"
"errors"
"fmt"
...
...
@@ -190,15 +191,20 @@ func (dht *IpfsDHT) sendRequest(ctx context.Context, p *peer.Peer, pmes *Message
return
rpmes
,
nil
}
func
(
dht
*
IpfsDHT
)
putValueToNetwork
(
ctx
context
.
Context
,
p
*
peer
.
Peer
,
key
string
,
value
[]
byte
)
error
{
func
(
dht
*
IpfsDHT
)
putValueToNetwork
(
ctx
context
.
Context
,
p
*
peer
.
Peer
,
key
string
,
value
[]
byte
)
error
{
pmes
:=
newMessage
(
Message_PUT_VALUE
,
string
(
key
),
0
)
pmes
.
Value
=
value
mes
,
err
:=
msg
.
FromObject
(
p
,
pmes
)
rpmes
,
err
:=
dht
.
sendRequest
(
ctx
,
p
,
pmes
)
if
err
!=
nil
{
return
err
}
return
dht
.
sender
.
SendMessage
(
ctx
,
mes
)
if
!
bytes
.
Equal
(
rpmes
.
Value
,
pmes
.
Value
)
{
return
errors
.
New
(
"value not put correctly"
)
}
return
nil
}
func
(
dht
*
IpfsDHT
)
putProvider
(
ctx
context
.
Context
,
p
*
peer
.
Peer
,
key
string
)
error
{
...
...
This diff is collapsed.
Click to expand it.
routing/dht/dht_test.go
View file @
043c09e1
...
...
@@ -117,7 +117,7 @@ func TestPing(t *testing.T) {
}
func
TestValueGetSet
(
t
*
testing
.
T
)
{
u
.
Debug
=
fals
e
u
.
Debug
=
tru
e
addrA
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/tcp/1235"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
This diff is collapsed.
Click to expand it.
routing/dht/handlers.go
View file @
043c09e1
...
...
@@ -38,7 +38,7 @@ func (dht *IpfsDHT) handlerForMsgType(t Message_MessageType) dhtHandler {
}
func
(
dht
*
IpfsDHT
)
handleGetValue
(
p
*
peer
.
Peer
,
pmes
*
Message
)
(
*
Message
,
error
)
{
u
.
DOut
(
"handleGetValue for key: %s
\n
"
,
pmes
.
GetKey
())
u
.
DOut
(
"
[%s]
handleGetValue for key: %s
\n
"
,
dht
.
self
.
ID
.
Pretty
(),
pmes
.
GetKey
())
// setup response
resp
:=
newMessage
(
pmes
.
GetType
(),
pmes
.
GetKey
(),
pmes
.
GetClusterLevel
())
...
...
@@ -50,11 +50,13 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *Message) (*Message, error
}
// let's first check if we have the value locally.
u
.
DOut
(
"[%s] handleGetValue looking into ds
\n
"
,
dht
.
self
.
ID
.
Pretty
())
dskey
:=
ds
.
NewKey
(
pmes
.
GetKey
())
iVal
,
err
:=
dht
.
datastore
.
Get
(
dskey
)
u
.
DOut
(
"[%s] handleGetValue looking into ds GOT %v
\n
"
,
dht
.
self
.
ID
.
Pretty
(),
iVal
)
// if we got an unexpected error, bail.
if
err
!=
ds
.
ErrNotFound
{
if
err
!=
nil
&&
err
!=
ds
.
ErrNotFound
{
return
nil
,
err
}
...
...
@@ -63,7 +65,7 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *Message) (*Message, error
// if we have the value, send it back
if
err
==
nil
{
u
.
DOut
(
"handleGetValue success!
\n
"
)
u
.
DOut
(
"
[%s]
handleGetValue success!
\n
"
,
dht
.
self
.
ID
.
Pretty
()
)
byts
,
ok
:=
iVal
.
([]
byte
)
if
!
ok
{
...
...
@@ -85,7 +87,6 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *Message) (*Message, error
if
closer
!=
nil
{
u
.
DOut
(
"handleGetValue returning a closer peer: '%s'
\n
"
,
closer
.
ID
.
Pretty
())
resp
.
CloserPeers
=
peersToPBPeers
([]
*
peer
.
Peer
{
closer
})
return
resp
,
nil
}
return
resp
,
nil
...
...
@@ -97,8 +98,8 @@ func (dht *IpfsDHT) handlePutValue(p *peer.Peer, pmes *Message) (*Message, error
defer
dht
.
dslock
.
Unlock
()
dskey
:=
ds
.
NewKey
(
pmes
.
GetKey
())
err
:=
dht
.
datastore
.
Put
(
dskey
,
pmes
.
GetValue
())
u
.
DOut
(
"[%s] handlePutValue %v %v"
,
dht
.
self
.
ID
.
Pretty
(),
dskey
,
pmes
.
GetValue
())
return
nil
,
err
u
.
DOut
(
"[%s] handlePutValue %v %v
\n
"
,
dht
.
self
.
ID
.
Pretty
(),
dskey
,
pmes
.
GetValue
())
return
pmes
,
err
}
func
(
dht
*
IpfsDHT
)
handlePing
(
p
*
peer
.
Peer
,
pmes
*
Message
)
(
*
Message
,
error
)
{
...
...
This diff is collapsed.
Click to expand it.
routing/dht/query.go
View file @
043c09e1
...
...
@@ -117,28 +117,29 @@ func (r *dhtQueryRunner) Run(peers []*peer.Peer) (*dhtQueryResult, error) {
// so workers are working.
// wait until they're done.
err
:=
u
.
ErrNotFound
select
{
case
<-
r
.
peersRemaining
.
Done
()
:
r
.
cancel
()
// ran all and nothing. cancel all outstanding workers.
r
.
RLock
()
defer
r
.
RUnlock
()
if
len
(
r
.
errs
)
>
0
{
return
nil
,
r
.
errs
[
0
]
err
=
r
.
errs
[
0
]
}
return
nil
,
u
.
ErrNotFound
case
<-
r
.
ctx
.
Done
()
:
r
.
RLock
()
defer
r
.
RUnlock
()
err
=
r
.
ctx
.
Err
()
}
if
r
.
result
!=
nil
&&
r
.
result
.
success
{
return
r
.
result
,
nil
}
return
nil
,
r
.
ctx
.
Err
()
if
r
.
result
!=
nil
&&
r
.
result
.
success
{
return
r
.
result
,
nil
}
return
nil
,
err
}
func
(
r
*
dhtQueryRunner
)
addPeerToQuery
(
next
*
peer
.
Peer
,
benchmark
*
peer
.
Peer
)
{
...
...
This diff is collapsed.
Click to expand it.
routing/dht/routing.go
View file @
043c09e1
...
...
@@ -89,6 +89,8 @@ func (dht *IpfsDHT) GetValue(key u.Key, timeout time.Duration) ([]byte, error) {
return
nil
,
err
}
u
.
DOut
(
"[%s] GetValue %v %v
\n
"
,
dht
.
self
.
ID
.
Pretty
(),
key
,
result
.
value
)
if
result
.
value
==
nil
{
return
nil
,
u
.
ErrNotFound
}
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment