-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4805 from ipfs/feat/coreapi/pubsub
coreapi: PubSub API
- Loading branch information
Showing
9 changed files
with
419 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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 pubsubOpts struct{} | ||
|
||
var PubSub pubsubOpts | ||
|
||
func (pubsubOpts) Topic(topic string) PubSubPeersOption { | ||
return func(settings *PubSubPeersSettings) error { | ||
settings.Topic = topic | ||
return nil | ||
} | ||
} | ||
|
||
func (pubsubOpts) Discover(discover bool) PubSubSubscribeOption { | ||
return func(settings *PubSubSubscribeSettings) error { | ||
settings.Discover = discover | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package iface | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options" | ||
|
||
peer "gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer" | ||
) | ||
|
||
// PubSubSubscription is an active PubSub subscription | ||
type PubSubSubscription interface { | ||
io.Closer | ||
|
||
// Next return the next incoming message | ||
Next(context.Context) (PubSubMessage, error) | ||
} | ||
|
||
// 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 | ||
|
||
// Seq returns message identifier | ||
Seq() []byte | ||
|
||
// Topics returns list of topics this message was set to | ||
Topics() []string | ||
} | ||
|
||
// 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 | ||
Peers(context.Context, ...options.PubSubPeersOption) ([]peer.ID, error) | ||
|
||
// 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, ...options.PubSubSubscribeOption) (PubSubSubscription, error) | ||
} |
Oops, something went wrong.