forked from cwtch.im/cwtch
74 lines
1.5 KiB
Go
74 lines
1.5 KiB
Go
|
package connections
|
||
|
|
||
|
import (
|
||
|
"git.mascherari.press/cwtch/model"
|
||
|
"time"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
type Manager struct {
|
||
|
peerConnections map[string]*PeerPeerConnection
|
||
|
serverConnections map[string]*PeerServerConnection
|
||
|
lock sync.Mutex
|
||
|
}
|
||
|
|
||
|
func NewConnectionsManager() *Manager {
|
||
|
m := new(Manager)
|
||
|
m.peerConnections = make(map[string]*PeerPeerConnection)
|
||
|
m.serverConnections = make(map[string]*PeerServerConnection)
|
||
|
return m;
|
||
|
}
|
||
|
|
||
|
func (m *Manager) ManagePeerConnection(host string, profile *model.Profile) {
|
||
|
m.lock.Lock()
|
||
|
ppc := NewPeerPeerConnection(host, profile)
|
||
|
go ppc.Run()
|
||
|
m.peerConnections[host] = ppc
|
||
|
m.lock.Unlock()
|
||
|
|
||
|
}
|
||
|
|
||
|
func (m *Manager) ManageServerConnection(host string) {
|
||
|
m.lock.Lock()
|
||
|
psc := NewPeerServerConnection(host)
|
||
|
go psc.Run()
|
||
|
m.serverConnections[host] = psc
|
||
|
m.lock.Unlock()
|
||
|
}
|
||
|
|
||
|
func (m *Manager) GetPeerPeerConnectionForOnion(host string) (ppc *PeerPeerConnection) {
|
||
|
m.lock.Lock()
|
||
|
ppc = m.peerConnections[host]
|
||
|
m.lock.Unlock()
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (m *Manager) GetPeerServerConnectionForOnion(host string) (psc *PeerServerConnection) {
|
||
|
m.lock.Lock()
|
||
|
psc = m.serverConnections[host]
|
||
|
m.lock.Unlock()
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (m *Manager) AttemptReconnections() {
|
||
|
m.lock.Lock()
|
||
|
for _, ppc := range m.peerConnections {
|
||
|
if ppc.GetState() == FAILED {
|
||
|
go ppc.Run()
|
||
|
}
|
||
|
}
|
||
|
m.lock.Unlock()
|
||
|
|
||
|
m.lock.Lock()
|
||
|
for _, psc := range m.serverConnections {
|
||
|
if psc.GetState() == FAILED {
|
||
|
go psc.Run()
|
||
|
}
|
||
|
}
|
||
|
m.lock.Unlock()
|
||
|
|
||
|
// Launch Another Run In 30 Seconds
|
||
|
time.Sleep(time.Second * 30)
|
||
|
go m.AttemptReconnections()
|
||
|
}
|