refactor(bitswap) meslistener -> notifications
Brian Tiger Chow committed
Sep 11, 2014 at 16:34 UTC
b28343cffeacf5f5e0d17e7f23dba4b67879f39f
1 file changed
+24
-28
bitswap/bitswap.go
+24
-28
@@ -3,6 +3,7 @@ package bitswap
3
import (
4
"time"
5
6
+ context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7
proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
8
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
9
@@ -38,7 +39,7 @@ type BitSwap struct {
39
// routing interface for communication
40
routing *dht.IpfsDHT
41
41
- listener *swarm.MessageListener
42
+ notifications *notifications
43
44
// partners is a map of currently active bitswap relationships.
45
// The Ledger has the peer.ID, and the peer connection works through net.
@@ -60,15 +61,15 @@ type BitSwap struct {
61
// NewBitSwap creates a new BitSwap instance. It does not check its parameters.
62
func NewBitSwap(p *peer.Peer, net swarm.Network, d ds.Datastore, r routing.IpfsRouting) *BitSwap {
63
bs := &BitSwap{
63
- peer: p,
64
- net: net,
65
- datastore: d,
66
- partners: LedgerMap{},
67
- wantList: KeySet{},
68
- routing: r.(*dht.IpfsDHT),
69
- meschan: net.GetChannel(swarm.PBWrapper_BITSWAP),
70
- haltChan: make(chan struct{}),
71
- listener: swarm.NewMessageListener(),
64
+ peer: p,
65
+ net: net,
66
+ datastore: d,
67
+ partners: LedgerMap{},
68
+ wantList: KeySet{},
69
+ routing: r.(*dht.IpfsDHT),
70
+ meschan: net.GetChannel(swarm.PBWrapper_BITSWAP),
71
+ haltChan: make(chan struct{}),
72
+ notifications: newNotifications(),
73
}
74
75
go bs.handleMessages()
@@ -83,7 +84,7 @@ func (bs *BitSwap) GetBlock(k u.Key, timeout time.Duration) (
84
tleft := timeout - time.Now().Sub(begin)
85
provs_ch := bs.routing.FindProvidersAsync(k, 20, timeout)
86
86
- valchan := make(chan []byte)
87
+ blockChannel := make(chan *blocks.Block)
88
after := time.After(tleft)
89
90
// TODO: when the data is received, shut down this for loop ASAP
@@ -96,7 +97,7 @@ func (bs *BitSwap) GetBlock(k u.Key, timeout time.Duration) (
97
return
98
}
99
select {
99
- case valchan <- blk:
100
+ case blockChannel <- blk:
101
default:
102
}
103
}(p)
@@ -104,31 +105,30 @@ func (bs *BitSwap) GetBlock(k u.Key, timeout time.Duration) (
105
}()
106
107
select {
107
- case blkdata := <-valchan:
108
- close(valchan)
109
- return blocks.NewBlock(blkdata)
108
+ case block := <-blockChannel:
109
+ close(blockChannel)
110
+ return block, nil
111
case <-after:
112
return nil, u.ErrTimeout
113
}
114
}
115
115
-func (bs *BitSwap) getBlock(k u.Key, p *peer.Peer, timeout time.Duration) ([]byte, error) {
116
+func (bs *BitSwap) getBlock(k u.Key, p *peer.Peer, timeout time.Duration) (*blocks.Block, error) {
117
u.DOut("[%s] getBlock '%s' from [%s]\n", bs.peer.ID.Pretty(), k.Pretty(), p.ID.Pretty())
118
119
+ ctx, _ := context.WithTimeout(context.Background(), timeout)
120
+ blockChannel := bs.notifications.Subscribe(ctx, k)
121
+
122
message := newMessage()
123
message.AppendWanted(k)
120
-
121
- after := time.After(timeout)
122
- resp := bs.listener.Listen(string(k), 1, timeout)
124
bs.meschan.Outgoing <- message.ToSwarm(p)
125
125
- select {
126
- case resp_mes := <-resp:
127
- return resp_mes.Data, nil
128
- case <-after:
126
+ block, ok := <-blockChannel
127
+ if !ok {
128
u.PErr("getBlock for '%s' timed out.\n", k.Pretty())
129
return nil, u.ErrTimeout
130
}
131
+ return block, nil
132
}
133
134
// HaveBlock announces the existance of a block to BitSwap, potentially sending
@@ -229,11 +229,7 @@ func (bs *BitSwap) blockReceive(p *peer.Peer, blk *blocks.Block) {
229
return
230
}
231
232
- mes := &swarm.Message{
233
- Peer: p,
234
- Data: blk.Data,
235
- }
236
- bs.listener.Respond(string(blk.Key()), mes)
232
+ bs.notifications.Publish(blk)
233
234
ledger := bs.getLedger(p)
235
ledger.ReceivedBytes(len(blk.Data))