package swarm
import (
"fmt"
"strings"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/transport"
ma "github.com/multiformats/go-multiaddr"
)
func (s *Swarm ) TransportForDialing (a ma .Multiaddr ) transport .Transport {
if a == nil {
return nil
}
protocols := a .Protocols ()
if len (protocols ) == 0 {
return nil
}
s .transports .RLock ()
defer s .transports .RUnlock ()
if len (s .transports .m ) == 0 {
if s .transports .m != nil {
log .Error ("you have no transports configured" )
}
return nil
}
if isRelayAddr (a ) {
return s .transports .m [ma .P_CIRCUIT ]
}
if id , _ := peer .IDFromP2PAddr (a ); id != "" {
a , _ = ma .SplitLast (a )
if a == nil {
return nil
}
}
for _ , t := range s .transports .m {
if t .CanDial (a ) {
return t
}
}
return nil
}
func (s *Swarm ) TransportForListening (a ma .Multiaddr ) transport .Transport {
protocols := a .Protocols ()
if len (protocols ) == 0 {
return nil
}
s .transports .RLock ()
defer s .transports .RUnlock ()
if len (s .transports .m ) == 0 {
return nil
}
selected := s .transports .m [protocols [len (protocols )-1 ].Code ]
for _ , p := range protocols {
transport , ok := s .transports .m [p .Code ]
if !ok {
continue
}
if transport .Proxy () {
selected = transport
}
}
return selected
}
func (s *Swarm ) AddTransport (t transport .Transport ) error {
protocols := t .Protocols ()
if len (protocols ) == 0 {
return fmt .Errorf ("useless transport handles no protocols: %T" , t )
}
s .transports .Lock ()
defer s .transports .Unlock ()
if s .transports .m == nil {
return ErrSwarmClosed
}
var registered []string
for _ , p := range protocols {
if _ , ok := s .transports .m [p ]; ok {
proto := ma .ProtocolWithCode (p )
name := proto .Name
if name == "" {
name = fmt .Sprintf ("unknown (%d)" , p )
}
registered = append (registered , name )
}
}
if len (registered ) > 0 {
return fmt .Errorf (
"transports already registered for protocol(s): %s" ,
strings .Join (registered , ", " ),
)
}
for _ , p := range protocols {
s .transports .m [p ] = t
}
return nil
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .