@cryptotaxi247 / kubo / commits / e0bd4a118

Corenet API: Fix codeclimate issues

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

Łukasz Magiera committed May 31, 2017 at 12:56 UTC e0bd4a11882d3aacdce51023c4e27ead9ee3c113
5 files changed +30 -15
core/commands/corenet.go
+11 -10
@@ -45,6 +45,7 @@ type CorenetStreamsOutput struct {
45 Streams []CorenetStreamInfoOutput
46 }
47
48 +// CorenetCmd is the 'ipfs corenet' command
49 var CorenetCmd = &cmds.Command{
50 Helptext: cmds.HelpText{
51 Tagline: "Libp2p stream mounting.",
@@ -55,15 +56,15 @@ Note: this command is experimental and subject to change as usecases and APIs ar
56 },
57
58 Subcommands: map[string]*cmds.Command{
58 - "ls": CorenetLsCmd,
59 - "streams": CorenetStreamsCmd,
60 - "dial": CorenetDialCmd,
61 - "listen": CorenetListenCmd,
62 - "close": CorenetCloseCmd,
59 + "ls": corenetLsCmd,
60 + "streams": corenetStreamsCmd,
61 + "dial": corenetDialCmd,
62 + "listen": corenetListenCmd,
63 + "close": corenetCloseCmd,
64 },
65 }
66
66 -var CorenetLsCmd = &cmds.Command{
67 +var corenetLsCmd = &cmds.Command{
68 Helptext: cmds.HelpText{
69 Tagline: "List active application protocol listeners.",
70 },
@@ -120,7 +121,7 @@ var CorenetLsCmd = &cmds.Command{
121 },
122 }
123
123 -var CorenetStreamsCmd = &cmds.Command{
124 +var corenetStreamsCmd = &cmds.Command{
125 Helptext: cmds.HelpText{
126 Tagline: "List active application protocol streams.",
127 },
@@ -184,7 +185,7 @@ var CorenetStreamsCmd = &cmds.Command{
185 },
186 }
187
187 -var CorenetListenCmd = &cmds.Command{
188 +var corenetListenCmd = &cmds.Command{
189 Helptext: cmds.HelpText{
190 Tagline: "Create application protocol listener and proxy to network multiaddr.",
191 },
@@ -306,7 +307,7 @@ func startStreaming(stream *corenet.StreamInfo) {
307 }()
308 }
309
309 -var CorenetDialCmd = &cmds.Command{
310 +var corenetDialCmd = &cmds.Command{
311 Helptext: cmds.HelpText{
312 Tagline: "Dial to an application service.",
313 },
@@ -427,7 +428,7 @@ func doAccept(n *core.IpfsNode, app *corenet.AppInfo, remote net.Stream, listene
428 startStreaming(&stream)
429 }
430
430 -var CorenetCloseCmd = &cmds.Command{
431 +var corenetCloseCmd = &cmds.Command{
432 Helptext: cmds.HelpText{
433 Tagline: "Closes an active stream listener or client.",
434 },
corenet/apps.go
+3
@@ -28,6 +28,7 @@ type AppInfo struct {
28 Registry *AppRegistry
29 }
30
31 +// Close closes the listener. Does not affect child streams
32 func (c *AppInfo) Close() error {
33 c.Registry.Deregister(c.Protocol)
34 c.Closer.Close()
@@ -39,10 +40,12 @@ type AppRegistry struct {
40 Apps []*AppInfo
41 }
42
43 +// Register registers appInfo in this registry
44 func (c *AppRegistry) Register(appInfo *AppInfo) {
45 c.Apps = append(c.Apps, appInfo)
46 }
47
48 +// Deregister deregisters protocol handler from this registry
49 func (c *AppRegistry) Deregister(proto string) {
50 foundAt := -1
51 for i, a := range c.Apps {
corenet/corenet.go
+2
@@ -1,10 +1,12 @@
1 package corenet
2
3 +// Corenet structure holds information on currently running streams/apps
4 type Corenet struct {
5 Apps AppRegistry
6 Streams StreamRegistry
7 }
8
9 +// NewCorenet creates new Corenet struct
10 func NewCorenet() *Corenet {
11 return &Corenet{}
12 }
corenet/net/net.go
+11 -5
@@ -11,12 +11,14 @@ import (
11 peer "gx/ipfs/QmdS9KpbDyPrieswibZhkod1oXqRwZJrUPzxCofAMWpFGq/go-libp2p-peer"
12 )
13
14 +// Listener wraps stream handler into a listener
15 type Listener interface {
16 Accept() (net.Stream, error)
17 Close() error
18 }
19
19 -type ipfsListener struct {
20 +// IpfsListener holds information on a listener
21 +type IpfsListener struct {
22 node *core.IpfsNode
23 conCh chan net.Stream
24 proto pro.ID
@@ -24,7 +26,8 @@ type ipfsListener struct {
26 cancel func()
27 }
28
27 -func (il *ipfsListener) Accept() (net.Stream, error) {
29 +// Accept waits for a connection from the listener
30 +func (il *IpfsListener) Accept() (net.Stream, error) {
31 select {
32 case c := <-il.conCh:
33 return c, nil
@@ -33,16 +36,18 @@ func (il *ipfsListener) Accept() (net.Stream, error) {
36 }
37 }
38
36 -func (il *ipfsListener) Close() error {
39 +// Close closes the listener and removes stream handler
40 +func (il *IpfsListener) Close() error {
41 il.cancel()
42 il.node.PeerHost.RemoveStreamHandler(il.proto)
43 return nil
44 }
45
42 -func Listen(nd *core.IpfsNode, protocol string) (*ipfsListener, error) {
46 +// Listen creates new IpfsListener
47 +func Listen(nd *core.IpfsNode, protocol string) (*IpfsListener, error) {
48 ctx, cancel := context.WithCancel(nd.Context())
49
45 - list := &ipfsListener{
50 + list := &IpfsListener{
51 node: nd,
52 proto: pro.ID(protocol),
53 conCh: make(chan net.Stream),
@@ -61,6 +66,7 @@ func Listen(nd *core.IpfsNode, protocol string) (*ipfsListener, error) {
66 return list, nil
67 }
68
69 +// Dial dials to a specified node and protocol
70 func Dial(nd *core.IpfsNode, p peer.ID, protocol string) (net.Stream, error) {
71 ctx, cancel := context.WithTimeout(nd.Context(), time.Second*30)
72 defer cancel()
corenet/streams.go
+3
@@ -25,6 +25,7 @@ type StreamInfo struct {
25 Registry *StreamRegistry
26 }
27
28 +// Close closes stream endpoints and deregisters it
29 func (c *StreamInfo) Close() error {
30 c.Local.Close()
31 c.Remote.Close()
@@ -39,12 +40,14 @@ type StreamRegistry struct {
40 nextID uint64
41 }
42
43 +// Register registers a stream to the registry
44 func (c *StreamRegistry) Register(streamInfo *StreamInfo) {
45 streamInfo.HandlerID = c.nextID
46 c.Streams = append(c.Streams, streamInfo)
47 c.nextID++
48 }
49
50 +// Deregister deregisters stream from the registry
51 func (c *StreamRegistry) Deregister(handlerID uint64) {
52 foundAt := -1
53 for i, s := range c.Streams {