message.go 2.75 KB
Newer Older
1 2 3
package message

import (
4
	proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
5
	blocks "github.com/jbenet/go-ipfs/blocks"
6
	pb "github.com/jbenet/go-ipfs/exchange/bitswap/message/internal/pb"
7
	netmsg "github.com/jbenet/go-ipfs/net/message"
8 9 10 11 12
	nm "github.com/jbenet/go-ipfs/net/message"
	peer "github.com/jbenet/go-ipfs/peer"
	u "github.com/jbenet/go-ipfs/util"
)

13 14 15
// TODO move message.go into the bitswap package
// TODO move bs/msg/internal/pb to bs/internal/pb and rename pb package to bitswap_pb

16
type BitSwapMessage interface {
17 18
	// Wantlist returns a slice of unique keys that represent data wanted by
	// the sender.
19
	Wantlist() []u.Key
20 21

	// Blocks returns a slice of unique blocks
22
	Blocks() []blocks.Block
23 24 25 26 27 28 29 30 31 32 33 34 35

	// AddWanted adds the key to the Wantlist.
	//
	// Insertion order determines priority. That is, earlier insertions are
	// deemed higher priority than keys inserted later.
	//
	// t = 0, msg.AddWanted(A)
	// t = 1, msg.AddWanted(B)
	//
	// implies Priority(A) > Priority(B)
	AddWanted(u.Key)

	AddBlock(blocks.Block)
36 37 38 39
	Exportable
}

type Exportable interface {
40
	ToProto() *pb.Message
41
	ToNet(p peer.Peer) (nm.NetMessage, error)
42 43
}

44
type impl struct {
45 46 47
	existsInWantlist map[u.Key]struct{}     // map to detect duplicates
	wantlist         []u.Key                // slice to preserve ordering
	blocks           map[u.Key]blocks.Block // map to detect duplicates
48 49
}

50
func New() BitSwapMessage {
51
	return &impl{
52 53 54
		blocks:           make(map[u.Key]blocks.Block),
		existsInWantlist: make(map[u.Key]struct{}),
		wantlist:         make([]u.Key, 0),
55
	}
56 57
}

58
func newMessageFromProto(pbm pb.Message) BitSwapMessage {
59 60
	m := New()
	for _, s := range pbm.GetWantlist() {
61
		m.AddWanted(u.Key(s))
62 63
	}
	for _, d := range pbm.GetBlocks() {
64
		b := blocks.NewBlock(d)
65
		m.AddBlock(*b)
66
	}
67
	return m
68 69
}

70
func (m *impl) Wantlist() []u.Key {
71
	return m.wantlist
72 73
}

74
func (m *impl) Blocks() []blocks.Block {
75 76 77 78 79
	bs := make([]blocks.Block, 0)
	for _, block := range m.blocks {
		bs = append(bs, block)
	}
	return bs
80 81
}

82
func (m *impl) AddWanted(k u.Key) {
83 84 85 86 87 88
	_, exists := m.existsInWantlist[k]
	if exists {
		return
	}
	m.existsInWantlist[k] = struct{}{}
	m.wantlist = append(m.wantlist, k)
89 90
}

91
func (m *impl) AddBlock(b blocks.Block) {
92
	m.blocks[b.Key()] = b
93 94 95
}

func FromNet(nmsg netmsg.NetMessage) (BitSwapMessage, error) {
96
	pb := new(pb.Message)
97 98 99
	if err := proto.Unmarshal(nmsg.Data(), pb); err != nil {
		return nil, err
	}
100
	m := newMessageFromProto(*pb)
101
	return m, nil
102 103
}

104
func (m *impl) ToProto() *pb.Message {
105
	pb := new(pb.Message)
106 107 108 109 110 111 112
	for _, k := range m.Wantlist() {
		pb.Wantlist = append(pb.Wantlist, string(k))
	}
	for _, b := range m.Blocks() {
		pb.Blocks = append(pb.Blocks, b.Data)
	}
	return pb
113 114
}

115
func (m *impl) ToNet(p peer.Peer) (nm.NetMessage, error) {
116 117
	return nm.FromObject(p, m.ToProto())
}