|
|
@ -3,6 +3,7 @@ package plugins |
|
|
|
import ( |
|
|
|
"cwtch.im/cwtch/event" |
|
|
|
"cwtch.im/cwtch/protocol/connections" |
|
|
|
"git.openprivacy.ca/openprivacy/log" |
|
|
|
"sync" |
|
|
|
"time" |
|
|
|
) |
|
|
@ -30,23 +31,29 @@ type contactRetry struct { |
|
|
|
bus event.Manager |
|
|
|
queue event.Queue |
|
|
|
networkUp bool |
|
|
|
|
|
|
|
running bool |
|
|
|
breakChan chan bool |
|
|
|
onion string |
|
|
|
|
|
|
|
connections sync.Map //[string]*contact
|
|
|
|
} |
|
|
|
|
|
|
|
// NewConnectionRetry returns a Plugin that when started will retry connecting to contacts with a backoff timing
|
|
|
|
func NewConnectionRetry(bus event.Manager) Plugin { |
|
|
|
cr := &contactRetry{bus: bus, queue: event.NewQueue(), breakChan: make(chan bool), connections: sync.Map{}, networkUp: false} |
|
|
|
func NewConnectionRetry(bus event.Manager, onion string) Plugin { |
|
|
|
cr := &contactRetry{bus: bus, queue: event.NewQueue(), breakChan: make(chan bool), connections: sync.Map{}, networkUp: false, onion: onion} |
|
|
|
return cr |
|
|
|
} |
|
|
|
|
|
|
|
func (cr *contactRetry) Start() { |
|
|
|
go cr.run() |
|
|
|
if !cr.running { |
|
|
|
go cr.run() |
|
|
|
} else { |
|
|
|
log.Errorf("Attempted to start Contact Retry plugin twice for %v", cr.onion) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (cr *contactRetry) run() { |
|
|
|
cr.running = true |
|
|
|
cr.bus.Subscribe(event.PeerStateChange, cr.queue) |
|
|
|
cr.bus.Subscribe(event.ACNStatus, cr.queue) |
|
|
|
cr.bus.Subscribe(event.ServerStateChange, cr.queue) |
|
|
@ -56,12 +63,12 @@ func (cr *contactRetry) run() { |
|
|
|
case e := <-cr.queue.OutChan(): |
|
|
|
switch e.EventType { |
|
|
|
case event.PeerStateChange: |
|
|
|
state := connections.ConnectionStateToType[e.Data[event.ConnectionState]] |
|
|
|
state := connections.ConnectionStateToType()[e.Data[event.ConnectionState]] |
|
|
|
peer := e.Data[event.RemotePeer] |
|
|
|
cr.handleEvent(peer, state, peerConn) |
|
|
|
|
|
|
|
case event.ServerStateChange: |
|
|
|
state := connections.ConnectionStateToType[e.Data[event.ConnectionState]] |
|
|
|
state := connections.ConnectionStateToType()[e.Data[event.ConnectionState]] |
|
|
|
server := e.Data[event.GroupServer] |
|
|
|
cr.handleEvent(server, state, serverConn) |
|
|
|
|
|
|
@ -102,6 +109,7 @@ func (cr *contactRetry) run() { |
|
|
|
}) |
|
|
|
|
|
|
|
case <-cr.breakChan: |
|
|
|
cr.running = false |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
@ -132,4 +140,5 @@ func (cr *contactRetry) handleEvent(id string, state connections.ConnectionState |
|
|
|
|
|
|
|
func (cr *contactRetry) Shutdown() { |
|
|
|
cr.breakChan <- true |
|
|
|
|
|
|
|
} |
|
|
|