ipfs swarm addrs: print out known addresses
this will help us introspect what's going on.
Juan Batiz-Benet committed
Feb 2, 2015 at 18:23 UTC
b0a1cf037167780e66f92d7b62404fffb0c7a330
1 file changed
+66
core/commands/swarm.go
+66
@@ -18,11 +18,16 @@ type stringList struct {
18
Strings []string
19
}
20
21
+type addrMap struct {
22
+ Addrs map[string][]string
23
+}
24
+
25
var SwarmCmd = &cmds.Command{
26
Helptext: cmds.HelpText{
27
Tagline: "swarm inspection tool",
28
Synopsis: `
29
ipfs swarm peers - List peers with open connections
30
+ipfs swarm addrs - List known addresses. Useful to debug.
31
ipfs swarm connect <address> - Open connection to a given address
32
ipfs swarm disconnect <address> - Close connection to a given address
33
`,
@@ -34,6 +39,7 @@ ipfs peers in the internet.
39
},
40
Subcommands: map[string]*cmds.Command{
41
"peers": swarmPeersCmd,
42
+ "addrs": swarmAddrsCmd,
43
"connect": swarmConnectCmd,
44
"disconnect": swarmDisconnectCmd,
45
},
@@ -77,6 +83,66 @@ ipfs swarm peers lists the set of peers this node is connected to.
83
Type: stringList{},
84
}
85
86
+var swarmAddrsCmd = &cmds.Command{
87
+ Helptext: cmds.HelpText{
88
+ Tagline: "List known addresses. Useful to debug.",
89
+ ShortDescription: `
90
+ipfs swarm addrs lists all addresses this node is aware of.
91
+`,
92
+ },
93
+ Run: func(req cmds.Request, res cmds.Response) {
94
+
95
+ n, err := req.Context().GetNode()
96
+ if err != nil {
97
+ res.SetError(err, cmds.ErrNormal)
98
+ return
99
+ }
100
+
101
+ if n.PeerHost == nil {
102
+ res.SetError(errNotOnline, cmds.ErrClient)
103
+ return
104
+ }
105
+
106
+ addrs := make(map[string][]string)
107
+ ps := n.PeerHost.Network().Peerstore()
108
+ for _, p := range ps.Peers() {
109
+ s := p.Pretty()
110
+ for _, a := range ps.Addrs(p) {
111
+ addrs[s] = append(addrs[s], a.String())
112
+ }
113
+ sort.Sort(sort.StringSlice(addrs[s]))
114
+ }
115
+
116
+ res.SetOutput(&addrMap{Addrs: addrs})
117
+ },
118
+ Marshalers: cmds.MarshalerMap{
119
+ cmds.Text: func(res cmds.Response) (io.Reader, error) {
120
+ m, ok := res.Output().(*addrMap)
121
+ if !ok {
122
+ return nil, errors.New("failed to cast map[string]string")
123
+ }
124
+
125
+ // sort the ids first
126
+ ids := make([]string, 0, len(m.Addrs))
127
+ for p := range m.Addrs {
128
+ ids = append(ids, p)
129
+ }
130
+ sort.Sort(sort.StringSlice(ids))
131
+
132
+ var buf bytes.Buffer
133
+ for _, p := range ids {
134
+ paddrs := m.Addrs[p]
135
+ buf.WriteString(fmt.Sprintf("%s (%d)\n", p, len(paddrs)))
136
+ for _, addr := range paddrs {
137
+ buf.WriteString("\t" + addr + "\n")
138
+ }
139
+ }
140
+ return &buf, nil
141
+ },
142
+ },
143
+ Type: addrMap{},
144
+}
145
+
146
var swarmConnectCmd = &cmds.Command{
147
Helptext: cmds.HelpText{
148
Tagline: "Open connection to a given address",