swarm_transport.go 2.35 KB
Newer Older
Steven Allen's avatar
Steven Allen committed
1 2 3 4 5 6
package swarm

import (
	"fmt"
	"strings"

tavit ohanian's avatar
tavit ohanian committed
7
	"gitlab.dms3.io/p2p/go-p2p-core/transport"
8

9
	ma "gitlab.dms3.io/mf/go-multiaddr"
Steven Allen's avatar
Steven Allen committed
10 11 12 13 14 15 16 17 18 19 20 21
)

// TransportForDialing retrieves the appropriate transport for dialing the given
// multiaddr.
func (s *Swarm) TransportForDialing(a ma.Multiaddr) transport.Transport {
	protocols := a.Protocols()
	if len(protocols) == 0 {
		return nil
	}

	s.transports.RLock()
	defer s.transports.RUnlock()
22
	if len(s.transports.m) == 0 {
23 24 25 26
		// make sure we're not just shutting down.
		if s.transports.m != nil {
			log.Error("you have no transports configured")
		}
27 28 29
		return nil
	}

Steven Allen's avatar
Steven Allen committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	for _, p := range protocols {
		transport, ok := s.transports.m[p.Code]
		if !ok {
			continue
		}
		if transport.Proxy() {
			return transport
		}
	}

	return s.transports.m[protocols[len(protocols)-1].Code]
}

// TransportForListening retrieves the appropriate transport for listening on
// the given multiaddr.
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()
53
	if len(s.transports.m) == 0 {
54 55 56 57
		// make sure we're not just shutting down.
		if s.transports.m != nil {
			log.Error("you have no transports configured")
		}
58 59 60
		return nil
	}

Steven Allen's avatar
Steven Allen committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	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
}

// AddTransport adds a transport to this swarm.
//
tavit ohanian's avatar
tavit ohanian committed
76
// Satisfies the Network interface from go-p2p-transport.
Steven Allen's avatar
Steven Allen committed
77 78 79
func (s *Swarm) AddTransport(t transport.Transport) error {
	protocols := t.Protocols()

80 81 82 83
	if len(protocols) == 0 {
		return fmt.Errorf("useless transport handles no protocols: %T", t)
	}

Steven Allen's avatar
Steven Allen committed
84 85
	s.transports.Lock()
	defer s.transports.Unlock()
86 87 88
	if s.transports.m == nil {
		return ErrSwarmClosed
	}
Steven Allen's avatar
Steven Allen committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	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
}