-
-
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.
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
- Loading branch information
Showing
5 changed files
with
120 additions
and
4 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,108 @@ | ||
package coreapi | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" | ||
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options" | ||
|
||
floodsub "gx/ipfs/QmSFihvoND3eDaAYRCeLgLPt62yCPgMZs1NSZmKFEtJQQw/go-libp2p-floodsub" | ||
peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer" | ||
) | ||
|
||
type PubSubAPI struct { | ||
*CoreAPI | ||
*caopts.PubSubOptions | ||
} | ||
|
||
type pubSubSubscription struct { | ||
subscription *floodsub.Subscription | ||
} | ||
|
||
type pubSubMessage struct { | ||
msg *floodsub.Message | ||
} | ||
|
||
func (api *PubSubAPI) Ls(ctx context.Context) ([]string, error) { | ||
if err := api.checkNode(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return api.node.Floodsub.GetTopics(), nil | ||
} | ||
|
||
func (api *PubSubAPI) Peers(ctx context.Context, opts ...caopts.PubSubPeersOption) ([]peer.ID, error) { | ||
if err := api.checkNode(); err != nil { | ||
return nil, err | ||
} | ||
|
||
settings, err := caopts.PubSubPeersOptions(opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
peers := api.node.Floodsub.ListPeers(settings.Topic) | ||
out := make([]peer.ID, len(peers)) | ||
|
||
for i, peer := range peers { | ||
out[i] = peer | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
func (api *PubSubAPI) Publish(ctx context.Context, topic string, data []byte) error { | ||
if err := api.checkNode(); err != nil { | ||
return err | ||
} | ||
|
||
return api.node.Floodsub.Publish(topic, data) | ||
} | ||
|
||
func (api *PubSubAPI) Subscribe(ctx context.Context, topic string, opts ...caopts.PubSubSubscribeOption) (coreiface.PubSubSubscription, error) { | ||
if err := api.checkNode(); err != nil { | ||
return nil, err | ||
} | ||
|
||
sub, err := api.node.Floodsub.Subscribe(topic) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &pubSubSubscription{sub}, nil | ||
} | ||
|
||
func (api *PubSubAPI) checkNode() error { | ||
if !api.node.OnlineMode() { | ||
return coreiface.ErrOffline | ||
} | ||
|
||
if api.node.Floodsub == nil { | ||
return errors.New("experimental pubsub feature not enabled. Run daemon with --enable-pubsub-experiment to use.") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (sub *pubSubSubscription) Close() error { | ||
sub.subscription.Cancel() | ||
return nil | ||
} | ||
|
||
func (sub *pubSubSubscription) Next(ctx context.Context) (coreiface.PubSubMessage, error) { | ||
msg, err := sub.subscription.Next(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &pubSubMessage{msg}, nil | ||
} | ||
|
||
func (msg *pubSubMessage) From() peer.ID { | ||
return peer.ID(msg.msg.From) | ||
} | ||
|
||
func (msg *pubSubMessage) Data() []byte { | ||
return msg.msg.Data | ||
} |