diff --git a/cmd/skywire-cli/commands/visor/transports.go b/cmd/skywire-cli/commands/visor/transports.go index b1b25a9ae3..e0cec94604 100644 --- a/cmd/skywire-cli/commands/visor/transports.go +++ b/cmd/skywire-cli/commands/visor/transports.go @@ -203,25 +203,20 @@ var rmTpCmd = &cobra.Command{ Long: "\n Remove transport(s) by id", DisableFlagsInUseLine: true, Run: func(cmd *cobra.Command, args []string) { - //TODO - //if removeAll { - // var pks cipher.PubKeys - // internal.Catch(cmd.Flags(), pks.Set(strings.Join(filterPubKeys, ","))) - // tID, err := clirpc.Client(cmd.Flags()).Transports(filterTypes, pks, showLogs) - // internal.Catch(cmd.Flags(), err) - // internal.Catch(cmd.Flags(), clirpc.Client(cmd.Flags()).RemoveTransport(tID)) - //} else { - if args[0] != "" { - tpID = args[0] - } - tID := internal.ParseUUID(cmd.Flags(), "transport-id", tpID) rpcClient, err := clirpc.Client(cmd.Flags()) - if err != nil { - os.Exit(1) + if removeAll { + internal.Catch(cmd.Flags(), rpcClient.RemoveAllTransports()) + internal.PrintOutput(cmd.Flags(), "OK", "OK\n") + } else if tpID != "" { + tID := internal.ParseUUID(cmd.Flags(), "transport-id", tpID) + if err != nil { + os.Exit(1) + } + internal.Catch(cmd.Flags(), rpcClient.RemoveTransport(tID)) + internal.PrintOutput(cmd.Flags(), "OK", "OK\n") + } else { + internal.PrintOutput(cmd.Flags(), "", cmd.Help()) } - internal.Catch(cmd.Flags(), rpcClient.RemoveTransport(tID)) - internal.PrintOutput(cmd.Flags(), "OK", "OK\n") - //} }, } diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index 3b5e62a10a..07a6db977f 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -482,6 +482,22 @@ func (tm *Manager) DeleteTransport(id uuid.UUID) { } } +// DeleteAllTransports deregisters all Transports in transport discovery and deletes them locally. +func (tm *Manager) DeleteAllTransports() { + tm.mx.Lock() + defer tm.mx.Unlock() + + if tm.isClosing() { + return + } + + // Deregister transports before closing the underlying transport. + for _, tp := range tm.tps { + tp.close() + delete(tm.tps, tp.Entry.ID) + } +} + // ReadPacket reads data packets from routes. func (tm *Manager) ReadPacket() (routing.Packet, error) { p, ok := <-tm.readCh diff --git a/pkg/visor/api.go b/pkg/visor/api.go index ec3f5319cf..8f6fb92cc0 100644 --- a/pkg/visor/api.go +++ b/pkg/visor/api.go @@ -99,6 +99,7 @@ type API interface { Transport(tid uuid.UUID) (*TransportSummary, error) AddTransport(remote cipher.PubKey, tpType string, timeout time.Duration) (*TransportSummary, error) RemoveTransport(tid uuid.UUID) error + RemoveAllTransports() error SetPublicAutoconnect(pAc bool) error GetPersistentTransports() ([]transport.PersistentTransports, error) SetPersistentTransports([]transport.PersistentTransports) error @@ -1192,6 +1193,12 @@ func (v *Visor) RemoveTransport(tid uuid.UUID) error { return nil } +// RemoveAllTransports implements API +func (v *Visor) RemoveAllTransports() error { + v.tpM.DeleteAllTransports() + return nil +} + // DiscoverTransportsByPK implements API. func (v *Visor) DiscoverTransportsByPK(pk cipher.PubKey) ([]*transport.Entry, error) { tpD := v.tpDiscClient() diff --git a/pkg/visor/rpc.go b/pkg/visor/rpc.go index 924ee69d8c..d8feb6f8c9 100644 --- a/pkg/visor/rpc.go +++ b/pkg/visor/rpc.go @@ -527,6 +527,13 @@ func (r *RPC) RemoveTransport(tid *uuid.UUID, _ *struct{}) (err error) { return r.visor.RemoveTransport(*tid) } +// RemoveAllTransports removes all Transports from the visor. +func (r *RPC) RemoveAllTransports(_ *struct{}, _ *struct{}) (err error) { + defer rpcutil.LogCall(r.log, "RemoveAllTransports", nil)(nil, &err) + + return r.visor.RemoveAllTransports() +} + /* <<< AVAILABLE TRANSPORTS >>> */ diff --git a/pkg/visor/rpc_client.go b/pkg/visor/rpc_client.go index 67051985af..75c1e7c025 100644 --- a/pkg/visor/rpc_client.go +++ b/pkg/visor/rpc_client.go @@ -394,6 +394,11 @@ func (rc *rpcClient) RemoveTransport(tid uuid.UUID) error { return rc.Call("RemoveTransport", &tid, &struct{}{}) } +// RemoveAllTransports calls RemoveAllTransports. +func (rc *rpcClient) RemoveAllTransports() error { + return rc.Call("RemoveAllTransports", &struct{}{}, &struct{}{}) +} + func (rc *rpcClient) DiscoverTransportsByPK(pk cipher.PubKey) ([]*transport.Entry, error) { entries := make([]*transport.Entry, 0) err := rc.Call("DiscoverTransportsByPK", &pk, &entries) @@ -1158,6 +1163,14 @@ func (mc *mockRPCClient) RemoveTransport(tid uuid.UUID) error { }) } +// RemoveAllTransports implements API. +func (mc *mockRPCClient) RemoveAllTransports() error { + return mc.do(true, func() error { + mc.o.Transports = []*TransportSummary{} + return nil + }) +} + func (mc *mockRPCClient) DiscoverTransportsByPK(cipher.PubKey) ([]*transport.Entry, error) { return nil, ErrNotImplemented }