2019-08-02 01:09:01 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cwtch.im/cwtch/event"
|
2020-02-10 22:09:24 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/connectivity"
|
|
|
|
"git.openprivacy.ca/openprivacy/log"
|
2019-08-02 01:09:01 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"cwtch.im/cwtch/app/plugins"
|
|
|
|
"cwtch.im/cwtch/peer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type appletPeers struct {
|
2020-02-03 18:46:15 +00:00
|
|
|
peerLock sync.Mutex
|
2019-08-02 01:09:01 +00:00
|
|
|
peers map[string]peer.CwtchPeer
|
|
|
|
launched bool // bit hacky, place holder while we transition to full multi peer support and a better api
|
|
|
|
}
|
|
|
|
|
|
|
|
type appletACN struct {
|
|
|
|
acn connectivity.ACN
|
|
|
|
}
|
|
|
|
|
|
|
|
type appletPlugins struct {
|
|
|
|
plugins sync.Map //map[string] []plugins.Plugin
|
|
|
|
}
|
|
|
|
|
|
|
|
// ***** applet ACN
|
|
|
|
|
|
|
|
func (a *appletACN) init(acn connectivity.ACN, publish func(int, string)) {
|
|
|
|
a.acn = acn
|
|
|
|
acn.SetStatusCallback(publish)
|
|
|
|
prog, status := acn.GetBootstrapStatus()
|
|
|
|
publish(prog, status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appletACN) Shutdown() {
|
|
|
|
a.acn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ***** appletPeers
|
|
|
|
|
|
|
|
func (ap *appletPeers) init() {
|
|
|
|
ap.peers = make(map[string]peer.CwtchPeer)
|
|
|
|
ap.launched = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// LaunchPeers starts each peer Listening and connecting to peers and groups
|
|
|
|
func (ap *appletPeers) LaunchPeers() {
|
|
|
|
log.Debugf("appletPeers LaunchPeers\n")
|
2020-02-03 18:46:15 +00:00
|
|
|
ap.peerLock.Lock()
|
|
|
|
defer ap.peerLock.Unlock()
|
2019-08-02 01:09:01 +00:00
|
|
|
if ap.launched {
|
|
|
|
return
|
|
|
|
}
|
2020-02-03 18:46:15 +00:00
|
|
|
for pid, p := range ap.peers {
|
|
|
|
log.Debugf("Launching %v\n", pid)
|
2019-08-02 01:09:01 +00:00
|
|
|
p.Listen()
|
2020-02-03 18:46:15 +00:00
|
|
|
log.Debugf("done Listen() for %v\n", pid)
|
2019-08-02 01:09:01 +00:00
|
|
|
p.StartPeersConnections()
|
2020-02-03 18:46:15 +00:00
|
|
|
log.Debugf("done StartPeersConnections() for %v\n", pid)
|
2019-08-02 01:09:01 +00:00
|
|
|
p.StartGroupConnections()
|
2020-02-03 18:46:15 +00:00
|
|
|
log.Debugf("done StartGroupConnections() for %v\n", pid)
|
2019-08-02 01:09:01 +00:00
|
|
|
}
|
|
|
|
ap.launched = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListPeers returns a map of onions to their profile's Name
|
|
|
|
func (ap *appletPeers) ListPeers() map[string]string {
|
|
|
|
keys := map[string]string{}
|
2020-02-03 18:46:15 +00:00
|
|
|
|
|
|
|
ap.peerLock.Lock()
|
|
|
|
defer ap.peerLock.Unlock()
|
2019-08-02 01:09:01 +00:00
|
|
|
for k, p := range ap.peers {
|
2020-02-03 18:46:15 +00:00
|
|
|
keys[k] = p.GetName()
|
2019-08-02 01:09:01 +00:00
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPeer returns a cwtchPeer for a given onion address
|
|
|
|
func (ap *appletPeers) GetPeer(onion string) peer.CwtchPeer {
|
|
|
|
if peer, ok := ap.peers[onion]; ok {
|
|
|
|
return peer
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ***** applet Plugins
|
|
|
|
|
|
|
|
func (ap *appletPlugins) Shutdown() {
|
2019-10-31 23:05:01 +00:00
|
|
|
log.Debugf("shutting down applet plugins...")
|
2019-08-02 01:09:01 +00:00
|
|
|
ap.plugins.Range(func(k, v interface{}) bool {
|
2019-10-31 23:05:01 +00:00
|
|
|
log.Debugf("shutting down plugins for %v", k)
|
|
|
|
ap.ShutdownPeer(k.(string))
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ap *appletPlugins) ShutdownPeer(peerid string) {
|
|
|
|
log.Debugf("shutting down plugins for %v", peerid)
|
|
|
|
pluginsI, ok := ap.plugins.Load(peerid)
|
|
|
|
if ok {
|
|
|
|
plugins := pluginsI.([]plugins.Plugin)
|
2019-08-02 01:09:01 +00:00
|
|
|
for _, plugin := range plugins {
|
2019-10-31 23:05:01 +00:00
|
|
|
log.Debugf("shutting down plugin: %v", plugin)
|
2019-08-02 01:09:01 +00:00
|
|
|
plugin.Shutdown()
|
|
|
|
}
|
2019-10-31 23:05:01 +00:00
|
|
|
}
|
2019-08-02 01:09:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 23:05:01 +00:00
|
|
|
func (ap *appletPlugins) AddPlugin(peerid string, id plugins.PluginID, bus event.Manager, acn connectivity.ACN) {
|
2019-08-02 01:09:01 +00:00
|
|
|
if _, exists := ap.plugins.Load(peerid); !exists {
|
|
|
|
ap.plugins.Store(peerid, []plugins.Plugin{})
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginsinf, _ := ap.plugins.Load(peerid)
|
|
|
|
peerPlugins := pluginsinf.([]plugins.Plugin)
|
|
|
|
|
2019-10-31 23:05:01 +00:00
|
|
|
newp := plugins.Get(id, bus, acn)
|
2019-08-02 01:09:01 +00:00
|
|
|
newp.Start()
|
|
|
|
peerPlugins = append(peerPlugins, newp)
|
2019-10-31 23:05:01 +00:00
|
|
|
log.Debugf("storing plugin for %v %v", peerid, peerPlugins)
|
2019-08-02 01:09:01 +00:00
|
|
|
ap.plugins.Store(peerid, peerPlugins)
|
|
|
|
}
|