message.go 1.93 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 13 14 15 16 17 18 19 20 21
	nm "github.com/jbenet/go-ipfs/net/message"
	peer "github.com/jbenet/go-ipfs/peer"
	u "github.com/jbenet/go-ipfs/util"
)

type BitSwapMessage interface {
	Wantlist() []u.Key
	Blocks() []blocks.Block
	AppendWanted(k u.Key)
	AppendBlock(b blocks.Block)
	Exportable
}

type Exportable interface {
22
	ToProto() *pb.PBMessage
23
	ToNet(p peer.Peer) (nm.NetMessage, error)
24 25 26 27
}

// message wraps a proto message for convenience
type message struct {
28 29
	wantlist []u.Key
	blocks   []blocks.Block
30 31 32 33 34 35
}

func New() *message {
	return new(message)
}

36
func newMessageFromProto(pbm pb.PBMessage) BitSwapMessage {
37 38 39 40 41
	m := New()
	for _, s := range pbm.GetWantlist() {
		m.AppendWanted(u.Key(s))
	}
	for _, d := range pbm.GetBlocks() {
42
		b := blocks.NewBlock(d)
43 44
		m.AppendBlock(*b)
	}
45
	return m
46 47
}

48 49
// TODO(brian): convert these into keys
func (m *message) Wantlist() []u.Key {
50
	return m.wantlist
51 52 53 54
}

// TODO(brian): convert these into blocks
func (m *message) Blocks() []blocks.Block {
55
	return m.blocks
56 57 58
}

func (m *message) AppendWanted(k u.Key) {
59
	m.wantlist = append(m.wantlist, k)
60 61 62
}

func (m *message) AppendBlock(b blocks.Block) {
63
	m.blocks = append(m.blocks, b)
64 65 66
}

func FromNet(nmsg netmsg.NetMessage) (BitSwapMessage, error) {
67
	pb := new(pb.PBMessage)
68 69 70
	if err := proto.Unmarshal(nmsg.Data(), pb); err != nil {
		return nil, err
	}
71
	m := newMessageFromProto(*pb)
72
	return m, nil
73 74
}

75 76
func (m *message) ToProto() *pb.PBMessage {
	pb := new(pb.PBMessage)
77 78 79 80 81 82 83
	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
84 85
}

86
func (m *message) ToNet(p peer.Peer) (nm.NetMessage, error) {
87 88
	return nm.FromObject(p, m.ToProto())
}