1
1
package commands
2
2
3
3
import (
4
+ "bytes"
4
5
"errors"
6
+ "fmt"
5
7
"io"
6
8
"strconv"
9
+ "text/tabwriter"
7
10
8
11
cmds "github.com/ipfs/go-ipfs/commands"
9
12
corenet "github.com/ipfs/go-ipfs/core/corenet"
10
13
11
- manet "gx/ipfs/Qmf1Gq7N45Rpuw7ev47uWgH6dLPtdnvcMRNPkVBwqjLJg2/go-multiaddr-net"
12
- ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr"
13
- net "gx/ipfs/QmVHSBsn8LEeay8m5ERebgUVuhzw838PsyTttCmP6GMJkg/go-libp2p-net"
14
14
peerstore "gx/ipfs/QmNUVzEjq3XWJ89hegahPvyfJbTXgTaom48pLb7YBD9gHQ/go-libp2p-peerstore"
15
+ net "gx/ipfs/QmVHSBsn8LEeay8m5ERebgUVuhzw838PsyTttCmP6GMJkg/go-libp2p-net"
16
+ ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr"
15
17
peer "gx/ipfs/QmdS9KpbDyPrieswibZhkod1oXqRwZJrUPzxCofAMWpFGq/go-libp2p-peer"
18
+ manet "gx/ipfs/Qmf1Gq7N45Rpuw7ev47uWgH6dLPtdnvcMRNPkVBwqjLJg2/go-multiaddr-net"
16
19
)
17
20
18
21
// Command output types.
19
22
type AppInfoOutput struct {
20
- Identity string
21
23
Protocol string
22
24
Address string
23
25
}
@@ -31,8 +33,11 @@ type StreamInfoOutput struct {
31
33
RemoteAddress string
32
34
}
33
35
34
- type ListCommandOutput struct {
35
- Apps []AppInfoOutput
36
+ type CorenetLsOutput struct {
37
+ Apps []AppInfoOutput
38
+ }
39
+
40
+ type CorenetStreamsOutput struct {
36
41
Streams []StreamInfoOutput
37
42
}
38
43
@@ -144,20 +149,20 @@ var CorenetCmd = &cmds.Command{
144
149
},
145
150
146
151
Subcommands : map [string ]* cmds.Command {
147
- "list" : listCmd ,
148
- "dial" : dialCmd ,
149
- "listen" : listenCmd ,
150
- "close" : closeCmd ,
152
+ "ls" : CorenetLsCmd ,
153
+ "streams" : CorenetStreamsCmd ,
154
+ "dial" : CorenetDialCmd ,
155
+ "listen" : CorenetListenCmd ,
156
+ "close" : CorenetCloseCmd ,
151
157
},
152
158
}
153
159
154
- var listCmd = & cmds.Command {
160
+ var CorenetLsCmd = & cmds.Command {
155
161
Helptext : cmds.HelpText {
156
- Tagline : "List active application protocol connections ." ,
162
+ Tagline : "List active application protocol listeners ." ,
157
163
},
158
164
Options : []cmds.Option {
159
- cmds .BoolOption ("apps" , "a" , "Display only local application protocol listeners." ).Default (false ),
160
- cmds .BoolOption ("streams" , "s" , "Display active application protocol streams." ).Default (false ),
165
+ cmds .BoolOption ("headers" , "v" , "Print table headers (HandlerId, Protocol, Local, Remote)." ).Default (false ),
161
166
},
162
167
Run : func (req cmds.Request , res cmds.Response ) {
163
168
n , err := req .InvocContext ().GetNode ()
@@ -171,16 +176,59 @@ var listCmd = &cmds.Command{
171
176
return
172
177
}
173
178
174
- var output ListCommandOutput
179
+ output := & CorenetLsOutput {}
175
180
176
181
for _ , a := range apps .apps {
177
182
output .Apps = append (output .Apps , AppInfoOutput {
178
- Identity : a .identity .Pretty (),
179
183
Protocol : a .protocol ,
180
184
Address : a .address .String (),
181
185
})
182
186
}
183
187
188
+ res .SetOutput (output )
189
+ },
190
+ Type : CorenetLsOutput {},
191
+ Marshalers : cmds.MarshalerMap {
192
+ cmds .Text : func (res cmds.Response ) (io.Reader , error ) {
193
+ headers , _ , _ := res .Request ().Option ("headers" ).Bool ()
194
+ list , _ := res .Output ().(* CorenetLsOutput )
195
+ buf := new (bytes.Buffer )
196
+ w := tabwriter .NewWriter (buf , 1 , 2 , 1 , ' ' , 0 )
197
+ for _ , app := range list .Apps {
198
+ if headers {
199
+ fmt .Fprintln (w , "Address\t Protocol" )
200
+ }
201
+
202
+ fmt .Fprintf (w , "%s\t %s\n " , app .Address , app .Protocol )
203
+ }
204
+ w .Flush ()
205
+
206
+ return buf , nil
207
+ },
208
+ },
209
+ }
210
+
211
+ var CorenetStreamsCmd = & cmds.Command {
212
+ Helptext : cmds.HelpText {
213
+ Tagline : "List active application protocol connections." ,
214
+ },
215
+ Options : []cmds.Option {
216
+ cmds .BoolOption ("headers" , "v" , "Print table headers (HandlerId, Protocol, Local, Remote)." ).Default (false ),
217
+ },
218
+ Run : func (req cmds.Request , res cmds.Response ) {
219
+ n , err := req .InvocContext ().GetNode ()
220
+ if err != nil {
221
+ res .SetError (err , cmds .ErrNormal )
222
+ return
223
+ }
224
+
225
+ if ! n .OnlineMode () {
226
+ res .SetError (errNotOnline , cmds .ErrClient )
227
+ return
228
+ }
229
+
230
+ output := & CorenetStreamsOutput {}
231
+
184
232
for _ , s := range streams .streams {
185
233
output .Streams = append (output .Streams , StreamInfoOutput {
186
234
HandlerId : strconv .FormatUint (s .handlerId , 10 ),
@@ -195,11 +243,30 @@ var listCmd = &cmds.Command{
195
243
})
196
244
}
197
245
198
- res .SetOutput (& output )
246
+ res .SetOutput (output )
247
+ },
248
+ Type : CorenetStreamsOutput {},
249
+ Marshalers : cmds.MarshalerMap {
250
+ cmds .Text : func (res cmds.Response ) (io.Reader , error ) {
251
+ headers , _ , _ := res .Request ().Option ("headers" ).Bool ()
252
+ list , _ := res .Output ().(* CorenetStreamsOutput )
253
+ buf := new (bytes.Buffer )
254
+ w := tabwriter .NewWriter (buf , 1 , 2 , 1 , ' ' , 0 )
255
+ for _ , stream := range list .Streams {
256
+ if headers {
257
+ fmt .Fprintln (w , "HandlerId\t Protocol\t Local\t Remote" )
258
+ }
259
+
260
+ fmt .Fprintf (w , "%s\t %s\t %s\t %s\n " , stream .HandlerId , stream .Protocol , stream .LocalAddress , stream .RemotePeer )
261
+ }
262
+ w .Flush ()
263
+
264
+ return buf , nil
265
+ },
199
266
},
200
267
}
201
268
202
- var listenCmd = & cmds.Command {
269
+ var CorenetListenCmd = & cmds.Command {
203
270
Helptext : cmds.HelpText {
204
271
Tagline : "Create application protocol listener and proxy to network multiaddr." ,
205
272
},
@@ -251,7 +318,6 @@ var listenCmd = &cmds.Command{
251
318
252
319
// Successful response.
253
320
res .SetOutput (& AppInfoOutput {
254
- Identity : app .identity .Pretty (),
255
321
Protocol : proto ,
256
322
Address : addr .String (),
257
323
})
@@ -313,7 +379,7 @@ func startStreaming(stream *cnStreamInfo) {
313
379
}()
314
380
}
315
381
316
- var dialCmd = & cmds.Command {
382
+ var CorenetDialCmd = & cmds.Command {
317
383
Helptext : cmds.HelpText {
318
384
Tagline : "Dial to an application service." ,
319
385
},
@@ -393,7 +459,6 @@ var dialCmd = &cmds.Command{
393
459
}
394
460
395
461
output := AppInfoOutput {
396
- Identity : app .identity .Pretty (),
397
462
Protocol : app .protocol ,
398
463
Address : app .address .String (),
399
464
}
@@ -427,7 +492,7 @@ func doAccept(app *cnAppInfo, remote net.Stream, listener manet.Listener) {
427
492
startStreaming (& stream )
428
493
}
429
494
430
- var closeCmd = & cmds.Command {
495
+ var CorenetCloseCmd = & cmds.Command {
431
496
Helptext : cmds.HelpText {
432
497
Tagline : "Closes an active stream listener or client." ,
433
498
},
0 commit comments