peermessagemanager.go 1.23 KB
Newer Older
1 2 3 4 5
package peermanager

import (
	"context"

Hannah Howard's avatar
Hannah Howard committed
6
	"github.com/libp2p/go-libp2p-core/peer"
7

8
	gsmsg "github.com/ipfs/go-graphsync/message"
9
	"github.com/ipfs/go-graphsync/notifications"
10 11 12 13 14
)

// PeerQueue is a process that sends messages to a peer
type PeerQueue interface {
	PeerProcess
15
	BuildMessage(blkSize uint64, buildMessageFn func(*gsmsg.Builder), notifees []notifications.Notifee)
16 17 18 19 20 21 22 23 24 25 26 27 28
}

// PeerQueueFactory provides a function that will create a PeerQueue.
type PeerQueueFactory func(ctx context.Context, p peer.ID) PeerQueue

// PeerMessageManager manages message queues for peers
type PeerMessageManager struct {
	*PeerManager
}

// NewMessageManager generates a new manger for sending messages
func NewMessageManager(ctx context.Context, createPeerQueue PeerQueueFactory) *PeerMessageManager {
	return &PeerMessageManager{
29
		PeerManager: New(ctx, func(ctx context.Context, p peer.ID) PeerHandler {
30 31 32 33 34
			return createPeerQueue(ctx, p)
		}),
	}
}

35 36
// BuildMessage allows you to modify the next message that is sent for the given peer
func (pmm *PeerMessageManager) BuildMessage(p peer.ID, blkSize uint64, buildMessageFn func(*gsmsg.Builder), notifees []notifications.Notifee) {
37
	pq := pmm.GetProcess(p).(PeerQueue)
38
	pq.BuildMessage(blkSize, buildMessageFn, notifees)
39
}