@cryptotaxi247 / kubo / commits / df9f10189

coreapi swarm: rewire connect/disconnect

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

Łukasz Magiera committed Sep 17, 2018 at 16:45 UTC df9f10189210804bb4bdc9b99d0e796a20f701e4
3 files changed +46 -91
core/commands/swarm.go
+9 -54
@@ -368,23 +368,13 @@ ipfs swarm connect /ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3
368 cmdkit.StringArg("address", true, true, "Address of peer to connect to.").EnableStdin(),
369 },
370 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
371 - n, err := cmdenv.GetNode(env)
371 + api, err := cmdenv.GetApi(env)
372 if err != nil {
373 return err
374 }
375
376 addrs := req.Arguments
377
378 - if n.PeerHost == nil {
379 - return err
380 - }
381 -
382 - // FIXME(steb): Nasty
383 - swrm, ok := n.PeerHost.Network().(*swarm.Swarm)
384 - if !ok {
385 - return fmt.Errorf("peerhost network was not swarm")
386 - }
387 -
378 pis, err := peersWithAddresses(addrs)
379 if err != nil {
380 return err
@@ -392,18 +382,16 @@ ipfs swarm connect /ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3
382
383 output := make([]string, len(pis))
384 for i, pi := range pis {
395 - swrm.Backoff().Clear(pi.ID)
396 -
385 output[i] = "connect " + pi.ID.Pretty()
386
399 - err := n.PeerHost.Connect(req.Context, pi)
387 + err := api.Swarm().Connect(req.Context, pi)
388 if err != nil {
389 return fmt.Errorf("%s failure: %s", output[i], err)
390 }
391 output[i] += " success"
392 }
393
406 - return cmds.EmitOnce(res, &stringList{addrs})
394 + return cmds.EmitOnce(res, &stringList{output})
395 },
396 Encoders: cmds.EncoderMap{
397 cmds.Text: cmds.MakeEncoder(stringListEncoder),
@@ -428,57 +416,24 @@ it will reconnect.
416 cmdkit.StringArg("address", true, true, "Address of peer to disconnect from.").EnableStdin(),
417 },
418 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
431 - n, err := cmdenv.GetNode(env)
419 + api, err := cmdenv.GetApi(env)
420 if err != nil {
421 return err
422 }
423
436 - addrs := req.Arguments
437 -
438 - if n.PeerHost == nil {
439 - return ErrNotOnline
440 - }
441 -
442 - iaddrs, err := parseAddresses(addrs)
424 + iaddrs, err := parseAddresses(req.Arguments)
425 if err != nil {
426 return err
427 }
428
429 output := make([]string, len(iaddrs))
430 for i, addr := range iaddrs {
449 - taddr := addr.Transport()
450 - id := addr.ID()
451 - output[i] = "disconnect " + id.Pretty()
452 -
453 - net := n.PeerHost.Network()
431 + output[i] = "disconnect " + addr.ID().Pretty()
432
455 - if taddr == nil {
456 - if net.Connectedness(id) != inet.Connected {
457 - output[i] += " failure: not connected"
458 - } else if err := net.ClosePeer(id); err != nil {
459 - output[i] += " failure: " + err.Error()
460 - } else {
461 - output[i] += " success"
462 - }
433 + if err := api.Swarm().Disconnect(req.Context, addr.Multiaddr()); err != nil {
434 + output[i] += " failure: " + err.Error()
435 } else {
464 - found := false
465 - for _, conn := range net.ConnsToPeer(id) {
466 - if !conn.RemoteMultiaddr().Equal(taddr) {
467 - continue
468 - }
469 -
470 - if err := conn.Close(); err != nil {
471 - output[i] += " failure: " + err.Error()
472 - } else {
473 - output[i] += " success"
474 - }
475 - found = true
476 - break
477 - }
478 -
479 - if !found {
480 - output[i] += " failure: conn not found"
481 - }
436 + output[i] += " success"
437 }
438 }
439 return cmds.EmitOnce(res, &stringList{output})
core/coreapi/interface/swarm.go
+15 -8
@@ -2,14 +2,22 @@ package iface
2
3 import (
4 "context"
5 + "errors"
6 "time"
7
8 ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
8 - peer "gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer"
9 + "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
10 + "gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer"
11 + pstore "gx/ipfs/QmfAQMFpgDU2U4BXG64qVr8HSiictfWvkSBz7Y2oDj65st/go-libp2p-peerstore"
12 )
13
11 -// PeerInfo contains information about a peer
12 -type PeerInfo interface {
14 +var (
15 + ErrNotConnected = errors.New("not connected")
16 + ErrConnNotFound = errors.New("conn not found")
17 + )
18 +
19 +// ConnectionInfo contains information about a peer
20 +type ConnectionInfo interface {
21 // ID returns PeerID
22 ID() peer.ID
23
@@ -20,18 +28,17 @@ type PeerInfo interface {
28 Latency(context.Context) (time.Duration, error)
29
30 // Streams returns list of streams established with the peer
23 - // TODO: should this return multicodecs?
24 - Streams(context.Context) ([]string, error)
31 + Streams(context.Context) ([]protocol.ID, error)
32 }
33
34 // SwarmAPI specifies the interface to libp2p swarm
35 type SwarmAPI interface {
29 - // Connect to a given address
30 - Connect(context.Context, ma.Multiaddr) error
36 + // Connect to a given peer
37 + Connect(context.Context, pstore.PeerInfo) error
38
39 // Disconnect from a given address
40 Disconnect(context.Context, ma.Multiaddr) error
41
42 // Peers returns the list of peers we are connected to
36 - Peers(context.Context) ([]PeerInfo, error)
43 + Peers(context.Context) ([]ConnectionInfo, error)
44 }
core/coreapi/swarm.go
+22 -29
@@ -2,7 +2,6 @@ package coreapi
2
3 import (
4 "context"
5 - "errors"
5 "fmt"
6 "time"
7
@@ -11,8 +10,10 @@ import (
10 swarm "gx/ipfs/QmPQoCVRHaGD25VffyB7DFV5qP65hFSQJdSDy75P1vYBKe/go-libp2p-swarm"
11 iaddr "gx/ipfs/QmSzdvo9aPzLj4HXWTcgGAp8N84tZc8LbLmFZFwUb1dpWk/go-ipfs-addr"
12 ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
13 + protocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
14 peer "gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer"
15 pstore "gx/ipfs/QmfAQMFpgDU2U4BXG64qVr8HSiictfWvkSBz7Y2oDj65st/go-libp2p-peerstore"
16 + inet "gx/ipfs/QmfDPh144WGBqRxZb1TGDHerbMnZATrHZggAPw7putNnBq/go-libp2p-net"
17 net "gx/ipfs/QmfDPh144WGBqRxZb1TGDHerbMnZATrHZggAPw7putNnBq/go-libp2p-net"
18 )
19
@@ -29,7 +30,7 @@ type connInfo struct {
30 muxer string
31 }
32
32 -func (api *SwarmAPI) Connect(ctx context.Context, addr ma.Multiaddr) error {
33 +func (api *SwarmAPI) Connect(ctx context.Context, pi pstore.PeerInfo) error {
34 if api.node.PeerHost == nil {
35 return coreiface.ErrOffline
36 }
@@ -39,16 +40,6 @@ func (api *SwarmAPI) Connect(ctx context.Context, addr ma.Multiaddr) error {
40 return fmt.Errorf("peerhost network was not swarm")
41 }
42
42 - ia, err := iaddr.ParseMultiaddr(ma.Multiaddr(addr))
43 - if err != nil {
44 - return err
45 - }
46 -
47 - pi := pstore.PeerInfo{
48 - ID: ia.ID(),
49 - Addrs: []ma.Multiaddr{ia.Transport()},
50 - }
51 -
43 swrm.Backoff().Clear(pi.ID)
44
45 return api.node.PeerHost.Connect(ctx, pi)
@@ -65,36 +56,38 @@ func (api *SwarmAPI) Disconnect(ctx context.Context, addr ma.Multiaddr) error {
56 }
57
58 taddr := ia.Transport()
59 + id := ia.ID()
60 + net := api.node.PeerHost.Network()
61
69 - found := false
70 - conns := api.node.PeerHost.Network().ConnsToPeer(ia.ID())
71 - for _, conn := range conns {
72 - if !conn.RemoteMultiaddr().Equal(taddr) {
73 - continue
62 + if taddr == nil {
63 + if net.Connectedness(id) != inet.Connected {
64 + return coreiface.ErrNotConnected
65 + } else if err := net.ClosePeer(id); err != nil {
66 + return err
67 }
68 + } else {
69 + for _, conn := range net.ConnsToPeer(id) {
70 + if !conn.RemoteMultiaddr().Equal(taddr) {
71 + continue
72 + }
73
76 - if err := conn.Close(); err != nil {
77 - return err
74 + return conn.Close()
75 }
79 - found = true
80 - break
81 - }
76
83 - if !found {
84 - return errors.New("conn not found")
77 + return coreiface.ErrConnNotFound
78 }
79
80 return nil
81 }
82
90 -func (api *SwarmAPI) Peers(context.Context) ([]coreiface.PeerInfo, error) {
83 +func (api *SwarmAPI) Peers(context.Context) ([]coreiface.ConnectionInfo, error) {
84 if api.node.PeerHost == nil {
85 return nil, coreiface.ErrOffline
86 }
87
88 conns := api.node.PeerHost.Network().Conns()
89
97 - var out []coreiface.PeerInfo
90 + var out []coreiface.ConnectionInfo
91 for _, c := range conns {
92 pid := c.RemotePeer()
93 addr := c.RemoteMultiaddr()
@@ -133,12 +126,12 @@ func (ci *connInfo) Latency(context.Context) (time.Duration, error) {
126 return ci.api.node.Peerstore.LatencyEWMA(peer.ID(ci.ID())), nil
127 }
128
136 -func (ci *connInfo) Streams(context.Context) ([]string, error) {
129 +func (ci *connInfo) Streams(context.Context) ([]protocol.ID, error) {
130 streams := ci.conn.GetStreams()
131
139 - out := make([]string, len(streams))
132 + out := make([]protocol.ID, len(streams))
133 for i, s := range streams {
141 - out[i] = string(s.Protocol())
134 + out[i] = s.Protocol()
135 }
136
137 return out, nil