-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bitswap #32
Bitswap #32
Changes from 1 commit
c5e7273
91e4675
678db4f
cfdf01d
af2f04a
fcff5a5
691d1b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,7 @@ func NewBitSwap(p *peer.Peer, net swarm.Network, d ds.Datastore, r routing.IpfsR | |
routing: r.(*dht.IpfsDHT), | ||
meschan: net.GetChannel(swarm.PBWrapper_BITSWAP), | ||
haltChan: make(chan struct{}), | ||
listener: swarm.NewMesListener(), | ||
} | ||
|
||
go bs.handleMessages() | ||
|
@@ -90,7 +91,7 @@ func (bs *BitSwap) GetBlock(k u.Key, timeout time.Duration) ( | |
ledger := bs.GetLedger(pr.Key()) | ||
blk, err := bs.getBlock(k, pr, tleft) | ||
if err != nil { | ||
u.PErr("%v\n", err) | ||
u.PErr("getBlock returned: %v\n", err) | ||
return | ||
} | ||
// NOTE: this credits everyone who sends us a block, | ||
|
@@ -106,13 +107,15 @@ func (bs *BitSwap) GetBlock(k u.Key, timeout time.Duration) ( | |
|
||
select { | ||
case blkdata := <-valchan: | ||
close(valchan) | ||
return blocks.NewBlock(blkdata) | ||
case <-after: | ||
return nil, u.ErrTimeout | ||
} | ||
} | ||
|
||
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()) | ||
// | ||
mes := new(PBMessage) | ||
mes.Id = proto.Uint64(swarm.GenerateMessageID()) | ||
|
@@ -161,6 +164,7 @@ func (bs *BitSwap) handleMessages() { | |
} | ||
if pmes.GetResponse() { | ||
bs.listener.Respond(pmes.GetId(), mes) | ||
continue | ||
} | ||
|
||
switch pmes.GetType() { | ||
|
@@ -176,30 +180,39 @@ func (bs *BitSwap) handleMessages() { | |
} | ||
|
||
func (bs *BitSwap) handleGetBlock(p *peer.Peer, pmes *PBMessage) { | ||
u.DOut("handleGetBlock.\n") | ||
ledger := bs.GetLedger(p.Key()) | ||
|
||
u.DOut("finding [%s] in datastore.\n", u.Key(pmes.GetKey()).Pretty()) | ||
idata, err := bs.datastore.Get(ds.NewKey(pmes.GetKey())) | ||
if err != nil { | ||
u.PErr("handleGetBlock datastore returned: %v\n", err) | ||
if err == ds.ErrNotFound { | ||
return | ||
} | ||
u.PErr("%v\n", err) | ||
return | ||
} | ||
|
||
u.DOut("found value!\n") | ||
data, ok := idata.([]byte) | ||
if !ok { | ||
u.PErr("Failed casting data from datastore.") | ||
return | ||
} | ||
|
||
if ledger.ShouldSend() { | ||
u.DOut("Sending value back!\n") | ||
resp := &Message{ | ||
Value: data, | ||
Response: true, | ||
ID: pmes.GetId(), | ||
Type: PBMessage_GET_BLOCK, | ||
Success: true, | ||
} | ||
bs.meschan.Outgoing <- swarm.NewMessage(p, resp.ToProtobuf()) | ||
ledger.SentBytes(uint64(len(data))) | ||
} else { | ||
u.DOut("Ledger decided not to send anything...\n") | ||
} | ||
} | ||
|
||
|
@@ -210,6 +223,7 @@ func (bs *BitSwap) GetLedger(k u.Key) *Ledger { | |
} | ||
|
||
l = new(Ledger) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the ledger may be in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do we want to use as a key for the ledgers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be something like |
||
l.Strategy = StandardStrategy | ||
l.Partner = peer.ID(k) | ||
bs.partners[k] = l | ||
return l | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go is so cool.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It satisfies me so much to be able to write code in patterns like this.