Move a couple of commmon bits of code out to utils.go
This commit is contained in:
1 parent
527ddfca0d
commit
c1856e1ff1
3 files changed
+23
-22
No files matched your search
+2
-9
@@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -43,10 +42,7 @@ func (cm *ConnectionMaker) InitiateConnection(address string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConnectionMaker) EnsureConnection(address string) {
|
func (cm *ConnectionMaker) EnsureConnection(address string) {
|
||||||
// If we've been given a port number, take it off
|
address = StripPortFromAddr(address)
|
||||||
if addrHost, _, err := net.SplitHostPort(address); err == nil {
|
|
||||||
address = addrHost
|
|
||||||
}
|
|
||||||
cm.queryChan <- &ConnectionMakerInteraction{
|
cm.queryChan <- &ConnectionMakerInteraction{
|
||||||
Interaction: Interaction{code: CMEnsure},
|
Interaction: Interaction{code: CMEnsure},
|
||||||
acceptAnyPeer: false,
|
acceptAnyPeer: false,
|
||||||
@@ -54,10 +50,7 @@ func (cm *ConnectionMaker) EnsureConnection(address string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConnectionMaker) ConnectionEstablished(address string) {
|
func (cm *ConnectionMaker) ConnectionEstablished(address string) {
|
||||||
// If we've been given a port number, take it off
|
address = StripPortFromAddr(address)
|
||||||
if addrHost, _, err := net.SplitHostPort(address); err == nil {
|
|
||||||
address = addrHost
|
|
||||||
}
|
|
||||||
cm.queryChan <- &ConnectionMakerInteraction{
|
cm.queryChan <- &ConnectionMakerInteraction{
|
||||||
Interaction: Interaction{code: CMEstablished},
|
Interaction: Interaction{code: CMEstablished},
|
||||||
address: address}
|
address: address}
|
||||||
|
|||||||
@@ -57,19 +57,8 @@ func (peer *Peer) ConnectionTo(name PeerName) (Connection, bool) {
|
|||||||
return conn, found // yes, you really can't inline that. FFS.
|
return conn, found // yes, you really can't inline that. FFS.
|
||||||
}
|
}
|
||||||
|
|
||||||
// given an address like '1.2.3.4:567', return the address if it has a port,
|
|
||||||
// otherwise return the address with weave's standard port number
|
|
||||||
func normalisePeerAddr(peerAddr string) string {
|
|
||||||
_, _, err := net.SplitHostPort(peerAddr)
|
|
||||||
if err == nil {
|
|
||||||
return peerAddr
|
|
||||||
} else {
|
|
||||||
return fmt.Sprintf("%s:%d", peerAddr, Port)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (peer *Peer) ConnectionOn(peerAddr string) (Connection, bool) {
|
func (peer *Peer) ConnectionOn(peerAddr string) (Connection, bool) {
|
||||||
peerAddr = normalisePeerAddr(peerAddr)
|
peerAddr = NormalisePeerAddr(peerAddr)
|
||||||
peer.RLock()
|
peer.RLock()
|
||||||
defer peer.RUnlock()
|
defer peer.RUnlock()
|
||||||
for _, conn := range peer.connections {
|
for _, conn := range peer.connections {
|
||||||
@@ -220,7 +209,7 @@ func (peer *Peer) CreateConnection(peerAddr string, acceptNewPeer bool) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// We're dialing the remote so that means connections will come from random ports
|
// We're dialing the remote so that means connections will come from random ports
|
||||||
addrStr := normalisePeerAddr(peerAddr)
|
addrStr := NormalisePeerAddr(peerAddr)
|
||||||
tcpAddr, tcpErr := net.ResolveTCPAddr("tcp4", addrStr)
|
tcpAddr, tcpErr := net.ResolveTCPAddr("tcp4", addrStr)
|
||||||
udpAddr, udpErr := net.ResolveUDPAddr("udp4", addrStr)
|
udpAddr, udpErr := net.ResolveUDPAddr("udp4", addrStr)
|
||||||
if tcpErr != nil || udpErr != nil {
|
if tcpErr != nil || udpErr != nil {
|
||||||
|
|||||||
@@ -97,3 +97,22 @@ func (lop ListOfPeers) Swap(i, j int) {
|
|||||||
func (lop ListOfPeers) Less(i, j int) bool {
|
func (lop ListOfPeers) Less(i, j int) bool {
|
||||||
return lop[i].Name < lop[i].Name
|
return lop[i].Name < lop[i].Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func StripPortFromAddr(address string) string {
|
||||||
|
// If we've been given a port number, take it off
|
||||||
|
if addrHost, _, err := net.SplitHostPort(address); err == nil {
|
||||||
|
return addrHost
|
||||||
|
}
|
||||||
|
return address
|
||||||
|
}
|
||||||
|
|
||||||
|
// given an address like '1.2.3.4:567', return the address if it has a port,
|
||||||
|
// otherwise return the address with weave's standard port number
|
||||||
|
func NormalisePeerAddr(peerAddr string) string {
|
||||||
|
_, _, err := net.SplitHostPort(peerAddr)
|
||||||
|
if err == nil {
|
||||||
|
return peerAddr
|
||||||
|
} else {
|
||||||
|
return fmt.Sprintf("%s:%d", peerAddr, Port)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user