Skip to content

Commit

Permalink
Change connection initiazation
Browse files Browse the repository at this point in the history
- First try the hostname instead of the IP addresses
- Always check for pairing state first
  • Loading branch information
DerAndereAndi committed Feb 21, 2024
1 parent eb6afd7 commit 073f3ca
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions hub/hub_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,24 +333,14 @@ func (h *Hub) prepareConnectionInitation(ski string, counter int, entry *api.Mdn
func (h *Hub) initateConnection(remoteService *api.ServiceDetails, entry *api.MdnsEntry) bool {
var err error

// try connecting via an IP address first
for _, address := range entry.Addresses {
// connection attempt is not relevant if the device is no longer paired
// or it is not queued for pairing
pairingState := h.ServiceForSKI(remoteService.SKI()).ConnectionStateDetail().State()
if !h.IsRemoteServiceForSKIPaired(remoteService.SKI()) && pairingState != api.ConnectionStateQueued {
return false
}

logging.Log().Debug("trying to connect to", remoteService.SKI(), "at", address)
if err = h.connectFoundService(remoteService, address.String(), strconv.Itoa(entry.Port), entry.Path); err != nil {
logging.Log().Debug("connection to", remoteService.SKI(), "failed: ", err)
} else {
return true
}
// connection attempt is not relevant if the device is no longer paired
// or it is not queued for pairing
pairingState := h.ServiceForSKI(remoteService.SKI()).ConnectionStateDetail().State()
if !h.IsRemoteServiceForSKIPaired(remoteService.SKI()) && pairingState != api.ConnectionStateQueued {
return false
}

// connectdion via IP address failed, try hostname
// try connetion via hostname
if len(entry.Host) > 0 {
logging.Log().Debug("trying to connect to", remoteService.SKI(), "at", entry.Host)
if err = h.connectFoundService(remoteService, entry.Host, strconv.Itoa(entry.Port), entry.Path); err != nil {
Expand All @@ -360,6 +350,16 @@ func (h *Hub) initateConnection(remoteService *api.ServiceDetails, entry *api.Md
}
}

// try connecting via the provided IP addresses
for _, address := range entry.Addresses {
logging.Log().Debug("trying to connect to", remoteService.SKI(), "at", address)
if err = h.connectFoundService(remoteService, address.String(), strconv.Itoa(entry.Port), entry.Path); err != nil {
logging.Log().Debug("connection to", remoteService.SKI(), "failed: ", err)
} else {
return true
}
}

// no connection could be estabished via any of the provided addresses
// because no service was reachable at any of the addresses
return false
Expand Down

0 comments on commit 073f3ca

Please sign in to comment.