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-dms3
Commits
421fe5bf
Commit
421fe5bf
authored
Sep 11, 2014
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(bitswap:message) add wrapper for proto
parent
c35a8d0d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
122 additions
and
12 deletions
+122
-12
bitswap/bitswap.go
bitswap/bitswap.go
+9
-12
bitswap/message.go
bitswap/message.go
+38
-0
bitswap/message_test.go
bitswap/message_test.go
+75
-0
No files found.
bitswap/bitswap.go
View file @
421fe5bf
...
...
@@ -115,13 +115,12 @@ func (bs *BitSwap) GetBlock(k u.Key, timeout time.Duration) (
func
(
bs
*
BitSwap
)
getBlock
(
k
u
.
Key
,
p
*
peer
.
Peer
,
timeout
time
.
Duration
)
([]
byte
,
error
)
{
u
.
DOut
(
"[%s] getBlock '%s' from [%s]
\n
"
,
bs
.
peer
.
ID
.
Pretty
(),
k
.
Pretty
(),
p
.
ID
.
Pretty
())
p
mes
:=
new
(
PB
Message
)
p
mes
.
Wantlist
=
[]
string
{
string
(
k
)
}
mes
sage
:=
newMessage
(
)
mes
sage
.
AppendWanted
(
k
)
after
:=
time
.
After
(
timeout
)
resp
:=
bs
.
listener
.
Listen
(
string
(
k
),
1
,
timeout
)
smes
:=
swarm
.
NewMessage
(
p
,
pmes
)
bs
.
meschan
.
Outgoing
<-
smes
bs
.
meschan
.
Outgoing
<-
message
.
ToSwarm
(
p
)
select
{
case
resp_mes
:=
<-
resp
:
...
...
@@ -149,11 +148,9 @@ func (bs *BitSwap) HaveBlock(blk *blocks.Block) error {
}
func
(
bs
*
BitSwap
)
SendBlock
(
p
*
peer
.
Peer
,
b
*
blocks
.
Block
)
{
pmes
:=
new
(
PBMessage
)
pmes
.
Blocks
=
[][]
byte
{
b
.
Data
}
swarm_mes
:=
swarm
.
NewMessage
(
p
,
pmes
)
bs
.
meschan
.
Outgoing
<-
swarm_mes
message
:=
newMessage
()
message
.
AppendBlock
(
b
)
bs
.
meschan
.
Outgoing
<-
message
.
ToSwarm
(
p
)
}
func
(
bs
*
BitSwap
)
handleMessages
()
{
...
...
@@ -257,14 +254,14 @@ func (bs *BitSwap) GetLedger(p *peer.Peer) *Ledger {
}
func
(
bs
*
BitSwap
)
SendWantList
(
wl
KeySet
)
error
{
p
mes
:=
new
(
PB
Message
)
mes
sage
:=
newMessage
(
)
for
k
,
_
:=
range
wl
{
p
mes
.
Wantlist
=
append
(
pmes
.
Wantlist
,
string
(
k
)
)
mes
sage
.
AppendWanted
(
k
)
}
// Lets just ping everybody all at once
for
_
,
ledger
:=
range
bs
.
partners
{
bs
.
meschan
.
Outgoing
<-
swarm
.
NewMessage
(
ledger
.
Partner
,
pmes
)
bs
.
meschan
.
Outgoing
<-
message
.
ToSwarm
(
ledger
.
Partner
)
}
return
nil
...
...
bitswap/message.go
0 → 100644
View file @
421fe5bf
package
bitswap
import
(
blocks
"github.com/jbenet/go-ipfs/blocks"
peer
"github.com/jbenet/go-ipfs/peer"
swarm
"github.com/jbenet/go-ipfs/swarm"
u
"github.com/jbenet/go-ipfs/util"
)
// message wraps a proto message for convenience
type
message
struct
{
pb
PBMessage
}
func
newMessageFromProto
(
pb
PBMessage
)
*
message
{
return
&
message
{
pb
:
pb
}
}
func
newMessage
()
*
message
{
return
new
(
message
)
}
func
(
m
*
message
)
AppendWanted
(
k
u
.
Key
)
{
m
.
pb
.
Wantlist
=
append
(
m
.
pb
.
Wantlist
,
string
(
k
))
}
func
(
m
*
message
)
AppendBlock
(
b
*
blocks
.
Block
)
{
m
.
pb
.
Blocks
=
append
(
m
.
pb
.
Blocks
,
b
.
Data
)
}
func
(
m
*
message
)
ToProto
()
*
PBMessage
{
cp
:=
m
.
pb
return
&
cp
}
func
(
m
*
message
)
ToSwarm
(
p
*
peer
.
Peer
)
*
swarm
.
Message
{
return
swarm
.
NewMessage
(
p
,
m
.
ToProto
())
}
bitswap/message_test.go
0 → 100644
View file @
421fe5bf
package
bitswap
import
(
"bytes"
"testing"
blocks
"github.com/jbenet/go-ipfs/blocks"
u
"github.com/jbenet/go-ipfs/util"
)
func
TestAppendWanted
(
t
*
testing
.
T
)
{
const
str
=
"foo"
m
:=
newMessage
()
m
.
AppendWanted
(
u
.
Key
(
str
))
if
!
contains
(
m
.
ToProto
()
.
GetWantlist
(),
str
)
{
t
.
Fail
()
}
}
func
TestNewMessageFromProto
(
t
*
testing
.
T
)
{
const
str
=
"a_key"
protoMessage
:=
new
(
PBMessage
)
protoMessage
.
Wantlist
=
[]
string
{
string
(
str
)}
if
!
contains
(
protoMessage
.
Wantlist
,
str
)
{
t
.
Fail
()
}
m
:=
newMessageFromProto
(
*
protoMessage
)
if
!
contains
(
m
.
ToProto
()
.
GetWantlist
(),
str
)
{
t
.
Fail
()
}
}
func
TestAppendBlock
(
t
*
testing
.
T
)
{
strs
:=
make
([]
string
,
2
)
strs
=
append
(
strs
,
"Celeritas"
)
strs
=
append
(
strs
,
"Incendia"
)
m
:=
newMessage
()
for
_
,
str
:=
range
strs
{
block
,
err
:=
blocks
.
NewBlock
([]
byte
(
str
))
if
err
!=
nil
{
t
.
Fail
()
}
m
.
AppendBlock
(
block
)
}
// assert strings are in proto message
for
_
,
blockbytes
:=
range
m
.
ToProto
()
.
GetBlocks
()
{
s
:=
bytes
.
NewBuffer
(
blockbytes
)
.
String
()
if
!
contains
(
strs
,
s
)
{
t
.
Fail
()
}
}
}
func
TestCopyProtoByValue
(
t
*
testing
.
T
)
{
const
str
=
"foo"
m
:=
newMessage
()
protoBeforeAppend
:=
m
.
ToProto
()
m
.
AppendWanted
(
u
.
Key
(
str
))
if
contains
(
protoBeforeAppend
.
GetWantlist
(),
str
)
{
t
.
Fail
()
}
}
func
contains
(
s
[]
string
,
x
string
)
bool
{
for
_
,
a
:=
range
s
{
if
a
==
x
{
return
true
}
}
return
false
}
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