use mailserver cycle or specific mailserver depending on settings

This commit is contained in:
Richard Ramos 2021-01-07 14:33:11 -04:00 committed by Iuri Matias
parent 2a6eb20094
commit 199d5a0342
1 changed files with 21 additions and 12 deletions

View File

@ -99,7 +99,7 @@ proc isActiveMailserverAvailable*(self:MailserverModel): bool =
proc connect(self: MailserverModel, enode: string) = proc connect(self: MailserverModel, enode: string) =
debug "Connecting to mailserver", enode=enode.substr[enode.len-40..enode.len-1] debug "Connecting to mailserver", enode=enode.substr[enode.len-40..enode.len-1]
var mailserverTrusted = false
# TODO: this should come from settings # TODO: this should come from settings
var knownMailservers = initHashSet[string]() var knownMailservers = initHashSet[string]()
for m in self.mailservers: for m in self.mailservers:
@ -120,6 +120,11 @@ proc connect(self: MailserverModel, enode: string) =
# Connecting and once a peerConnected signal is received, we mark it as # Connecting and once a peerConnected signal is received, we mark it as
# Connected and then as Trusted # Connected and then as Trusted
if self.nodes.hasKey(enode) and self.nodes[enode] == MailserverStatus.Connected:
self.trustPeer(enode)
status_mailservers.update(enode)
mailserverTrusted = true
else:
# Attempt to connect to mailserver by adding it as a peer # Attempt to connect to mailserver by adding it as a peer
self.nodes[enode] = MailserverStatus.Connecting self.nodes[enode] = MailserverStatus.Connecting
addPeer(enode) addPeer(enode)
@ -127,7 +132,8 @@ proc connect(self: MailserverModel, enode: string) =
nodesLock.release() nodesLock.release()
activeMailserverLock.release() activeMailserverLock.release()
if mailserverTrusted:
self.events.emit("mailserverAvailable", Args())
@ -234,15 +240,18 @@ proc cycleMailservers(self: MailserverModel) =
proc checkConnection() {.thread.} = proc checkConnection() {.thread.} =
{.gcsafe.}: {.gcsafe.}:
#TODO: connect to current mailserver from the settings
# or setup a random mailserver:
let sleepDuration = 10000 let sleepDuration = 10000
while true: while true:
trace "Verifying mailserver connection state..." trace "Verifying mailserver connection state..."
withLock modelLock: withLock modelLock:
# TODO: have a timeout for reconnection before changing to a different server let pinnedMailserver = status_settings.getPinnedMailserver()
if pinnedMailserver != "" and mailserverModel.getActiveMailserver() != pinnedMailserver:
# connect to current mailserver from the settings
mailserverModel.connect(pinnedMailserver)
else:
# or setup a random mailserver:
if not mailserverModel.isActiveMailserverAvailable: if not mailserverModel.isActiveMailserverAvailable:
# TODO: have a timeout for reconnection before changing to a different server
mailserverModel.cycleMailservers() mailserverModel.cycleMailservers()
sleep(sleepDuration) sleep(sleepDuration)