<commit_before>package commands

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"path"
	"sort"

	cmds "github.com/ipfs/go-ipfs/commands"
	iaddr "github.com/ipfs/go-ipfs/thirdparty/ipfsaddr"
	peer "<KEY>"
	swarm "<KEY>"

	mafilter "<KEY>"
	ma "gx/<KEY>"
)

type stringList struct {
	Strings []string
}

type addrMap struct {
	Addrs map[string][]string
}

var SwarmCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "Swarm inspection tool.",
		ShortDescription: `
'ipfs swarm' is a tool to manipulate the network swarm. The swarm is the
component that opens, listens for, and maintains connections to other
ipfs peers in the internet.
`,
	},
	Subcommands: map[string]*cmds.Command{
		"addrs":      swarmAddrsCmd,
		"connect":    swarmConnectCmd,
		"disconnect": swarmDisconnectCmd,
		"filters":    swarmFiltersCmd,
		"peers":      swarmPeersCmd,
	},
}

var swarmPeersCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "List peers with open connections.",
		ShortDescription: `
'ipfs swarm peers' lists the set of peers this node is connected to.
`,
	},
	Run: func(req cmds.Request, res cmds.Response) {

		log.Debug("ipfs swarm peers")
		n, err := req.InvocContext().GetNode()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		if n.PeerHost == nil {
			res.SetError(errNotOnline, cmds.ErrClient)
			return
		}

		conns := n.PeerHost.Network().Conns()
		addrs := make([]string, len(conns))
		for i, c := range conns {
			pid := c.RemotePeer()
			addr := c.RemoteMultiaddr()
			addrs[i] = fmt.Sprintf("%s/ipfs/%s", addr, pid.Pretty())
		}

		sort.Sort(sort.StringSlice(addrs))<commit_msg>Added a verbose option for swarm peers.<commit_after>package commands

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"path"
	"sort"
	"time"

	cmds "github.com/ipfs/go-ipfs/commands"
	iaddr "github.com/ipfs/go-ipfs/thirdparty/ipfsaddr"
	peer "gx/ipfs/QmZpD74pUj6vuxTp1o6LhA3JavC2Bvh9fsWPPVvHnD9sE7/go-libp2p-peer"
	swarm "gx/ipfs/QmZpVD1kkRwoC67vNknvCrY72pjmVdtZ7txSk8mtCbuwd3/go-libp2p/p2p/net/swarm"

	mafilter "gx/<KEY>"
	ma "gx/ipfs/QmYzDkkgAEmrcNzFCiYo6L1dTX4EAG1gZkbtdbd9trL4vd/go-multiaddr"
	context "gx/ipfs/<KEY>"
)

type stringList struct {
	Strings []string
}

type addrMap struct {
	Addrs map[string][]string
}

var SwarmCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "Swarm inspection tool.",
		ShortDescription: `
'ipfs swarm' is a tool to manipulate the network swarm. The swarm is the
component that opens, listens for, and maintains connections to other
ipfs peers in the internet.
`,
	},
	Subcommands: map[string]*cmds.Command{
		"addrs":      swarmAddrsCmd,
		"connect":    swarmConnectCmd,
		"disconnect": swarmDisconnectCmd,
		"filters":    swarmFiltersCmd,
		"peers":      swarmPeersCmd,
	},
}

var swarmPeersCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "List peers with open connections.",
		ShortDescription: `
'ipfs swarm peers' lists the set of peers this node is connected to.
`,
	},
	Options: []cmds.Option{
		cmds.BoolOption("verbose", "v",
			`Also display latency along with peer information in the following form:
        <peer address> <latency>
        `),
	},
	Run: func(req cmds.Request, res cmds.Response) {
		timeout := time.Duration(5) * time.Second

		log.Debug("ipfs swarm peers")
		n, err := req.InvocContext().GetNode()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		if n.PeerHost == nil {
			res.SetError(errNotOnline, cmds.ErrClient)
			return
		}

		verbose, _, _ := req.Option("verbose").Bool()
		conns := n.PeerHost.Network().Conns()
		addrs := make([]string, len(conns))
		pingService := n.Ping

		for i, c := range conns {
			pid := c.RemotePeer()
			addr := c.RemoteMultiaddr()

			if verbose {
				pContext, cancelFn := context.WithTimeout(req.Context(), timeout)
				defer cancelFn()

				ch, _ := pingService.Ping(pContext, pid)
				duration := <-ch
				var durStr string

				if pContext.Err() != nil {
					durStr = pContext.Err().Error()
				} else {
					durStr = duration.String()
				}
				addrs[i] = fmt.Sprintf("%s/ipfs/%s %s", addr, pid.Pretty(), durStr)
			} else {
				addrs[i] = fmt.Sprintf("%s/ipfs/%s", addr, pid.Pretty())
			}
		}

		sort.Sort(sort.StringSlice(addrs))
{'fraction_non_alphanumeric': 0.140371782650143, 'fraction_numerical': 0.00667302192564347, 'mean_word_length': 3.7531143827859568, 'pattern_counts': {'":': 10, '<': 12, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1642455', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1593, 'n_words': 362}