-
-
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: Jeromy <[email protected]>
- Loading branch information
1 parent
e30576d
commit 20ff2f8
Showing
3 changed files
with
147 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package commands | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"reflect" | ||
|
||
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util" | ||
|
||
cmds "github.com/ipfs/go-ipfs/commands" | ||
floodsub "github.com/whyrusleeping/go-floodsub" | ||
) | ||
|
||
var PubsubCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Only pubsub by the vaguest technical definition", | ||
}, | ||
Subcommands: map[string]*cmds.Command{ | ||
"pub": PubsubPubCmd, | ||
"sub": PubsubSubCmd, | ||
}, | ||
} | ||
var PubsubSubCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "subscribe to messages on a given topic", | ||
}, | ||
Arguments: []cmds.Argument{ | ||
cmds.StringArg("topic", true, false, "String name of topic to subscribe to."), | ||
}, | ||
Run: func(req cmds.Request, res cmds.Response) { | ||
n, err := req.InvocContext().GetNode() | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
// Must be online! | ||
if !n.OnlineMode() { | ||
res.SetError(errNotOnline, cmds.ErrClient) | ||
return | ||
} | ||
|
||
topic := req.Arguments()[0] | ||
msgs, err := n.Floodsub.Subscribe(topic) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
out := make(chan interface{}) | ||
res.SetOutput((<-chan interface{})(out)) | ||
|
||
ctx := req.Context() | ||
go func() { | ||
defer close(out) | ||
for { | ||
select { | ||
case msg, ok := <-msgs: | ||
if !ok { | ||
return | ||
} | ||
out <- msg | ||
case <-ctx.Done(): | ||
n.Floodsub.Unsub(topic) | ||
} | ||
} | ||
}() | ||
}, | ||
Marshalers: cmds.MarshalerMap{ | ||
cmds.Text: func(res cmds.Response) (io.Reader, error) { | ||
outChan, ok := res.Output().(<-chan interface{}) | ||
if !ok { | ||
fmt.Println(reflect.TypeOf(res.Output())) | ||
return nil, u.ErrCast() | ||
} | ||
|
||
marshal := func(v interface{}) (io.Reader, error) { | ||
obj, ok := v.(*floodsub.Message) | ||
if !ok { | ||
return nil, u.ErrCast() | ||
} | ||
|
||
return bytes.NewReader(obj.Data), nil | ||
} | ||
|
||
return &cmds.ChannelMarshaler{ | ||
Channel: outChan, | ||
Marshaler: marshal, | ||
Res: res, | ||
}, nil | ||
}, | ||
}, | ||
Type: floodsub.Message{}, | ||
} | ||
var PubsubPubCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Publish a message to a given pubsub sub topic.", | ||
ShortDescription: `pubsub sub pub pub hubbub sub? | ||
pub. Pubsub subsub subpubsub hubbub pub sub grub subhubbub. | ||
Pubsubs sub hubbub sub tub, dub dub dub wub wub wub wub wub. Pubsub. | ||
Pubsub subsub pubsub hubbub dubtub? Wub. Wub. Pubsub. | ||
Nubs, Tubs, Wubs, pub Dubs. Pub subdub pub pub sub nub. | ||
ipfs pubsub pub "pubsub hubbub" | ||
Pub: hubbub pub sub sub tub pub bub. | ||
`, | ||
}, | ||
Arguments: []cmds.Argument{ | ||
cmds.StringArg("data", true, false, "Data to send to david dias. (and only him)").EnableStdin(), | ||
}, | ||
Options: []cmds.Option{ | ||
cmds.StringOption("topic", "t", "Topic to pubusb to."), | ||
}, | ||
Run: func(req cmds.Request, res cmds.Response) { | ||
n, err := req.InvocContext().GetNode() | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
// Must be online! | ||
if !n.OnlineMode() { | ||
res.SetError(errNotOnline, cmds.ErrClient) | ||
return | ||
} | ||
|
||
topic, found, _ := req.Option("topic").String() | ||
if !found { | ||
res.SetError(fmt.Errorf("topic required"), cmds.ErrNormal) | ||
return | ||
} | ||
|
||
err = n.Floodsub.Publish(topic, []byte(req.Arguments()[0])) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
}, | ||
} |
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