@cryptotaxi247 / kubo / commits / f9a934700

coreapi: pubsub interface

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com> This commit was moved from ipfs/interface-go-ipfs-core@ccaec46f3deaf46e20f9c265d5920f68251e4da4 This commit was moved from ipfs/boxo@f06c01e06b04ec7a27b0738f984e0f30b005c711

Łukasz Magiera committed Mar 10, 2018 at 19:23 UTC f9a93470069c116015b79f33f105275896f9917a
2 files changed +107
core/coreiface/options/pubsub.go new
+56
@@ -0,0 +1,56 @@
1 +package options
2 +
3 +type PubSubPeersSettings struct {
4 + Topic string
5 +}
6 +
7 +type PubSubSubscribeSettings struct {
8 + Discover bool
9 +}
10 +
11 +type PubSubPeersOption func(*PubSubPeersSettings) error
12 +type PubSubSubscribeOption func(*PubSubSubscribeSettings) error
13 +
14 +func PubSubPeersOptions(opts ...PubSubPeersOption) (*PubSubPeersSettings, error) {
15 + options := &PubSubPeersSettings{
16 + Topic: "",
17 + }
18 +
19 + for _, opt := range opts {
20 + err := opt(options)
21 + if err != nil {
22 + return nil, err
23 + }
24 + }
25 + return options, nil
26 +}
27 +
28 +func PubSubSubscribeOptions(opts ...PubSubSubscribeOption) (*PubSubSubscribeSettings, error) {
29 + options := &PubSubSubscribeSettings{
30 + Discover: false,
31 + }
32 +
33 + for _, opt := range opts {
34 + err := opt(options)
35 + if err != nil {
36 + return nil, err
37 + }
38 + }
39 + return options, nil
40 +}
41 +
42 +type PubSubOptions struct{}
43 +
44 +func (api *PubSubOptions) WithTopic(topic string) PubSubPeersOption {
45 + return func(settings *PubSubPeersSettings) error {
46 + settings.Topic = topic
47 + return nil
48 + }
49 +}
50 +
51 +func (api *PubSubOptions) WithDiscover(discover bool) PubSubSubscribeOption {
52 + return func(settings *PubSubSubscribeSettings) error {
53 + settings.Discover = discover
54 + return nil
55 + }
56 +}
core/coreiface/pubsub.go new
+51
@@ -0,0 +1,51 @@
1 +package iface
2 +
3 +import (
4 + "context"
5 + "io"
6 +
7 + options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
8 +
9 + peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer"
10 +)
11 +
12 +// PubSubSubscription is an active PubSub subscription
13 +type PubSubSubscription interface {
14 + io.Closer
15 +
16 + // Chan return incoming message channel
17 + Chan(context.Context) <-chan PubSubMessage
18 +}
19 +
20 +// PubSubMessage is a single PubSub message
21 +type PubSubMessage interface {
22 + // From returns id of a peer from which the message has arrived
23 + From() peer.ID
24 +
25 + // Data returns the message body
26 + Data() []byte
27 +}
28 +
29 +// PubSubAPI specifies the interface to PubSub
30 +type PubSubAPI interface {
31 + // Ls lists subscribed topics by name
32 + Ls(context.Context) ([]string, error)
33 +
34 + // Peers list peers we are currently pubsubbing with
35 + // TODO: WithTopic
36 + Peers(context.Context, ...options.PubSubPeersOption) ([]peer.ID, error)
37 +
38 + // WithTopic is an option for peers which specifies a topic filter for the
39 + // function
40 + WithTopic(topic string) options.PubSubPeersOption
41 +
42 + // Publish a message to a given pubsub topic
43 + Publish(context.Context, string, []byte) error
44 +
45 + // Subscribe to messages on a given topic
46 + Subscribe(context.Context, string) (PubSubSubscription, error)
47 +
48 + // WithDiscover is an option for Subscribe which specifies whether to try to
49 + // discover other peers subscribed to the same topic
50 + WithDiscover(discover bool) options.PubSubSubscribeOption
51 +}