Skip to content

Commit

Permalink
coreapi: pubsub interface
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <[email protected]>
  • Loading branch information
magik6k committed Mar 24, 2018
1 parent 3cbf97b commit 740d99f
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
56 changes: 56 additions & 0 deletions core/coreapi/interface/options/pubsub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package options

type PubSubPeersSettings struct {
Topic string
}

type PubSubSubscribeSettings struct {
Discover bool
}

type PubSubPeersOption func(*PubSubPeersSettings) error
type PubSubSubscribeOption func(*PubSubSubscribeSettings) error

func PubSubPeersOptions(opts ...PubSubPeersOption) (*PubSubPeersSettings, error) {
options := &PubSubPeersSettings{
Topic: "",
}

for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}

func PubSubSubscribeOptions(opts ...PubSubSubscribeOption) (*PubSubSubscribeSettings, error) {
options := &PubSubSubscribeSettings{
Discover: false,
}

for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}

type PubSubOptions struct{}

func (api *PubSubOptions) WithTopic(topic string) PubSubPeersOption {
return func(settings *PubSubPeersSettings) error {
settings.Topic = topic
return nil
}
}

func (api *PubSubOptions) WithDiscover(discover bool) PubSubSubscribeOption {
return func(settings *PubSubSubscribeSettings) error {
settings.Discover = discover
return nil
}
}
51 changes: 51 additions & 0 deletions core/coreapi/interface/pubsub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package iface

import (
"context"
"io"

options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"

peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer"
)

// PubSubSubscription is an active PubSub subscription
type PubSubSubscription interface {
io.Closer

// Chan return incoming message channel
Chan(context.Context) <-chan PubSubMessage
}

// PubSubMessage is a single PubSub message
type PubSubMessage interface {
// From returns id of a peer from which the message has arrived
From() peer.ID

// Data returns the message body
Data() []byte
}

// PubSubAPI specifies the interface to PubSub
type PubSubAPI interface {
// Ls lists subscribed topics by name
Ls(context.Context) ([]string, error)

// Peers list peers we are currently pubsubbing with
// TODO: WithTopic
Peers(context.Context, ...options.PubSubPeersOption) ([]peer.ID, error)

// WithTopic is an option for peers which specifies a topic filter for the
// function
WithTopic(topic string) options.PubSubPeersOption

// Publish a message to a given pubsub topic
Publish(context.Context, string, []byte) error

// Subscribe to messages on a given topic
Subscribe(context.Context, string) (PubSubSubscription, error)

// WithDiscover is an option for Subscribe which specifies whether to try to
// discover other peers subscribed to the same topic
WithDiscover(discover bool) options.PubSubSubscribeOption
}

0 comments on commit 740d99f

Please sign in to comment.