@cryptotaxi247 / kubo / commits / fc08d5331

Corenet API: Apply suggestions, cleanups

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed May 28, 2017 at 23:36 UTC fc08d5331bf8ca41d0b1e22df7abddb566f0c08d
3 files changed +95 -39
core/commands/corenet.go
+82 -36
@@ -9,6 +9,7 @@ import (
9 "text/tabwriter"
10
11 cmds "github.com/ipfs/go-ipfs/commands"
12 + core "github.com/ipfs/go-ipfs/core"
13 corenet "github.com/ipfs/go-ipfs/core/corenet"
14
15 peerstore "gx/ipfs/QmNUVzEjq3XWJ89hegahPvyfJbTXgTaom48pLb7YBD9gHQ/go-libp2p-peerstore"
@@ -18,14 +19,13 @@ import (
19 manet "gx/ipfs/Qmf1Gq7N45Rpuw7ev47uWgH6dLPtdnvcMRNPkVBwqjLJg2/go-multiaddr-net"
20 )
21
21 -// Command output types.
22 -type AppInfoOutput struct {
22 +type CorenetAppInfoOutput struct {
23 Protocol string
24 Address string
25 }
26
27 -type StreamInfoOutput struct {
28 - HandlerId string
27 +type CorenetStreamInfoOutput struct {
28 + HandlerID string
29 Protocol string
30 LocalPeer string
31 LocalAddress string
@@ -34,11 +34,11 @@ type StreamInfoOutput struct {
34 }
35
36 type CorenetLsOutput struct {
37 - Apps []AppInfoOutput
37 + Apps []CorenetAppInfoOutput
38 }
39
40 type CorenetStreamsOutput struct {
41 - Streams []StreamInfoOutput
41 + Streams []CorenetStreamInfoOutput
42 }
43
44 // cnAppInfo holds information on a local application protocol listener service.
@@ -91,7 +91,7 @@ func (c *cnAppRegistry) Deregister(proto string) {
91
92 // cnStreamInfo holds information on active incoming and outgoing protocol app streams.
93 type cnStreamInfo struct {
94 - handlerId uint64
94 + handlerID uint64
95
96 protocol string
97
@@ -108,7 +108,7 @@ type cnStreamInfo struct {
108 func (c *cnStreamInfo) Close() error {
109 c.local.Close()
110 c.remote.Close()
111 - streams.Deregister(c.handlerId)
111 + streams.Deregister(c.handlerID)
112 return nil
113 }
114
@@ -116,19 +116,19 @@ func (c *cnStreamInfo) Close() error {
116 type cnStreamRegistry struct {
117 streams []*cnStreamInfo
118
119 - nextId uint64
119 + nextID uint64
120 }
121
122 func (c *cnStreamRegistry) Register(streamInfo *cnStreamInfo) {
123 - streamInfo.handlerId = c.nextId
123 + streamInfo.handlerID = c.nextID
124 c.streams = append(c.streams, streamInfo)
125 - c.nextId += 1
125 + c.nextID++
126 }
127
128 -func (c *cnStreamRegistry) Deregister(handlerId uint64) {
128 +func (c *cnStreamRegistry) Deregister(handlerID uint64) {
129 foundAt := -1
130 for i, s := range c.streams {
131 - if s.handlerId == handlerId {
131 + if s.handlerID == handlerID {
132 foundAt = i
133 break
134 }
@@ -145,7 +145,11 @@ var streams cnStreamRegistry
145
146 var CorenetCmd = &cmds.Command{
147 Helptext: cmds.HelpText{
148 - Tagline: "Application network streams.",
148 + Tagline: "Libp2p stream mounting.",
149 + ShortDescription: `
150 +Expose a local application to remote peers over libp2p
151 +
152 +Note: this command is experimental and subject to change as usecases and APIs are refined`,
153 },
154
155 Subcommands: map[string]*cmds.Command{
@@ -162,7 +166,7 @@ var CorenetLsCmd = &cmds.Command{
166 Tagline: "List active application protocol listeners.",
167 },
168 Options: []cmds.Option{
165 - cmds.BoolOption("headers", "v", "Print table headers (HandlerId, Protocol, Local, Remote).").Default(false),
169 + cmds.BoolOption("headers", "v", "Print table headers (HandlerID, Protocol, Local, Remote).").Default(false),
170 },
171 Run: func(req cmds.Request, res cmds.Response) {
172 n, err := req.InvocContext().GetNode()
@@ -171,6 +175,12 @@ var CorenetLsCmd = &cmds.Command{
175 return
176 }
177
178 + err = checkEnabled(n)
179 + if err != nil {
180 + res.SetError(err, cmds.ErrNormal)
181 + return
182 + }
183 +
184 if !n.OnlineMode() {
185 res.SetError(errNotOnline, cmds.ErrClient)
186 return
@@ -179,7 +189,7 @@ var CorenetLsCmd = &cmds.Command{
189 output := &CorenetLsOutput{}
190
191 for _, a := range apps.apps {
182 - output.Apps = append(output.Apps, AppInfoOutput{
192 + output.Apps = append(output.Apps, CorenetAppInfoOutput{
193 Protocol: a.protocol,
194 Address: a.address.String(),
195 })
@@ -210,10 +220,10 @@ var CorenetLsCmd = &cmds.Command{
220
221 var CorenetStreamsCmd = &cmds.Command{
222 Helptext: cmds.HelpText{
213 - Tagline: "List active application protocol connections.",
223 + Tagline: "List active application protocol streams.",
224 },
225 Options: []cmds.Option{
216 - cmds.BoolOption("headers", "v", "Print table headers (HandlerId, Protocol, Local, Remote).").Default(false),
226 + cmds.BoolOption("headers", "v", "Print table headers (HandlerID, Protocol, Local, Remote).").Default(false),
227 },
228 Run: func(req cmds.Request, res cmds.Response) {
229 n, err := req.InvocContext().GetNode()
@@ -222,6 +232,12 @@ var CorenetStreamsCmd = &cmds.Command{
232 return
233 }
234
235 + err = checkEnabled(n)
236 + if err != nil {
237 + res.SetError(err, cmds.ErrNormal)
238 + return
239 + }
240 +
241 if !n.OnlineMode() {
242 res.SetError(errNotOnline, cmds.ErrClient)
243 return
@@ -230,8 +246,8 @@ var CorenetStreamsCmd = &cmds.Command{
246 output := &CorenetStreamsOutput{}
247
248 for _, s := range streams.streams {
233 - output.Streams = append(output.Streams, StreamInfoOutput{
234 - HandlerId: strconv.FormatUint(s.handlerId, 10),
249 + output.Streams = append(output.Streams, CorenetStreamInfoOutput{
250 + HandlerID: strconv.FormatUint(s.handlerID, 10),
251
252 Protocol: s.protocol,
253
@@ -254,10 +270,10 @@ var CorenetStreamsCmd = &cmds.Command{
270 w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
271 for _, stream := range list.Streams {
272 if headers {
257 - fmt.Fprintln(w, "HandlerId\tProtocol\tLocal\tRemote")
273 + fmt.Fprintln(w, "HandlerID\tProtocol\tLocal\tRemote")
274 }
275
260 - fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", stream.HandlerId, stream.Protocol, stream.LocalAddress, stream.RemotePeer)
276 + fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", stream.HandlerID, stream.Protocol, stream.LocalAddress, stream.RemotePeer)
277 }
278 w.Flush()
279
@@ -281,6 +297,12 @@ var CorenetListenCmd = &cmds.Command{
297 return
298 }
299
300 + err = checkEnabled(n)
301 + if err != nil {
302 + res.SetError(err, cmds.ErrNormal)
303 + return
304 + }
305 +
306 if !n.OnlineMode() {
307 res.SetError(errNotOnline, cmds.ErrClient)
308 return
@@ -288,7 +310,7 @@ var CorenetListenCmd = &cmds.Command{
310
311 proto := "/app/" + req.Arguments()[0]
312 if checkProtoExists(n.PeerHost.Mux().Protocols(), proto) {
291 - res.SetError(errors.New("Protocol handler already registered."), cmds.ErrNormal)
313 + res.SetError(errors.New("protocol handler already registered"), cmds.ErrNormal)
314 return
315 }
316
@@ -317,7 +339,7 @@ var CorenetListenCmd = &cmds.Command{
339 apps.Register(&app)
340
341 // Successful response.
320 - res.SetOutput(&AppInfoOutput{
342 + res.SetOutput(&CorenetAppInfoOutput{
343 Protocol: proto,
344 Address: addr.String(),
345 })
@@ -395,6 +417,12 @@ var CorenetDialCmd = &cmds.Command{
417 return
418 }
419
420 + err = checkEnabled(n)
421 + if err != nil {
422 + res.SetError(err, cmds.ErrNormal)
423 + return
424 + }
425 +
426 if !n.OnlineMode() {
427 res.SetError(errNotOnline, cmds.ErrClient)
428 return
@@ -454,11 +482,11 @@ var CorenetDialCmd = &cmds.Command{
482 go doAccept(&app, remote, listener)
483
484 default:
457 - res.SetError(errors.New("Unsupported protocol: "+lnet), cmds.ErrNormal)
485 + res.SetError(errors.New("unsupported protocol: "+lnet), cmds.ErrNormal)
486 return
487 }
488
461 - output := AppInfoOutput{
489 + output := CorenetAppInfoOutput{
490 Protocol: app.protocol,
491 Address: app.address.String(),
492 }
@@ -497,8 +525,8 @@ var CorenetCloseCmd = &cmds.Command{
525 Tagline: "Closes an active stream listener or client.",
526 },
527 Arguments: []cmds.Argument{
500 - cmds.StringArg("HandlerId", false, false, "Application listener or client HandlerId"),
501 - cmds.StringArg("Protocol", false, false, "Application listener or client HandlerId"),
528 + cmds.StringArg("HandlerID", false, false, "Application listener or client HandlerID"),
529 + cmds.StringArg("Protocol", false, false, "Application listener or client HandlerID"),
530 },
531 Options: []cmds.Option{
532 cmds.BoolOption("all", "a", "Close all streams and listeners.").Default(false),
@@ -510,6 +538,12 @@ var CorenetCloseCmd = &cmds.Command{
538 return
539 }
540
541 + err = checkEnabled(n)
542 + if err != nil {
543 + res.SetError(err, cmds.ErrNormal)
544 + return
545 + }
546 +
547 if !n.OnlineMode() {
548 res.SetError(errNotOnline, cmds.ErrClient)
549 return
@@ -518,27 +552,27 @@ var CorenetCloseCmd = &cmds.Command{
552 closeAll, _, _ := req.Option("all").Bool()
553
554 var proto string
521 - var handlerId uint64
555 + var handlerID uint64
556
523 - useHandlerId := false
557 + useHandlerID := false
558
559 if !closeAll && len(req.Arguments()) == 0 {
526 - res.SetError(errors.New("You must supply a handlerId or stream protocol."), cmds.ErrNormal)
560 + res.SetError(errors.New(" handlerID nor stream protocol"), cmds.ErrNormal)
561 return
562
563 } else if !closeAll {
530 - handlerId, err = strconv.ParseUint(req.Arguments()[0], 10, 64)
564 + handlerID, err = strconv.ParseUint(req.Arguments()[0], 10, 64)
565 if err != nil {
566 proto = "/app/" + req.Arguments()[0]
567
568 } else {
535 - useHandlerId = true
569 + useHandlerID = true
570 }
571 }
572
539 - if closeAll || useHandlerId {
573 + if closeAll || useHandlerID {
574 for _, s := range streams.streams {
541 - if !closeAll && handlerId != s.handlerId {
575 + if !closeAll && handlerID != s.handlerID {
576 continue
577 }
578 s.Close()
@@ -548,7 +582,7 @@ var CorenetCloseCmd = &cmds.Command{
582 }
583 }
584
551 - if closeAll || !useHandlerId {
585 + if closeAll || !useHandlerID {
586 for _, a := range apps.apps {
587 if !closeAll && a.protocol != proto {
588 continue
@@ -564,3 +598,15 @@ var CorenetCloseCmd = &cmds.Command{
598 }
599 },
600 }
601 +
602 +func checkEnabled(n *core.IpfsNode) error {
603 + config, err := n.Repo.Config()
604 + if err != nil {
605 + return err
606 + }
607 +
608 + if !config.Experimental.Libp2pStreamMounting {
609 + return errors.New("libp2p stream mounting not enabled")
610 + }
611 + return nil
612 +}
repo/config/experiments.go
+3 -2
@@ -1,6 +1,7 @@
1 package config
2
3 type Experiments struct {
4 - FilestoreEnabled bool
5 - ShardingEnabled bool
4 + FilestoreEnabled bool
5 + ShardingEnabled bool
6 + Libp2pStreamMounting bool
7 }
test/sharness/t0180-corenet.sh
+10 -1
@@ -26,7 +26,16 @@ test_expect_success "test ports are closed" '
26 (! (netstat -ln | grep "LISTEN" | grep ":10102 "))
27 '
28
29 -test_expect_success 'start ipfs listener' '
29 +test_must_fail 'fail without config option being enabled' '
30 + ipfsi 0 exp corenet ls
31 +'
32 +
33 +test_expect_success "enable filestore config setting" '
34 + ipfsi 0 config --json Experimental.Libp2pStreamMounting true
35 + ipfsi 1 config --json Experimental.Libp2pStreamMounting true
36 +'
37 +
38 +test_expect_success 'start corenet listener' '
39 ipfsi 0 exp corenet listen corenet-test /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log
40 '
41